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
#[allow(unused_imports)]
pub(crate) use std::sync::{LockResult, PoisonError, TryLockResult};
#[cfg(not(feature = "parking_lot"))]
pub(crate) use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
#[cfg(feature = "parking_lot")]
pub(crate) use self::parking_lot_impl::*;
#[cfg(feature = "parking_lot")]
mod parking_lot_impl {
pub(crate) use parking_lot::{RwLockReadGuard, RwLockWriteGuard};
use std::sync::{LockResult, TryLockError, TryLockResult};
#[derive(Debug)]
pub(crate) struct RwLock<T> {
inner: parking_lot::RwLock<T>,
}
impl<T> RwLock<T> {
pub(crate) fn new(val: T) -> Self {
Self {
inner: parking_lot::RwLock::new(val),
}
}
#[inline]
pub(crate) fn get_mut(&mut self) -> LockResult<&mut T> {
Ok(self.inner.get_mut())
}
#[inline]
pub(crate) fn read(&self) -> LockResult<RwLockReadGuard<'_, T>> {
Ok(self.inner.read())
}
#[inline]
#[allow(dead_code)]
pub(crate) fn try_read(&self) -> TryLockResult<RwLockReadGuard<'_, T>> {
self.inner.try_read().ok_or(TryLockError::WouldBlock)
}
#[inline]
pub(crate) fn write(&self) -> LockResult<RwLockWriteGuard<'_, T>> {
Ok(self.inner.write())
}
}
}