diff options
| author | bors <bors@rust-lang.org> | 2020-10-04 06:48:17 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-10-04 06:48:17 +0000 |
| commit | 32cbc65e6bf793d99dc609d11f4a4c93176cdbe2 (patch) | |
| tree | 63c17253897262e610c61eb3c67144edf0da4186 /library/std/src/sys | |
| parent | 2251766944f355a039e67aeb13ab630b2d46bf9b (diff) | |
| parent | b1ce7a38a6c03ddff23ef7e59e74cab6452ed9b0 (diff) | |
| download | rust-32cbc65e6bf793d99dc609d11f4a4c93176cdbe2.tar.gz rust-32cbc65e6bf793d99dc609d11f4a4c93176cdbe2.zip | |
Auto merge of #77380 - fusion-engineering-forks:unbox-the-mutex, r=dtolnay
Unbox mutexes and condvars on some platforms Both mutexes and condition variables contained a Box containing the actual os-specific object. This was done because moving these objects may cause undefined behaviour on some platforms. However, this is not needed on Windows[1], Wasm[2], cloudabi[2], and 'unsupported'[3], were the box was only needlessly making them less efficient. This change gets rid of the box on those platforms. On those platforms, `Condvar` can no longer verify it is only used with one `Mutex`, as mutexes no longer have a stable address. This was addressed and considered acceptable in #76932. [1]\: https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-initializesrwlock [2]\: These are just a single atomic integer together with futex wait/wake calls/instructions. [3]\: The `unsupported` platform doesn't support multiple threads at all.
Diffstat (limited to 'library/std/src/sys')
| -rw-r--r-- | library/std/src/sys/cloudabi/condvar.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/cloudabi/mutex.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/hermit/condvar.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/sgx/condvar.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/sgx/mutex.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/unix/condvar.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/unix/mutex.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/unsupported/condvar.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/unsupported/mutex.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/vxworks/condvar.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/vxworks/mutex.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/wasm/condvar_atomics.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/wasm/mutex_atomics.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/windows/condvar.rs | 2 | ||||
| -rw-r--r-- | library/std/src/sys/windows/mutex.rs | 5 |
15 files changed, 33 insertions, 0 deletions
diff --git a/library/std/src/sys/cloudabi/condvar.rs b/library/std/src/sys/cloudabi/condvar.rs index dabdc0c9b51..82d89b260fa 100644 --- a/library/std/src/sys/cloudabi/condvar.rs +++ b/library/std/src/sys/cloudabi/condvar.rs @@ -15,6 +15,8 @@ pub struct Condvar { condvar: UnsafeCell<AtomicU32>, } +pub type MovableCondvar = Condvar; + unsafe impl Send for Condvar {} unsafe impl Sync for Condvar {} diff --git a/library/std/src/sys/cloudabi/mutex.rs b/library/std/src/sys/cloudabi/mutex.rs index 580ab0e8ad8..66839e05bf0 100644 --- a/library/std/src/sys/cloudabi/mutex.rs +++ b/library/std/src/sys/cloudabi/mutex.rs @@ -15,6 +15,8 @@ extern "C" { // implemented identically. pub struct Mutex(RWLock); +pub type MovableMutex = Mutex; + pub unsafe fn raw(m: &Mutex) -> *mut AtomicU32 { rwlock::raw(&m.0) } diff --git a/library/std/src/sys/hermit/condvar.rs b/library/std/src/sys/hermit/condvar.rs index 52c8c3b17e8..b45e8718f08 100644 --- a/library/std/src/sys/hermit/condvar.rs +++ b/library/std/src/sys/hermit/condvar.rs @@ -14,6 +14,8 @@ pub struct Condvar { sem2: *const c_void, } +pub type MovableCondvar = Box<Condvar>; + unsafe impl Send for Condvar {} unsafe impl Sync for Condvar {} diff --git a/library/std/src/sys/sgx/condvar.rs b/library/std/src/sys/sgx/condvar.rs index ed6dbcf4971..dfe22e6a1b3 100644 --- a/library/std/src/sys/sgx/condvar.rs +++ b/library/std/src/sys/sgx/condvar.rs @@ -7,6 +7,8 @@ pub struct Condvar { inner: SpinMutex<WaitVariable<()>>, } +pub type MovableCondvar = Box<Condvar>; + impl Condvar { pub const fn new() -> Condvar { Condvar { inner: SpinMutex::new(WaitVariable::new(())) } diff --git a/library/std/src/sys/sgx/mutex.rs b/library/std/src/sys/sgx/mutex.rs index 4911c2f5387..8874517dac6 100644 --- a/library/std/src/sys/sgx/mutex.rs +++ b/library/std/src/sys/sgx/mutex.rs @@ -8,6 +8,8 @@ pub struct Mutex { inner: SpinMutex<WaitVariable<bool>>, } +pub type MovableMutex = Box<Mutex>; + // Implementation according to “Operating Systems: Three Easy Pieces”, chapter 28 impl Mutex { pub const fn new() -> Mutex { diff --git a/library/std/src/sys/unix/condvar.rs b/library/std/src/sys/unix/condvar.rs index 9f1847943f3..e38f91af9f0 100644 --- a/library/std/src/sys/unix/condvar.rs +++ b/library/std/src/sys/unix/condvar.rs @@ -6,6 +6,8 @@ pub struct Condvar { inner: UnsafeCell<libc::pthread_cond_t>, } +pub type MovableCondvar = Box<Condvar>; + unsafe impl Send for Condvar {} unsafe impl Sync for Condvar {} diff --git a/library/std/src/sys/unix/mutex.rs b/library/std/src/sys/unix/mutex.rs index 45c600f75f5..ebc737b75ae 100644 --- a/library/std/src/sys/unix/mutex.rs +++ b/library/std/src/sys/unix/mutex.rs @@ -5,6 +5,8 @@ pub struct Mutex { inner: UnsafeCell<libc::pthread_mutex_t>, } +pub type MovableMutex = Box<Mutex>; + #[inline] pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t { m.inner.get() diff --git a/library/std/src/sys/unsupported/condvar.rs b/library/std/src/sys/unsupported/condvar.rs index a578eee8ccc..35d12a69c8a 100644 --- a/library/std/src/sys/unsupported/condvar.rs +++ b/library/std/src/sys/unsupported/condvar.rs @@ -3,6 +3,8 @@ use crate::time::Duration; pub struct Condvar {} +pub type MovableCondvar = Condvar; + impl Condvar { pub const fn new() -> Condvar { Condvar {} diff --git a/library/std/src/sys/unsupported/mutex.rs b/library/std/src/sys/unsupported/mutex.rs index 9ef8af52eb5..06ea9a1e2c1 100644 --- a/library/std/src/sys/unsupported/mutex.rs +++ b/library/std/src/sys/unsupported/mutex.rs @@ -4,6 +4,8 @@ pub struct Mutex { locked: UnsafeCell<bool>, } +pub type MovableMutex = Mutex; + unsafe impl Send for Mutex {} unsafe impl Sync for Mutex {} // no threads on this platform diff --git a/library/std/src/sys/vxworks/condvar.rs b/library/std/src/sys/vxworks/condvar.rs index 5a77966d974..b4724be7c7c 100644 --- a/library/std/src/sys/vxworks/condvar.rs +++ b/library/std/src/sys/vxworks/condvar.rs @@ -6,6 +6,8 @@ pub struct Condvar { inner: UnsafeCell<libc::pthread_cond_t>, } +pub type MovableCondvar = Box<Condvar>; + unsafe impl Send for Condvar {} unsafe impl Sync for Condvar {} diff --git a/library/std/src/sys/vxworks/mutex.rs b/library/std/src/sys/vxworks/mutex.rs index 103d87e3d2f..dd7582c21a7 100644 --- a/library/std/src/sys/vxworks/mutex.rs +++ b/library/std/src/sys/vxworks/mutex.rs @@ -5,6 +5,8 @@ pub struct Mutex { inner: UnsafeCell<libc::pthread_mutex_t>, } +pub type MovableMutex = Box<Mutex>; + #[inline] pub unsafe fn raw(m: &Mutex) -> *mut libc::pthread_mutex_t { m.inner.get() diff --git a/library/std/src/sys/wasm/condvar_atomics.rs b/library/std/src/sys/wasm/condvar_atomics.rs index d86bb60507b..a96bb18e6ef 100644 --- a/library/std/src/sys/wasm/condvar_atomics.rs +++ b/library/std/src/sys/wasm/condvar_atomics.rs @@ -9,6 +9,8 @@ pub struct Condvar { cnt: AtomicUsize, } +pub type MovableCondvar = Condvar; + // Condition variables are implemented with a simple counter internally that is // likely to cause spurious wakeups. Blocking on a condition variable will first // read the value of the internal counter, unlock the given mutex, and then diff --git a/library/std/src/sys/wasm/mutex_atomics.rs b/library/std/src/sys/wasm/mutex_atomics.rs index 4b1a7c9b481..2970fcf806c 100644 --- a/library/std/src/sys/wasm/mutex_atomics.rs +++ b/library/std/src/sys/wasm/mutex_atomics.rs @@ -8,6 +8,8 @@ pub struct Mutex { locked: AtomicUsize, } +pub type MovableMutex = Mutex; + // Mutexes have a pretty simple implementation where they contain an `i32` // internally that is 0 when unlocked and 1 when the mutex is locked. // Acquisition has a fast path where it attempts to cmpxchg the 0 to a 1, and diff --git a/library/std/src/sys/windows/condvar.rs b/library/std/src/sys/windows/condvar.rs index 8f7f6854cc2..44547a5c51a 100644 --- a/library/std/src/sys/windows/condvar.rs +++ b/library/std/src/sys/windows/condvar.rs @@ -8,6 +8,8 @@ pub struct Condvar { inner: UnsafeCell<c::CONDITION_VARIABLE>, } +pub type MovableCondvar = Condvar; + unsafe impl Send for Condvar {} unsafe impl Sync for Condvar {} diff --git a/library/std/src/sys/windows/mutex.rs b/library/std/src/sys/windows/mutex.rs index e2aaca59fe2..fa51b006c34 100644 --- a/library/std/src/sys/windows/mutex.rs +++ b/library/std/src/sys/windows/mutex.rs @@ -29,6 +29,11 @@ pub struct Mutex { lock: AtomicUsize, } +// Windows SRW Locks are movable (while not borrowed). +// ReentrantMutexes (in Inner) are not, but those are stored indirectly through +// a Box, so do not move when the Mutex it self is moved. +pub type MovableMutex = Mutex; + unsafe impl Send for Mutex {} unsafe impl Sync for Mutex {} |
