diff options
Diffstat (limited to 'library/std/src/sys/unix')
| -rw-r--r-- | library/std/src/sys/unix/locks/futex.rs | 98 | ||||
| -rw-r--r-- | library/std/src/sys/unix/locks/mod.rs | 4 | ||||
| -rw-r--r-- | library/std/src/sys/unix/locks/pthread_remutex.rs | 46 | 
3 files changed, 2 insertions, 146 deletions
| diff --git a/library/std/src/sys/unix/locks/futex.rs b/library/std/src/sys/unix/locks/futex.rs index b166e7c453c..7a63af1ad7c 100644 --- a/library/std/src/sys/unix/locks/futex.rs +++ b/library/std/src/sys/unix/locks/futex.rs @@ -1,6 +1,5 @@ -use crate::cell::UnsafeCell; use crate::sync::atomic::{ - AtomicU32, AtomicUsize, + AtomicU32, Ordering::{Acquire, Relaxed, Release}, }; use crate::sys::futex::{futex_wait, futex_wake, futex_wake_all}; @@ -163,98 +162,3 @@ impl Condvar { r } } - -/// A reentrant mutex. Used by stdout().lock() and friends. -/// -/// The 'owner' field tracks which thread has locked the mutex. -/// -/// We use current_thread_unique_ptr() as the thread identifier, -/// which is just the address of a thread local variable. -/// -/// If `owner` is set to the identifier of the current thread, -/// we assume the mutex is already locked and instead of locking it again, -/// we increment `lock_count`. -/// -/// When unlocking, we decrement `lock_count`, and only unlock the mutex when -/// it reaches zero. -/// -/// `lock_count` is protected by the mutex and only accessed by the thread that has -/// locked the mutex, so needs no synchronization. -/// -/// `owner` can be checked by other threads that want to see if they already -/// hold the lock, so needs to be atomic. If it compares equal, we're on the -/// same thread that holds the mutex and memory access can use relaxed ordering -/// since we're not dealing with multiple threads. If it compares unequal, -/// synchronization is left to the mutex, making relaxed memory ordering for -/// the `owner` field fine in all cases. -pub struct ReentrantMutex { - mutex: Mutex, - owner: AtomicUsize, - lock_count: UnsafeCell<u32>, -} - -unsafe impl Send for ReentrantMutex {} -unsafe impl Sync for ReentrantMutex {} - -impl ReentrantMutex { - #[inline] - pub const unsafe fn uninitialized() -> Self { - Self { mutex: Mutex::new(), owner: AtomicUsize::new(0), lock_count: UnsafeCell::new(0) } - } - - #[inline] - pub unsafe fn init(&self) {} - - #[inline] - pub unsafe fn destroy(&self) {} - - pub unsafe fn try_lock(&self) -> bool { - let this_thread = current_thread_unique_ptr(); - if self.owner.load(Relaxed) == this_thread { - self.increment_lock_count(); - true - } else if self.mutex.try_lock() { - self.owner.store(this_thread, Relaxed); - debug_assert_eq!(*self.lock_count.get(), 0); - *self.lock_count.get() = 1; - true - } else { - false - } - } - - pub unsafe fn lock(&self) { - let this_thread = current_thread_unique_ptr(); - if self.owner.load(Relaxed) == this_thread { - self.increment_lock_count(); - } else { - self.mutex.lock(); - self.owner.store(this_thread, Relaxed); - debug_assert_eq!(*self.lock_count.get(), 0); - *self.lock_count.get() = 1; - } - } - - unsafe fn increment_lock_count(&self) { - *self.lock_count.get() = (*self.lock_count.get()) - .checked_add(1) - .expect("lock count overflow in reentrant mutex"); - } - - pub unsafe fn unlock(&self) { - *self.lock_count.get() -= 1; - if *self.lock_count.get() == 0 { - self.owner.store(0, Relaxed); - self.mutex.unlock(); - } - } -} - -/// Get an address that is unique per running thread. -/// -/// This can be used as a non-null usize-sized ID. -pub fn current_thread_unique_ptr() -> usize { - // Use a non-drop type to make sure it's still available during thread destruction. - thread_local! { static X: u8 = const { 0 } } - X.with(|x| <*const _>::addr(x)) -} diff --git a/library/std/src/sys/unix/locks/mod.rs b/library/std/src/sys/unix/locks/mod.rs index e0404f40c69..17796f8894b 100644 --- a/library/std/src/sys/unix/locks/mod.rs +++ b/library/std/src/sys/unix/locks/mod.rs @@ -5,15 +5,13 @@ cfg_if::cfg_if! { ))] { mod futex; mod futex_rwlock; - pub use futex::{Mutex, MovableMutex, Condvar, MovableCondvar, ReentrantMutex}; + pub use futex::{Mutex, MovableMutex, Condvar, MovableCondvar}; pub use futex_rwlock::{RwLock, MovableRwLock}; } else { mod pthread_mutex; - mod pthread_remutex; mod pthread_rwlock; mod pthread_condvar; pub use pthread_mutex::{Mutex, MovableMutex}; - pub use pthread_remutex::ReentrantMutex; pub use pthread_rwlock::{RwLock, MovableRwLock}; pub use pthread_condvar::{Condvar, MovableCondvar}; } diff --git a/library/std/src/sys/unix/locks/pthread_remutex.rs b/library/std/src/sys/unix/locks/pthread_remutex.rs deleted file mode 100644 index b006181ee3a..00000000000 --- a/library/std/src/sys/unix/locks/pthread_remutex.rs +++ /dev/null @@ -1,46 +0,0 @@ -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); - } -} | 
