Skip to content

Commit aba7c77

Browse files
committed
Implement dummy Mutex, Condvar and RwLock
1 parent 002a5db commit aba7c77

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed

lightning/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ max_level_debug = []
2525
# This is unsafe to use in production because it may result in the counterparty publishing taking our funds.
2626
unsafe_revoked_tx_signing = []
2727
unstable = []
28+
2829
no_std = ["hashbrown"]
30+
std = []
31+
32+
default = ["std"]
2933

3034
[dependencies]
3135
bitcoin = "0.26"

lightning/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ mod prelude {
5454
pub use self::hashbrown::{HashMap, HashSet, hash_map};
5555
}
5656

57+
#[cfg(feature = "std")]
5758
mod sync {
5859
pub use ::std::sync::{Arc, Mutex, Condvar, MutexGuard, RwLock, RwLockReadGuard};
5960
}
61+
62+
#[cfg(not(feature = "std"))]
63+
mod sync;

lightning/src/ln/channel.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use core::{cmp,mem,fmt};
4646
use core::ops::Deref;
4747
#[cfg(any(test, feature = "fuzztarget"))]
4848
use sync::Mutex;
49+
#[cfg(debug_assertions)]
4950
use sync;
5051
use bitcoin::hashes::hex::ToHex;
5152
use bitcoin::blockdata::opcodes::all::OP_PUSHBYTES_0;

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4959,6 +4959,7 @@ mod tests {
49594959
use ln::features::InitFeatures;
49604960
use ln::msgs::ChannelMessageHandler;
49614961

4962+
#[cfg(feature = "std")]
49624963
#[test]
49634964
fn test_wait_timeout() {
49644965
let persistence_notifier = Arc::new(PersistenceNotifier::new());

lightning/src/sync.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
}

0 commit comments

Comments
 (0)