Skip to content

Commit 5fdaaa7

Browse files
committed
Auto merge of rust-lang#95173 - m-ou-se:sys-locks-module, r=dtolnay
Move std::sys::{mutex, condvar, rwlock} to std::sys::locks. This cleans up the the std::sys modules a bit by putting the locks in a single module called `locks` rather than spread over the three modules `mutex`, `condvar`, and `rwlock`. This makes it easier to organise lock implementations, which helps with rust-lang#93740.
2 parents b7f7e50 + 8c60dbc commit 5fdaaa7

32 files changed

+133
-100
lines changed

std/src/sys/hermit/condvar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::ffi::c_void;
22
use crate::ptr;
33
use crate::sync::atomic::{AtomicUsize, Ordering::SeqCst};
44
use crate::sys::hermit::abi;
5-
use crate::sys::mutex::Mutex;
5+
use crate::sys::locks::Mutex;
66
use crate::time::Duration;
77

88
// The implementation is inspired by Andrew D. Birrell's paper

std/src/sys/hermit/mod.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ pub mod alloc;
2222
pub mod args;
2323
#[path = "../unix/cmath.rs"]
2424
pub mod cmath;
25-
pub mod condvar;
2625
pub mod env;
2726
pub mod fd;
2827
pub mod fs;
2928
#[path = "../unsupported/io.rs"]
3029
pub mod io;
3130
pub mod memchr;
32-
pub mod mutex;
3331
pub mod net;
3432
pub mod os;
3533
#[path = "../unix/os_str.rs"]
@@ -40,14 +38,23 @@ pub mod path;
4038
pub mod pipe;
4139
#[path = "../unsupported/process.rs"]
4240
pub mod process;
43-
pub mod rwlock;
4441
pub mod stdio;
4542
pub mod thread;
4643
pub mod thread_local_dtor;
4744
#[path = "../unsupported/thread_local_key.rs"]
4845
pub mod thread_local_key;
4946
pub mod time;
5047

48+
mod condvar;
49+
mod mutex;
50+
mod rwlock;
51+
52+
pub mod locks {
53+
pub use super::condvar::*;
54+
pub use super::mutex::*;
55+
pub use super::rwlock::*;
56+
}
57+
5158
use crate::io::ErrorKind;
5259

5360
#[allow(unused_extern_crates)]

std/src/sys/hermit/rwlock.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::cell::UnsafeCell;
2-
use crate::sys::condvar::Condvar;
3-
use crate::sys::mutex::Mutex;
2+
use crate::sys::locks::{Condvar, Mutex};
43

54
pub struct RWLock {
65
lock: Mutex,

std/src/sys/itron/condvar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! POSIX conditional variable implementation based on user-space wait queues.
22
use super::{abi, error::expect_success_aborting, spin::SpinMutex, task, time::with_tmos_strong};
3-
use crate::{mem::replace, ptr::NonNull, sys::mutex::Mutex, time::Duration};
3+
use crate::{mem::replace, ptr::NonNull, sys::locks::Mutex, time::Duration};
44

55
// The implementation is inspired by the queue-based implementation shown in
66
// Andrew D. Birrell's paper "Implementing Condition Variables with Semaphores"

std/src/sys/sgx/condvar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::sys::mutex::Mutex;
1+
use crate::sys::locks::Mutex;
22
use crate::time::Duration;
33

44
use super::waitqueue::{SpinMutex, WaitQueue, WaitVariable};

std/src/sys/sgx/mod.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ pub mod alloc;
1515
pub mod args;
1616
#[path = "../unix/cmath.rs"]
1717
pub mod cmath;
18-
pub mod condvar;
1918
pub mod env;
2019
pub mod fd;
2120
#[path = "../unsupported/fs.rs"]
2221
pub mod fs;
2322
#[path = "../unsupported/io.rs"]
2423
pub mod io;
2524
pub mod memchr;
26-
pub mod mutex;
2725
pub mod net;
2826
pub mod os;
2927
#[path = "../unix/os_str.rs"]
@@ -33,12 +31,21 @@ pub mod path;
3331
pub mod pipe;
3432
#[path = "../unsupported/process.rs"]
3533
pub mod process;
36-
pub mod rwlock;
3734
pub mod stdio;
3835
pub mod thread;
3936
pub mod thread_local_key;
4037
pub mod time;
4138

39+
mod condvar;
40+
mod mutex;
41+
mod rwlock;
42+
43+
pub mod locks {
44+
pub use super::condvar::*;
45+
pub use super::mutex::*;
46+
pub use super::rwlock::*;
47+
}
48+
4249
// SAFETY: must be called only once during runtime initialization.
4350
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
4451
pub unsafe fn init(argc: isize, argv: *const *const u8) {

std/src/sys/solid/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,21 @@ pub mod path;
3737
pub mod pipe;
3838
#[path = "../unsupported/process.rs"]
3939
pub mod process;
40-
pub mod rwlock;
4140
pub mod stdio;
42-
pub use self::itron::{condvar, mutex, thread};
41+
pub use self::itron::thread;
4342
pub mod memchr;
4443
pub mod thread_local_dtor;
4544
pub mod thread_local_key;
4645
pub mod time;
4746

47+
mod rwlock;
48+
49+
pub mod locks {
50+
pub use super::itron::condvar::*;
51+
pub use super::itron::mutex::*;
52+
pub use super::rwlock::*;
53+
}
54+
4855
// SAFETY: must be called only once during runtime initialization.
4956
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
5057
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}

std/src/sys/unix/locks/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mod pthread_condvar;
2+
mod pthread_mutex;
3+
mod pthread_remutex;
4+
mod pthread_rwlock;
5+
pub use pthread_condvar::{Condvar, MovableCondvar};
6+
pub use pthread_mutex::{MovableMutex, Mutex};
7+
pub use pthread_remutex::ReentrantMutex;
8+
pub use pthread_rwlock::{MovableRWLock, RWLock};

std/src/sys/unix/condvar.rs renamed to std/src/sys/unix/locks/pthread_condvar.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::cell::UnsafeCell;
2-
use crate::sys::mutex::{self, Mutex};
2+
use crate::sys::locks::{pthread_mutex, Mutex};
33
use crate::time::Duration;
44

55
pub struct Condvar {
@@ -79,7 +79,7 @@ impl Condvar {
7979

8080
#[inline]
8181
pub unsafe fn wait(&self, mutex: &Mutex) {
82-
let r = libc::pthread_cond_wait(self.inner.get(), mutex::raw(mutex));
82+
let r = libc::pthread_cond_wait(self.inner.get(), pthread_mutex::raw(mutex));
8383
debug_assert_eq!(r, 0);
8484
}
8585

@@ -111,7 +111,7 @@ impl Condvar {
111111
let timeout =
112112
sec.map(|s| libc::timespec { tv_sec: s, tv_nsec: nsec as _ }).unwrap_or(TIMESPEC_MAX);
113113

114-
let r = libc::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex), &timeout);
114+
let r = libc::pthread_cond_timedwait(self.inner.get(), pthread_mutex::raw(mutex), &timeout);
115115
assert!(r == libc::ETIMEDOUT || r == 0);
116116
r == 0
117117
}
@@ -169,7 +169,7 @@ impl Condvar {
169169
.unwrap_or(TIMESPEC_MAX);
170170

171171
// And wait!
172-
let r = libc::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex), &timeout);
172+
let r = libc::pthread_cond_timedwait(self.inner.get(), pthread_mutex::raw(mutex), &timeout);
173173
debug_assert!(r == libc::ETIMEDOUT || r == 0);
174174

175175
// ETIMEDOUT is not a totally reliable method of determining timeout due

std/src/sys/unix/mutex.rs renamed to std/src/sys/unix/locks/pthread_mutex.rs

+1-43
Original file line numberDiff line numberDiff line change
@@ -90,49 +90,7 @@ impl Mutex {
9090
}
9191
}
9292

