about summary refs log tree commit diff
path: root/library/std/src/sys_common
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-11-12 01:31:39 +0000
committerbors <bors@rust-lang.org>2022-11-12 01:31:39 +0000
commitb0c6527912cee113b29a33d7db0e801a58a94de5 (patch)
treebf37c6e6c71262c0f2602f842a761880b2d46c2d /library/std/src/sys_common
parent42325c525b9d3885847a3f803abe53c562d289da (diff)
parentf30614a08877fac8c78113bd7823e1f4c9d20633 (diff)
downloadrust-b0c6527912cee113b29a33d7db0e801a58a94de5.tar.gz
rust-b0c6527912cee113b29a33d7db0e801a58a94de5.zip
Auto merge of #103150 - joboet:remove_lock_wrappers, r=m-ou-se
Remove lock wrappers in `sys_common`

This moves the lazy allocation to `sys` (SGX and UNIX). While this leads to a bit more verbosity, it will simplify future improvements by making room in `sys_common` for platform-independent implementations.

This also removes the condvar check on SGX as it is not necessary for soundness and will be removed anyway once mutex has been made movable.

For simplicity's sake, `libunwind` also uses lazy allocation now on SGX. This will require an update to the C definitions before merging this (CC `@raoulstrackx).`

r? `@m-ou-se`
Diffstat (limited to 'library/std/src/sys_common')
-rw-r--r--library/std/src/sys_common/condvar.rs57
-rw-r--r--library/std/src/sys_common/condvar/check.rs58
-rw-r--r--library/std/src/sys_common/mod.rs3
-rw-r--r--library/std/src/sys_common/mutex.rs50
-rw-r--r--library/std/src/sys_common/remutex.rs10
-rw-r--r--library/std/src/sys_common/rwlock.rs71
6 files changed, 5 insertions, 244 deletions
diff --git a/library/std/src/sys_common/condvar.rs b/library/std/src/sys_common/condvar.rs
deleted file mode 100644
index 8bc5b24115d..00000000000
--- a/library/std/src/sys_common/condvar.rs
+++ /dev/null
@@ -1,57 +0,0 @@
-use crate::sys::locks as imp;
-use crate::sys_common::mutex::MovableMutex;
-use crate::time::Duration;
-
-mod check;
-
-type CondvarCheck = <imp::MovableMutex as check::CondvarCheck>::Check;
-
-/// An OS-based condition variable.
-pub struct Condvar {
-    inner: imp::MovableCondvar,
-    check: CondvarCheck,
-}
-
-impl Condvar {
-    /// Creates a new condition variable for use.
-    #[inline]
-    #[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
-    pub const fn new() -> Self {
-        Self { inner: imp::MovableCondvar::new(), check: CondvarCheck::new() }
-    }
-
-    /// Signals one waiter on this condition variable to wake up.
-    #[inline]
-    pub fn notify_one(&self) {
-        unsafe { self.inner.notify_one() };
-    }
-
-    /// Awakens all current waiters on this condition variable.
-    #[inline]
-    pub fn notify_all(&self) {
-        unsafe { self.inner.notify_all() };
-    }
-
-    /// Waits for a signal on the specified mutex.
-    ///
-    /// Behavior is undefined if the mutex is not locked by the current thread.
-    ///
-    /// May panic if used with more than one mutex.
-    #[inline]
-    pub unsafe fn wait(&self, mutex: &MovableMutex) {
-        self.check.verify(mutex);
-        self.inner.wait(mutex.raw())
-    }
-
-    /// Waits for a signal on the specified mutex with a timeout duration
-    /// specified by `dur` (a relative time into the future).
-    ///
-    /// Behavior is undefined if the mutex is not locked by the current thread.
-    ///
-    /// May panic if used with more than one mutex.
-    #[inline]
-    pub unsafe fn wait_timeout(&self, mutex: &MovableMutex, dur: Duration) -> bool {
-        self.check.verify(mutex);
-        self.inner.wait_timeout(mutex.raw(), dur)
-    }
-}
diff --git a/library/std/src/sys_common/condvar/check.rs b/library/std/src/sys_common/condvar/check.rs
deleted file mode 100644
index 4ac9e62bf86..00000000000
--- a/library/std/src/sys_common/condvar/check.rs
+++ /dev/null
@@ -1,58 +0,0 @@
-use crate::ptr;
-use crate::sync::atomic::{AtomicPtr, Ordering};
-use crate::sys::locks as imp;
-use crate::sys_common::lazy_box::{LazyBox, LazyInit};
-use crate::sys_common::mutex::MovableMutex;
-
-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<T: LazyInit> CondvarCheck for LazyBox<T> {
-    type Check = SameMutexCheck;
-}
-
-pub struct SameMutexCheck {
-    addr: AtomicPtr<()>,
-}
-
-#[allow(dead_code)]
-impl SameMutexCheck {
-    pub const fn new() -> Self {
-        Self { addr: AtomicPtr::new(ptr::null_mut()) }
-    }
-    pub fn verify(&self, mutex: &MovableMutex) {
-        let addr = mutex.raw() as *const imp::Mutex as *const () as *mut _;
-        // Relaxed is okay here because we never read through `self.addr`, and only use it to
-        // compare addresses.
-        match self.addr.compare_exchange(
-            ptr::null_mut(),
-            addr,
-            Ordering::Relaxed,
-            Ordering::Relaxed,
-        ) {
-            Ok(_) => {}               // Stored the address
-            Err(n) if n == addr => {} // Lost a race to store the same address
-            _ => panic!("attempted to use a condition variable with two mutexes"),
-        }
-    }
-}
-
-/// Unboxed mutexes may move, so `Condvar` can not require its address to stay
-/// constant.
-impl CondvarCheck for imp::Mutex {
-    type Check = NoCheck;
-}
-
-pub struct NoCheck;
-
-#[allow(dead_code)]
-impl NoCheck {
-    #[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
-    pub const fn new() -> Self {
-        Self
-    }
-    pub fn verify(&self, _: &MovableMutex) {}
-}
diff --git a/library/std/src/sys_common/mod.rs b/library/std/src/sys_common/mod.rs
index 8c19f9332dc..069b13e9d85 100644
--- a/library/std/src/sys_common/mod.rs
+++ b/library/std/src/sys_common/mod.rs
@@ -21,16 +21,13 @@
 mod tests;
 
 pub mod backtrace;
