]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/sync/nostd_sync.rs
Adapt intro node process_pending_htlcs test for non-intro nodes
[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 into_inner(self) -> LockResult<T> {
41                 Ok(self.inner.into_inner())
42         }
43 }
44
45 impl<'a, T: 'a> LockTestExt<'a> for Mutex<T> {
46         #[inline]
47         fn held_by_thread(&self) -> LockHeldState {
48                 if self.inner.try_borrow_mut().is_err() { return LockHeldState::HeldByThread; }
49                 else { return LockHeldState::NotHeldByThread; }
50         }
51         type ExclLock = MutexGuard<'a, T>;
52         #[inline]
53         fn unsafe_well_ordered_double_lock_self(&'a self) -> MutexGuard<T> { self.lock().unwrap() }
54 }
55
56 pub struct RwLock<T: ?Sized> {
57         inner: RefCell<T>
58 }
59
60 pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
61         lock: Ref<'a, T>,
62 }
63
64 pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
65         lock: RefMut<'a, T>,
66 }
67
68 impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
69         type Target = T;
70
71         fn deref(&self) -> &T {
72                 &self.lock.deref()
73         }
74 }
75
76 impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
77         type Target = T;
78
79         fn deref(&self) -> &T {
80                 &self.lock.deref()
81         }
82 }
83
84 impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
85         fn deref_mut(&mut self) -> &mut T {
86                 self.lock.deref_mut()
87         }
88 }
89
90 impl<T> RwLock<T> {
91         pub fn new(inner: T) -> RwLock<T> {
92                 RwLock { inner: RefCell::new(inner) }
93         }
94
95         pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
96                 Ok(RwLockReadGuard { lock: self.inner.borrow() })
97         }
98
99         pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
100                 Ok(RwLockWriteGuard { lock: self.inner.borrow_mut() })
101         }
102
103         pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
104                 match self.inner.try_borrow_mut() {
105                         Ok(lock) => Ok(RwLockWriteGuard { lock }),
106                         Err(_) => Err(())
107                 }
108         }
109 }
110
111 impl<'a, T: 'a> LockTestExt<'a> for RwLock<T> {
112         #[inline]
113         fn held_by_thread(&self) -> LockHeldState {
114                 if self.inner.try_borrow_mut().is_err() { return LockHeldState::HeldByThread; }
115                 else { return LockHeldState::NotHeldByThread; }
116         }
117         type ExclLock = RwLockWriteGuard<'a, T>;
118         #[inline]
119         fn unsafe_well_ordered_double_lock_self(&'a self) -> RwLockWriteGuard<T> { self.write().unwrap() }
120 }
121
122 pub type FairRwLock<T> = RwLock<T>;