]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/common/remutex.rs
Auto merge of #35048 - tmiasko:monotonic-wait-timeout, r=alexcrichton
[rust.git] / src / libstd / sys / common / remutex.rs
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.
4 //
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
11 use fmt;
12 use marker;
13 use ops::Deref;
14 use sys_common::poison::{self, TryLockError, TryLockResult, LockResult};
15 use sys::mutex as sys;
16
17 /// A re-entrant mutual exclusion
18 ///
19 /// This mutex will block *other* threads waiting for the lock to become
20 /// available. The thread which has already locked the mutex can lock it
21 /// multiple times without blocking, preventing a common source of deadlocks.
22 pub struct ReentrantMutex<T> {
23     inner: Box<sys::ReentrantMutex>,
24     poison: poison::Flag,
25     data: T,
26 }
27
28 unsafe impl<T: Send> Send for ReentrantMutex<T> {}
29 unsafe impl<T: Send> Sync for ReentrantMutex<T> {}
30
31
32 /// An RAII implementation of a "scoped lock" of a mutex. When this structure is
33 /// dropped (falls out of scope), the lock will be unlocked.
34 ///
35 /// The data protected by the mutex can be accessed through this guard via its
36 /// Deref implementation.
37 ///
38 /// # Mutability
39 ///
40 /// Unlike `MutexGuard`, `ReentrantMutexGuard` does not implement `DerefMut`,
41 /// because implementation of the trait would violate Rust’s reference aliasing
42 /// rules. Use interior mutability (usually `RefCell`) in order to mutate the
43 /// guarded data.
44 #[must_use]
45 pub struct ReentrantMutexGuard<'a, T: 'a> {
46     // funny underscores due to how Deref currently works (it disregards field
47     // privacy).
48     __lock: &'a ReentrantMutex<T>,
49     __poison: poison::Guard,
50 }
51
52 impl<'a, T> !marker::Send for ReentrantMutexGuard<'a, T> {}
53
54
55 impl<T> ReentrantMutex<T> {
56     /// Creates a new reentrant mutex in an unlocked state.
57     pub fn new(t: T) -> ReentrantMutex<T> {
58         unsafe {
59             let mut mutex = ReentrantMutex {
60                 inner: box sys::ReentrantMutex::uninitialized(),
61                 poison: poison::Flag::new(),
62                 data: t,
63             };
64             mutex.inner.init();
65             mutex
66         }
67     }
68
69     /// Acquires a mutex, blocking the current thread until it is able to do so.
70     ///
71     /// This function will block the caller until it is available to acquire the mutex.
72     /// Upon returning, the thread is the only thread with the mutex held. When the thread
73     /// calling this method already holds the lock, the call shall succeed without
74     /// blocking.
75     ///
76     /// # Errors
77     ///
78     /// If another user of this mutex panicked while holding the mutex, then
79     /// this call will return failure if the mutex would otherwise be
80     /// acquired.
81     pub fn lock(&self) -> LockResult<ReentrantMutexGuard<T>> {
82         unsafe { self.inner.lock() }
83         ReentrantMutexGuard::new(&self)
84     }
85
86     /// Attempts to acquire this lock.
87     ///
88     /// If the lock could not be acquired at this time, then `Err` is returned.
89     /// Otherwise, an RAII guard is returned.
90     ///
91     /// This function does not block.
92     ///
93     /// # Errors
94     ///
95     /// If another user of this mutex panicked while holding the mutex, then
96     /// this call will return failure if the mutex would otherwise be
97     /// acquired.
98     pub fn try_lock(&self) -> TryLockResult<ReentrantMutexGuard<T>> {
99         if unsafe { self.inner.try_lock() } {
100             Ok(ReentrantMutexGuard::new(&self)?)
101         } else {
102             Err(TryLockError::WouldBlock)
103         }
104     }
105 }
106
107 impl<T> Drop for ReentrantMutex<T> {
108     fn drop(&mut self) {
109         // This is actually safe b/c we know that there is no further usage of
110         // this mutex (it's up to the user to arrange for a mutex to get
111         // dropped, that's not our job)
112         unsafe { self.inner.destroy() }
113     }
114 }
115
116 impl<T: fmt::Debug + 'static> fmt::Debug for ReentrantMutex<T> {
117     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
118         match self.try_lock() {
119             Ok(guard) => write!(f, "ReentrantMutex {{ data: {:?} }}", &*guard),
120             Err(TryLockError::Poisoned(err)) => {
121                 write!(f, "ReentrantMutex {{ data: Poisoned({:?}) }}", &**err.get_ref())
122             },
123             Err(TryLockError::WouldBlock) => write!(f, "ReentrantMutex {{ <locked> }}")
124         }
125     }
126 }
127
128 impl<'mutex, T> ReentrantMutexGuard<'mutex, T> {
129     fn new(lock: &'mutex ReentrantMutex<T>)
130             -> LockResult<ReentrantMutexGuard<'mutex, T>> {
131         poison::map_result(lock.poison.borrow(), |guard| {
132             ReentrantMutexGuard {
133                 __lock: lock,
134                 __poison: guard,
135             }
136         })
137     }
138 }
139
140 impl<'mutex, T> Deref for ReentrantMutexGuard<'mutex, T> {
141     type Target = T;
142
143     fn deref(&self) -> &T {
144         &self.__lock.data
145     }
146 }
147
148 impl<'a, T> Drop for ReentrantMutexGuard<'a, T> {
149     #[inline]
150     fn drop(&mut self) {
151         unsafe {
152             self.__lock.poison.done(&self.__poison);
153             self.__lock.inner.unlock();
154         }
155     }
156 }
157
158
159 #[cfg(test)]
160 mod tests {
161     use sys_common::remutex::{ReentrantMutex, ReentrantMutexGuard};
162     use cell::RefCell;
163     use sync::Arc;
164     use thread;
165
166     #[test]
167     fn smoke() {
168         let m = ReentrantMutex::new(());
169         {
170             let a = m.lock().unwrap();
171             {
172                 let b = m.lock().unwrap();
173                 {
174                     let c = m.lock().unwrap();
175                     assert_eq!(*c, ());
176                 }
177                 assert_eq!(*b, ());
178             }
179             assert_eq!(*a, ());
180         }
181     }
182
183     #[test]
184     fn is_mutex() {
185         let m = Arc::new(ReentrantMutex::new(RefCell::new(0)));
186         let m2 = m.clone();
187         let lock = m.lock().unwrap();
188         let child = thread::spawn(move || {
189             let lock = m2.lock().unwrap();
190             assert_eq!(*lock.borrow(), 4950);
191         });
192         for i in 0..100 {
193             let lock = m.lock().unwrap();
194             *lock.borrow_mut() += i;
195         }
196         drop(lock);
197         child.join().unwrap();
198     }
199
200     #[test]
201     fn trylock_works() {
202         let m = Arc::new(ReentrantMutex::new(()));
203         let m2 = m.clone();
204         let _lock = m.try_lock().unwrap();
205         let _lock2 = m.try_lock().unwrap();
206         thread::spawn(move || {
207             let lock = m2.try_lock();
208             assert!(lock.is_err());
209         }).join().unwrap();
210         let _lock3 = m.try_lock().unwrap();
211     }
212
213     pub struct Answer<'a>(pub ReentrantMutexGuard<'a, RefCell<u32>>);
214     impl<'a> Drop for Answer<'a> {
215         fn drop(&mut self) {
216             *self.0.borrow_mut() = 42;
217         }
218     }
219
220     #[test]
221     fn poison_works() {
222         let m = Arc::new(ReentrantMutex::new(RefCell::new(0)));
223         let mc = m.clone();
224         let result = thread::spawn(move ||{
225             let lock = mc.lock().unwrap();
226             *lock.borrow_mut() = 1;
227             let lock2 = mc.lock().unwrap();
228             *lock.borrow_mut() = 2;
229             let _answer = Answer(lock2);
230             panic!("What the answer to my lifetimes dilemma is?");
231         }).join();
232         assert!(result.is_err());
233         let r = m.lock().err().unwrap().into_inner();
234         assert_eq!(*r.borrow(), 42);
235     }
236 }