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 complete_future(future_state) {
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();
60 if let Some(existing_state) = &lock.1 {
61 let mut locked = existing_state.lock().unwrap();
62 if locked.callbacks_made {
63 // If the existing `FutureState` has completed and actually made callbacks,
64 // consider the notification flag to have been cleared and reset the future state.
69 self_idx = locked.next_idx;
73 if let Some(existing_state) = &lock.1 {
74 Future { state: Arc::clone(&existing_state), self_idx }
76 let state = Arc::new(Mutex::new(FutureState {
77 callbacks: Vec::new(),
78 std_future_callbacks: Vec::new(),
79 callbacks_with_state: Vec::new(),
81 callbacks_made: false,
84 lock.1 = Some(Arc::clone(&state));
85 Future { state, self_idx: 0 }
89 #[cfg(any(test, feature = "_test_utils"))]
90 pub fn notify_pending(&self) -> bool {
91 self.notify_pending.lock().unwrap().0
95 macro_rules! define_callback { ($($bounds: path),*) => {
96 /// A callback which is called when a [`Future`] completes.
98 /// Note that this MUST NOT call back into LDK directly, it must instead schedule actions to be
99 /// taken later. Rust users should use the [`std::future::Future`] implementation for [`Future`]
102 /// Note that the [`std::future::Future`] implementation may only work for runtimes which schedule
103 /// futures when they receive a wake, rather than immediately executing them.
104 pub trait FutureCallback : $($bounds +)* {
105 /// The method which is called.
109 impl<F: Fn() $(+ $bounds)*> FutureCallback for F {
110 fn call(&self) { (self)(); }
114 #[cfg(feature = "std")]
115 define_callback!(Send);
116 #[cfg(not(feature = "std"))]
119 pub(crate) struct FutureState {
120 // `callbacks` count as having woken the users' code (as they go direct to the user), but
121 // `std_future_callbacks` and `callbacks_with_state` do not (as the first just wakes a future,
122 // we only count it after another `poll()` and the second wakes a `Sleeper` which handles
123 // setting `callbacks_made` itself).
124 callbacks: Vec<Box<dyn FutureCallback>>,
125 std_future_callbacks: Vec<(usize, StdWaker)>,
126 callbacks_with_state: Vec<Box<dyn Fn(&Arc<Mutex<FutureState>>) -> () + Send>>,
128 callbacks_made: bool,
132 fn complete_future(this: &Arc<Mutex<FutureState>>) -> bool {
133 let mut state_lock = this.lock().unwrap();
134 let state = &mut *state_lock;
135 for callback in state.callbacks.drain(..) {
137 state.callbacks_made = true;
139 for (_, waker) in state.std_future_callbacks.drain(..) {
140 waker.0.wake_by_ref();
142 for callback in state.callbacks_with_state.drain(..) {
145 state.complete = true;
149 /// A simple future which can complete once, and calls some callback(s) when it does so.
151 state: Arc<Mutex<FutureState>>,
156 /// Registers a callback to be called upon completion of this future. If the future has already
157 /// completed, the callback will be called immediately.
159 /// This is not exported to bindings users, use the bindings-only `register_callback_fn` instead
160 pub fn register_callback(&self, callback: Box<dyn FutureCallback>) {
161 let mut state = self.state.lock().unwrap();
163 state.callbacks_made = true;
167 state.callbacks.push(callback);
171 // C bindings don't (currently) know how to map `Box<dyn Trait>`, and while it could add the
172 // following wrapper, doing it in the bindings is currently much more work than simply doing it
174 /// Registers a callback to be called upon completion of this future. If the future has already
175 /// completed, the callback will be called immediately.
177 pub fn register_callback_fn<F: 'static + FutureCallback>(&self, callback: F) {
178 self.register_callback(Box::new(callback));
181 /// Waits until this [`Future`] completes.
182 #[cfg(feature = "std")]
184 Sleeper::from_single_future(&self).wait();
187 /// Waits until this [`Future`] completes or the given amount of time has elapsed.
189 /// Returns true if the [`Future`] completed, false if the time elapsed.
190 #[cfg(feature = "std")]
191 pub fn wait_timeout(&self, max_wait: Duration) -> bool {
192 Sleeper::from_single_future(&self).wait_timeout(max_wait)
196 pub fn poll_is_complete(&self) -> bool {
197 let mut state = self.state.lock().unwrap();
199 state.callbacks_made = true;
205 impl Drop for Future {
207 self.state.lock().unwrap().std_future_callbacks.retain(|(idx, _)| *idx != self.self_idx);
211 use core::task::Waker;
212 struct StdWaker(pub Waker);
214 /// This is not exported to bindings users as Rust Futures aren't usable in language bindings.
215 impl<'a> StdFuture for Future {
218 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
219 let mut state = self.state.lock().unwrap();
221 state.callbacks_made = true;
224 let waker = cx.waker().clone();
225 state.std_future_callbacks.retain(|(idx, _)| *idx != self.self_idx);
226 state.std_future_callbacks.push((self.self_idx, StdWaker(waker)));
232 /// A struct which can be used to select across many [`Future`]s at once without relying on a full
234 #[cfg(feature = "std")]
236 notifiers: Vec<Arc<Mutex<FutureState>>>,
239 #[cfg(feature = "std")]
241 /// Constructs a new sleeper from one future, allowing blocking on it.
242 pub fn from_single_future(future: &Future) -> Self {
243 Self { notifiers: vec![Arc::clone(&future.state)] }
245 /// Constructs a new sleeper from two futures, allowing blocking on both at once.
246 // Note that this is the common case - a ChannelManager and ChainMonitor.
247 pub fn from_two_futures(fut_a: &Future, fut_b: &Future) -> Self {
248 Self { notifiers: vec![Arc::clone(&fut_a.state), Arc::clone(&fut_b.state)] }
250 /// Constructs a new sleeper on many futures, allowing blocking on all at once.
251 pub fn new(futures: Vec<Future>) -> Self {
252 Self { notifiers: futures.into_iter().map(|f| Arc::clone(&f.state)).collect() }
254 /// Prepares to go into a wait loop body, creating a condition variable which we can block on
255 /// and an `Arc<Mutex<Option<_>>>` which gets set to the waking `Future`'s state prior to the
256 /// condition variable being woken.
257 fn setup_wait(&self) -> (Arc<Condvar>, Arc<Mutex<Option<Arc<Mutex<FutureState>>>>>) {
258 let cv = Arc::new(Condvar::new());
259 let notified_fut_mtx = Arc::new(Mutex::new(None));
261 for notifier_mtx in self.notifiers.iter() {
262 let cv_ref = Arc::clone(&cv);
263 let notified_fut_ref = Arc::clone(¬ified_fut_mtx);
264 let mut notifier = notifier_mtx.lock().unwrap();
265 if notifier.complete {
266 *notified_fut_mtx.lock().unwrap() = Some(Arc::clone(¬ifier_mtx));
269 notifier.callbacks_with_state.push(Box::new(move |notifier_ref| {
270 *notified_fut_ref.lock().unwrap() = Some(Arc::clone(notifier_ref));
275 (cv, notified_fut_mtx)
278 /// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed.
280 let (cv, notified_fut_mtx) = self.setup_wait();
281 let notified_fut = cv.wait_while(notified_fut_mtx.lock().unwrap(), |fut_opt| fut_opt.is_none())
282 .unwrap().take().expect("CV wait shouldn't have returned until the notifying future was set");
283 notified_fut.lock().unwrap().callbacks_made = true;
286 /// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed or the
287 /// given amount of time has elapsed. Returns true if a [`Future`] completed, false if the time
289 pub fn wait_timeout(&self, max_wait: Duration) -> bool {
290 let (cv, notified_fut_mtx) = self.setup_wait();
292 match cv.wait_timeout_while(notified_fut_mtx.lock().unwrap(), max_wait, |fut_opt| fut_opt.is_none()) {
293 Ok((_, e)) if e.timed_out() => return false,
294 Ok((mut notified_fut, _)) =>
295 notified_fut.take().expect("CV wait shouldn't have returned until the notifying future was set"),
296 Err(_) => panic!("Previous panic while a lock was held led to a lock panic"),
298 notified_fut.lock().unwrap().callbacks_made = true;
306 use core::sync::atomic::{AtomicBool, Ordering};
307 use core::future::Future as FutureTrait;
308 use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
311 fn notifier_pre_notified_future() {
312 // Previously, if we generated a future after a `Notifier` had been notified, the future
313 // would never complete. This tests this behavior, ensuring the future instead completes
315 let notifier = Notifier::new();
318 let callback = Arc::new(AtomicBool::new(false));
319 let callback_ref = Arc::clone(&callback);
320 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
321 assert!(callback.load(Ordering::SeqCst));
325 fn notifier_future_completes_wake() {
326 // Previously, if we were only using the `Future` interface to learn when a `Notifier` has
327 // been notified, we'd never mark the notifier as not-awaiting-notify. This caused the
328 // `lightning-background-processor` to persist in a tight loop.
329 let notifier = Notifier::new();
331 // First check the simple case, ensuring if we get notified a new future isn't woken until
332 // a second `notify`.
333 let callback = Arc::new(AtomicBool::new(false));
334 let callback_ref = Arc::clone(&callback);
335 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
336 assert!(!callback.load(Ordering::SeqCst));
339 assert!(callback.load(Ordering::SeqCst));
341 let callback = Arc::new(AtomicBool::new(false));
342 let callback_ref = Arc::clone(&callback);
343 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
344 assert!(!callback.load(Ordering::SeqCst));
347 assert!(callback.load(Ordering::SeqCst));
349 // Then check the case where the future is fetched before the notification, but a callback
350 // is only registered after the `notify`, ensuring that it is still sufficient to ensure we
351 // don't get an instant-wake when we get a new future.
352 let future = notifier.get_future();
355 let callback = Arc::new(AtomicBool::new(false));
356 let callback_ref = Arc::clone(&callback);
357 future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
358 assert!(callback.load(Ordering::SeqCst));
360 let callback = Arc::new(AtomicBool::new(false));
361 let callback_ref = Arc::clone(&callback);
362 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
363 assert!(!callback.load(Ordering::SeqCst));
367 fn new_future_wipes_notify_bit() {
368 // Previously, if we were only using the `Future` interface to learn when a `Notifier` has
369 // been notified, we'd never mark the notifier as not-awaiting-notify if a `Future` is
370 // fetched after the notify bit has been set.
371 let notifier = Notifier::new();
374 let callback = Arc::new(AtomicBool::new(false));
375 let callback_ref = Arc::clone(&callback);
376 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
377 assert!(callback.load(Ordering::SeqCst));
379 let callback = Arc::new(AtomicBool::new(false));
380 let callback_ref = Arc::clone(&callback);
381 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
382 assert!(!callback.load(Ordering::SeqCst));
385 assert!(callback.load(Ordering::SeqCst));
388 #[cfg(feature = "std")]
390 fn test_wait_timeout() {
391 use crate::sync::Arc;
394 let persistence_notifier = Arc::new(Notifier::new());
395 let thread_notifier = Arc::clone(&persistence_notifier);
397 let exit_thread = Arc::new(AtomicBool::new(false));
398 let exit_thread_clone = exit_thread.clone();
399 thread::spawn(move || {
401 thread_notifier.notify();
402 if exit_thread_clone.load(Ordering::SeqCst) {
408 // Check that we can block indefinitely until updates are available.
409 let _ = persistence_notifier.get_future().wait();
411 // Check that the Notifier will return after the given duration if updates are
414 if persistence_notifier.get_future().wait_timeout(Duration::from_millis(100)) {
419 exit_thread.store(true, Ordering::SeqCst);
421 // Check that the Notifier will return after the given duration even if no updates
424 if !persistence_notifier.get_future().wait_timeout(Duration::from_millis(100)) {
430 #[cfg(feature = "std")]
432 fn test_state_drops() {
433 // Previously, there was a leak if a `Notifier` was `drop`ed without ever being notified
434 // but after having been slept-on. This tests for that leak.
435 use crate::sync::Arc;
438 let notifier_a = Arc::new(Notifier::new());
439 let notifier_b = Arc::new(Notifier::new());
441 let thread_notifier_a = Arc::clone(¬ifier_a);
443 let future_a = notifier_a.get_future();
444 let future_state_a = Arc::downgrade(&future_a.state);
446 let future_b = notifier_b.get_future();
447 let future_state_b = Arc::downgrade(&future_b.state);
449 let join_handle = thread::spawn(move || {
450 // Let the other thread get to the wait point, then notify it.
451 std::thread::sleep(Duration::from_millis(50));
452 thread_notifier_a.notify();
455 // Wait on the other thread to finish its sleep, note that the leak only happened if we
456 // actually have to sleep here, not if we immediately return.
457 Sleeper::from_two_futures(&future_a, &future_b).wait();
459 join_handle.join().unwrap();
461 // then drop the notifiers and make sure the future states are gone.
462 mem::drop(notifier_a);
463 mem::drop(notifier_b);
467 assert!(future_state_a.upgrade().is_none() && future_state_b.upgrade().is_none());
471 fn test_future_callbacks() {
472 let future = Future {
473 state: Arc::new(Mutex::new(FutureState {
474 callbacks: Vec::new(),
475 std_future_callbacks: Vec::new(),
476 callbacks_with_state: Vec::new(),
478 callbacks_made: false,
483 let callback = Arc::new(AtomicBool::new(false));
484 let callback_ref = Arc::clone(&callback);
485 future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
487 assert!(!callback.load(Ordering::SeqCst));
488 complete_future(&future.state);
489 assert!(callback.load(Ordering::SeqCst));
490 complete_future(&future.state);
494 fn test_pre_completed_future_callbacks() {
495 let future = Future {
496 state: Arc::new(Mutex::new(FutureState {
497 callbacks: Vec::new(),
498 std_future_callbacks: Vec::new(),
499 callbacks_with_state: Vec::new(),
501 callbacks_made: false,
506 complete_future(&future.state);
508 let callback = Arc::new(AtomicBool::new(false));
509 let callback_ref = Arc::clone(&callback);
510 future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
512 assert!(callback.load(Ordering::SeqCst));
513 assert!(future.state.lock().unwrap().callbacks.is_empty());
516 // Rather annoyingly, there's no safe way in Rust std to construct a Waker despite it being
517 // totally possible to construct from a trait implementation (though somewhat less efficient
518 // compared to a raw VTable). Instead, we have to write out a lot of boilerplate to build a
519 // waker, which we do here with a trivial Arc<AtomicBool> data element to track woke-ness.
520 const WAKER_V_TABLE: RawWakerVTable = RawWakerVTable::new(waker_clone, wake, wake_by_ref, drop);
521 unsafe fn wake_by_ref(ptr: *const ()) { let p = ptr as *const Arc<AtomicBool>; assert!(!(*p).fetch_or(true, Ordering::SeqCst)); }
522 unsafe fn drop(ptr: *const ()) { let p = ptr as *mut Arc<AtomicBool>; let _freed = Box::from_raw(p); }
523 unsafe fn wake(ptr: *const ()) { wake_by_ref(ptr); drop(ptr); }
524 unsafe fn waker_clone(ptr: *const ()) -> RawWaker {
525 let p = ptr as *const Arc<AtomicBool>;
526 RawWaker::new(Box::into_raw(Box::new(Arc::clone(&*p))) as *const (), &WAKER_V_TABLE)
529 fn create_waker() -> (Arc<AtomicBool>, Waker) {
530 let a = Arc::new(AtomicBool::new(false));
531 let waker = unsafe { Waker::from_raw(waker_clone((&a as *const Arc<AtomicBool>) as *const ())) };
537 let mut future = Future {
538 state: Arc::new(Mutex::new(FutureState {
539 callbacks: Vec::new(),
540 std_future_callbacks: Vec::new(),
541 callbacks_with_state: Vec::new(),
543 callbacks_made: false,
548 let mut second_future = Future { state: Arc::clone(&future.state), self_idx: 1 };
550 let (woken, waker) = create_waker();
551 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
552 assert!(!woken.load(Ordering::SeqCst));
554 let (second_woken, second_waker) = create_waker();
555 assert_eq!(Pin::new(&mut second_future).poll(&mut Context::from_waker(&second_waker)), Poll::Pending);
556 assert!(!second_woken.load(Ordering::SeqCst));
558 complete_future(&future.state);
559 assert!(woken.load(Ordering::SeqCst));
560 assert!(second_woken.load(Ordering::SeqCst));
561 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
562 assert_eq!(Pin::new(&mut second_future).poll(&mut Context::from_waker(&second_waker)), Poll::Ready(()));
566 #[cfg(feature = "std")]
567 fn test_dropped_future_doesnt_count() {
568 // Tests that if a Future gets drop'd before it is poll()ed `Ready` it doesn't count as
569 // having been woken, leaving the notify-required flag set.
570 let notifier = Notifier::new();
573 // If we get a future and don't touch it we're definitely still notify-required.
574 notifier.get_future();
575 assert!(notifier.get_future().wait_timeout(Duration::from_millis(1)));
576 assert!(!notifier.get_future().wait_timeout(Duration::from_millis(1)));
578 // Even if we poll'd once but didn't observe a `Ready`, we should be notify-required.
579 let mut future = notifier.get_future();
580 let (woken, waker) = create_waker();
581 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
584 assert!(woken.load(Ordering::SeqCst));
585 assert!(notifier.get_future().wait_timeout(Duration::from_millis(1)));
587 // However, once we do poll `Ready` it should wipe the notify-required flag.
588 let mut future = notifier.get_future();
589 let (woken, waker) = create_waker();
590 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
593 assert!(woken.load(Ordering::SeqCst));
594 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
595 assert!(!notifier.get_future().wait_timeout(Duration::from_millis(1)));
599 fn test_poll_post_notify_completes() {
600 // Tests that if we have a future state that has completed, and we haven't yet requested a
601 // new future, if we get a notify prior to requesting that second future it is generated
603 let notifier = Notifier::new();
606 let mut future = notifier.get_future();
607 let (woken, waker) = create_waker();
608 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
609 assert!(!woken.load(Ordering::SeqCst));
612 let mut future = notifier.get_future();
613 let (woken, waker) = create_waker();
614 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
615 assert!(!woken.load(Ordering::SeqCst));
617 let mut future = notifier.get_future();
618 let (woken, waker) = create_waker();
619 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
620 assert!(!woken.load(Ordering::SeqCst));
623 assert!(woken.load(Ordering::SeqCst));
624 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
628 fn test_poll_post_notify_completes_initial_notified() {
629 // Identical to the previous test, but the first future completes via a wake rather than an
630 // immediate `Poll::Ready`.
631 let notifier = Notifier::new();
633 let mut future = notifier.get_future();
634 let (woken, waker) = create_waker();
635 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
638 assert!(woken.load(Ordering::SeqCst));
639 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
642 let mut future = notifier.get_future();
643 let (woken, waker) = create_waker();
644 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
645 assert!(!woken.load(Ordering::SeqCst));
647 let mut future = notifier.get_future();
648 let (woken, waker) = create_waker();
649 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
650 assert!(!woken.load(Ordering::SeqCst));
653 assert!(woken.load(Ordering::SeqCst));
654 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
658 #[cfg(feature = "std")]
659 fn test_multi_future_sleep() {
660 // Tests the `Sleeper` with multiple futures.
661 let notifier_a = Notifier::new();
662 let notifier_b = Notifier::new();
664 // Set both notifiers as woken without sleeping yet.
667 Sleeper::from_two_futures(¬ifier_a.get_future(), ¬ifier_b.get_future()).wait();
669 // One future has woken us up, but the other should still have a pending notification.
670 Sleeper::from_two_futures(¬ifier_a.get_future(), ¬ifier_b.get_future()).wait();
672 // However once we've slept twice, we should no longer have any pending notifications
673 assert!(!Sleeper::from_two_futures(¬ifier_a.get_future(), ¬ifier_b.get_future())
674 .wait_timeout(Duration::from_millis(10)));
676 // Test ordering somewhat more.
678 Sleeper::from_two_futures(¬ifier_a.get_future(), ¬ifier_b.get_future()).wait();
682 #[cfg(feature = "std")]
683 fn sleeper_with_pending_callbacks() {
684 // This is similar to the above `test_multi_future_sleep` test, but in addition registers
685 // "normal" callbacks which will cause the futures to assume notification has occurred,
686 // rather than waiting for a woken sleeper.
687 let notifier_a = Notifier::new();
688 let notifier_b = Notifier::new();
690 // Set both notifiers as woken without sleeping yet.
694 // After sleeping one future (not guaranteed which one, however) will have its notification
696 Sleeper::from_two_futures(¬ifier_a.get_future(), ¬ifier_b.get_future()).wait();
698 // By registering a callback on the futures for both notifiers, one will complete
699 // immediately, but one will remain tied to the notifier, and will complete once the
700 // notifier is next woken, which will be considered the completion of the notification.
701 let callback_a = Arc::new(AtomicBool::new(false));
702 let callback_b = Arc::new(AtomicBool::new(false));
703 let callback_a_ref = Arc::clone(&callback_a);
704 let callback_b_ref = Arc::clone(&callback_b);
705 notifier_a.get_future().register_callback(Box::new(move || assert!(!callback_a_ref.fetch_or(true, Ordering::SeqCst))));
706 notifier_b.get_future().register_callback(Box::new(move || assert!(!callback_b_ref.fetch_or(true, Ordering::SeqCst))));
707 assert!(callback_a.load(Ordering::SeqCst) ^ callback_b.load(Ordering::SeqCst));
709 // If we now notify both notifiers again, the other callback will fire, completing the
710 // notification, and we'll be back to one pending notification.
714 assert!(callback_a.load(Ordering::SeqCst) && callback_b.load(Ordering::SeqCst));
715 Sleeper::from_two_futures(¬ifier_a.get_future(), ¬ifier_b.get_future()).wait();
716 assert!(!Sleeper::from_two_futures(¬ifier_a.get_future(), ¬ifier_b.get_future())
717 .wait_timeout(Duration::from_millis(10)));
721 #[cfg(feature = "std")]
722 fn multi_poll_stores_single_waker() {
723 // When a `Future` is `poll()`ed multiple times, only the last `Waker` should be called,
724 // but previously we'd store all `Waker`s until they're all woken at once. This tests a few
725 // cases to ensure `Future`s avoid storing an endless set of `Waker`s.
726 let notifier = Notifier::new();
727 let future_state = Arc::clone(¬ifier.get_future().state);
728 assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 0);
730 // Test that simply polling a future twice doesn't result in two pending `Waker`s.
731 let mut future_a = notifier.get_future();
732 assert_eq!(Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)), Poll::Pending);
733 assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 1);
734 assert_eq!(Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)), Poll::Pending);
735 assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 1);
737 // If we poll a second future, however, that will store a second `Waker`.
738 let mut future_b = notifier.get_future();
739 assert_eq!(Pin::new(&mut future_b).poll(&mut Context::from_waker(&create_waker().1)), Poll::Pending);
740 assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 2);
742 // but when we drop the `Future`s, the pending Wakers will also be dropped.
744 assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 1);
746 assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 0);
748 // Further, after polling a future twice, if the notifier is woken all Wakers are dropped.
749 let mut future_a = notifier.get_future();
750 assert_eq!(Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)), Poll::Pending);
751 assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 1);
752 assert_eq!(Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)), Poll::Pending);
753 assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 1);
755 assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 0);
756 assert_eq!(Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)), Poll::Ready(()));
757 assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 0);