diff options
| author | Keegan McAllister <kmcallister@mozilla.com> | 2015-01-23 11:47:04 -0800 |
|---|---|---|
| committer | Keegan McAllister <kmcallister@mozilla.com> | 2015-02-06 21:36:07 -0800 |
| commit | 96c3a13680d521387007e2f6575483c24561ecb3 (patch) | |
| tree | 67b3c14eb0ed9c19614efdbaa3c141891430616a /src/libstd/sync | |
| parent | 7ebf9bc5c22155d622537ded42b4ebf94238b296 (diff) | |
| download | rust-96c3a13680d521387007e2f6575483c24561ecb3.tar.gz rust-96c3a13680d521387007e2f6575483c24561ecb3.zip | |
sync: Add is_poisoned to Mutex and RwLock
Diffstat (limited to 'src/libstd/sync')
| -rw-r--r-- | src/libstd/sync/mutex.rs | 13 | ||||
| -rw-r--r-- | src/libstd/sync/rwlock.rs | 13 |
2 files changed, 26 insertions, 0 deletions
diff --git a/src/libstd/sync/mutex.rs b/src/libstd/sync/mutex.rs index 7531d5b058d..74692c1273c 100644 --- a/src/libstd/sync/mutex.rs +++ b/src/libstd/sync/mutex.rs @@ -228,6 +228,17 @@ impl<T: Send> Mutex<T> { Err(TryLockError::WouldBlock) } } + + /// Determine whether the lock is poisoned. + /// + /// If another thread is active, the lock can still become poisoned at any + /// time. You should not trust a `false` value for program correctness + /// without additional synchronization. + #[inline] + #[unstable(feature = "std_misc")] + pub fn is_poisoned(&self) -> bool { + self.inner.poison.get() + } } #[unsafe_destructor] @@ -458,12 +469,14 @@ mod test { #[test] fn test_mutex_arc_poison() { let arc = Arc::new(Mutex::new(1)); + assert!(!arc.is_poisoned()); let arc2 = arc.clone(); let _ = Thread::scoped(move|| { let lock = arc2.lock().unwrap(); assert_eq!(*lock, 2); }).join(); assert!(arc.lock().is_err()); + assert!(arc.is_poisoned()); } #[test] diff --git a/src/libstd/sync/rwlock.rs b/src/libstd/sync/rwlock.rs index 6efbcf89415..c4f1f2ccadd 100644 --- a/src/libstd/sync/rwlock.rs +++ b/src/libstd/sync/rwlock.rs @@ -237,6 +237,17 @@ impl<T: Send + Sync> RwLock<T> { Err(TryLockError::WouldBlock) } } + + /// Determine whether the lock is poisoned. + /// + /// If another thread is active, the lock can still become poisoned at any + /// time. You should not trust a `false` value for program correctness + /// without additional synchronization. + #[inline] + #[unstable(feature = "std_misc")] + pub fn is_poisoned(&self) -> bool { + self.inner.poison.get() + } } #[unsafe_destructor] @@ -451,12 +462,14 @@ mod tests { #[test] fn test_rw_arc_poison_ww() { let arc = Arc::new(RwLock::new(1)); + assert!(!arc.is_poisoned()); let arc2 = arc.clone(); let _: Result<uint, _> = Thread::scoped(move|| { let _lock = arc2.write().unwrap(); panic!(); }).join(); assert!(arc.write().is_err()); + assert!(arc.is_poisoned()); } #[test] |
