Skip to content

Commit 96c3a13

Browse files
author
Keegan McAllister
committed
sync: Add is_poisoned to Mutex and RwLock
1 parent 7ebf9bc commit 96c3a13

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/libstd/sync/mutex.rs

+13
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,17 @@ impl<T: Send> Mutex<T> {
228228
Err(TryLockError::WouldBlock)
229229
}
230230
}
231+
232+
/// Determine whether the lock is poisoned.
233+
///
234+
/// If another thread is active, the lock can still become poisoned at any
235+
/// time. You should not trust a `false` value for program correctness
236+
/// without additional synchronization.
237+
#[inline]
238+
#[unstable(feature = "std_misc")]
239+
pub fn is_poisoned(&self) -> bool {
240+
self.inner.poison.get()
241+
}
231242
}
232243

233244
#[unsafe_destructor]
@@ -458,12 +469,14 @@ mod test {
458469
#[test]
459470
fn test_mutex_arc_poison() {
460471
let arc = Arc::new(Mutex::new(1));
472+
assert!(!arc.is_poisoned());
461473
let arc2 = arc.clone();
462474
let _ = Thread::scoped(move|| {
463475
let lock = arc2.lock().unwrap();
464476
assert_eq!(*lock, 2);
465477
}).join();
466478
assert!(arc.lock().is_err());
479+
assert!(arc.is_poisoned());
467480
}
468481

469482
#[test]

src/libstd/sync/rwlock.rs

+13
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ impl<T: Send + Sync> RwLock<T> {
237237
Err(TryLockError::WouldBlock)
238238
}
239239
}
240+
241+
/// Determine whether the lock is poisoned.
242+
///
243+
/// If another thread is active, the lock can still become poisoned at any
244+
/// time. You should not trust a `false` value for program correctness
245+
/// without additional synchronization.
246+
#[inline]
247+
#[unstable(feature = "std_misc")]
248+
pub fn is_poisoned(&self) -> bool {
249+
self.inner.poison.get()
250+
}
240251
}
241252

242253
#[unsafe_destructor]
@@ -451,12 +462,14 @@ mod tests {
451462
#[test]
452463
fn test_rw_arc_poison_ww() {
453464
let arc = Arc::new(RwLock::new(1));
465+
assert!(!arc.is_poisoned());
454466
let arc2 = arc.clone();
455467
let _: Result<uint, _> = Thread::scoped(move|| {
456468
let _lock = arc2.write().unwrap();
457469
panic!();
458470
}).join();
459471
assert!(arc.write().is_err());
472+
assert!(arc.is_poisoned());
460473
}
461474

462475
#[test]

0 commit comments

Comments
 (0)