Skip to content

Commit a0a3a6b

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

File tree

5 files changed

+133
-10
lines changed

5 files changed

+133
-10
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: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ use util::scid_utils::scid_from_parts;
4444
use prelude::*;
4545
use core::{cmp,mem,fmt};
4646
use core::ops::Deref;
47-
#[cfg(any(test, feature = "fuzztarget"))]
47+
#[cfg(any(test, feature = "fuzztarget", debug_assertions))]
4848
use sync::Mutex;
49-
use sync;
5049
use bitcoin::hashes::hex::ToHex;
5150
use bitcoin::blockdata::opcodes::all::OP_PUSHBYTES_0;
5251

@@ -375,10 +374,10 @@ pub(super) struct Channel<Signer: Sign> {
375374

376375
#[cfg(debug_assertions)]
377376
/// Max to_local and to_remote outputs in a locally-generated commitment transaction
378-
holder_max_commitment_tx_output: sync::Mutex<(u64, u64)>,
377+
holder_max_commitment_tx_output: Mutex<(u64, u64)>,
379378
#[cfg(debug_assertions)]
380379
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
381-
counterparty_max_commitment_tx_output: sync::Mutex<(u64, u64)>,
380+
counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
382381

383382
last_sent_closing_fee: Option<(u32, u64, Signature)>, // (feerate, fee, holder_sig)
384383

@@ -596,9 +595,9 @@ impl<Signer: Sign> Channel<Signer> {
596595
monitor_pending_failures: Vec::new(),
597596

598597
#[cfg(debug_assertions)]
599-
holder_max_commitment_tx_output: sync::Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
598+
holder_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
600599
#[cfg(debug_assertions)]
601-
counterparty_max_commitment_tx_output: sync::Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
600+
counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
602601

603602
last_sent_closing_fee: None,
604603

@@ -837,9 +836,9 @@ impl<Signer: Sign> Channel<Signer> {
837836
monitor_pending_failures: Vec::new(),
838837

839838
#[cfg(debug_assertions)]
840-
holder_max_commitment_tx_output: sync::Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
839+
holder_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
841840
#[cfg(debug_assertions)]
842-
counterparty_max_commitment_tx_output: sync::Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
841+
counterparty_max_commitment_tx_output: Mutex::new((msg.push_msat, msg.funding_satoshis * 1000 - msg.push_msat)),
843842

844843
last_sent_closing_fee: None,
845844

@@ -4944,9 +4943,9 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<&'a K> for Channel<Signer>
49444943
feerate_per_kw,
49454944

49464945
#[cfg(debug_assertions)]
4947-
holder_max_commitment_tx_output: sync::Mutex::new((0, 0)),
4946+
holder_max_commitment_tx_output: Mutex::new((0, 0)),
49484947
#[cfg(debug_assertions)]
4949-
counterparty_max_commitment_tx_output: sync::Mutex::new((0, 0)),
4948+
counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
49504949

49514950
last_sent_closing_fee,
49524951

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)