-pub mod condvar;
 pub mod fs;
 pub mod io;
 pub mod lazy_box;
 pub mod memchr;
-pub mod mutex;
 pub mod once;
 pub mod process;
 pub mod remutex;
-pub mod rwlock;
 pub mod thread;
 pub mod thread_info;
 pub mod thread_local_dtor;
diff --git a/library/std/src/sys_common/mutex.rs b/library/std/src/sys_common/mutex.rs
deleted file mode 100644
index 98046f20f89..00000000000
--- a/library/std/src/sys_common/mutex.rs
+++ /dev/null
@@ -1,50 +0,0 @@
-use crate::sys::locks as imp;
-
-/// An OS-based mutual exclusion lock.
-///
-/// This mutex cleans up its resources in its `Drop` implementation, may safely
-/// be moved (when not borrowed), and does not cause UB when used reentrantly.
-///
-/// This mutex does not implement poisoning.
-///
-/// This is either a wrapper around `LazyBox<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.
-    #[inline]
-    #[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
-    pub const fn new() -> Self {
-        Self(imp::MovableMutex::new())
-    }
-
-    pub(super) fn raw(&self) -> &imp::Mutex {
-        &self.0
-    }
-
-    /// Locks the mutex blocking the current thread until it is available.
-    #[inline]
-    pub fn raw_lock(&self) {
-        unsafe { self.0.lock() }
-    }
-
-    /// Attempts to lock the mutex without blocking, returning whether it was
-    /// successfully acquired or not.
-    #[inline]
-    pub fn try_lock(&self) -> bool {
-        unsafe { self.0.try_lock() }
-    }
-
-    /// Unlocks the mutex.
-    ///
-    /// Behavior is undefined if the current thread does not actually hold the
-    /// mutex.
-    #[inline]
-    pub unsafe fn raw_unlock(&self) {
-        self.0.unlock()
-    }
-}
diff --git a/library/std/src/sys_common/remutex.rs b/library/std/src/sys_common/remutex.rs
index b448ae3a997..4c054da6471 100644
--- a/library/std/src/sys_common/remutex.rs
+++ b/library/std/src/sys_common/remutex.rs
@@ -1,11 +1,11 @@
 #[cfg(all(test, not(target_os = "emscripten")))]
 mod tests;
 
-use super::mutex as sys;
 use crate::cell::UnsafeCell;
 use crate::ops::Deref;
 use crate::panic::{RefUnwindSafe, UnwindSafe};
 use crate::sync::atomic::{AtomicUsize, Ordering::Relaxed};
