27cfb9b8f782c4bafabecd5180a5d0d2256105c3
[rust-lightning] / lightning / src / sync / nostd_sync.rs
1 pub use ::alloc::sync::Arc;
2 use core::ops::{Deref, DerefMut};
3 use core::cell::{RefCell, Ref, RefMut};
4 use super::{LockTestExt, LockHeldState};
5
6 pub type LockResult<Guard> = Result<Guard, ()>;
7
8 pub struct Mutex<T: ?Sized> {
9         inner: RefCell<T>
10 }
11
12 #[must_use = "if unused the Mutex will immediately unlock"]
13 pub struct MutexGuard<'a, T: ?Sized + 'a> {
14         lock: RefMut<'a, T>,
15 }
16
17 impl<T: ?Sized> Deref for MutexGuard<'_, T> {
18         type Target = T;
19
20         fn deref(&self) -> &T {
21                 &self.lock.deref()
22         }
23 }
24
25 impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
26         fn deref_mut(&mut self) -> &mut T {
27                 self.lock.deref_mut()
28         }
29 }
30
31 impl<T> Mutex<T> {
32         pub fn new(inner: T) -> Mutex<T> {
33                 Mutex { inner: RefCell::new(inner) }
34         }
35
36         pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
37                 Ok(MutexGuard { lock: self.inner.borrow_mut() })
38         }
39
40         pub fn try_lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
41                 Ok(MutexGuard { lock: self.inner.borrow_mut() })
42         }
43
44         pub fn into_inner(self) -> LockResult<T> {
45                 Ok(self.inner.into_inner())
46         }
47 }
48
49 impl<'a, T: 'a> LockTestExt<'a> for Mutex<T> {
50         #[inline]
51         fn held_by_thread(&self) -> LockHeldState {
52                 if self.inner.try_borrow_mut().is_err() { return LockHeldState::HeldByThread; }
53                 else { return LockHeldState::NotHeldByThread; }
54         }
55         type ExclLock = MutexGuard<'a, T>;
56         #[inline]
57         fn unsafe_well_ordered_double_lock_self(&'a self) -> MutexGuard<T> { self.lock().unwrap() }
58 }
59
60 pub struct RwLock<T: ?Sized> {
61         inner: RefCell<T>
62 }
63
64 pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
65         lock: Ref<'a, T>,
66 }
67
68 pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
69         lock: RefMut<'a, T>,
70 }
71
72 impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
73         type Target = T;
74
75         fn deref(&self) -> &T {
76                 &self.lock.deref()
77         }
78 }
79
80 impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
81         type Target = T;
82
83         fn deref(&self) -> &T {
84                 &self.lock.deref()
85         }
86 }
87
88 impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
89         fn deref_mut(&mut self) -> &mut T {
90                 self.lock.deref_mut()
91         }
92 }
93
94 impl<T> RwLock<T> {
95         pub fn new(inner: T) -> RwLock<T> {
96                 RwLock { inner: RefCell::new(inner) }
97         }
98
99         pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
100                 Ok(RwLockReadGuard { lock: self.inner.borrow() })
101         }
102
103         pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
104                 Ok(RwLockWriteGuard { lock: self.inner.borrow_mut() })
105         }
106
107         pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
108                 match self.inner.try_borrow_mut() {
109                         Ok(lock) => Ok(RwLockWriteGuard { lock }),
110                         Err(_) => Err(())
111                 }
112         }
113 }
114
115 impl<'a, T: 'a> LockTestExt<'a> for RwLock<T> {
116         #[inline]
117         fn held_by_thread(&self) -> LockHeldState {
118                 if self.inner.try_borrow_mut().is_err() { return LockHeldState::HeldByThread; }
119                 else { return LockHeldState::NotHeldByThread; }
120         }
121         type ExclLock = RwLockWriteGuard<'a, T>;
122         #[inline]
123         fn unsafe_well_ordered_double_lock_self(&'a self) -> RwLockWriteGuard<T> { self.write().unwrap() }
124 }
125
126 pub type FairRwLock<T> = RwLock<T>;