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