Skip to content

Commit 3873afc

Browse files
committed
Hold a reference to the Arc<FutureState> when completing futures
This will allow us to pass in that state to the callbacks in the next commit.
1 parent b734735 commit 3873afc

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

lightning/src/util/wakers.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Notifier {
4545
pub(crate) fn notify(&self) {
4646
let mut lock = self.notify_pending.lock().unwrap();
4747
if let Some(future_state) = &lock.1 {
48-
if future_state.lock().unwrap().complete() {
48+
if complete_future(future_state) {
4949
lock.1 = None;
5050
return;
5151
}
@@ -116,15 +116,15 @@ pub(crate) struct FutureState {
116116
callbacks_made: bool,
117117
}
118118

119-
impl FutureState {
120-
fn complete(&mut self) -> bool {
121-
for (counts_as_call, callback) in self.callbacks.drain(..) {
122-
callback.call();
123-
self.callbacks_made |= counts_as_call;
124-
}
125-
self.complete = true;
126-
self.callbacks_made
119+
fn complete_future(this: &Arc<Mutex<FutureState>>) -> bool {
120+
let mut state_lock = this.lock().unwrap();
121+
let state = &mut *state_lock;
122+
for (counts_as_call, callback) in state.callbacks.drain(..) {
123+
callback.call();
124+
state.callbacks_made |= counts_as_call;
127125
}
126+
state.complete = true;
127+
state.callbacks_made
128128
}
129129

130130
/// A simple future which can complete once, and calls some callback(s) when it does so.
@@ -421,9 +421,9 @@ mod tests {
421421
future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
422422

423423
assert!(!callback.load(Ordering::SeqCst));
424-
future.state.lock().unwrap().complete();
424+
complete_future(&future.state);
425425
assert!(callback.load(Ordering::SeqCst));
426-
future.state.lock().unwrap().complete();
426+
complete_future(&future.state);
427427
}
428428

429429
#[test]
@@ -435,7 +435,7 @@ mod tests {
435435
callbacks_made: false,
436436
}))
437437
};
438-
future.state.lock().unwrap().complete();
438+
complete_future(&future.state);
439439

440440
let callback = Arc::new(AtomicBool::new(false));
441441
let callback_ref = Arc::clone(&callback);
@@ -483,7 +483,7 @@ mod tests {
483483
assert_eq!(Pin::new(&mut second_future).poll(&mut Context::from_waker(&second_waker)), Poll::Pending);
484484
assert!(!second_woken.load(Ordering::SeqCst));
485485

486-
future.state.lock().unwrap().complete();
486+
complete_future(&future.state);
487487
assert!(woken.load(Ordering::SeqCst));
488488
assert!(second_woken.load(Ordering::SeqCst));
489489
assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));

0 commit comments

Comments
 (0)