1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use error::{Error, FromError};
18 pub struct Flag { failed: UnsafeCell<bool> }
19 pub const FLAG_INIT: Flag = Flag { failed: UnsafeCell { value: false } };
23 pub fn borrow(&self) -> LockResult<Guard> {
24 let ret = Guard { panicking: Thread::panicking() };
25 if unsafe { *self.failed.get() } {
26 Err(new_poison_error(ret))
33 pub fn done(&self, guard: &Guard) {
34 if !guard.panicking && Thread::panicking() {
35 unsafe { *self.failed.get() = true; }
40 pub fn get(&self) -> bool {
41 unsafe { *self.failed.get() }
45 #[allow(missing_copy_implementations)]
50 /// A type of error which can be returned whenever a lock is acquired.
52 /// Both Mutexes and RwLocks are poisoned whenever a task fails while the lock
53 /// is held. The precise semantics for when a lock is poisoned is documented on
54 /// each lock, but once a lock is poisoned then all future acquisitions will
55 /// return this error.
58 pub struct PoisonError<T> {
62 /// An enumeration of possible errors which can occur while calling the
63 /// `try_lock` method.
66 pub enum TryLockError<T> {
67 /// The lock could not be acquired because another task failed while holding
70 Poisoned(PoisonError<T>),
71 /// The lock could not be acquired at this time because the operation would
77 /// A type alias for the result of a lock method which can be poisoned.
79 /// The `Ok` variant of this result indicates that the primitive was not
80 /// poisoned, and the `Guard` is contained within. The `Err` variant indicates
81 /// that the primitive was poisoned. Note that the `Err` variant *also* carries
82 /// the associated guard, and it can be acquired through the `into_inner`
85 pub type LockResult<Guard> = Result<Guard, PoisonError<Guard>>;
87 /// A type alias for the result of a nonblocking locking method.
89 /// For more information, see `LockResult`. A `TryLockResult` doesn't
90 /// necessarily hold the associated guard in the `Err` type as the lock may not
91 /// have been acquired for other reasons.
93 pub type TryLockResult<Guard> = Result<Guard, TryLockError<Guard>>;
96 impl<T> fmt::Display for PoisonError<T> {
97 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
98 self.description().fmt(f)
102 impl<T> Error for PoisonError<T> {
103 fn description(&self) -> &str {
104 "poisoned lock: another task failed inside"
108 impl<T> PoisonError<T> {
109 /// Consumes this error indicating that a lock is poisoned, returning the
110 /// underlying guard to allow access regardless.
111 #[deprecated="renamed to into_inner"]
112 pub fn into_guard(self) -> T { self.guard }
114 /// Consumes this error indicating that a lock is poisoned, returning the
115 /// underlying guard to allow access regardless.
117 pub fn into_inner(self) -> T { self.guard }
119 /// Reaches into this error indicating that a lock is poisoned, returning a
120 /// reference to the underlying guard to allow access regardless.
122 pub fn get_ref(&self) -> &T { &self.guard }
124 /// Reaches into this error indicating that a lock is poisoned, returning a
125 /// mutable reference to the underlying guard to allow access regardless.
127 pub fn get_mut(&mut self) -> &mut T { &mut self.guard }
130 impl<T> FromError<PoisonError<T>> for TryLockError<T> {
131 fn from_error(err: PoisonError<T>) -> TryLockError<T> {
132 TryLockError::Poisoned(err)
137 impl<T> fmt::Display for TryLockError<T> {
138 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
139 self.description().fmt(f)
143 impl<T> Error for TryLockError<T> {
144 fn description(&self) -> &str {
146 TryLockError::Poisoned(ref p) => p.description(),
147 TryLockError::WouldBlock => "try_lock failed because the operation would block"
151 fn cause(&self) -> Option<&Error> {
153 TryLockError::Poisoned(ref p) => Some(p),
159 pub fn new_poison_error<T>(guard: T) -> PoisonError<T> {
160 PoisonError { guard: guard }
163 pub fn map_result<T, U, F>(result: LockResult<T>, f: F)
165 where F: FnOnce(T) -> U {
168 Err(PoisonError { guard }) => Err(new_poison_error(f(guard)))