93-
pub struct ReentrantMutex {
94-
inner: UnsafeCell<libc::pthread_mutex_t>,
95-
}
96-
97-
unsafe impl Send for ReentrantMutex {}
98-
unsafe impl Sync for ReentrantMutex {}
99-
100-
impl ReentrantMutex {
101-
pub const unsafe fn uninitialized() -> ReentrantMutex {
102-
ReentrantMutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) }
103-
}
104-
105-
pub unsafe fn init(&self) {
106-
let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit();
107-
cvt_nz(libc::pthread_mutexattr_init(attr.as_mut_ptr())).unwrap();
108-
let attr = PthreadMutexAttr(&mut attr);
109-
cvt_nz(libc::pthread_mutexattr_settype(attr.0.as_mut_ptr(), libc::PTHREAD_MUTEX_RECURSIVE))
110-
.unwrap();
111-
cvt_nz(libc::pthread_mutex_init(self.inner.get(), attr.0.as_ptr())).unwrap();
112-
}
113-
114-
pub unsafe fn lock(&self) {
115-
let result = libc::pthread_mutex_lock(self.inner.get());
116-
debug_assert_eq!(result, 0);
117-
}
118-
119-
#[inline]
120-
pub unsafe fn try_lock(&self) -> bool {
121-
libc::pthread_mutex_trylock(self.inner.get()) == 0
122-
}
123-
124-
pub unsafe fn unlock(&self) {
125-
let result = libc::pthread_mutex_unlock(self.inner.get());
126-
debug_assert_eq!(result, 0);
127-
}
128-
129-
pub unsafe fn destroy(&self) {
130-
let result = libc::pthread_mutex_destroy(self.inner.get());
131-
debug_assert_eq!(result, 0);
132-
}
133-
}
134-
135-
struct PthreadMutexAttr<'a>(&'a mut MaybeUninit<libc::pthread_mutexattr_t>);
93+
pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit<libc::pthread_mutexattr_t>);
13694

13795
impl Drop for PthreadMutexAttr<'_> {
13896
fn drop(&mut self) {
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use super::pthread_mutex::PthreadMutexAttr;
2+
use crate::cell::UnsafeCell;
3+
use crate::mem::MaybeUninit;
4+
use crate::sys::cvt_nz;
5+
6+
pub struct ReentrantMutex {
7+
inner: UnsafeCell<libc::pthread_mutex_t>,
8+
}
9+
10+
unsafe impl Send for ReentrantMutex {}
11+
unsafe impl Sync for ReentrantMutex {}
12+
13+
impl ReentrantMutex {
14+
pub const unsafe fn uninitialized() -> ReentrantMutex {
15+
ReentrantMutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) }
16+
}
17+
18+
pub unsafe fn init(&self) {
19+
let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit();
20+
cvt_nz(libc::pthread_mutexattr_init(attr.as_mut_ptr())).unwrap();
21+
let attr = PthreadMutexAttr(&mut attr);
22+
cvt_nz(libc::pthread_mutexattr_settype(attr.0.as_mut_ptr(), libc::PTHREAD_MUTEX_RECURSIVE))
23+
.unwrap();
24+
cvt_nz(libc::pthread_mutex_init(self.inner.get(), attr.0.as_ptr())).unwrap();
25+
}
26+
27+
pub unsafe fn lock(&self) {
28+
let result = libc::pthread_mutex_lock(self.inner.get());
29+
debug_assert_eq!(result, 0);
30+
}
31+
32+
#[inline]
33+
pub unsafe fn try_lock(&self) -> bool {
34+
libc::pthread_mutex_trylock(self.inner.get()) == 0
35+
}
36+
37+
pub unsafe fn unlock(&self) {
38+
let result = libc::pthread_mutex_unlock(self.inner.get());
39+
debug_assert_eq!(result, 0);
40+
}
41+
42+
pub unsafe fn destroy(&self) {
43+
let result = libc::pthread_mutex_destroy(self.inner.get());
44+
debug_assert_eq!(result, 0);
45+
}
46+
}
File renamed without changes.

