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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#![cfg_attr(test, deny(warnings))]
#![deny(missing_docs)]
use std::sync::{PoisonError, LockResult};
use std::thread;
#[derive(Debug)]
pub struct Poison<T: ?Sized> {
raw: RawPoison,
data: T
}
#[derive(Debug)]
pub struct PoisonGuard<'poison, T: ?Sized + 'poison> {
data: &'poison mut T,
guard: RawPoisonGuard<'poison>
}
impl<T> Poison<T> {
#[inline]
pub fn new(val: T) -> Self {
Poison {
raw: RawPoison::new(),
data: val,
}
}
#[inline]
pub fn poisoned(val: T) -> Self {
Poison {
raw: RawPoison::poisoned(),
data: val,
}
}
#[inline]
pub fn into_inner(self) -> LockResult<T> {
if self.raw.poisoned {
Err(PoisonError::new(self.data))
} else {
Ok(self.data)
}
}
}
impl<T: ?Sized> Poison<T> {
#[inline]
pub fn lock(&mut self) -> LockResult<PoisonGuard<T>> {
let data = &mut self.data;
map_result(self.raw.lock(), move |lock| PoisonGuard { data: data, guard: lock })
}
#[inline]
pub fn heal(&mut self) {
self.raw.heal();
}
#[inline]
pub fn get(&self) -> LockResult<&T> {
if self.raw.poisoned {
Err(PoisonError::new(&self.data))
} else {
Ok(&self.data)
}
}
pub unsafe fn get_mut(&mut self) -> &mut T { &mut self.data }
}
impl<'poison, T: ?Sized> PoisonGuard<'poison, T> {
pub fn get(&self) -> &T { &self.data }
pub fn get_mut(&mut self) -> &mut T { &mut self.data }
pub unsafe fn into_mut(self) -> &'poison mut T { self.data }
pub fn into_raw(self) -> RawPoisonGuard<'poison> { self.guard }
}
#[derive(Debug)]
pub struct RawPoison {
poisoned: bool
}
#[derive(Debug)]
pub struct RawPoisonGuard<'poison> {
poison: &'poison mut RawPoison,
panicking: bool
}
impl RawPoison {
#[inline]
pub fn new() -> RawPoison {
RawPoison { poisoned: false }
}
#[inline]
pub fn poisoned() -> RawPoison {
RawPoison { poisoned: true }
}
#[inline]
pub fn heal(&mut self) {
self.poisoned = false;
}
#[inline]
pub fn lock(&mut self) -> LockResult<RawPoisonGuard> {
let poisoned = self.poisoned;
let guard = RawPoisonGuard {
poison: self,
panicking: thread::panicking()
};
if poisoned {
Err(PoisonError::new(guard))
} else {
Ok(guard)
}
}
}
impl<'poison> Drop for RawPoisonGuard<'poison> {
#[inline]
fn drop(&mut self) {
if !self.panicking && thread::panicking() {
self.poison.poisoned = true;
}
}
}
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(e) => Err(PoisonError::new(f(e.into_inner())))
}
}
#[cfg(test)]
mod test {
use std::sync::{Mutex, Arc};
use std::thread;
use {Poison, RawPoison};
#[test]
fn test_poison() {
let x1 = Arc::new(Mutex::new(Poison::new(12)));
let x2 = x1.clone();
thread::spawn(move || {
let mut _ml = x1.lock().unwrap();
let _pl = _ml.lock().unwrap();
panic!();
}).join().unwrap_err();
match x2.lock() {
Err(mut p) => {
p.get_mut().lock().unwrap_err();
p.get_mut().heal();
p.get_mut().lock().unwrap();
},
Ok(_) => panic!("Mutex not poisoned?")
};
}
#[test]
fn test_raw_poison() {
let x1 = Arc::new(Mutex::new(RawPoison::new()));
let x2 = x1.clone();
thread::spawn(move || {
let mut _ml = x1.lock().unwrap();
let _pl = _ml.lock().unwrap();
panic!();
}).join().unwrap_err();
match x2.lock() {
Err(mut p) => {
p.get_mut().lock().unwrap_err();
p.get_mut().heal();
p.get_mut().lock().unwrap();
},
Ok(_) => panic!("Mutex not poisoned?")
};
}
}