X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fwakers.rs;h=8eb6c25c6189b6aa27dcc62266dc9a40b709f689;hb=593d8c4610f082441563d4906d64175a354b1cfc;hp=b81dacbd0b33b32c5ce49c997aabc9064743a18e;hpb=8c6cb9953a3b00ce3da25fbdfd8ada0ec48fc63f;p=rust-lightning diff --git a/lightning/src/util/wakers.rs b/lightning/src/util/wakers.rs index b81dacbd..8eb6c25c 100644 --- a/lightning/src/util/wakers.rs +++ b/lightning/src/util/wakers.rs @@ -15,19 +15,17 @@ use alloc::sync::Arc; use core::mem; -use core::time::Duration; -use sync::{Condvar, Mutex}; +use crate::sync::{Condvar, Mutex}; -use prelude::{Box, Vec}; +use crate::prelude::*; #[cfg(any(test, feature = "std"))] -use std::time::Instant; +use std::time::{Duration, Instant}; use core::future::Future as StdFuture; use core::task::{Context, Poll}; use core::pin::Pin; -use prelude::*; /// Used to signal to one of many waiters that the condition they're waiting on has happened. pub(crate) struct Notifier { @@ -105,7 +103,7 @@ impl Notifier { Future { state: Arc::new(Mutex::new(FutureState { callbacks: Vec::new(), - complete: false, + complete: true, })) } } else if let Some(existing_state) = &lock.1 { @@ -165,6 +163,8 @@ pub struct Future { impl Future { /// Registers a callback to be called upon completion of this future. If the future has already /// completed, the callback will be called immediately. + /// + /// (C-not exported) use the bindings-only `register_callback_fn` instead pub fn register_callback(&self, callback: Box) { let mut state = self.state.lock().unwrap(); if state.complete { @@ -174,6 +174,16 @@ impl Future { state.callbacks.push(callback); } } + + // C bindings don't (currently) know how to map `Box`, and while it could add the + // following wrapper, doing it in the bindings is currently much more work than simply doing it + // here. + /// Registers a callback to be called upon completion of this future. If the future has already + /// completed, the callback will be called immediately. + #[cfg(c_bindings)] + pub fn register_callback_fn(&self, callback: F) { + self.register_callback(Box::new(callback)); + } } mod std_future { @@ -207,10 +217,24 @@ mod tests { use core::future::Future as FutureTrait; use core::task::{Context, Poll, RawWaker, RawWakerVTable, Waker}; + #[test] + fn notifier_pre_notified_future() { + // Previously, if we generated a future after a `Notifier` had been notified, the future + // would never complete. This tests this behavior, ensuring the future instead completes + // immediately. + let notifier = Notifier::new(); + notifier.notify(); + + let callback = Arc::new(AtomicBool::new(false)); + let callback_ref = Arc::clone(&callback); + notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst)))); + assert!(callback.load(Ordering::SeqCst)); + } + #[cfg(feature = "std")] #[test] fn test_wait_timeout() { - use sync::Arc; + use crate::sync::Arc; use std::thread; let persistence_notifier = Arc::new(Notifier::new()); @@ -294,7 +318,7 @@ mod tests { // waker, which we do here with a trivial Arc data element to track woke-ness. const WAKER_V_TABLE: RawWakerVTable = RawWakerVTable::new(waker_clone, wake, wake_by_ref, drop); unsafe fn wake_by_ref(ptr: *const ()) { let p = ptr as *const Arc; assert!(!(*p).fetch_or(true, Ordering::SeqCst)); } - unsafe fn drop(ptr: *const ()) { let p = ptr as *mut Arc; Box::from_raw(p); } + unsafe fn drop(ptr: *const ()) { let p = ptr as *mut Arc; let _freed = Box::from_raw(p); } unsafe fn wake(ptr: *const ()) { wake_by_ref(ptr); drop(ptr); } unsafe fn waker_clone(ptr: *const ()) -> RawWaker { let p = ptr as *const Arc;