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
#[allow(unused_imports)]
#[cfg(feature = "parking_lot")]
pub(crate) use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
#[cfg(not(feature = "parking_lot"))]
pub(crate) use self::std_impl::*;
#[cfg(not(feature = "parking_lot"))]
mod std_impl {
use std::sync::{self, PoisonError, TryLockError};
pub(crate) use std::sync::{RwLockReadGuard, RwLockWriteGuard};
#[derive(Debug)]
pub(crate) struct RwLock<T> {
inner: sync::RwLock<T>,
}
impl<T> RwLock<T> {
pub(crate) fn new(val: T) -> Self {
Self {
inner: sync::RwLock::new(val),
}
}
#[inline]
pub(crate) fn get_mut(&mut self) -> &mut T {
self.inner.get_mut().unwrap_or_else(PoisonError::into_inner)
}
#[inline]
pub(crate) fn read(&self) -> RwLockReadGuard<'_, T> {
self.inner.read().unwrap_or_else(PoisonError::into_inner)
}
#[inline]
#[allow(dead_code)]
pub(crate) fn try_read(&self) -> Option<RwLockReadGuard<'_, T>> {
match self.inner.try_read() {
Ok(guard) => Some(guard),
Err(TryLockError::Poisoned(e)) => Some(e.into_inner()),
Err(TryLockError::WouldBlock) => None,
}
}
#[inline]
pub(crate) fn write(&self) -> RwLockWriteGuard<'_, T> {
self.inner.write().unwrap_or_else(PoisonError::into_inner)
}
#[inline]
#[allow(dead_code)]
pub(crate) fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>> {
match self.inner.try_write() {
Ok(guard) => Some(guard),
Err(TryLockError::Poisoned(e)) => Some(e.into_inner()),
Err(TryLockError::WouldBlock) => None,
}
}
}
}