1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::fmt;
use std::thread;
use std::error::Error;
use std::sync::atomic::{AtomicBool, Ordering};
pub type LockResult<Guard> = Result<Guard, PoisonError<Guard>>;
pub type TryLockResult<Guard> = Result<Guard, TryLockError<Guard>>;
pub struct PoisonError<T>
{
guard: T,
}
impl<T> fmt::Debug for PoisonError<T>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
f.debug_struct("PoisonError").finish_non_exhaustive()
}
}
impl<T> fmt::Display for PoisonError<T>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
"poisoned lock: another task failed inside".fmt(f)
}
}
impl<T> Error for PoisonError<T>
{
#[allow(deprecated)]
fn description(&self) -> &str
{
"poisoned lock: another task failed inside"
}
}
impl<T> PoisonError<T>
{
pub fn new(guard: T) -> PoisonError<T>
{
PoisonError { guard }
}
pub fn into_inner(self) -> T
{
self.guard
}
pub fn get_ref(&self) -> &T
{
&self.guard
}
pub fn get_mut(&mut self) -> &mut T
{
&mut self.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(PoisonError::new(f(guard))),
}
}
pub struct Flag {
failed: AtomicBool,
}
impl Flag {
pub const fn new() -> Flag {
Flag { failed: AtomicBool::new(false) }
}
#[inline]
pub fn borrow(&self) -> LockResult<Guard> {
let ret = Guard { panicking: thread::panicking() };
if self.get() { Err(PoisonError::new(ret)) } else { Ok(ret) }
}
#[inline]
pub fn done(&self, guard: &Guard) {
if !guard.panicking && thread::panicking() {
self.failed.store(true, Ordering::Relaxed);
}
}
#[inline]
pub fn get(&self) -> bool {
self.failed.load(Ordering::Relaxed)
}
}
pub struct Guard {
panicking: bool,
}
pub enum TryLockError<T> {
Poisoned( PoisonError<T>),
WouldBlock,
}
impl<T> From<PoisonError<T>> for TryLockError<T> {
fn from(err: PoisonError<T>) -> TryLockError<T> {
TryLockError::Poisoned(err)
}
}
impl<T> fmt::Debug for TryLockError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
TryLockError::Poisoned(..) => "Poisoned(..)".fmt(f),
TryLockError::WouldBlock => "WouldBlock".fmt(f),
}
}
}
impl<T> fmt::Display for TryLockError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
TryLockError::Poisoned(..) => "poisoned lock: another task failed inside",
TryLockError::WouldBlock => "try_lock failed because the operation would block",
}
.fmt(f)
}
}
impl<T> Error for TryLockError<T> {
#[allow(deprecated, deprecated_in_future)]
fn description(&self) -> &str {
match *self {
TryLockError::Poisoned(ref p) => p.description(),
TryLockError::WouldBlock => "try_lock failed because the operation would block",
}
}
#[allow(deprecated)]
fn cause(&self) -> Option<&dyn Error> {
match *self {
TryLockError::Poisoned(ref p) => Some(p),
_ => None,
}
}
}