+use crate::sys::locks as sys;
 
 /// A re-entrant mutual exclusion
 ///
@@ -39,7 +39,7 @@ use crate::sync::atomic::{AtomicUsize, Ordering::Relaxed};
 /// synchronization is left to the mutex, making relaxed memory ordering for
 /// the `owner` field fine in all cases.
 pub struct ReentrantMutex<T> {
-    mutex: sys::MovableMutex,
+    mutex: sys::Mutex,
     owner: AtomicUsize,
     lock_count: UnsafeCell<u32>,
     data: T,
@@ -74,7 +74,7 @@ impl<T> ReentrantMutex<T> {
     /// Creates a new reentrant mutex in an unlocked state.
     pub const fn new(t: T) -> ReentrantMutex<T> {
         ReentrantMutex {
-            mutex: sys::MovableMutex::new(),
+            mutex: sys::Mutex::new(),
             owner: AtomicUsize::new(0),
             lock_count: UnsafeCell::new(0),
             data: t,
@@ -100,7 +100,7 @@ impl<T> ReentrantMutex<T> {
             if self.owner.load(Relaxed) == this_thread {
                 self.increment_lock_count();
             } else {
-                self.mutex.raw_lock();
+                self.mutex.lock();
                 self.owner.store(this_thread, Relaxed);
                 debug_assert_eq!(*self.lock_count.get(), 0);
                 *self.lock_count.get() = 1;
@@ -162,7 +162,7 @@ impl<T> Drop for ReentrantMutexGuard<'_, T> {
             *self.lock.lock_count.get() -= 1;
             if *self.lock.lock_count.get() == 0 {
                 self.lock.owner.store(0, Relaxed);
-                self.lock.mutex.raw_unlock();
+                self.lock.mutex.unlock();
             }
         }
     }
diff --git a/library/std/src/sys_common/rwlock.rs b/library/std/src/sys_common/rwlock.rs
deleted file mode 100644
index 042981dac60..00000000000
--- a/library/std/src/sys_common/rwlock.rs
+++ /dev/null
@@ -1,71 +0,0 @@
-use crate::sys::locks as imp;
-
-/// An OS-based reader-writer lock.
-///
-/// This rwlock cleans up its resources in its `Drop` implementation and may
-/// safely be moved (when not borrowed).
-///
-/// This rwlock does not implement poisoning.
-///
-/// This is either a wrapper around `LazyBox<imp::RwLock>` or `imp::RwLock`,
-/// depending on the platform. It is boxed on platforms where `imp::RwLock` may
-/// not be moved.
-pub struct MovableRwLock(imp::MovableRwLock);
-
-impl MovableRwLock {
-    /// Creates a new reader-writer lock for use.
-    #[inline]
-    #[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
-    pub const fn new() -> Self {
-        Self(imp::MovableRwLock::new())
-    }
-
-    /// Acquires shared access to the underlying lock, blocking the current
-    /// thread to do so.
-    #[inline]
-    pub fn read(&self) {
-        unsafe { self.0.read() }
-    }
-
-    /// Attempts to acquire shared access to this lock, returning whether it
-    /// succeeded or not.
-    ///
-    /// This function does not block the current thread.
-    #[inline]
-    pub fn try_read(&self) -> bool {
-        unsafe { self.0.try_read() }
-    }
-
-    /// Acquires write access to the underlying lock, blocking the current thread
-    /// to do so.
-    #[inline]
-    pub fn write(&self) {
-        unsafe { self.0.write() }
-    }
-
-    /// Attempts to acquire exclusive access to this lock, returning whether it
-    /// succeeded or not.
-    ///
-    /// This function does not block the current thread.
-    #[inline]
-    pub fn try_write(&self) -> bool {
-        unsafe { self.0.try_write() }
-    }
-
-    /// Unlocks previously acquired shared access to this lock.
-    ///
-    /// Behavior is undefined if the current thread does not have shared access.
-    #[inline]
-    pub unsafe fn read_unlock(&self) {
-        self.0.read_unlock()
-    }
-
-    /// Unlocks previously acquired exclusive access to this lock.
-    ///
-    /// Behavior is undefined if the current thread does not currently have
-    /// exclusive access.
-    #[inline]
-    pub unsafe fn write_unlock(&self) {
-        self.0.write_unlock()
-    }
-}