sync: Expose PoisonError::new

This commit is contained in:
Keegan McAllister 2015-01-23 11:58:49 -08:00
parent 96c3a13680
commit 7324c2cf4f
2 changed files with 14 additions and 12 deletions

View File

@ -16,7 +16,7 @@
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
///
@ -228,7 +228,7 @@ pub fn wait<'a, T>(&'static self, guard: MutexGuard<'a, T>)
mutex::guard_poison(&guard).get()
};
if poisoned {
Err(poison::new_poison_error(guard))
Err(PoisonError::new(guard))
} else {
Ok(guard)
}
@ -249,7 +249,7 @@ pub fn wait_timeout<'a, T>(&'static self, guard: MutexGuard<'a, T>, dur: Duratio
(mutex::guard_poison(&guard).get(), success)
};
if poisoned {
Err(poison::new_poison_error((guard, success)))
Err(PoisonError::new((guard, success)))
} else {
Ok((guard, success))
}
@ -276,7 +276,7 @@ pub fn wait_timeout_with<'a, T, F>(&'static self,
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());
@ -284,7 +284,7 @@ pub fn wait_timeout_with<'a, T, F>(&'static self,
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;
@ -292,7 +292,7 @@ pub fn wait_timeout_with<'a, T, F>(&'static self,
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));
}
}

View File

@ -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)
}
@ -110,6 +110,12 @@ fn description(&self) -> &str {
}
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")]
@ -171,15 +177,11 @@ fn cause(&self) -> Option<&Error> {
}
}
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)))
}
}