4 use crate::panic::{RefUnwindSafe, UnwindSafe};
5 use crate::sys::mutex as sys;
7 /// A re-entrant mutual exclusion
9 /// This mutex will block *other* threads waiting for the lock to become
10 /// available. The thread which has already locked the mutex can lock it
11 /// multiple times without blocking, preventing a common source of deadlocks.
12 pub struct ReentrantMutex<T> {
13 inner: sys::ReentrantMutex,
17 unsafe impl<T: Send> Send for ReentrantMutex<T> {}
18 unsafe impl<T: Send> Sync for ReentrantMutex<T> {}
20 impl<T> UnwindSafe for ReentrantMutex<T> {}
21 impl<T> RefUnwindSafe for ReentrantMutex<T> {}
23 /// An RAII implementation of a "scoped lock" of a mutex. When this structure is
24 /// dropped (falls out of scope), the lock will be unlocked.
26 /// The data protected by the mutex can be accessed through this guard via its
27 /// Deref implementation.
31 /// Unlike `MutexGuard`, `ReentrantMutexGuard` does not implement `DerefMut`,
32 /// because implementation of the trait would violate Rust’s reference aliasing
33 /// rules. Use interior mutability (usually `RefCell`) in order to mutate the
35 #[must_use = "if unused the ReentrantMutex will immediately unlock"]
36 pub struct ReentrantMutexGuard<'a, T: 'a> {
37 // funny underscores due to how Deref currently works (it disregards field
39 __lock: &'a ReentrantMutex<T>,
42 impl<T> !marker::Send for ReentrantMutexGuard<'_, T> {}
44 impl<T> ReentrantMutex<T> {
45 /// Creates a new reentrant mutex in an unlocked state.
49 /// This function is unsafe because it is required that `init` is called
50 /// once this mutex is in its final resting place, and only then are the
51 /// lock/unlock methods safe.
52 pub const unsafe fn new(t: T) -> ReentrantMutex<T> {
53 ReentrantMutex { inner: sys::ReentrantMutex::uninitialized(), data: t }
56 /// Initializes this mutex so it's ready for use.
60 /// Unsafe to call more than once, and must be called after this will no
61 /// longer move in memory.
62 pub unsafe fn init(&self) {
66 /// Acquires a mutex, blocking the current thread until it is able to do so.
68 /// This function will block the caller until it is available to acquire the mutex.
69 /// Upon returning, the thread is the only thread with the mutex held. When the thread
70 /// calling this method already holds the lock, the call shall succeed without
75 /// If another user of this mutex panicked while holding the mutex, then
76 /// this call will return failure if the mutex would otherwise be
78 pub fn lock(&self) -> ReentrantMutexGuard<'_, T> {
79 unsafe { self.inner.lock() }
80 ReentrantMutexGuard::new(&self)
83 /// Attempts to acquire this lock.
85 /// If the lock could not be acquired at this time, then `Err` is returned.
86 /// Otherwise, an RAII guard is returned.
88 /// This function does not block.
92 /// If another user of this mutex panicked while holding the mutex, then
93 /// this call will return failure if the mutex would otherwise be
95 pub fn try_lock(&self) -> Option<ReentrantMutexGuard<'_, T>> {
96 if unsafe { self.inner.try_lock() } { Some(ReentrantMutexGuard::new(&self)) } else { None }
100 impl<T> Drop for ReentrantMutex<T> {
102 // This is actually safe b/c we know that there is no further usage of
103 // this mutex (it's up to the user to arrange for a mutex to get
104 // dropped, that's not our job)
105 unsafe { self.inner.destroy() }
109 impl<T: fmt::Debug + 'static> fmt::Debug for ReentrantMutex<T> {
110 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111 match self.try_lock() {
112 Some(guard) => f.debug_struct("ReentrantMutex").field("data", &*guard).finish(),
114 struct LockedPlaceholder;
115 impl fmt::Debug for LockedPlaceholder {
116 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117 f.write_str("<locked>")
121 f.debug_struct("ReentrantMutex").field("data", &LockedPlaceholder).finish()
127 impl<'mutex, T> ReentrantMutexGuard<'mutex, T> {
128 fn new(lock: &'mutex ReentrantMutex<T>) -> ReentrantMutexGuard<'mutex, T> {
129 ReentrantMutexGuard { __lock: lock }
133 impl<T> Deref for ReentrantMutexGuard<'_, T> {
136 fn deref(&self) -> &T {
141 impl<T> Drop for ReentrantMutexGuard<'_, T> {
145 self.__lock.inner.unlock();
150 #[cfg(all(test, not(target_os = "emscripten")))]
152 use crate::cell::RefCell;
153 use crate::sync::Arc;
154 use crate::sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
160 let m = ReentrantMutex::new(());
181 let m = Arc::new(ReentrantMutex::new(RefCell::new(0)));
187 let child = thread::spawn(move || {
188 let lock = m2.lock();
189 assert_eq!(*lock.borrow(), 4950);
193 *lock.borrow_mut() += i;
196 child.join().unwrap();
202 let m = Arc::new(ReentrantMutex::new(()));
207 let _lock = m.try_lock();
208 let _lock2 = m.try_lock();
209 thread::spawn(move || {
210 let lock = m2.try_lock();
211 assert!(lock.is_none());
215 let _lock3 = m.try_lock();
218 pub struct Answer<'a>(pub ReentrantMutexGuard<'a, RefCell<u32>>);
219 impl Drop for Answer<'_> {
221 *self.0.borrow_mut() = 42;