Skip to content

Commit 41f5c12

Browse files
committed
Drop the dummy no-std Condvar which never sleeps
In `no-std`, we exposed `wait` functions which rely on a dummy `Condvar` which never actually sleeps. This is somwhat nonsensical, not to mention confusing to users. Instead, we simply remove the `wait` methods in `no-std` builds.
1 parent 1fd2696 commit 41f5c12

File tree

3 files changed

+35
-39
lines changed

3 files changed

+35
-39
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7851,6 +7851,7 @@ mod tests {
78517851
use bitcoin::hashes::Hash;
78527852
use bitcoin::hashes::sha256::Hash as Sha256;
78537853
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
7854+
#[cfg(feature = "std")]
78547855
use core::time::Duration;
78557856
use core::sync::atomic::Ordering;
78567857
use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
@@ -7876,9 +7877,9 @@ mod tests {
78767877

78777878
// All nodes start with a persistable update pending as `create_network` connects each node
78787879
// with all other nodes to make most tests simpler.
7879-
assert!(nodes[0].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7880-
assert!(nodes[1].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7881-
assert!(nodes[2].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7880+
assert!(nodes[0].node.get_persistable_update_future().poll_is_complete());
7881+
assert!(nodes[1].node.get_persistable_update_future().poll_is_complete());
7882+
assert!(nodes[2].node.get_persistable_update_future().poll_is_complete());
78827883

78837884
let mut chan = create_announced_chan_between_nodes(&nodes, 0, 1);
78847885

@@ -7892,28 +7893,28 @@ mod tests {
78927893
&nodes[0].node.get_our_node_id()).pop().unwrap();
78937894

78947895
// The first two nodes (which opened a channel) should now require fresh persistence
7895-
assert!(nodes[0].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7896-
assert!(nodes[1].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7896+
assert!(nodes[0].node.get_persistable_update_future().poll_is_complete());
7897+
assert!(nodes[1].node.get_persistable_update_future().poll_is_complete());
78977898
// ... but the last node should not.
7898-
assert!(!nodes[2].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7899+
assert!(!nodes[2].node.get_persistable_update_future().poll_is_complete());
78997900
// After persisting the first two nodes they should no longer need fresh persistence.
7900-
assert!(!nodes[0].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7901-
assert!(!nodes[1].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7901+
assert!(!nodes[0].node.get_persistable_update_future().poll_is_complete());
7902+
assert!(!nodes[1].node.get_persistable_update_future().poll_is_complete());
79027903

79037904
// Node 3, unrelated to the only channel, shouldn't care if it receives a channel_update
79047905
// about the channel.
79057906
nodes[2].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan.0);
79067907
nodes[2].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &chan.1);
7907-
assert!(!nodes[2].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7908+
assert!(!nodes[2].node.get_persistable_update_future().poll_is_complete());
79087909

79097910
// The nodes which are a party to the channel should also ignore messages from unrelated
79107911
// parties.
79117912
nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan.0);
79127913
nodes[0].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan.1);
79137914
nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan.0);
79147915
nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &chan.1);
7915-
assert!(!nodes[0].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7916-
assert!(!nodes[1].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7916+
assert!(!nodes[0].node.get_persistable_update_future().poll_is_complete());
7917+
assert!(!nodes[1].node.get_persistable_update_future().poll_is_complete());
79177918

79187919
// At this point the channel info given by peers should still be the same.
79197920
assert_eq!(nodes[0].node.list_channels()[0], node_a_chan_info);
@@ -7930,17 +7931,17 @@ mod tests {
79307931
// persisted and that its channel info remains the same.
79317932
nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &as_update);
79327933
nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &bs_update);
7933-
assert!(!nodes[0].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7934-
assert!(!nodes[1].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7934+
assert!(!nodes[0].node.get_persistable_update_future().poll_is_complete());
7935+
assert!(!nodes[1].node.get_persistable_update_future().poll_is_complete());
79357936
assert_eq!(nodes[0].node.list_channels()[0], node_a_chan_info);
79367937
assert_eq!(nodes[1].node.list_channels()[0], node_b_chan_info);
79377938

79387939
// Finally, deliver the other peers' message, ensuring each node needs to be persisted and
79397940
// the channel info has updated.
79407941
nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &bs_update);
79417942
nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &as_update);
7942-
assert!(nodes[0].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7943-
assert!(nodes[1].node.get_persistable_update_future().wait_timeout(Duration::from_millis(1)));
7943+
assert!(nodes[0].node.get_persistable_update_future().poll_is_complete());
7944+
assert!(nodes[1].node.get_persistable_update_future().poll_is_complete());
79447945
assert_ne!(nodes[0].node.list_channels()[0], node_a_chan_info);
79457946
assert_ne!(nodes[1].node.list_channels()[0], node_b_chan_info);
79467947
}

lightning/src/sync/nostd_sync.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
11
pub use ::alloc::sync::Arc;
22
use core::ops::{Deref, DerefMut};
3-
use core::time::Duration;
43
use core::cell::{RefCell, Ref, RefMut};
54
use super::{LockTestExt, LockHeldState};
65

76
pub type LockResult<Guard> = Result<Guard, ()>;
87

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-
288
pub struct Mutex<T: ?Sized> {
299
inner: RefCell<T>
3010
}

lightning/src/util/wakers.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
1616
use alloc::sync::Arc;
1717
use core::mem;
18-
use crate::sync::{Condvar, Mutex};
18+
use crate::sync::Mutex;
1919

2020
use crate::prelude::*;
2121

22-
#[cfg(any(test, feature = "std"))]
22+
#[cfg(feature = "std")]
23+
use crate::sync::Condvar;
24+
#[cfg(feature = "std")]
2325
use std::time::Duration;
2426

2527
use core::future::Future as StdFuture;
@@ -160,17 +162,27 @@ impl Future {
160162
}
161163

162164
/// Waits until this [`Future`] completes.
165+
#[cfg(feature = "std")]
163166
pub fn wait(self) {
164167
Sleeper::from_single_future(self).wait();
165168
}
166169

167170
/// Waits until this [`Future`] completes or the given amount of time has elapsed.
168171
///
169172
/// Returns true if the [`Future`] completed, false if the time elapsed.
170-
#[cfg(any(test, feature = "std"))]
173+
#[cfg(feature = "std")]
171174
pub fn wait_timeout(self, max_wait: Duration) -> bool {
172175
Sleeper::from_single_future(self).wait_timeout(max_wait)
173176
}
177+
178+
#[cfg(test)]
179+
pub fn poll_is_complete(&self) -> bool {
180+
let mut state = self.state.lock().unwrap();
181+
if state.complete {
182+
state.callbacks_made = true;
183+
true
184+
} else { false }
185+
}
174186
}
175187

176188
use core::task::Waker;
@@ -198,10 +210,12 @@ impl<'a> StdFuture for Future {
198210

199211
/// A struct which can be used to select across many [`Future`]s at once without relying on a full
200212
/// async context.
213+
#[cfg(feature = "std")]
201214
pub struct Sleeper {
202215
notifiers: Vec<Arc<Mutex<FutureState>>>,
203216
}
204217

218+
#[cfg(feature = "std")]
205219
impl Sleeper {
206220
/// Constructs a new sleeper from one future, allowing blocking on it.
207221
pub fn from_single_future(future: Future) -> Self {
@@ -252,7 +266,6 @@ impl Sleeper {
252266
/// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed or the
253267
/// given amount of time has elapsed. Returns true if a [`Future`] completed, false if the time
254268
/// elapsed.
255-
#[cfg(any(test, feature = "std"))]
256269
pub fn wait_timeout(&self, max_wait: Duration) -> bool {
257270
let (cv, notified_fut_mtx) = self.setup_wait();
258271
let notified_fut =
@@ -478,6 +491,7 @@ mod tests {
478491
}
479492

480493
#[test]
494+
#[cfg(feature = "std")]
481495
fn test_dropped_future_doesnt_count() {
482496
// Tests that if a Future gets drop'd before it is poll()ed `Ready` it doesn't count as
483497
// having been woken, leaving the notify-required flag set.
@@ -569,6 +583,7 @@ mod tests {
569583
}
570584

571585
#[test]
586+
#[cfg(feature = "std")]
572587
fn test_multi_future_sleep() {
573588
// Tests the `Sleeper` with multiple futures.
574589
let notifier_a = Notifier::new();

0 commit comments

Comments
 (0)