Merge pull request #2148 from TheBlueMatt/2023-04-claim-from-closed
[rust-lightning] / lightning / src / util / wakers.rs
index 834721fd4aa3abf207e70405774f29958f743bf4..602c2ee04b7d0fb2b85554e8d5566aef8f3fd6d6 100644 (file)
 
 use alloc::sync::Arc;
 use core::mem;
-use crate::sync::{Condvar, Mutex};
+use crate::sync::Mutex;
 
 use crate::prelude::*;
 
-#[cfg(any(test, feature = "std"))]
+#[cfg(feature = "std")]
+use crate::sync::Condvar;
+#[cfg(feature = "std")]
 use std::time::Duration;
 
 use core::future::Future as StdFuture;
@@ -39,15 +41,6 @@ impl Notifier {
                }
        }
 
-       pub(crate) fn wait(&self) {
-               Sleeper::from_single_future(self.get_future()).wait();
-       }
-
-       #[cfg(any(test, feature = "std"))]
-       pub(crate) fn wait_timeout(&self, max_wait: Duration) -> bool {
-               Sleeper::from_single_future(self.get_future()).wait_timeout(max_wait)
-       }
-
        /// Wake waiters, tracking that wake needs to occur even if there are currently no waiters.
        pub(crate) fn notify(&self) {
                let mut lock = self.notify_pending.lock().unwrap();
@@ -167,6 +160,29 @@ impl Future {
        pub fn register_callback_fn<F: 'static + FutureCallback>(&self, callback: F) {
                self.register_callback(Box::new(callback));
        }
+
+       /// Waits until this [`Future`] completes.
+       #[cfg(feature = "std")]
+       pub fn wait(self) {
+               Sleeper::from_single_future(self).wait();
+       }
+
+       /// Waits until this [`Future`] completes or the given amount of time has elapsed.
+       ///
+       /// Returns true if the [`Future`] completed, false if the time elapsed.
+       #[cfg(feature = "std")]
+       pub fn wait_timeout(self, max_wait: Duration) -> bool {
+               Sleeper::from_single_future(self).wait_timeout(max_wait)
+       }
+
+       #[cfg(test)]
+       pub fn poll_is_complete(&self) -> bool {
+               let mut state = self.state.lock().unwrap();
+               if state.complete {
+                       state.callbacks_made = true;
+                       true
+               } else { false }
+       }
 }
 
 use core::task::Waker;
@@ -194,10 +210,12 @@ impl<'a> StdFuture for Future {
 
 /// A struct which can be used to select across many [`Future`]s at once without relying on a full
 /// async context.
+#[cfg(feature = "std")]
 pub struct Sleeper {
        notifiers: Vec<Arc<Mutex<FutureState>>>,
 }
 
+#[cfg(feature = "std")]
 impl Sleeper {
        /// Constructs a new sleeper from one future, allowing blocking on it.
        pub fn from_single_future(future: Future) -> Self {
@@ -248,7 +266,6 @@ impl Sleeper {
        /// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed or the
        /// given amount of time has elapsed. Returns true if a [`Future`] completed, false if the time
        /// elapsed.
-       #[cfg(any(test, feature = "std"))]
        pub fn wait_timeout(&self, max_wait: Duration) -> bool {
                let (cv, notified_fut_mtx) = self.setup_wait();
                let notified_fut =
@@ -369,12 +386,12 @@ mod tests {
                });
 
                // Check that we can block indefinitely until updates are available.
-               let _ = persistence_notifier.wait();
+               let _ = persistence_notifier.get_future().wait();
 
                // Check that the Notifier will return after the given duration if updates are
                // available.
                loop {
-                       if persistence_notifier.wait_timeout(Duration::from_millis(100)) {
+                       if persistence_notifier.get_future().wait_timeout(Duration::from_millis(100)) {
                                break
                        }
                }
@@ -384,7 +401,7 @@ mod tests {
                // Check that the Notifier will return after the given duration even if no updates
                // are available.
                loop {
-                       if !persistence_notifier.wait_timeout(Duration::from_millis(100)) {
+                       if !persistence_notifier.get_future().wait_timeout(Duration::from_millis(100)) {
                                break
                        }
                }
@@ -474,6 +491,7 @@ mod tests {
        }
 
        #[test]
+       #[cfg(feature = "std")]
        fn test_dropped_future_doesnt_count() {
                // Tests that if a Future gets drop'd before it is poll()ed `Ready` it doesn't count as
                // having been woken, leaving the notify-required flag set.
@@ -482,8 +500,8 @@ mod tests {
 
                // If we get a future and don't touch it we're definitely still notify-required.
                notifier.get_future();
-               assert!(notifier.wait_timeout(Duration::from_millis(1)));
-               assert!(!notifier.wait_timeout(Duration::from_millis(1)));
+               assert!(notifier.get_future().wait_timeout(Duration::from_millis(1)));
+               assert!(!notifier.get_future().wait_timeout(Duration::from_millis(1)));
 
                // Even if we poll'd once but didn't observe a `Ready`, we should be notify-required.
                let mut future = notifier.get_future();
@@ -492,7 +510,7 @@ mod tests {
 
                notifier.notify();
                assert!(woken.load(Ordering::SeqCst));
-               assert!(notifier.wait_timeout(Duration::from_millis(1)));
+               assert!(notifier.get_future().wait_timeout(Duration::from_millis(1)));
 
                // However, once we do poll `Ready` it should wipe the notify-required flag.
                let mut future = notifier.get_future();
@@ -502,7 +520,7 @@ mod tests {
                notifier.notify();
                assert!(woken.load(Ordering::SeqCst));
                assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
-               assert!(!notifier.wait_timeout(Duration::from_millis(1)));
+               assert!(!notifier.get_future().wait_timeout(Duration::from_millis(1)));
        }
 
        #[test]
@@ -565,6 +583,7 @@ mod tests {
        }
 
        #[test]
+       #[cfg(feature = "std")]
        fn test_multi_future_sleep() {
                // Tests the `Sleeper` with multiple futures.
                let notifier_a = Notifier::new();