diff options
| author | bors <bors@rust-lang.org> | 2022-03-23 06:01:48 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-03-23 06:01:48 +0000 |
| commit | 36748cf814dcf6bbd6408e925a0b4770b7d47719 (patch) | |
| tree | 77c3dc03911ea12ff43022a5a59316e18436668b /library/std/src/sys | |
| parent | 7b0bf9efc939341b48c6e9a335dee8a280085100 (diff) | |
| parent | 733153f2e550d46fe6f794c969df91368580e0b8 (diff) | |
| download | rust-36748cf814dcf6bbd6408e925a0b4770b7d47719.tar.gz rust-36748cf814dcf6bbd6408e925a0b4770b7d47719.zip | |
Auto 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 https://github.com/rust-lang/rust/issues/93740.
Diffstat (limited to 'library/std/src/sys')
27 files changed, 124 insertions, 90 deletions
diff --git a/library/std/src/sys/hermit/condvar.rs b/library/std/src/sys/hermit/condvar.rs index b62f21a9dac..f6083530005 100644 --- a/library/std/src/sys/hermit/condvar.rs +++ b/library/std/src/sys/hermit/condvar.rs @@ -2,7 +2,7 @@ use crate::ffi::c_void; use crate::ptr; use crate::sync::atomic::{AtomicUsize, Ordering::SeqCst}; use crate::sys::hermit::abi; -use crate::sys::mutex::Mutex; +use crate::sys::locks::Mutex; use crate::time::Duration; // The implementation is inspired by Andrew D. Birrell's paper diff --git a/library/std/src/sys/hermit/mod.rs b/library/std/src/sys/hermit/mod.rs index b798c97448b..08eca423802 100644 --- a/library/std/src/sys/hermit/mod.rs +++ b/library/std/src/sys/hermit/mod.rs @@ -22,14 +22,12 @@ pub mod alloc; pub mod args; #[path = "../unix/cmath.rs"] pub mod cmath; -pub mod condvar; pub mod env; pub mod fd; pub mod fs; #[path = "../unsupported/io.rs"] pub mod io; pub mod memchr; -pub mod mutex; pub mod net; pub mod os; #[path = "../unix/os_str.rs"] @@ -40,7 +38,6 @@ pub mod path; pub mod pipe; #[path = "../unsupported/process.rs"] pub mod process; -pub mod rwlock; pub mod stdio; pub mod thread; pub mod thread_local_dtor; @@ -48,6 +45,16 @@ pub mod thread_local_dtor; pub mod thread_local_key; pub mod time; +mod condvar; +mod mutex; +mod rwlock; + +pub mod locks { + pub use super::condvar::*; + pub use super::mutex::*; + pub use super::rwlock::*; +} + use crate::io::ErrorKind; #[allow(unused_extern_crates)] diff --git a/library/std/src/sys/hermit/rwlock.rs b/library/std/src/sys/hermit/rwlock.rs index 64eaa2fc482..1cca809764c 100644 --- a/library/std/src/sys/hermit/rwlock.rs +++ b/library/std/src/sys/hermit/rwlock.rs @@ -1,6 +1,5 @@ use crate::cell::UnsafeCell; -use crate::sys::condvar::Condvar; -use crate::sys::mutex::Mutex; +use crate::sys::locks::{Condvar, Mutex}; pub struct RWLock { lock: Mutex, diff --git a/library/std/src/sys/itron/condvar.rs b/library/std/src/sys/itron/condvar.rs index 2992a6a5429..ed26c528027 100644 --- a/library/std/src/sys/itron/condvar.rs +++ b/library/std/src/sys/itron/condvar.rs @@ -1,6 +1,6 @@ //! POSIX conditional variable implementation based on user-space wait queues. use super::{abi, error::expect_success_aborting, spin::SpinMutex, task, time::with_tmos_strong}; -use crate::{mem::replace, ptr::NonNull, sys::mutex::Mutex, time::Duration}; +use crate::{mem::replace, ptr::NonNull, sys::locks::Mutex, time::Duration}; // The implementation is inspired by the queue-based implementation shown in // Andrew D. Birrell's paper "Implementing Condition Variables with Semaphores" diff --git a/library/std/src/sys/sgx/condvar.rs b/library/std/src/sys/sgx/condvar.rs index 55ac052d975..c9736880b08 100644 --- a/library/std/src/sys/sgx/condvar.rs +++ b/library/std/src/sys/sgx/condvar.rs @@ -1,4 +1,4 @@ -use crate::sys::mutex::Mutex; +use crate::sys::locks::Mutex; use crate::time::Duration; use super::waitqueue::{SpinMutex, WaitQueue, WaitVariable}; diff --git a/library/std/src/sys/sgx/mod.rs b/library/std/src/sys/sgx/mod.rs index 158c92e7a77..1333edb9881 100644 --- a/library/std/src/sys/sgx/mod.rs +++ b/library/std/src/sys/sgx/mod.rs @@ -15,7 +15,6 @@ pub mod alloc; pub mod args; #[path = "../unix/cmath.rs"] pub mod cmath; -pub mod condvar; pub mod env; pub mod fd; #[path = "../unsupported/fs.rs"] @@ -23,7 +22,6 @@ pub mod fs; #[path = "../unsupported/io.rs"] pub mod io; pub mod memchr; -pub mod mutex; pub mod net; pub mod os; #[path = "../unix/os_str.rs"] @@ -33,12 +31,21 @@ pub mod path; pub mod pipe; #[path = "../unsupported/process.rs"] pub mod process; -pub mod rwlock; pub mod stdio; pub mod thread; pub mod thread_local_key; pub mod time; +mod condvar; +mod mutex; +mod rwlock; + +pub mod locks { + pub use super::condvar::*; + pub use super::mutex::*; + pub use super::rwlock::*; +} + // SAFETY: must be called only once during runtime initialization. // NOTE: this is not guaranteed to run, for example when Rust code is called externally. pub unsafe fn init(argc: isize, argv: *const *const u8) { diff --git a/library/std/src/sys/solid/mod.rs b/library/std/src/sys/solid/mod.rs index 049460755d6..492b1a55475 100644 --- a/library/std/src/sys/solid/mod.rs +++ b/library/std/src/sys/solid/mod.rs @@ -37,14 +37,21 @@ pub mod path; pub mod pipe; #[path = "../unsupported/process.rs"] pub mod process; -pub mod rwlock; pub mod stdio; -pub use self::itron::{condvar, mutex, thread}; +pub use self::itron::thread; pub mod memchr; pub mod thread_local_dtor; pub mod thread_local_key; pub mod time; +mod rwlock; + +pub mod locks { + pub use super::itron::condvar::*; + pub use super::itron::mutex::*; + pub use super::rwlock::*; +} + // SAFETY: must be called only once during runtime initialization. // NOTE: this is not guaranteed to run, for example when Rust code is called externally. pub unsafe fn init(_argc: isize, _argv: *const *const u8) {} diff --git a/library/std/src/sys/unix/locks/mod.rs b/library/std/src/sys/unix/locks/mod.rs new file mode 100644 index 00000000000..f07a9f93b79 --- /dev/null +++ b/library/std/src/sys/unix/locks/mod.rs @@ -0,0 +1,8 @@ +mod pthread_condvar; +mod pthread_mutex; +mod pthread_remutex; +mod pthread_rwlock; +pub use pthread_condvar::{Condvar, MovableCondvar}; +pub use pthread_mutex::{MovableMutex, Mutex}; +pub use pthread_remutex::ReentrantMutex; +pub use pthread_rwlock::{MovableRWLock, RWLock}; diff --git a/library/std/src/sys/unix/condvar.rs b/library/std/src/sys/unix/locks/pthread_condvar.rs index 61261c0aa84..099aa68706f 100644 --- a/library/std/src/sys/unix/condvar.rs +++ b/library/std/src/sys/unix/locks/pthread_condvar.rs @@ -1,5 +1,5 @@ use crate::cell::UnsafeCell; -use crate::sys::mutex::{self, Mutex}; +use crate::sys::locks::{pthread_mutex, Mutex}; use crate::time::Duration; pub struct Condvar { @@ -79,7 +79,7 @@ impl Condvar { #[inline] pub unsafe fn wait(&self, mutex: &Mutex) { - let r = libc::pthread_cond_wait(self.inner.get(), mutex::raw(mutex)); + let r = libc::pthread_cond_wait(self.inner.get(), pthread_mutex::raw(mutex)); debug_assert_eq!(r, 0); } @@ -111,7 +111,7 @@ impl Condvar { let timeout = sec.map(|s| libc::timespec { tv_sec: s, tv_nsec: nsec as _ }).unwrap_or(TIMESPEC_MAX); - let r = libc::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex), &timeout); + let r = libc::pthread_cond_timedwait(self.inner.get(), pthread_mutex::raw(mutex), &timeout); assert!(r == libc::ETIMEDOUT || r == 0); r == 0 } @@ -169,7 +169,7 @@ impl Condvar { .unwrap_or(TIMESPEC_MAX); // And wait! - let r = libc::pthread_cond_timedwait(self.inner.get(), mutex::raw(mutex), &timeout); + let r = libc::pthread_cond_timedwait(self.inner.get(), pthread_mutex::raw(mutex), &timeout); debug_assert!(r == libc::ETIMEDOUT || r == 0); // ETIMEDOUT is not a totally reliable method of determining timeout due diff --git a/library/std/src/sys/unix/mutex.rs b/library/std/src/sys/unix/locks/pthread_mutex.rs index 89c55eb859d..09cfa2f50ec 100644 --- a/library/std/src/sys/unix/mutex.rs +++ b/library/std/src/sys/unix/locks/pthread_mutex.rs @@ -90,49 +90,7 @@ impl Mutex { } } -pub struct ReentrantMutex { - inner: UnsafeCell<libc::pthread_mutex_t>, -} - -unsafe impl Send for ReentrantMutex {} -unsafe impl Sync for ReentrantMutex {} - -impl ReentrantMutex { - pub const unsafe fn uninitialized() -> ReentrantMutex { - ReentrantMutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) } - } - - pub unsafe fn init(&self) { - let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit(); - cvt_nz(libc::pthread_mutexattr_init(attr.as_mut_ptr())).unwrap(); - let attr = PthreadMutexAttr(&mut attr); - cvt_nz(libc::pthread_mutexattr_settype(attr.0.as_mut_ptr(), libc::PTHREAD_MUTEX_RECURSIVE)) - .unwrap(); - cvt_nz(libc::pthread_mutex_init(self.inner.get(), attr.0.as_ptr())).unwrap(); - } - - pub unsafe fn lock(&self) { - let result = libc::pthread_mutex_lock(self.inner.get()); - debug_assert_eq!(result, 0); - } - - #[inline] - pub unsafe fn try_lock(&self) -> bool { - libc::pthread_mutex_trylock(self.inner.get()) == 0 - } - - pub unsafe fn unlock(&self) { - let result = libc::pthread_mutex_unlock(self.inner.get()); - debug_assert_eq!(result, 0); - } - - pub unsafe fn destroy(&self) { - let result = libc::pthread_mutex_destroy(self.inner.get()); - debug_assert_eq!(result, 0); - } -} - -struct PthreadMutexAttr<'a>(&'a mut MaybeUninit<libc::pthread_mutexattr_t>); +pub(super) struct PthreadMutexAttr<'a>(pub &'a mut MaybeUninit<libc::pthread_mutexattr_t>); impl Drop for PthreadMutexAttr<'_> { fn drop(&mut self) { diff --git a/library/std/src/sys/unix/locks/pthread_remutex.rs b/library/std/src/sys/unix/locks/pthread_remutex.rs new file mode 100644 index 00000000000..b006181ee3a --- /dev/null +++ b/library/std/src/sys/unix/locks/pthread_remutex.rs @@ -0,0 +1,46 @@ +use super::pthread_mutex::PthreadMutexAttr; +use crate::cell::UnsafeCell; +use crate::mem::MaybeUninit; +use crate::sys::cvt_nz; + +pub struct ReentrantMutex { + inner: UnsafeCell<libc::pthread_mutex_t>, +} + +unsafe impl Send for ReentrantMutex {} +unsafe impl Sync for ReentrantMutex {} + +impl ReentrantMutex { + pub const unsafe fn uninitialized() -> ReentrantMutex { + ReentrantMutex { inner: UnsafeCell::new(libc::PTHREAD_MUTEX_INITIALIZER) } + } + + pub unsafe fn init(&self) { + let mut attr = MaybeUninit::<libc::pthread_mutexattr_t>::uninit(); + cvt_nz(libc::pthread_mutexattr_init(attr.as_mut_ptr())).unwrap(); + let attr = PthreadMutexAttr(&mut attr); + cvt_nz(libc::pthread_mutexattr_settype(attr.0.as_mut_ptr(), libc::PTHREAD_MUTEX_RECURSIVE)) + .unwrap(); + cvt_nz(libc::pthread_mutex_init(self.inner.get(), attr.0.as_ptr())).unwrap(); + } + + pub unsafe fn lock(&self) { + let result = libc::pthread_mutex_lock(self.inner.get()); + debug_assert_eq!(result, 0); + } + + #[inline] + pub unsafe fn try_lock(&self) -> bool { + libc::pthread_mutex_trylock(self.inner.get()) == 0 + } + + pub unsafe fn unlock(&self) { + let result = libc::pthread_mutex_unlock(self.inner.get()); + debug_assert_eq!(result, 0); + } + + pub unsafe fn destroy(&self) { + let result = libc::pthread_mutex_destroy(self.inner.get()); + debug_assert_eq!(result, 0); + } +} diff --git a/library/std/src/sys/unix/rwlock.rs b/library/std/src/sys/unix/locks/pthread_rwlock.rs index 1318c5b8e3a..1318c5b8e3a 100644 --- a/library/std/src/sys/unix/rwlock.rs +++ b/library/std/src/sys/unix/locks/pthread_rwlock.rs diff --git a/library/std/src/sys/unix/mod.rs b/library/std/src/sys/unix/mod.rs index 7423d90263d..e65c11b6d09 100644 --- a/library/std/src/sys/unix/mod.rs +++ b/library/std/src/sys/unix/mod.rs @@ -14,7 +14,6 @@ pub mod android; pub mod args; #[path = "../unix/cmath.rs"] pub mod cmath; -pub mod condvar; pub mod env; pub mod fd; pub mod fs; @@ -24,8 +23,8 @@ pub mod io; pub mod kernel_copy; #[cfg(target_os = "l4re")] mod l4re; +pub mod locks; pub mod memchr; -pub mod mutex; #[cfg(not(target_os = "l4re"))] pub mod net; #[cfg(target_os = "l4re")] @@ -36,7 +35,6 @@ pub mod path; pub mod pipe; pub mod process; pub mod rand; -pub mod rwlock; pub mod stack_overflow; pub mod stdio; pub mod thread; diff --git a/library/std/src/sys/unsupported/condvar.rs b/library/std/src/sys/unsupported/locks/condvar.rs index 35d12a69c8a..8dbe03bad9b 100644 --- a/library/std/src/sys/unsupported/condvar.rs +++ b/library/std/src/sys/unsupported/locks/condvar.rs @@ -1,4 +1,4 @@ -use crate::sys::mutex::Mutex; +use crate::sys::locks::Mutex; use crate::time::Duration; pub struct Condvar {} diff --git a/library/std/src/sys/unsupported/locks/mod.rs b/library/std/src/sys/unsupported/locks/mod.rs new file mode 100644 index 00000000000..5634f106339 --- /dev/null +++ b/library/std/src/sys/unsupported/locks/mod.rs @@ -0,0 +1,6 @@ +mod condvar; +mod mutex; +mod rwlock; +pub use condvar::{Condvar, MovableCondvar}; +pub use mutex::{MovableMutex, Mutex, ReentrantMutex}; +pub use rwlock::{MovableRWLock, RWLock}; diff --git a/library/std/src/sys/unsupported/mutex.rs b/library/std/src/sys/unsupported/locks/mutex.rs index b3203c16c50..b3203c16c50 100644 --- a/library/std/src/sys/unsupported/mutex.rs +++ b/library/std/src/sys/unsupported/locks/mutex.rs diff --git a/library/std/src/sys/unsupported/rwlock.rs b/library/std/src/sys/unsupported/locks/rwlock.rs index 8438adeb5b5..8438adeb5b5 100644 --- a/library/std/src/sys/unsupported/rwlock.rs +++ b/library/std/src/sys/unsupported/locks/rwlock.rs diff --git a/library/std/src/sys/unsupported/mod.rs b/library/std/src/sys/unsupported/mod.rs index a1276193bda..7bf6d40b76d 100644 --- a/library/std/src/sys/unsupported/mod.rs +++ b/library/std/src/sys/unsupported/mod.rs @@ -4,11 +4,10 @@ pub mod alloc; pub mod args; #[path = "../unix/cmath.rs"] pub mod cmath; -pub mod condvar; pub mod env; pub mod fs; pub mod io; -pub mod mutex; +pub mod locks; pub mod net; pub mod os; #[path = "../unix/os_str.rs"] @@ -17,7 +16,6 @@ pub mod os_str; pub mod path; pub mod pipe; pub mod process; -pub mod rwlock; pub mod stdio; pub mod thread; #[cfg(target_thread_local)] diff --git a/library/std/src/sys/wasi/mod.rs b/library/std/src/sys/wasi/mod.rs index f878941939c..683a07a34dc 100644 --- a/library/std/src/sys/wasi/mod.rs +++ b/library/std/src/sys/wasi/mod.rs @@ -22,14 +22,12 @@ pub mod alloc; pub mod args; #[path = "../unix/cmath.rs"] pub mod cmath; -#[path = "../unsupported/condvar.rs"] -pub mod condvar; pub mod env; pub mod fd; pub mod fs; pub mod io; -#[path = "../unsupported/mutex.rs"] -pub mod mutex; +#[path = "../unsupported/locks/mod.rs"] +pub mod locks; pub mod net; pub mod os; #[path = "../unix/os_str.rs"] @@ -40,8 +38,6 @@ pub mod path; pub mod pipe; #[path = "../unsupported/process.rs"] pub mod process; -#[path = "../unsupported/rwlock.rs"] -pub mod rwlock; pub mod stdio; pub mod thread; #[path = "../unsupported/thread_local_dtor.rs"] diff --git a/library/std/src/sys/wasm/atomics/condvar.rs b/library/std/src/sys/wasm/atomics/condvar.rs index 0c1c076cc91..f06c07c5409 100644 --- a/library/std/src/sys/wasm/atomics/condvar.rs +++ b/library/std/src/sys/wasm/atomics/condvar.rs @@ -2,7 +2,7 @@ use crate::arch::wasm32; use crate::cmp; use crate::mem; use crate::sync::atomic::{AtomicUsize, Ordering::SeqCst}; -use crate::sys::mutex::Mutex; +use crate::sys::locks::Mutex; use crate::time::Duration; pub struct Condvar { diff --git a/library/std/src/sys/wasm/atomics/rwlock.rs b/library/std/src/sys/wasm/atomics/rwlock.rs index 64eaa2fc482..1cca809764c 100644 --- a/library/std/src/sys/wasm/atomics/rwlock.rs +++ b/library/std/src/sys/wasm/atomics/rwlock.rs @@ -1,6 +1,5 @@ use crate::cell::UnsafeCell; -use crate::sys::condvar::Condvar; -use crate::sys::mutex::Mutex; +use crate::sys::locks::{Condvar, Mutex}; pub struct RWLock { lock: Mutex, diff --git a/library/std/src/sys/wasm/mod.rs b/library/std/src/sys/wasm/mod.rs index c81d653a5e3..9f6700caf14 100644 --- a/library/std/src/sys/wasm/mod.rs +++ b/library/std/src/sys/wasm/mod.rs @@ -50,22 +50,23 @@ pub mod time; cfg_if::cfg_if! { if #[cfg(target_feature = "atomics")] { #[path = "atomics/condvar.rs"] - pub mod condvar; + mod condvar; #[path = "atomics/mutex.rs"] - pub mod mutex; + mod mutex; #[path = "atomics/rwlock.rs"] - pub mod rwlock; + mod rwlock; + pub mod locks { + pub use super::condvar::*; + pub use super::mutex::*; + pub use super::rwlock::*; + } #[path = "atomics/futex.rs"] pub mod futex; #[path = "atomics/thread.rs"] pub mod thread; } else { - #[path = "../unsupported/condvar.rs"] - pub mod condvar; - #[path = "../unsupported/mutex.rs"] - pub mod mutex; - #[path = "../unsupported/rwlock.rs"] - pub mod rwlock; + #[path = "../unsupported/locks/mod.rs"] + pub mod locks; #[path = "../unsupported/thread.rs"] pub mod thread; } diff --git a/library/std/src/sys/windows/condvar.rs b/library/std/src/sys/windows/locks/condvar.rs index 44547a5c51a..dfd8cfdceee 100644 --- a/library/std/src/sys/windows/condvar.rs +++ b/library/std/src/sys/windows/locks/condvar.rs @@ -1,6 +1,6 @@ use crate::cell::UnsafeCell; use crate::sys::c; -use crate::sys::mutex::{self, Mutex}; +use crate::sys::locks::{mutex, Mutex}; use crate::sys::os; use crate::time::Duration; @@ -31,7 +31,7 @@ impl Condvar { let r = c::SleepConditionVariableSRW( self.inner.get(), mutex::raw(mutex), - super::dur2timeout(dur), + crate::sys::windows::dur2timeout(dur), 0, ); if r == 0 { diff --git a/library/std/src/sys/windows/locks/mod.rs b/library/std/src/sys/windows/locks/mod.rs new file mode 100644 index 00000000000..5634f106339 --- /dev/null +++ b/library/std/src/sys/windows/locks/mod.rs @@ -0,0 +1,6 @@ +mod condvar; +mod mutex; +mod rwlock; +pub use condvar::{Condvar, MovableCondvar}; +pub use mutex::{MovableMutex, Mutex, ReentrantMutex}; +pub use rwlock::{MovableRWLock, RWLock}; diff --git a/library/std/src/sys/windows/mutex.rs b/library/std/src/sys/windows/locks/mutex.rs index 56f91ebe582..56f91ebe582 100644 --- a/library/std/src/sys/windows/mutex.rs +++ b/library/std/src/sys/windows/locks/mutex.rs diff --git a/library/std/src/sys/windows/rwlock.rs b/library/std/src/sys/windows/locks/rwlock.rs index b7a5b1e7acc..b7a5b1e7acc 100644 --- a/library/std/src/sys/windows/rwlock.rs +++ b/library/std/src/sys/windows/locks/rwlock.rs diff --git a/library/std/src/sys/windows/mod.rs b/library/std/src/sys/windows/mod.rs index 6097e628768..62814eaaa56 100644 --- a/library/std/src/sys/windows/mod.rs +++ b/library/std/src/sys/windows/mod.rs @@ -16,13 +16,12 @@ pub mod alloc; pub mod args; pub mod c; pub mod cmath; -pub mod condvar; pub mod env; pub mod fs; pub mod handle; pub mod io; +pub mod locks; pub mod memchr; -pub mod mutex; pub mod net; pub mod os; pub mod os_str; @@ -30,7 +29,6 @@ pub mod path; pub mod pipe; pub mod process; pub mod rand; -pub mod rwlock; pub mod thread; pub mod thread_local_dtor; pub mod thread_local_key; |
