sync: Expose PoisonError::new
This commit is contained in:
parent
96c3a13680
commit
7324c2cf4f
2 changed files with 14 additions and 12 deletions
|
@ -16,7 +16,7 @@ use sys::time::SteadyTime;
|
||||||
use sys_common::condvar as sys;
|
use sys_common::condvar as sys;
|
||||||
use sys_common::mutex as sys_mutex;
|
use sys_common::mutex as sys_mutex;
|
||||||
use time::Duration;
|
use time::Duration;
|
||||||
use sync::{mutex, MutexGuard};
|
use sync::{mutex, MutexGuard, PoisonError};
|
||||||
|
|
||||||
/// A Condition Variable
|
/// A Condition Variable
|
||||||
///
|
///
|
||||||
|
@ -228,7 +228,7 @@ impl StaticCondvar {
|
||||||
mutex::guard_poison(&guard).get()
|
mutex::guard_poison(&guard).get()
|
||||||
};
|
};
|
||||||
if poisoned {
|
if poisoned {
|
||||||
Err(poison::new_poison_error(guard))
|
Err(PoisonError::new(guard))
|
||||||
} else {
|
} else {
|
||||||
Ok(guard)
|
Ok(guard)
|
||||||
}
|
}
|
||||||
|
@ -249,7 +249,7 @@ impl StaticCondvar {
|
||||||
(mutex::guard_poison(&guard).get(), success)
|
(mutex::guard_poison(&guard).get(), success)
|
||||||
};
|
};
|
||||||
if poisoned {
|
if poisoned {
|
||||||
Err(poison::new_poison_error((guard, success)))
|
Err(PoisonError::new((guard, success)))
|
||||||
} else {
|
} else {
|
||||||
Ok((guard, success))
|
Ok((guard, success))
|
||||||
}
|
}
|
||||||
|
@ -276,7 +276,7 @@ impl StaticCondvar {
|
||||||
while !f(guard_result
|
while !f(guard_result
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.map(|g| &mut **g)
|
.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 now = SteadyTime::now();
|
||||||
let consumed = &now - &start;
|
let consumed = &now - &start;
|
||||||
let guard = guard_result.unwrap_or_else(|e| e.into_inner());
|
let guard = guard_result.unwrap_or_else(|e| e.into_inner());
|
||||||
|
@ -284,7 +284,7 @@ impl StaticCondvar {
|
||||||
Ok((new_guard, no_timeout)) => (Ok(new_guard), no_timeout),
|
Ok((new_guard, no_timeout)) => (Ok(new_guard), no_timeout),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let (new_guard, no_timeout) = err.into_inner();
|
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;
|
guard_result = new_guard_result;
|
||||||
|
@ -292,7 +292,7 @@ impl StaticCondvar {
|
||||||
let result = f(guard_result
|
let result = f(guard_result
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.map(|g| &mut **g)
|
.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));
|
return poison::map_result(guard_result, |g| (g, result));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ impl Flag {
|
||||||
pub fn borrow(&self) -> LockResult<Guard> {
|
pub fn borrow(&self) -> LockResult<Guard> {
|
||||||
let ret = Guard { panicking: Thread::panicking() };
|
let ret = Guard { panicking: Thread::panicking() };
|
||||||
if unsafe { *self.failed.get() } {
|
if unsafe { *self.failed.get() } {
|
||||||
Err(new_poison_error(ret))
|
Err(PoisonError::new(ret))
|
||||||
} else {
|
} else {
|
||||||
Ok(ret)
|
Ok(ret)
|
||||||
}
|
}
|
||||||
|
@ -110,6 +110,12 @@ impl<T> Error for PoisonError<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> 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
|
/// Consumes this error indicating that a lock is poisoned, returning the
|
||||||
/// underlying guard to allow access regardless.
|
/// underlying guard to allow access regardless.
|
||||||
#[unstable(feature = "std_misc")]
|
#[unstable(feature = "std_misc")]
|
||||||
|
@ -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)
|
pub fn map_result<T, U, F>(result: LockResult<T>, f: F)
|
||||||
-> LockResult<U>
|
-> LockResult<U>
|
||||||
where F: FnOnce(T) -> U {
|
where F: FnOnce(T) -> U {
|
||||||
match result {
|
match result {
|
||||||
Ok(t) => Ok(f(t)),
|
Ok(t) => Ok(f(t)),
|
||||||
Err(PoisonError { guard }) => Err(new_poison_error(f(guard)))
|
Err(PoisonError { guard }) => Err(PoisonError::new(f(guard)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue