Skip to content

Expose mutex poisoning helpers #21565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/libstd/sync/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use sys::time::SteadyTime;
use sys_common::condvar as sys;
use sys_common::mutex as sys_mutex;
use time::Duration;
use sync::{mutex, MutexGuard};
use sync::{mutex, MutexGuard, PoisonError};

/// A Condition Variable
///
Expand Down Expand Up @@ -228,7 +228,7 @@ impl StaticCondvar {
mutex::guard_poison(&guard).get()
};
if poisoned {
Err(poison::new_poison_error(guard))
Err(PoisonError::new(guard))
} else {
Ok(guard)
}
Expand All @@ -249,7 +249,7 @@ impl StaticCondvar {
(mutex::guard_poison(&guard).get(), success)
};
if poisoned {
Err(poison::new_poison_error((guard, success)))
Err(PoisonError::new((guard, success)))
} else {
Ok((guard, success))
}
Expand All @@ -276,23 +276,23 @@ impl StaticCondvar {
while !f(guard_result
.as_mut()
.map(|g| &mut **g)
.map_err(|e| poison::new_poison_error(&mut **e.get_mut()))) {
.map_err(|e| PoisonError::new(&mut **e.get_mut()))) {
let now = SteadyTime::now();
let consumed = &now - &start;
let guard = guard_result.unwrap_or_else(|e| e.into_inner());
let (new_guard_result, no_timeout) = match self.wait_timeout(guard, dur - consumed) {
Ok((new_guard, no_timeout)) => (Ok(new_guard), no_timeout),
Err(err) => {
let (new_guard, no_timeout) = err.into_inner();
(Err(poison::new_poison_error(new_guard)), no_timeout)
(Err(PoisonError::new(new_guard)), no_timeout)
}
};
guard_result = new_guard_result;
if !no_timeout {
let result = f(guard_result
.as_mut()
.map(|g| &mut **g)
.map_err(|e| poison::new_poison_error(&mut **e.get_mut())));
.map_err(|e| PoisonError::new(&mut **e.get_mut())));
return poison::map_result(guard_result, |g| (g, result));
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/libstd/sync/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
14 changes: 8 additions & 6 deletions src/libstd/sync/poison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Flag {
pub fn borrow(&self) -> LockResult<Guard> {
let ret = Guard { panicking: Thread::panicking() };
if unsafe { *self.failed.get() } {
Err(new_poison_error(ret))
Err(PoisonError::new(ret))
} else {
Ok(ret)
}
Expand Down Expand Up @@ -110,6 +110,12 @@ impl<T> Error for PoisonError<T> {
}

impl<T> PoisonError<T> {
/// Create a `PoisonError`.
#[unstable(feature = "std_misc")]
pub fn new(guard: T) -> PoisonError<T> {
PoisonError { guard: guard }
}

/// Consumes this error indicating that a lock is poisoned, returning the
/// underlying guard to allow access regardless.
#[unstable(feature = "std_misc")]
Expand Down Expand Up @@ -171,15 +177,11 @@ impl<T> Error for TryLockError<T> {
}
}

pub fn new_poison_error<T>(guard: T) -> PoisonError<T> {
PoisonError { guard: guard }
}

pub fn map_result<T, U, F>(result: LockResult<T>, f: F)
-> LockResult<U>
where F: FnOnce(T) -> U {
match result {
Ok(t) => Ok(f(t)),
Err(PoisonError { guard }) => Err(new_poison_error(f(guard)))
Err(PoisonError { guard }) => Err(PoisonError::new(f(guard)))
}
}
13 changes: 13 additions & 0 deletions src/libstd/sync/rwlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down