Merge pull request #2067 from benthecarman/fix-typos
[rust-lightning] / lightning / src / util / wakers.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
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
8 // licenses.
9
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
12 //! be re-persisted.
13 //!
14 //! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
15
16 use alloc::sync::Arc;
17 use core::mem;
18 use crate::sync::{Condvar, Mutex, MutexGuard};
19
20 use crate::prelude::*;
21
22 #[cfg(any(test, feature = "std"))]
23 use std::time::{Duration, Instant};
24
25 use core::future::Future as StdFuture;
26 use core::task::{Context, Poll};
27 use core::pin::Pin;
28
29
30 /// Used to signal to one of many waiters that the condition they're waiting on has happened.
31 pub(crate) struct Notifier {
32         notify_pending: Mutex<(bool, Option<Arc<Mutex<FutureState>>>)>,
33         condvar: Condvar,
34 }
35
36 macro_rules! check_woken {
37         ($guard: expr, $retval: expr) => { {
38                 if $guard.0 {
39                         $guard.0 = false;
40                         if $guard.1.as_ref().map(|l| l.lock().unwrap().complete).unwrap_or(false) {
41                                 // If we're about to return as woken, and the future state is marked complete, wipe
42                                 // the future state and let the next future wait until we get a new notify.
43                                 $guard.1.take();
44                         }
45                         return $retval;
46                 }
47         } }
48 }
49
50 impl Notifier {
51         pub(crate) fn new() -> Self {
52                 Self {
53                         notify_pending: Mutex::new((false, None)),
54                         condvar: Condvar::new(),
55                 }
56         }
57
58         fn propagate_future_state_to_notify_flag(&self) -> MutexGuard<(bool, Option<Arc<Mutex<FutureState>>>)> {
59                 let mut lock = self.notify_pending.lock().unwrap();
60                 if let Some(existing_state) = &lock.1 {
61                         if existing_state.lock().unwrap().callbacks_made {
62                                 // If the existing `FutureState` has completed and actually made callbacks,
63                                 // consider the notification flag to have been cleared and reset the future state.
64                                 lock.1.take();
65                                 lock.0 = false;
66                         }
67                 }
68                 lock
69         }
70
71         pub(crate) fn wait(&self) {
72                 loop {
73                         let mut guard = self.propagate_future_state_to_notify_flag();
74                         check_woken!(guard, ());
75                         guard = self.condvar.wait(guard).unwrap();
76                         check_woken!(guard, ());
77                 }
78         }
79
80         #[cfg(any(test, feature = "std"))]
81         pub(crate) fn wait_timeout(&self, max_wait: Duration) -> bool {
82                 let current_time = Instant::now();
83                 loop {
84                         let mut guard = self.propagate_future_state_to_notify_flag();
85                         check_woken!(guard, true);
86                         guard = self.condvar.wait_timeout(guard, max_wait).unwrap().0;
87                         check_woken!(guard, true);
88                         // Due to spurious wakeups that can happen on `wait_timeout`, here we need to check if the
89                         // desired wait time has actually passed, and if not then restart the loop with a reduced wait
90                         // time. Note that this logic can be highly simplified through the use of
91                         // `Condvar::wait_while` and `Condvar::wait_timeout_while`, if and when our MSRV is raised to
92                         // 1.42.0.
93                         let elapsed = current_time.elapsed();
94                         if elapsed >= max_wait {
95                                 return false;
96                         }
97                         match max_wait.checked_sub(elapsed) {
98                                 None => return false,
99                                 Some(_) => continue
100                         }
101                 }
102         }
103
104         /// Wake waiters, tracking that wake needs to occur even if there are currently no waiters.
105         pub(crate) fn notify(&self) {
106                 let mut lock = self.notify_pending.lock().unwrap();
107                 if let Some(future_state) = &lock.1 {
108                         if future_state.lock().unwrap().complete() {
109                                 lock.1 = None;
110                                 return;
111                         }
112                 }
113                 lock.0 = true;
114                 mem::drop(lock);
115                 self.condvar.notify_all();
116         }
117
118         /// Gets a [`Future`] that will get woken up with any waiters
119         pub(crate) fn get_future(&self) -> Future {
120                 let mut lock = self.propagate_future_state_to_notify_flag();
121                 if let Some(existing_state) = &lock.1 {
122                         Future { state: Arc::clone(&existing_state) }
123                 } else {
124                         let state = Arc::new(Mutex::new(FutureState {
125                                 callbacks: Vec::new(),
126                                 complete: lock.0,
127                                 callbacks_made: false,
128                         }));
129                         lock.1 = Some(Arc::clone(&state));
130                         Future { state }
131                 }
132         }
133
134         #[cfg(any(test, feature = "_test_utils"))]
135         pub fn notify_pending(&self) -> bool {
136                 self.notify_pending.lock().unwrap().0
137         }
138 }
139
140 /// A callback which is called when a [`Future`] completes.
141 ///
142 /// Note that this MUST NOT call back into LDK directly, it must instead schedule actions to be
143 /// taken later. Rust users should use the [`std::future::Future`] implementation for [`Future`]
144 /// instead.
145 ///
146 /// Note that the [`std::future::Future`] implementation may only work for runtimes which schedule
147 /// futures when they receive a wake, rather than immediately executing them.
148 pub trait FutureCallback : Send {
149         /// The method which is called.
150         fn call(&self);
151 }
152
153 impl<F: Fn() + Send> FutureCallback for F {
154         fn call(&self) { (self)(); }
155 }
156
157 pub(crate) struct FutureState {
158         // When we're tracking whether a callback counts as having woken the user's code, we check the
159         // first bool - set to false if we're just calling a Waker, and true if we're calling an actual
160         // user-provided function.
161         callbacks: Vec<(bool, Box<dyn FutureCallback>)>,
162         complete: bool,
163         callbacks_made: bool,
164 }
165
166 impl FutureState {
167         fn complete(&mut self) -> bool {
168                 for (counts_as_call, callback) in self.callbacks.drain(..) {
169                         callback.call();
170                         self.callbacks_made |= counts_as_call;
171                 }
172                 self.complete = true;
173                 self.callbacks_made
174         }
175 }
176
177 /// A simple future which can complete once, and calls some callback(s) when it does so.
178 pub struct Future {
179         state: Arc<Mutex<FutureState>>,
180 }
181
182 impl Future {
183         /// Registers a callback to be called upon completion of this future. If the future has already
184         /// completed, the callback will be called immediately.
185         ///
186         /// (C-not exported) use the bindings-only `register_callback_fn` instead
187         pub fn register_callback(&self, callback: Box<dyn FutureCallback>) {
188                 let mut state = self.state.lock().unwrap();
189                 if state.complete {
190                         state.callbacks_made = true;
191                         mem::drop(state);
192                         callback.call();
193                 } else {
194                         state.callbacks.push((true, callback));
195                 }
196         }
197
198         // C bindings don't (currently) know how to map `Box<dyn Trait>`, and while it could add the
199         // following wrapper, doing it in the bindings is currently much more work than simply doing it
200         // here.
201         /// Registers a callback to be called upon completion of this future. If the future has already
202         /// completed, the callback will be called immediately.
203         #[cfg(c_bindings)]
204         pub fn register_callback_fn<F: 'static + FutureCallback>(&self, callback: F) {
205                 self.register_callback(Box::new(callback));
206         }
207 }
208
209 use core::task::Waker;
210 struct StdWaker(pub Waker);
211 impl FutureCallback for StdWaker {
212         fn call(&self) { self.0.wake_by_ref() }
213 }
214
215 /// (C-not exported) as Rust Futures aren't usable in language bindings.
216 impl<'a> StdFuture for Future {
217         type Output = ();
218
219         fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
220                 let mut state = self.state.lock().unwrap();
221                 if state.complete {
222                         state.callbacks_made = true;
223                         Poll::Ready(())
224                 } else {
225                         let waker = cx.waker().clone();
226                         state.callbacks.push((false, Box::new(StdWaker(waker))));
227                         Poll::Pending
228                 }
229         }
230 }
231
232 #[cfg(test)]
233 mod tests {
234         use super::*;
235         use core::sync::atomic::{AtomicBool, Ordering};
236         use core::future::Future as FutureTrait;
237         use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
238
239         #[test]
240         fn notifier_pre_notified_future() {
241                 // Previously, if we generated a future after a `Notifier` had been notified, the future
242                 // would never complete. This tests this behavior, ensuring the future instead completes
243                 // immediately.
244                 let notifier = Notifier::new();
245                 notifier.notify();
246
247                 let callback = Arc::new(AtomicBool::new(false));
248                 let callback_ref = Arc::clone(&callback);
249                 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
250                 assert!(callback.load(Ordering::SeqCst));
251         }
252
253         #[test]
254         fn notifier_future_completes_wake() {
255                 // Previously, if we were only using the `Future` interface to learn when a `Notifier` has
256                 // been notified, we'd never mark the notifier as not-awaiting-notify. This caused the
257                 // `lightning-background-processor` to persist in a tight loop.
258                 let notifier = Notifier::new();
259
260                 // First check the simple case, ensuring if we get notified a new future isn't woken until
261                 // a second `notify`.
262                 let callback = Arc::new(AtomicBool::new(false));
263                 let callback_ref = Arc::clone(&callback);
264                 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
265                 assert!(!callback.load(Ordering::SeqCst));
266
267                 notifier.notify();
268                 assert!(callback.load(Ordering::SeqCst));
269
270                 let callback = Arc::new(AtomicBool::new(false));
271                 let callback_ref = Arc::clone(&callback);
272                 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
273                 assert!(!callback.load(Ordering::SeqCst));
274
275                 notifier.notify();
276                 assert!(callback.load(Ordering::SeqCst));
277
278                 // Then check the case where the future is fetched before the notification, but a callback
279                 // is only registered after the `notify`, ensuring that it is still sufficient to ensure we
280                 // don't get an instant-wake when we get a new future.
281                 let future = notifier.get_future();
282                 notifier.notify();
283
284                 let callback = Arc::new(AtomicBool::new(false));
285                 let callback_ref = Arc::clone(&callback);
286                 future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
287                 assert!(callback.load(Ordering::SeqCst));
288
289                 let callback = Arc::new(AtomicBool::new(false));
290                 let callback_ref = Arc::clone(&callback);
291                 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
292                 assert!(!callback.load(Ordering::SeqCst));
293         }
294
295         #[test]
296         fn new_future_wipes_notify_bit() {
297                 // Previously, if we were only using the `Future` interface to learn when a `Notifier` has
298                 // been notified, we'd never mark the notifier as not-awaiting-notify if a `Future` is
299                 // fetched after the notify bit has been set.
300                 let notifier = Notifier::new();
301                 notifier.notify();
302
303                 let callback = Arc::new(AtomicBool::new(false));
304                 let callback_ref = Arc::clone(&callback);
305                 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
306                 assert!(callback.load(Ordering::SeqCst));
307
308                 let callback = Arc::new(AtomicBool::new(false));
309                 let callback_ref = Arc::clone(&callback);
310                 notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
311                 assert!(!callback.load(Ordering::SeqCst));
312
313                 notifier.notify();
314                 assert!(callback.load(Ordering::SeqCst));
315         }
316
317         #[cfg(feature = "std")]
318         #[test]
319         fn test_wait_timeout() {
320                 use crate::sync::Arc;
321                 use std::thread;
322
323                 let persistence_notifier = Arc::new(Notifier::new());
324                 let thread_notifier = Arc::clone(&persistence_notifier);
325
326                 let exit_thread = Arc::new(AtomicBool::new(false));
327                 let exit_thread_clone = exit_thread.clone();
328                 thread::spawn(move || {
329                         loop {
330                                 let mut lock = thread_notifier.notify_pending.lock().unwrap();
331                                 lock.0 = true;
332                                 thread_notifier.condvar.notify_all();
333
334                                 if exit_thread_clone.load(Ordering::SeqCst) {
335                                         break
336                                 }
337                         }
338                 });
339
340                 // Check that we can block indefinitely until updates are available.
341                 let _ = persistence_notifier.wait();
342
343                 // Check that the Notifier will return after the given duration if updates are
344                 // available.
345                 loop {
346                         if persistence_notifier.wait_timeout(Duration::from_millis(100)) {
347                                 break
348                         }
349                 }
350
351                 exit_thread.store(true, Ordering::SeqCst);
352
353                 // Check that the Notifier will return after the given duration even if no updates
354                 // are available.
355                 loop {
356                         if !persistence_notifier.wait_timeout(Duration::from_millis(100)) {
357                                 break
358                         }
359                 }
360         }
361
362         #[test]
363         fn test_future_callbacks() {
364                 let future = Future {
365                         state: Arc::new(Mutex::new(FutureState {
366                                 callbacks: Vec::new(),
367                                 complete: false,
368                                 callbacks_made: false,
369                         }))
370                 };
371                 let callback = Arc::new(AtomicBool::new(false));
372                 let callback_ref = Arc::clone(&callback);
373                 future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
374
375                 assert!(!callback.load(Ordering::SeqCst));
376                 future.state.lock().unwrap().complete();
377                 assert!(callback.load(Ordering::SeqCst));
378                 future.state.lock().unwrap().complete();
379         }
380
381         #[test]
382         fn test_pre_completed_future_callbacks() {
383                 let future = Future {
384                         state: Arc::new(Mutex::new(FutureState {
385                                 callbacks: Vec::new(),
386                                 complete: false,
387                                 callbacks_made: false,
388                         }))
389                 };
390                 future.state.lock().unwrap().complete();
391
392                 let callback = Arc::new(AtomicBool::new(false));
393                 let callback_ref = Arc::clone(&callback);
394                 future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
395
396                 assert!(callback.load(Ordering::SeqCst));
397                 assert!(future.state.lock().unwrap().callbacks.is_empty());
398         }
399
400         // Rather annoyingly, there's no safe way in Rust std to construct a Waker despite it being
401         // totally possible to construct from a trait implementation (though somewhat less effecient
402         // compared to a raw VTable). Instead, we have to write out a lot of boilerplate to build a
403         // waker, which we do here with a trivial Arc<AtomicBool> data element to track woke-ness.
404         const WAKER_V_TABLE: RawWakerVTable = RawWakerVTable::new(waker_clone, wake, wake_by_ref, drop);
405         unsafe fn wake_by_ref(ptr: *const ()) { let p = ptr as *const Arc<AtomicBool>; assert!(!(*p).fetch_or(true, Ordering::SeqCst)); }
406         unsafe fn drop(ptr: *const ()) { let p = ptr as *mut Arc<AtomicBool>; let _freed = Box::from_raw(p); }
407         unsafe fn wake(ptr: *const ()) { wake_by_ref(ptr); drop(ptr); }
408         unsafe fn waker_clone(ptr: *const ()) -> RawWaker {
409                 let p = ptr as *const Arc<AtomicBool>;
410                 RawWaker::new(Box::into_raw(Box::new(Arc::clone(&*p))) as *const (), &WAKER_V_TABLE)
411         }
412
413         fn create_waker() -> (Arc<AtomicBool>, Waker) {
414                 let a = Arc::new(AtomicBool::new(false));
415                 let waker = unsafe { Waker::from_raw(waker_clone((&a as *const Arc<AtomicBool>) as *const ())) };
416                 (a, waker)
417         }
418
419         #[test]
420         fn test_future() {
421                 let mut future = Future {
422                         state: Arc::new(Mutex::new(FutureState {
423                                 callbacks: Vec::new(),
424                                 complete: false,
425                                 callbacks_made: false,
426                         }))
427                 };
428                 let mut second_future = Future { state: Arc::clone(&future.state) };
429
430                 let (woken, waker) = create_waker();
431                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
432                 assert!(!woken.load(Ordering::SeqCst));
433
434                 let (second_woken, second_waker) = create_waker();
435                 assert_eq!(Pin::new(&mut second_future).poll(&mut Context::from_waker(&second_waker)), Poll::Pending);
436                 assert!(!second_woken.load(Ordering::SeqCst));
437
438                 future.state.lock().unwrap().complete();
439                 assert!(woken.load(Ordering::SeqCst));
440                 assert!(second_woken.load(Ordering::SeqCst));
441                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
442                 assert_eq!(Pin::new(&mut second_future).poll(&mut Context::from_waker(&second_waker)), Poll::Ready(()));
443         }
444
445         #[test]
446         fn test_dropped_future_doesnt_count() {
447                 // Tests that if a Future gets drop'd before it is poll()ed `Ready` it doesn't count as
448                 // having been woken, leaving the notify-required flag set.
449                 let notifier = Notifier::new();
450                 notifier.notify();
451
452                 // If we get a future and don't touch it we're definitely still notify-required.
453                 notifier.get_future();
454                 assert!(notifier.wait_timeout(Duration::from_millis(1)));
455                 assert!(!notifier.wait_timeout(Duration::from_millis(1)));
456
457                 // Even if we poll'd once but didn't observe a `Ready`, we should be notify-required.
458                 let mut future = notifier.get_future();
459                 let (woken, waker) = create_waker();
460                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
461
462                 notifier.notify();
463                 assert!(woken.load(Ordering::SeqCst));
464                 assert!(notifier.wait_timeout(Duration::from_millis(1)));
465
466                 // However, once we do poll `Ready` it should wipe the notify-required flag.
467                 let mut future = notifier.get_future();
468                 let (woken, waker) = create_waker();
469                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
470
471                 notifier.notify();
472                 assert!(woken.load(Ordering::SeqCst));
473                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
474                 assert!(!notifier.wait_timeout(Duration::from_millis(1)));
475         }
476
477         #[test]
478         fn test_poll_post_notify_completes() {
479                 // Tests that if we have a future state that has completed, and we haven't yet requested a
480                 // new future, if we get a notify prior to requesting that second future it is generated
481                 // pre-completed.
482                 let notifier = Notifier::new();
483
484                 notifier.notify();
485                 let mut future = notifier.get_future();
486                 let (woken, waker) = create_waker();
487                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
488                 assert!(!woken.load(Ordering::SeqCst));
489
490                 notifier.notify();
491                 let mut future = notifier.get_future();
492                 let (woken, waker) = create_waker();
493                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
494                 assert!(!woken.load(Ordering::SeqCst));
495
496                 let mut future = notifier.get_future();
497                 let (woken, waker) = create_waker();
498                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
499                 assert!(!woken.load(Ordering::SeqCst));
500
501                 notifier.notify();
502                 assert!(woken.load(Ordering::SeqCst));
503                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
504         }
505
506         #[test]
507         fn test_poll_post_notify_completes_initial_notified() {
508                 // Identical to the previous test, but the first future completes via a wake rather than an
509                 // immediate `Poll::Ready`.
510                 let notifier = Notifier::new();
511
512                 let mut future = notifier.get_future();
513                 let (woken, waker) = create_waker();
514                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
515
516                 notifier.notify();
517                 assert!(woken.load(Ordering::SeqCst));
518                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
519
520                 notifier.notify();
521                 let mut future = notifier.get_future();
522                 let (woken, waker) = create_waker();
523                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
524                 assert!(!woken.load(Ordering::SeqCst));
525
526                 let mut future = notifier.get_future();
527                 let (woken, waker) = create_waker();
528                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Pending);
529                 assert!(!woken.load(Ordering::SeqCst));
530
531                 notifier.notify();
532                 assert!(woken.load(Ordering::SeqCst));
533                 assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
534         }
535 }