]> git.proxmox.com Git - rustc.git/blob - src/libstd/sync/poison.rs
32c8150ba4070172473cfe7d7602dca56a8c09de
[rustc.git] / src / libstd / sync / poison.rs
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.
4 //
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.
10
11 use prelude::v1::*;
12
13 use cell::UnsafeCell;
14 use error::{Error, FromError};
15 use fmt;
16 use thread;
17
18 pub struct Flag { failed: UnsafeCell<bool> }
19 pub const FLAG_INIT: Flag = Flag { failed: UnsafeCell { value: false } };
20
21 impl Flag {
22 #[inline]
23 pub fn borrow(&self) -> LockResult<Guard> {
24 let ret = Guard { panicking: thread::panicking() };
25 if unsafe { *self.failed.get() } {
26 Err(PoisonError::new(ret))
27 } else {
28 Ok(ret)
29 }
30 }
31
32 #[inline]
33 pub fn done(&self, guard: &Guard) {
34 if !guard.panicking && thread::panicking() {
35 unsafe { *self.failed.get() = true; }
36 }
37 }
38
39 #[inline]
40 pub fn get(&self) -> bool {
41 unsafe { *self.failed.get() }
42 }
43 }
44
45 pub struct Guard {
46 panicking: bool,
47 }
48
49 /// A type of error which can be returned whenever a lock is acquired.
50 ///
51 /// Both Mutexes and RwLocks are poisoned whenever a task fails while the lock
52 /// is held. The precise semantics for when a lock is poisoned is documented on
53 /// each lock, but once a lock is poisoned then all future acquisitions will
54 /// return this error.
55 #[stable(feature = "rust1", since = "1.0.0")]
56 pub struct PoisonError<T> {
57 guard: T,
58 }
59
60 /// An enumeration of possible errors which can occur while calling the
61 /// `try_lock` method.
62 #[stable(feature = "rust1", since = "1.0.0")]
63 pub enum TryLockError<T> {
64 /// The lock could not be acquired because another task failed while holding
65 /// the lock.
66 #[stable(feature = "rust1", since = "1.0.0")]
67 Poisoned(PoisonError<T>),
68 /// The lock could not be acquired at this time because the operation would
69 /// otherwise block.
70 #[stable(feature = "rust1", since = "1.0.0")]
71 WouldBlock,
72 }
73
74 /// A type alias for the result of a lock method which can be poisoned.
75 ///
76 /// The `Ok` variant of this result indicates that the primitive was not
77 /// poisoned, and the `Guard` is contained within. The `Err` variant indicates
78 /// that the primitive was poisoned. Note that the `Err` variant *also* carries
79 /// the associated guard, and it can be acquired through the `into_inner`
80 /// method.
81 #[stable(feature = "rust1", since = "1.0.0")]
82 pub type LockResult<Guard> = Result<Guard, PoisonError<Guard>>;
83
84 /// A type alias for the result of a nonblocking locking method.
85 ///
86 /// For more information, see `LockResult`. A `TryLockResult` doesn't
87 /// necessarily hold the associated guard in the `Err` type as the lock may not
88 /// have been acquired for other reasons.
89 #[stable(feature = "rust1", since = "1.0.0")]
90 pub type TryLockResult<Guard> = Result<Guard, TryLockError<Guard>>;
91
92 #[stable(feature = "rust1", since = "1.0.0")]
93 impl<T> fmt::Debug for PoisonError<T> {
94 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95 "PoisonError { inner: .. }".fmt(f)
96 }
97 }
98
99 #[stable(feature = "rust1", since = "1.0.0")]
100 impl<T> fmt::Display for PoisonError<T> {
101 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
102 self.description().fmt(f)
103 }
104 }
105
106 impl<T> Error for PoisonError<T> {
107 fn description(&self) -> &str {
108 "poisoned lock: another task failed inside"
109 }
110 }
111
112 impl<T> PoisonError<T> {
113 /// Create a `PoisonError`.
114 #[unstable(feature = "std_misc")]
115 pub fn new(guard: T) -> PoisonError<T> {
116 PoisonError { guard: guard }
117 }
118
119 /// Consumes this error indicating that a lock is poisoned, returning the
120 /// underlying guard to allow access regardless.
121 #[unstable(feature = "std_misc")]
122 #[deprecated(since = "1.0.0", reason = "renamed to into_inner")]
123 pub fn into_guard(self) -> T { self.guard }
124
125 /// Consumes this error indicating that a lock is poisoned, returning the
126 /// underlying guard to allow access regardless.
127 #[unstable(feature = "std_misc")]
128 pub fn into_inner(self) -> T { self.guard }
129
130 /// Reaches into this error indicating that a lock is poisoned, returning a
131 /// reference to the underlying guard to allow access regardless.
132 #[unstable(feature = "std_misc")]
133 pub fn get_ref(&self) -> &T { &self.guard }
134
135 /// Reaches into this error indicating that a lock is poisoned, returning a
136 /// mutable reference to the underlying guard to allow access regardless.
137 #[unstable(feature = "std_misc")]
138 pub fn get_mut(&mut self) -> &mut T { &mut self.guard }
139 }
140
141 impl<T> FromError<PoisonError<T>> for TryLockError<T> {
142 fn from_error(err: PoisonError<T>) -> TryLockError<T> {
143 TryLockError::Poisoned(err)
144 }
145 }
146
147 #[stable(feature = "rust1", since = "1.0.0")]
148 impl<T> fmt::Debug for TryLockError<T> {
149 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
150 match *self {
151 TryLockError::Poisoned(..) => "Poisoned(..)".fmt(f),
152 TryLockError::WouldBlock => "WouldBlock".fmt(f)
153 }
154 }
155 }
156
157 #[stable(feature = "rust1", since = "1.0.0")]
158 impl<T> fmt::Display for TryLockError<T> {
159 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
160 self.description().fmt(f)
161 }
162 }
163
164 impl<T> Error for TryLockError<T> {
165 fn description(&self) -> &str {
166 match *self {
167 TryLockError::Poisoned(ref p) => p.description(),
168 TryLockError::WouldBlock => "try_lock failed because the operation would block"
169 }
170 }
171
172 fn cause(&self) -> Option<&Error> {
173 match *self {
174 TryLockError::Poisoned(ref p) => Some(p),
175 _ => None
176 }
177 }
178 }
179
180 pub fn map_result<T, U, F>(result: LockResult<T>, f: F)
181 -> LockResult<U>
182 where F: FnOnce(T) -> U {
183 match result {
184 Ok(t) => Ok(f(t)),
185 Err(PoisonError { guard }) => Err(PoisonError::new(f(guard)))
186 }
187 }