diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2020-10-01 01:06:35 +0200 |
|---|---|---|
| committer | Mara Bos <m-ou.se@m-ou.se> | 2020-10-02 09:47:08 +0200 |
| commit | 58deb7001deceb74cec38590a161d781d5d953b4 (patch) | |
| tree | 27097158ebb5b75a191b13c903d15d5fda0a3140 /library/std/src/sys_common | |
| parent | a8c2d4fc3d29496aa0a3563ec9d44f6222597fe3 (diff) | |
| download | rust-58deb7001deceb74cec38590a161d781d5d953b4.tar.gz rust-58deb7001deceb74cec38590a161d781d5d953b4.zip | |
Make it possible to have unboxed mutexes on specific platforms.
This commit keeps all mutexes boxed on all platforms, but makes it trivial to remove the box on some platforms later.
Diffstat (limited to 'library/std/src/sys_common')
| -rw-r--r-- | library/std/src/sys_common/condvar.rs | 4 | ||||
| -rw-r--r-- | library/std/src/sys_common/condvar/check.rs | 33 | ||||
| -rw-r--r-- | library/std/src/sys_common/mutex.rs | 9 |
3 files changed, 37 insertions, 9 deletions
diff --git a/library/std/src/sys_common/condvar.rs b/library/std/src/sys_common/condvar.rs index c65f1f81509..acd8b69e9ac 100644 --- a/library/std/src/sys_common/condvar.rs +++ b/library/std/src/sys_common/condvar.rs @@ -1,10 +1,12 @@ use crate::sys::condvar as imp; +use crate::sys::mutex as mutex_imp; use crate::sys_common::mutex::MovableMutex; use crate::time::Duration; -use check::CondvarCheck; mod check; +type CondvarCheck = <mutex_imp::MovableMutex as check::CondvarCheck>::Check; + /// An OS-based condition variable. pub struct Condvar { inner: Box<imp::Condvar>, diff --git a/library/std/src/sys_common/condvar/check.rs b/library/std/src/sys_common/condvar/check.rs index 949b53f30b1..fecb732b910 100644 --- a/library/std/src/sys_common/condvar/check.rs +++ b/library/std/src/sys_common/condvar/check.rs @@ -2,13 +2,22 @@ use crate::sync::atomic::{AtomicUsize, Ordering}; use crate::sys::mutex as mutex_imp; use crate::sys_common::mutex::MovableMutex; -/// A `Condvar` will check it's only ever used with the same mutex, based on -/// its (stable) address. -pub struct CondvarCheck { +pub trait CondvarCheck { + type Check; +} + +/// For boxed mutexes, a `Condvar` will check it's only ever used with the same +/// mutex, based on its (stable) address. +impl CondvarCheck for Box<mutex_imp::Mutex> { + type Check = SameMutexCheck; +} + +pub struct SameMutexCheck { addr: AtomicUsize, } -impl CondvarCheck { +#[allow(dead_code)] +impl SameMutexCheck { pub const fn new() -> Self { Self { addr: AtomicUsize::new(0) } } @@ -21,3 +30,19 @@ impl CondvarCheck { } } } + +/// Unboxed mutexes may move, so `Condvar` can not require its address to stay +/// constant. +impl CondvarCheck for mutex_imp::Mutex { + type Check = NoCheck; +} + +pub struct NoCheck; + +#[allow(dead_code)] +impl NoCheck { + pub const fn new() -> Self { + Self + } + pub fn verify(&self, _: &MovableMutex) {} +} diff --git a/library/std/src/sys_common/mutex.rs b/library/std/src/sys_common/mutex.rs index e30163b3a89..a1e11d24465 100644 --- a/library/std/src/sys_common/mutex.rs +++ b/library/std/src/sys_common/mutex.rs @@ -58,16 +58,17 @@ impl Drop for StaticMutexGuard<'_> { /// /// This mutex does not implement poisoning. /// -/// This is a wrapper around `Box<imp::Mutex>`, to allow the object to be moved -/// without moving the raw mutex. -pub struct MovableMutex(Box<imp::Mutex>); +/// This is either a wrapper around `Box<imp::Mutex>` or `imp::Mutex`, +/// depending on the platform. It is boxed on platforms where `imp::Mutex` may +/// not be moved. +pub struct MovableMutex(imp::MovableMutex); unsafe impl Sync for MovableMutex {} impl MovableMutex { /// Creates a new mutex. pub fn new() -> Self { - let mut mutex = box imp::Mutex::new(); + let mut mutex = imp::MovableMutex::from(imp::Mutex::new()); unsafe { mutex.init() }; Self(mutex) } |
