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
//! Abstracts over sync primitive implementations.
//!
//! Optionally, we allow the Rust standard library's `RwLock` to be replaced
//! with the `parking_lot` crate's implementation. This may provide improved
//! performance in some cases. However, the `parking_lot` dependency is an
//! opt-in feature flag. Because `parking_lot::RwLock` has a slightly different
//! API than `std::sync::RwLock` (it does not support poisoning on panics), we
//! wrap the `std::sync` version to ignore poisoning.

#[allow(unused_imports)] // may be used later;
#[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)] // may be used later;
        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)] // may be used later;
        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,
            }
        }
    }
}