1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Utilities which allow users to block on some future notification from LDK. These are
11 //! specifically used by [`ChannelManager`] to allow waiting until the [`ChannelManager`] needs to
14 //! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
18 use crate::sync::Mutex;
20 use crate::prelude::*;
22 #[cfg(feature = "std")]
23 use crate::sync::Condvar;
24 #[cfg(feature = "std")]
25 use std::time::Duration;
27 use core::future::Future as StdFuture;
28 use core::task::{Context, Poll};
32 /// Used to signal to one of many waiters that the condition they're waiting on has happened.
33 pub(crate) struct Notifier {
34 notify_pending: Mutex<(bool, Option<Arc<Mutex<FutureState>>>)>,
38 pub(crate) fn new() -> Self {
40 notify_pending: Mutex::new((false, None)),
44 /// Wake waiters, tracking that wake needs to occur even if there are currently no waiters.
45 pub(crate) fn notify(&self) {
46 let mut lock = self.notify_pending.lock().unwrap();
47 if let Some(future_state) = &lock.1 {
48 if future_state.lock().unwrap().complete() {
56 /// Gets a [`Future`] that will get woken up with any waiters
57 pub(crate) fn get_future(&self) -> Future {
58 let mut lock = self.notify_pending.lock().unwrap();
59 if let Some(existing_state) = &lock.1 {
60 if existing_state.lock().unwrap().callbacks_made {
61 // If the existing `FutureState` has completed and actually made callbacks,
62 // consider the notification flag to have been cleared and reset the future state.
67 if let Some(existing_state) = &lock.1 {
68 Future { state: Arc::clone(&existing_state) }
70 let state = Arc::new(Mutex::new(FutureState {
71 callbacks: Vec::new(),
73 callbacks_made: false,
75 lock.1 = Some(Arc::clone(&state));
80 #[cfg(any(test, feature = "_test_utils"))]
81 pub fn notify_pending(&self) -> bool {
82 self.notify_pending.lock().unwrap().0
86 macro_rules! define_callback { ($($bounds: path),*) => {
87 /// A callback which is called when a [`Future`] completes.
89 /// Note that this MUST NOT call back into LDK directly, it must instead schedule actions to be
90 /// taken later. Rust users should use the [`std::future::Future`] implementation for [`Future`]
93 /// Note that the [`std::future::Future`] implementation may only work for runtimes which schedule
94 /// futures when they receive a wake, rather than immediately executing them.
95 pub trait FutureCallback : $($bounds +)* {
96 /// The method which is called.
100 impl<F: Fn() $(+ $bounds)*> FutureCallback for F {
101 fn call(&self) { (self)(); }
105 #[cfg(feature = "std")]
106 define_callback!(Send);
107 #[cfg(not(feature = "std"))]
110 pub(crate) struct FutureState {
111 // When we're tracking whether a callback counts as having woken the user's code, we check the
112 // first bool - set to false if we're just calling a Waker, and true if we're calling an actual
113 // user-provided function.
114 callbacks: Vec<(bool, Box<dyn FutureCallback>)>,
116 callbacks_made: bool,
120 fn complete(&mut self) -> bool {
121 for (counts_as_call, callback) in self.callbacks.drain(..) {
123 self.callbacks_made |= counts_as_call;
125 self.complete = true;
130 /// A simple future which can complete once, and calls some callback(s) when it does so.
132 /// Clones can be made and all futures cloned from the same source will complete at the same time.
135 state: Arc<Mutex<FutureState>>,
139 /// Registers a callback to be called upon completion of this future. If the future has already
140 /// completed, the callback will be called immediately.
142 /// This is not exported to bindings users, use the bindings-only `register_callback_fn` instead
143 pub fn register_callback(&self, callback: Box<dyn FutureCallback>) {
144 let mut state = self.state.lock().unwrap();
146 state.callbacks_made = true;
150 state.callbacks.push((true, callback));
154 // C bindings don't (currently) know how to map `Box<dyn Trait>`, and while it could add the
155 // following wrapper, doing it in the bindings is currently much more work than simply doing it
157 /// Registers a callback to be called upon completion of this future. If the future has already
158 /// completed, the callback will be called immediately.
160 pub fn register_callback_fn<F: 'static + FutureCallback>(&self, callback: F) {
161 self.register_callback(Box::new(callback));
164 /// Waits until this [`Future`] completes.
165 #[cfg(feature = "std")]
167 Sleeper::from_single_future(self).wait();
170 /// Waits until this [`Future`] completes or the given amount of time has elapsed.
172 /// Returns true if the [`Future`] completed, false if the time elapsed.
173 #[cfg(feature = "std")]
174 pub fn wait_timeout(self, max_wait: Duration) -> bool {
175 Sleeper::from_single_future(self).wait_timeout(max_wait)
179 pub fn poll_is_complete(&self) -> bool {
180 let mut state = self.state.lock().unwrap();
182 state.callbacks_made = true;
188 use core::task::Waker;
189 struct StdWaker(pub Waker);
190 impl FutureCallback for StdWaker {
191 fn call(&self) { self.0.wake_by_ref() }
194 /// This is not exported to bindings users as Rust Futures aren't usable in language bindings.
195 impl<'a> StdFuture for Future {
198 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
199 let mut state = self.state.lock().unwrap();
201 state.callbacks_made = true;
204 let waker = cx.waker().clone();
205 state.callbacks.push((false, Box::new(StdWaker(waker))));
211 /// A struct which can be used to select across many [`Future`]s at once without relying on a full
213 #[cfg(feature = "std")]
215 notifiers: Vec<Arc<Mutex<FutureState>>>,
218 #[cfg(feature = "std")]
220 /// Constructs a new sleeper from one future, allowing blocking on it.
221 pub fn from_single_future(future: Future) -> Self {
222 Self { notifiers: vec![future.state] }
224 /// Constructs a new sleeper from two futures, allowing blocking on both at once.
225 // Note that this is the common case - a ChannelManager and ChainMonitor.
226 pub fn from_two_futures(fut_a: Future, fut_b: Future) -> Self {
227 Self { notifiers: vec![fut_a.state, fut_b.state] }
229 /// Constructs a new sleeper on many futures, allowing blocking on all at once.
230 pub fn new(futures: Vec<Future>) -> Self {
231 Self { notifiers: futures.into_iter().map(|f| f.state).collect() }
233 /// Prepares to go into a wait loop body, creating a condition variable which we can block on
234 /// and an `Arc<Mutex<Option<_>>>` which gets set to the waking `Future`'s state prior to the
235 /// condition variable being woken.
236 fn setup_wait(&self) -> (Arc<Condvar>, Arc<Mutex<Option<Arc<Mutex<FutureState>>>>>) {
237 let cv = Arc::new(Condvar::new());
238 let notified_fut_mtx = Arc::new(Mutex::new(None));
240 for notifier_mtx in self.notifiers.iter() {
241 let cv_ref = Arc::clone(&cv);
242 let notified_fut_ref = Arc::clone(¬ified_fut_mtx);
243 let notifier_ref = Arc::clone(¬ifier_mtx);
244 let mut notifier = notifier_mtx.lock().unwrap();
245 if notifier.complete {
246 *notified_fut_mtx.lock().unwrap() = Some(notifier_ref);
249 notifier.callbacks.push((false, Box::new(move || {
250 *notified_fut_ref.lock().unwrap() = Some(Arc::clone(¬ifier_ref));
255 (cv, notified_fut_mtx)
258 /// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed.
260 let (cv, notified_fut_mtx) = self.setup_wait();
261 let notified_fut = cv.wait_while(notified_fut_mtx.lock().unwrap(), |fut_opt| fut_opt.is_none())
262 .unwrap().take().expect("CV wait shouldn't have returned until the notifying future was set");
263 notified_fut.lock().unwrap().callbacks_made = true;
266 /// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed or the
267 /// given amount of time has elapsed. Returns true if a [`Future`] completed, false if the time
269 pub fn wait_timeout(&self, max_wait: Duration) -> bool {
270 let (cv, notified_fut_mtx) = self.setup_wait();
272 match cv.wait_timeout_while(notified_fut_mtx.lock().unwrap(), max_wait, |fut_opt| fut_opt.is_none()) {
273 Ok((_, e)) if e.timed_out() => return false,
274 Ok((mut notified_fut, _)) =>
275 notified_fut.take().expect("CV wait shouldn't have returned until the notifying future was set"),
276 Err(_) => panic!("Previous panic while a lock was held led to a lock panic"),
278 notified_fut.lock().unwrap().callbacks_made = true;
286 use core::sync::atomic::{AtomicBool, Ordering};
287 use core::future::Future as FutureTrait;
288 use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
291 fn notifier_pre_notified_future() {
292 // Previously, if we generated a future after a `Notifier` had been notified, the future
293 // would never complete. This tests this behavior, ensuring the future instead completes
295 let notifier = Notifier::new();
298 let callback = Arc::new(AtomicBool::new(false));
299 let callback_ref = Arc::clone(&callback);
300 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
301 assert!(callback.load(Ordering::SeqCst));
305 fn notifier_future_completes_wake() {
306 // Previously, if we were only using the `Future` interface to learn when a `Notifier` has
307 // been notified, we'd never mark the notifier as not-awaiting-notify. This caused the
308 // `lightning-background-processor` to persist in a tight loop.
309 let notifier = Notifier::new();
311 // First check the simple case, ensuring if we get notified a new future isn't woken until
312 // a second `notify`.
313 let callback = Arc::new(AtomicBool::new(false));
314 let callback_ref = Arc::clone(&callback);
315 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
316 assert!(!callback.load(Ordering::SeqCst));
319 assert!(callback.load(Ordering::SeqCst));
321 let callback = Arc::new(AtomicBool::new(false));
322 let callback_ref = Arc::clone(&callback);
323 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
324 assert!(!callback.load(Ordering::SeqCst));
327 assert!(callback.load(Ordering::SeqCst));
329 // Then check the case where the future is fetched before the notification, but a callback
330 // is only registered after the `notify`, ensuring that it is still sufficient to ensure we
331 // don't get an instant-wake when we get a new future.
332 let future = notifier.get_future();
335 let callback = Arc::new(AtomicBool::new(false));
336 let callback_ref = Arc::clone(&callback);
337 future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
338 assert!(callback.load(Ordering::SeqCst));
340 let callback = Arc::new(AtomicBool::new(false));
341 let callback_ref = Arc::clone(&callback);
342 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
343 assert!(!callback.load(Ordering::SeqCst));
347 fn new_future_wipes_notify_bit() {
348 // Previously, if we were only using the `Future` interface to learn when a `Notifier` has
349 // been notified, we'd never mark the notifier as not-awaiting-notify if a `Future` is
350 // fetched after the notify bit has been set.
351 let notifier = Notifier::new();
354 let callback = Arc::new(AtomicBool::new(false));
355 let callback_ref = Arc::clone(&callback);
356 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
357 assert!(callback.load(Ordering::SeqCst));
359 let callback = Arc::new(AtomicBool::new(false));
360 let callback_ref = Arc::clone(&callback);
361 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
362 assert!(!callback.load(Ordering::SeqCst));
365 assert!(callback.load(Ordering::SeqCst));
368 #[cfg(feature = "std")]
370 fn test_wait_timeout() {
371 use crate::sync::Arc;
374 let persistence_notifier = Arc::new(Notifier::new());
375 let thread_notifier = Arc::clone(&persistence_notifier);
377 let exit_thread = Arc::new(AtomicBool::new(false));
378 let exit_thread_clone = exit_thread.clone();
379 thread::spawn(move || {
381 thread_notifier.notify();
382 if exit_thread_clone.load(Ordering::SeqCst) {
388 // Check that we can block indefinitely until updates are available.
389 let _ = persistence_notifier.get_future().wait();
391 // Check that the Notifier will return after the given duration if updates are
394 if persistence_notifier.get_future().wait_timeout(Duration::from_millis(100)) {
399 exit_thread.store(true, Ordering::SeqCst);
401 // Check that the Notifier will return after the given duration even if no updates
404 if !persistence_notifier.get_future().wait_timeout(Duration::from_millis(100)) {
411 fn test_future_callbacks() {
412 let future = Future {
413 state: Arc::new(Mutex::new(FutureState {
414 callbacks: Vec::new(),
416 callbacks_made: false,
419 let callback = Arc::new(AtomicBool::new(false));
420 let callback_ref = Arc::clone(&callback);
421 future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
423 assert!(!callback.load(Ordering::SeqCst));
424 future.state.lock().unwrap().complete();
425 assert!(callback.load(Ordering::SeqCst));
426 future.state.lock().unwrap().complete();
430 fn test_pre_completed_future_callbacks() {
431 let future = Future {
432 state: Arc::new(Mutex::new(FutureState {
433 callbacks: Vec::new(),
435 callbacks_made: false,
438 future.state.lock().unwrap().complete();
440 let callback = Arc::new(AtomicBool::new(false));
441 let callback_ref = Arc::clone(&callback);
442 future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
444 assert!(callback.load(Ordering::SeqCst));
445 assert!(future.state.lock().unwrap().callbacks.is_empty());
448 // Rather annoyingly, there's no safe way in Rust std to construct a Waker despite it being
449 // totally possible to construct from a trait implementation (though somewhat less effecient
450 // compared to a raw VTable). Instead, we have to write out a lot of boilerplate to build a
451 // waker, which we do here with a trivial Arc<AtomicBool> data element to track woke-ness.
452 const WAKER_V_TABLE: RawWakerVTable = RawWakerVTable::new(waker_clone, wake, wake_by_ref, drop);
453 unsafe fn wake_by_ref(ptr: *const ()) { let p = ptr as *const Arc<AtomicBool>; assert!(!(*p).fetch_or(true, Ordering::SeqCst)); }
454 unsafe fn drop(ptr: *const ()) { let p = ptr as *mut Arc<AtomicBool>; let _freed = Box::from_raw(p); }
455 unsafe fn wake(ptr: *const ()) { wake_by_ref(ptr); drop(ptr); }
456 unsafe fn waker_clone(ptr: *const ()) -> RawWaker {
457 let p = ptr as *const Arc<AtomicBool>;
458 RawWaker::new(Box::into_raw(Box::new(Arc::clone(&*p))) as *const (), &WAKER_V_TABLE)
461 fn create_waker() -> (Arc<AtomicBool>, Waker) {
462 let a = Arc::new(AtomicBool::new(false));
463 let waker = unsafe { Waker::from_raw(waker_clone((&a as *const Arc<AtomicBool>) as *const ())) };
469 let mut future = Future {
470 state: Arc::new(Mutex::new(FutureState {
471 callbacks: Vec::new(),
473 callbacks_made: false,
476 let mut second_future = Future { state: Arc::clone(&future.state) };
478 let (woken, waker) = create_waker();
479 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
480 assert!(!woken.load(Ordering::SeqCst));
482 let (second_woken, second_waker) = create_waker();
483 assert_eq!(Pin::new(&mut second_future).poll(&mut Context::from_waker(&second_waker)), Poll::Pending);
484 assert!(!second_woken.load(Ordering::SeqCst));
486 future.state.lock().unwrap().complete();
487 assert!(woken.load(Ordering::SeqCst));
488 assert!(second_woken.load(Ordering::SeqCst));
489 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
490 assert_eq!(Pin::new(&mut second_future).poll(&mut Context::from_waker(&second_waker)), Poll::Ready(()));
494 #[cfg(feature = "std")]
495 fn test_dropped_future_doesnt_count() {
496 // Tests that if a Future gets drop'd before it is poll()ed `Ready` it doesn't count as
497 // having been woken, leaving the notify-required flag set.
498 let notifier = Notifier::new();
501 // If we get a future and don't touch it we're definitely still notify-required.
502 notifier.get_future();
503 assert!(notifier.get_future().wait_timeout(Duration::from_millis(1)));
504 assert!(!notifier.get_future().wait_timeout(Duration::from_millis(1)));
506 // Even if we poll'd once but didn't observe a `Ready`, we should be notify-required.
507 let mut future = notifier.get_future();
508 let (woken, waker) = create_waker();
509 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
512 assert!(woken.load(Ordering::SeqCst));
513 assert!(notifier.get_future().wait_timeout(Duration::from_millis(1)));
515 // However, once we do poll `Ready` it should wipe the notify-required flag.
516 let mut future = notifier.get_future();
517 let (woken, waker) = create_waker();
518 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
521 assert!(woken.load(Ordering::SeqCst));
522 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
523 assert!(!notifier.get_future().wait_timeout(Duration::from_millis(1)));
527 fn test_poll_post_notify_completes() {
528 // Tests that if we have a future state that has completed, and we haven't yet requested a
529 // new future, if we get a notify prior to requesting that second future it is generated
531 let notifier = Notifier::new();
534 let mut future = notifier.get_future();
535 let (woken, waker) = create_waker();
536 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
537 assert!(!woken.load(Ordering::SeqCst));
540 let mut future = notifier.get_future();
541 let (woken, waker) = create_waker();
542 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
543 assert!(!woken.load(Ordering::SeqCst));
545 let mut future = notifier.get_future();
546 let (woken, waker) = create_waker();
547 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
548 assert!(!woken.load(Ordering::SeqCst));
551 assert!(woken.load(Ordering::SeqCst));
552 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
556 fn test_poll_post_notify_completes_initial_notified() {
557 // Identical to the previous test, but the first future completes via a wake rather than an
558 // immediate `Poll::Ready`.
559 let notifier = Notifier::new();
561 let mut future = notifier.get_future();
562 let (woken, waker) = create_waker();
563 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
566 assert!(woken.load(Ordering::SeqCst));
567 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
570 let mut future = notifier.get_future();
571 let (woken, waker) = create_waker();
572 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
573 assert!(!woken.load(Ordering::SeqCst));
575 let mut future = notifier.get_future();
576 let (woken, waker) = create_waker();
577 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
578 assert!(!woken.load(Ordering::SeqCst));
581 assert!(woken.load(Ordering::SeqCst));
582 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
586 #[cfg(feature = "std")]
587 fn test_multi_future_sleep() {
588 // Tests the `Sleeper` with multiple futures.
589 let notifier_a = Notifier::new();
590 let notifier_b = Notifier::new();
592 // Set both notifiers as woken without sleeping yet.
595 Sleeper::from_two_futures(notifier_a.get_future(), notifier_b.get_future()).wait();
597 // One future has woken us up, but the other should still have a pending notification.
598 Sleeper::from_two_futures(notifier_a.get_future(), notifier_b.get_future()).wait();
600 // However once we've slept twice, we should no longer have any pending notifications
601 assert!(!Sleeper::from_two_futures(notifier_a.get_future(), notifier_b.get_future())
602 .wait_timeout(Duration::from_millis(10)));
604 // Test ordering somewhat more.
606 Sleeper::from_two_futures(notifier_a.get_future(), notifier_b.get_future()).wait();
610 #[cfg(feature = "std")]
611 fn sleeper_with_pending_callbacks() {
612 // This is similar to the above `test_multi_future_sleep` test, but in addition registers
613 // "normal" callbacks which will cause the futures to assume notification has occurred,
614 // rather than waiting for a woken sleeper.
615 let notifier_a = Notifier::new();
616 let notifier_b = Notifier::new();
618 // Set both notifiers as woken without sleeping yet.
622 // After sleeping one future (not guaranteed which one, however) will have its notification
624 Sleeper::from_two_futures(notifier_a.get_future(), notifier_b.get_future()).wait();
626 // By registering a callback on the futures for both notifiers, one will complete
627 // immediately, but one will remain tied to the notifier, and will complete once the
628 // notifier is next woken, which will be considered the completion of the notification.
629 let callback_a = Arc::new(AtomicBool::new(false));
630 let callback_b = Arc::new(AtomicBool::new(false));
631 let callback_a_ref = Arc::clone(&callback_a);
632 let callback_b_ref = Arc::clone(&callback_b);
633 notifier_a.get_future().register_callback(Box::new(move || assert!(!callback_a_ref.fetch_or(true, Ordering::SeqCst))));
634 notifier_b.get_future().register_callback(Box::new(move || assert!(!callback_b_ref.fetch_or(true, Ordering::SeqCst))));
635 assert!(callback_a.load(Ordering::SeqCst) ^ callback_b.load(Ordering::SeqCst));
637 // If we now notify both notifiers again, the other callback will fire, completing the
638 // notification, and we'll be back to one pending notification.
642 assert!(callback_a.load(Ordering::SeqCst) && callback_b.load(Ordering::SeqCst));
643 Sleeper::from_two_futures(notifier_a.get_future(), notifier_b.get_future()).wait();
644 assert!(!Sleeper::from_two_futures(notifier_a.get_future(), notifier_b.get_future())
645 .wait_timeout(Duration::from_millis(10)));