Implement dummy Mutex, Condvar and RwLock
[rust-lightning] / lightning / src / 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
6 pub type LockResult<Guard> = Result<Guard, ()>;
7
8 pub struct Condvar {}
9
10 impl Condvar {
11         pub fn new() -> Condvar {
12                 Condvar { }
13         }
14
15         pub fn wait<'a, T>(&'a self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
16                 Ok(guard)
17         }
18
19         #[allow(unused)]
20         pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, _dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
21                 Ok((guard, ()))
22         }
23
24         pub fn notify_all(&self) {}
25 }
26
27 pub struct Mutex<T: ?Sized> {
28         inner: RefCell<T>
29 }
30
31 #[must_use = "if unused the Mutex will immediately unlock"]
32 pub struct MutexGuard<'a, T: ?Sized + 'a> {
33         lock: RefMut<'a, T>,
34 }
35
36 impl<T: ?Sized> Deref for MutexGuard<'_, T> {
37         type Target = T;
38
39         fn deref(&self) -> &T {
40                 &self.lock.deref()
41         }
42 }
43
44 impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
45         fn deref_mut(&mut self) -> &mut T {
46                 self.lock.deref_mut()
47         }
48 }
49
50 impl<T> Mutex<T> {
51         pub fn new(inner: T) -> Mutex<T> {
52                 Mutex { inner: RefCell::new(inner) }
53         }
54
55         pub fn lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
56                 Ok(MutexGuard { lock: self.inner.borrow_mut() })
57         }
58
59         pub fn try_lock<'a>(&'a self) -> LockResult<MutexGuard<'a, T>> {
60                 Ok(MutexGuard { lock: self.inner.borrow_mut() })
61         }
62 }
63
64 pub struct RwLock<T: ?Sized> {
65         inner: RefCell<T>
66 }
67
68 pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
69         lock: Ref<'a, T>,
70 }
71
72 pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
73         lock: RefMut<'a, T>,
74 }
75
76 impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
77         type Target = T;
78
79         fn deref(&self) -> &T {
80                 &self.lock.deref()
81         }
82 }
83
84 impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
85         type Target = T;
86
87         fn deref(&self) -> &T {
88                 &self.lock.deref()
89         }
90 }
91
92 impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
93         fn deref_mut(&mut self) -> &mut T {
94                 self.lock.deref_mut()
95         }
96 }
97
98 impl<T> RwLock<T> {
99         pub fn new(inner: T) -> RwLock<T> {
100                 RwLock { inner: RefCell::new(inner) }
101         }
102
103         pub fn read<'a>(&'a self) -> LockResult<RwLockReadGuard<'a, T>> {
104                 Ok(RwLockReadGuard { lock: self.inner.borrow() })
105         }
106
107         pub fn write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
108                 Ok(RwLockWriteGuard { lock: self.inner.borrow_mut() })
109         }
110
111         pub fn try_write<'a>(&'a self) -> LockResult<RwLockWriteGuard<'a, T>> {
112                 // There is no try, grasshopper - only used for tests and expected to fail
113                 Err(())
114         }
115 }