Skip to content

Commit 9c74640

Browse files
authored
Rollup merge of #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 #93740.
2 parents e77d593 + 8ddb34d commit 9c74640

File tree

26 files changed

+127
-92
lines changed

26 files changed

+127
-92
lines changed

library/std/src/sys/hermit/mod.rs

Lines changed: 10 additions & 3 deletions
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)]

library/std/src/sys/sgx/mod.rs

Lines changed: 10 additions & 3 deletions
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) {

library/std/src/sys/solid/mod.rs

Lines changed: 9 additions & 2 deletions
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) {}

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

Lines changed: 8 additions & 0 deletions
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};

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

Lines changed: 4 additions & 4 deletions
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

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

Lines changed: 1 addition & 43 deletions
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) {
Lines changed: 46 additions & 0 deletions
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+
}

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

Lines changed: 1 addition & 3 deletions
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;

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

Lines changed: 1 addition & 1 deletion
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 {}
Lines changed: 6 additions & 0 deletions
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};

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

Lines changed: 1 addition & 3 deletions
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)]

library/std/src/sys/wasi/mod.rs

Lines changed: 2 additions & 6 deletions
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"]

library/std/src/sys/wasm/mod.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,23 @@ pub mod time;
5050
cfg_if::cfg_if! {
5151
if #[cfg(target_feature = "atomics")] {
5252
#[path = "atomics/condvar.rs"]
53-
pub mod condvar;
53+
mod condvar;
5454
#[path = "atomics/mutex.rs"]
55-
pub mod mutex;
55+
mod mutex;
5656
#[path = "atomics/rwlock.rs"]
57-
pub mod rwlock;
57+
mod rwlock;
58+
pub mod locks {
59+
pub use super::condvar::*;
60+
pub use super::mutex::*;
61+
pub use super::rwlock::*;
62+
}
5863
#[path = "atomics/futex.rs"]
5964
pub mod futex;
6065
#[path = "atomics/thread.rs"]
6166
pub mod thread;
6267
} else {
63-
#[path = "../unsupported/condvar.rs"]
64-
pub mod condvar;
65-
#[path = "../unsupported/mutex.rs"]
66-
pub mod mutex;
67-
#[path = "../unsupported/rwlock.rs"]
68-
pub mod rwlock;
68+
#[path = "../unsupported/locks/mod.rs"]
69+
pub mod locks;
6970
#[path = "../unsupported/thread.rs"]
7071
pub mod thread;
7172
}

library/std/src/sys/windows/condvar.rs renamed to library/std/src/sys/windows/locks/condvar.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cell::UnsafeCell;
22
use crate::sys::c;
3-
use crate::sys::mutex::{self, Mutex};
3+
use crate::sys::locks::{mutex, Mutex};
44
use crate::sys::os;
55
use crate::time::Duration;
66

@@ -31,7 +31,7 @@ impl Condvar {
3131
let r = c::SleepConditionVariableSRW(
3232
self.inner.get(),
3333
mutex::raw(mutex),
34-
super::dur2timeout(dur),
34+
crate::sys::windows::dur2timeout(dur),
3535
0,
3636
);
3737
if r == 0 {
Lines changed: 6 additions & 0 deletions
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};

0 commit comments

Comments
 (0)