std/src/sys/unix/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub mod android;
1414
pub mod args;
1515
#[path = "../unix/cmath.rs"]
1616
pub mod cmath;
17-
pub mod condvar;
1817
pub mod env;
1918
pub mod fd;
2019
pub mod fs;
@@ -24,8 +23,8 @@ pub mod io;
2423
pub mod kernel_copy;
2524
#[cfg(target_os = "l4re")]
2625
mod l4re;
26+
pub mod locks;
2727
pub mod memchr;
28-
pub mod mutex;
2928
#[cfg(not(target_os = "l4re"))]
3029
pub mod net;
3130
#[cfg(target_os = "l4re")]
@@ -36,7 +35,6 @@ pub mod path;
3635
pub mod pipe;
3736
pub mod process;
3837
pub mod rand;
39-
pub mod rwlock;
4038
pub mod stack_overflow;
4139
pub mod stdio;
4240
pub mod thread;

std/src/sys/unsupported/condvar.rs renamed to std/src/sys/unsupported/locks/condvar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::sys::mutex::Mutex;
1+
use crate::sys::locks::Mutex;
22
use crate::time::Duration;
33

44
pub struct Condvar {}

std/src/sys/unsupported/locks/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mod condvar;
2+
mod mutex;
3+
mod rwlock;
4+
pub use condvar::{Condvar, MovableCondvar};
5+
pub use mutex::{MovableMutex, Mutex, ReentrantMutex};
6+
pub use rwlock::{MovableRWLock, RWLock};

std/src/sys/unsupported/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ pub mod alloc;
44
pub mod args;
55
#[path = "../unix/cmath.rs"]
66
pub mod cmath;
7-
pub mod condvar;
87
pub mod env;
98
pub mod fs;
109
pub mod io;
11-
pub mod mutex;
10+
pub mod locks;
1211
pub mod net;
1312
pub mod os;
1413
#[path = "../unix/os_str.rs"]
@@ -17,7 +16,6 @@ pub mod os_str;
1716
pub mod path;
1817
pub mod pipe;
1918
pub mod process;
20-
pub mod rwlock;
2119
pub mod stdio;
2220
pub mod thread;
2321
#[cfg(target_thread_local)]

std/src/sys/wasi/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ pub mod alloc;
2222
pub mod args;
2323
#[path = "../unix/cmath.rs"]
2424
pub mod cmath;
25-
#[path = "../unsupported/condvar.rs"]
26-
pub mod condvar;
2725
pub mod env;
2826
pub mod fd;
2927
pub mod fs;
3028
pub mod io;
31-
#[path = "../unsupported/mutex.rs"]
32-
pub mod mutex;
29+
#[path = "../unsupported/locks/mod.rs"]
30+
pub mod locks;
3331
pub mod net;
3432
pub mod os;
3533
#[path = "../unix/os_str.rs"]
@@ -40,8 +38,6 @@ pub mod path;
4038
pub mod pipe;
4139
#[path = "../unsupported/process.rs"]
4240
pub mod process;
43-
#[path = "../unsupported/rwlock.rs"]
44-
pub mod rwlock;
4541
pub mod stdio;
4642
pub mod thread;
4743
#[path = "../unsupported/thread_local_dtor.rs"]

std/src/sys/wasm/atomics/condvar.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::arch::wasm32;
22
use crate::cmp;
33
use crate::mem;
44
use crate::sync::atomic::{AtomicUsize, Ordering::SeqCst};
5-
use crate::sys::mutex::Mutex;
5+
use crate::sys::locks::Mutex;
66
use crate::time::Duration;
77

88
pub struct Condvar {

std/src/sys/wasm/atomics/rwlock.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::cell::UnsafeCell;
2-
use crate::sys::condvar::Condvar;
3-
use crate::sys::mutex::Mutex;
2+
use crate::sys::locks::{Condvar, Mutex};
43

54
pub struct RWLock {
65
lock: Mutex,

0 commit comments

Comments
 (0)