1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 #![unstable(feature = "reentrant_mutex", reason = "new API")]
17 use sys_common::poison::{self, TryLockError, TryLockResult, LockResult};
18 use sys::mutex as sys;
20 /// A re-entrant mutual exclusion
22 /// This mutex will block *other* threads waiting for the lock to become
23 /// available. The thread which has already locked the mutex can lock it
24 /// multiple times without blocking, preventing a common source of deadlocks.
25 pub struct ReentrantMutex<T> {
26 inner: Box<sys::ReentrantMutex>,
31 unsafe impl<T: Send> Send for ReentrantMutex<T> {}
32 unsafe impl<T: Send> Sync for ReentrantMutex<T> {}
35 /// An RAII implementation of a "scoped lock" of a mutex. When this structure is
36 /// dropped (falls out of scope), the lock will be unlocked.
38 /// The data protected by the mutex can be accessed through this guard via its
39 /// Deref and DerefMut implementations
41 pub struct ReentrantMutexGuard<'a, T: 'a> {
42 // funny underscores due to how Deref/DerefMut currently work (they
43 // disregard field privacy).
44 __lock: &'a ReentrantMutex<T>,
45 __poison: poison::Guard,
48 impl<'a, T> !marker::Send for ReentrantMutexGuard<'a, T> {}
51 impl<T> ReentrantMutex<T> {
52 /// Creates a new reentrant mutex in an unlocked state.
53 pub fn new(t: T) -> ReentrantMutex<T> {
55 let mut mutex = ReentrantMutex {
56 inner: box sys::ReentrantMutex::uninitialized(),
57 poison: poison::Flag::new(),
65 /// Acquires a mutex, blocking the current thread until it is able to do so.
67 /// This function will block the caller until it is available to acquire the mutex.
68 /// Upon returning, the thread is the only thread with the mutex held. When the thread
69 /// calling this method already holds the lock, the call shall succeed without
74 /// If another user of this mutex panicked while holding the mutex, then
75 /// this call will return failure if the mutex would otherwise be
77 pub fn lock(&self) -> LockResult<ReentrantMutexGuard<T>> {
78 unsafe { self.inner.lock() }
79 ReentrantMutexGuard::new(&self)
82 /// Attempts to acquire this lock.
84 /// If the lock could not be acquired at this time, then `Err` is returned.
85 /// Otherwise, an RAII guard is returned.
87 /// This function does not block.
91 /// If another user of this mutex panicked while holding the mutex, then
92 /// this call will return failure if the mutex would otherwise be
94 pub fn try_lock(&self) -> TryLockResult<ReentrantMutexGuard<T>> {
95 if unsafe { self.inner.try_lock() } {
96 Ok(try!(ReentrantMutexGuard::new(&self)))
98 Err(TryLockError::WouldBlock)
103 impl<T> Drop for ReentrantMutex<T> {
105 // This is actually safe b/c we know that there is no further usage of
106 // this mutex (it's up to the user to arrange for a mutex to get
107 // dropped, that's not our job)
108 unsafe { self.inner.destroy() }
112 impl<T: fmt::Debug + 'static> fmt::Debug for ReentrantMutex<T> {
113 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
114 match self.try_lock() {
115 Ok(guard) => write!(f, "ReentrantMutex {{ data: {:?} }}", &*guard),
116 Err(TryLockError::Poisoned(err)) => {
117 write!(f, "ReentrantMutex {{ data: Poisoned({:?}) }}", &**err.get_ref())
119 Err(TryLockError::WouldBlock) => write!(f, "ReentrantMutex {{ <locked> }}")
124 impl<'mutex, T> ReentrantMutexGuard<'mutex, T> {
125 fn new(lock: &'mutex ReentrantMutex<T>)
126 -> LockResult<ReentrantMutexGuard<'mutex, T>> {
127 poison::map_result(lock.poison.borrow(), |guard| {
128 ReentrantMutexGuard {
136 impl<'mutex, T> Deref for ReentrantMutexGuard<'mutex, T> {
139 fn deref<'a>(&'a self) -> &'a T {
144 impl<'a, T> Drop for ReentrantMutexGuard<'a, T> {
148 self.__lock.poison.done(&self.__poison);
149 self.__lock.inner.unlock();
158 use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
166 let m = ReentrantMutex::new(());
168 let a = m.lock().unwrap();
170 let b = m.lock().unwrap();
172 let c = m.lock().unwrap();
183 let m = ReentrantMutex::new(RefCell::new(0));
184 let lock = m.lock().unwrap();
185 let handle = thread::scoped(|| {
186 let lock = m.lock().unwrap();
187 assert_eq!(*lock.borrow(), 4950);
190 let lock = m.lock().unwrap();
191 *lock.borrow_mut() += i;
199 let m = ReentrantMutex::new(());
200 let lock = m.try_lock().unwrap();
201 let lock2 = m.try_lock().unwrap();
204 let lock = m.try_lock();
205 assert!(lock.is_err());
208 let lock3 = m.try_lock().unwrap();
211 pub struct Answer<'a>(pub ReentrantMutexGuard<'a, RefCell<u32>>);
212 impl<'a> Drop for Answer<'a> {
214 *self.0.borrow_mut() = 42;
220 let m = Arc::new(ReentrantMutex::new(RefCell::new(0)));
222 let result = thread::spawn(move ||{
223 let lock = mc.lock().unwrap();
224 *lock.borrow_mut() = 1;
225 let lock2 = mc.lock().unwrap();
226 *lock.borrow_mut() = 2;
227 let answer = Answer(lock2);
228 panic!("What the answer to my lifetimes dilemma is?");
231 assert!(result.is_err());
232 let r = m.lock().err().unwrap().into_inner();
233 assert_eq!(*r.borrow(), 42);