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)]

//! # poison
//!
//! Provides ergonomic poisoning primitives for building poisonable structures.

use std::sync::{PoisonError, LockResult};
use std::thread;

/// A typed poisoning wrapper.
///
/// Enforces that access to the contained data respects poisoning.
#[derive(Debug)]
pub struct Poison<T: ?Sized> {
    raw: RawPoison,
    data: T
}

/// A poison guard on an associated Poison.
///
/// If the current thread panics before this instance is dropped, the
/// Poision will become poisoned when this instance drops.
#[derive(Debug)]
pub struct PoisonGuard<'poison, T: ?Sized + 'poison> {
    data: &'poison mut T,
    guard: RawPoisonGuard<'poison>
}

impl<T> Poison<T> {
    /// Create a new Poison in the non-poisoned state.
    #[inline]
    pub fn new(val: T) -> Self {
        Poison {
            raw: RawPoison::new(),
            data: val,
        }
    }

    /// Create a new Poison that is already poisoned.
    #[inline]
    pub fn poisoned(val: T) -> Self {
        Poison {
            raw: RawPoison::poisoned(),
            data: val,
        }
    }

    /// Extract the data from the Poison.
    ///
    /// Returns PoisonError if the Poison is poisoned.
    #[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> {
    /// Get a poison lock on this poison.
    ///
    /// Returns PoisonError if the Poison is poisoned.
    #[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 })
    }

    /// Heal the Poison, unpoisoning it if it is poisoned.
    #[inline]
    pub fn heal(&mut self) {
        self.raw.heal();
    }

    /// Get an immutable reference to the data in this poison.
    ///
    /// There is no guard for an immutable reference, since the data must either
    /// be immutable or internally poisoned if it has interior mutability.
    #[inline]
    pub fn get(&self) -> LockResult<&T> {
        if self.raw.poisoned {
            Err(PoisonError::new(&self.data))
        } else {
            Ok(&self.data)
        }
    }

    /// Get a mutable reference without a guard.
    ///
    /// Should only be used in combination with PoisonGuard::into_raw.
    pub unsafe fn get_mut(&mut self) -> &mut T { &mut self.data }
}

impl<'poison, T: ?Sized> PoisonGuard<'poison, T> {
    /// Get an immutable reference to the data.
    pub fn get(&self) -> &T { &self.data }

    /// Get a mutable reference to the data.
    pub fn get_mut(&mut self) -> &mut T { &mut self.data }

    /// Get a reference that escapes the guard.
    ///
    /// Should only be used if the data will be externally poisoned.
    pub unsafe fn into_mut(self) -> &'poison mut T { self.data }

    /// Get the raw poison guard.
    pub fn into_raw(self) -> RawPoisonGuard<'poison> { self.guard }
}

/// A raw poisoning primitive, can be used to build automatically poisoning structures.
#[derive(Debug)]
pub struct RawPoison {
    poisoned: bool
}

/// A guard on a RawPoison.
///
/// If the current thread panics before this instance is dropped, the RawPoison
/// will become poisoned when this instance drops.
#[derive(Debug)]
pub struct RawPoisonGuard<'poison> {
    poison: &'poison mut RawPoison,
    panicking: bool
}

impl RawPoison {
    /// Create a new RawPoison in a non-poisoned state.
    #[inline]
    pub fn new() -> RawPoison {
        RawPoison { poisoned: false }
    }

    /// Create a new RawPoison which is already poisoned.
    #[inline]
    pub fn poisoned() -> RawPoison {
        RawPoison { poisoned: true }
    }

    /// Heal the RawPoison if it is poisoned.
    #[inline]
    pub fn heal(&mut self) {
        self.poisoned = false;
    }

    /// Get a poison lock on this RawPoison.
    ///
    /// If the RawPoison is already poisoned, returns PoisonError.
    #[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;
        }
    }
}

/// A simple, useful combinator for dealing with LockResult.
///
/// Applies the action to either the Ok or Err variants
/// of the LockResult and returns a new LockResult in the same
/// state with a new value.
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?")
        };
    }
}