Skip to content

Commit d18d5ba

Browse files
committed
rustfmt: Run on util/wakers.rs
1 parent e6471f3 commit d18d5ba

File tree

1 file changed

+105
-49
lines changed

1 file changed

+105
-49
lines changed

lightning/src/util/wakers.rs

Lines changed: 105 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
//!
1414
//! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
1515
16+
use crate::sync::Mutex;
1617
use alloc::sync::Arc;
1718
use core::mem;
18-
use crate::sync::Mutex;
1919

2020
#[allow(unused_imports)]
2121
use crate::prelude::*;
@@ -26,9 +26,8 @@ use crate::sync::Condvar;
2626
use std::time::Duration;
2727

2828
use core::future::Future as StdFuture;
29-
use core::task::{Context, Poll};
3029
use core::pin::Pin;
31-
30+
use core::task::{Context, Poll};
3231

3332
/// Used to signal to one of many waiters that the condition they're waiting on has happened.
3433
pub(crate) struct Notifier {
@@ -37,9 +36,7 @@ pub(crate) struct Notifier {
3736

3837
impl Notifier {
3938
pub(crate) fn new() -> Self {
40-
Self {
41-
notify_pending: Mutex::new((false, None)),
42-
}
39+
Self { notify_pending: Mutex::new((false, None)) }
4340
}
4441

4542
/// Wake waiters, tracking that wake needs to occur even if there are currently no waiters.
@@ -199,7 +196,9 @@ impl Future {
199196
if state.complete {
200197
state.callbacks_made = true;
201198
true
202-
} else { false }
199+
} else {
200+
false
201+
}
203202
}
204203
}
205204

@@ -252,11 +251,8 @@ impl Sleeper {
252251
// Note that this is the common case - a ChannelManager, a ChainMonitor, and an
253252
// OnionMessenger.
254253
pub fn from_three_futures(fut_a: &Future, fut_b: &Future, fut_c: &Future) -> Self {
255-
let notifiers = vec![
256-
Arc::clone(&fut_a.state),
257-
Arc::clone(&fut_b.state),
258-
Arc::clone(&fut_c.state)
259-
];
254+
let notifiers =
255+
vec![Arc::clone(&fut_a.state), Arc::clone(&fut_b.state), Arc::clone(&fut_c.state)];
260256
Self { notifiers }
261257
}
262258
/// Constructs a new sleeper on many futures, allowing blocking on all at once.
@@ -290,8 +286,11 @@ impl Sleeper {
290286
/// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed.
291287
pub fn wait(&self) {
292288
let (cv, notified_fut_mtx) = self.setup_wait();
293-
let notified_fut = cv.wait_while(notified_fut_mtx.lock().unwrap(), |fut_opt| fut_opt.is_none())
294-
.unwrap().take().expect("CV wait shouldn't have returned until the notifying future was set");
289+
let notified_fut = cv
290+
.wait_while(notified_fut_mtx.lock().unwrap(), |fut_opt| fut_opt.is_none())
291+
.unwrap()
292+
.take()
293+
.expect("CV wait shouldn't have returned until the notifying future was set");
295294
notified_fut.lock().unwrap().callbacks_made = true;
296295
}
297296

@@ -301,10 +300,13 @@ impl Sleeper {
301300
pub fn wait_timeout(&self, max_wait: Duration) -> bool {
302301
let (cv, notified_fut_mtx) = self.setup_wait();
303302
let notified_fut =
304-
match cv.wait_timeout_while(notified_fut_mtx.lock().unwrap(), max_wait, |fut_opt| fut_opt.is_none()) {
303+
match cv.wait_timeout_while(notified_fut_mtx.lock().unwrap(), max_wait, |fut_opt| {
304+
fut_opt.is_none()
305+
}) {
305306
Ok((_, e)) if e.timed_out() => return false,
306-
Ok((mut notified_fut, _)) =>
307-
notified_fut.take().expect("CV wait shouldn't have returned until the notifying future was set"),
307+
Ok((mut notified_fut, _)) => notified_fut
308+
.take()
309+
.expect("CV wait shouldn't have returned until the notifying future was set"),
308310
Err(_) => panic!("Previous panic while a lock was held led to a lock panic"),
309311
};
310312
notified_fut.lock().unwrap().callbacks_made = true;
@@ -315,8 +317,8 @@ impl Sleeper {
315317
#[cfg(test)]
316318
mod tests {
317319
use super::*;
318-
use core::sync::atomic::{AtomicBool, Ordering};
319320
use core::future::Future as FutureTrait;
321+
use core::sync::atomic::{AtomicBool, Ordering};
320322
use core::task::{RawWaker, RawWakerVTable};
321323

322324
#[test]
@@ -329,7 +331,9 @@ mod tests {
329331

330332
let callback = Arc::new(AtomicBool::new(false));
331333
let callback_ref = Arc::clone(&callback);
332-
notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
334+
notifier.get_future().register_callback(Box::new(move || {
335+
assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))
336+
}));
333337
assert!(callback.load(Ordering::SeqCst));
334338
}
335339

@@ -344,15 +348,19 @@ mod tests {
344348
// a second `notify`.
345349
let callback = Arc::new(AtomicBool::new(false));
346350
let callback_ref = Arc::clone(&callback);
347-
notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
351+
notifier.get_future().register_callback(Box::new(move || {
352+
assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))
353+
}));
348354
assert!(!callback.load(Ordering::SeqCst));
349355

350356
notifier.notify();
351357
assert!(callback.load(Ordering::SeqCst));
352358

353359
let callback = Arc::new(AtomicBool::new(false));
354360
let callback_ref = Arc::clone(&callback);
355-
notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
361+
notifier.get_future().register_callback(Box::new(move || {
362+
assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))
363+
}));
356364
assert!(!callback.load(Ordering::SeqCst));
357365

358366
notifier.notify();
@@ -366,12 +374,16 @@ mod tests {
366374

367375
let callback = Arc::new(AtomicBool::new(false));
368376
let callback_ref = Arc::clone(&callback);
369-
future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
377+
future.register_callback(Box::new(move || {
378+
assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))
379+
}));
370380
assert!(callback.load(Ordering::SeqCst));
371381

372382
let callback = Arc::new(AtomicBool::new(false));
373383
let callback_ref = Arc::clone(&callback);
374-
notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
384+
notifier.get_future().register_callback(Box::new(move || {
385+
assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))
386+
}));
375387
assert!(!callback.load(Ordering::SeqCst));
376388
}
377389

@@ -385,12 +397,16 @@ mod tests {
385397

386398
let callback = Arc::new(AtomicBool::new(false));
387399
let callback_ref = Arc::clone(&callback);
388-
notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
400+
notifier.get_future().register_callback(Box::new(move || {
401+
assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))
402+
}));
389403
assert!(callback.load(Ordering::SeqCst));
390404

391405
let callback = Arc::new(AtomicBool::new(false));
392406
let callback_ref = Arc::clone(&callback);
393-
notifier.get_future().register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
407+
notifier.get_future().register_callback(Box::new(move || {
408+
assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))
409+
}));
394410
assert!(!callback.load(Ordering::SeqCst));
395411

396412
notifier.notify();
@@ -408,12 +424,10 @@ mod tests {
408424

409425
let exit_thread = Arc::new(AtomicBool::new(false));
410426
let exit_thread_clone = exit_thread.clone();
411-
thread::spawn(move || {
412-
loop {
413-
thread_notifier.notify();
414-
if exit_thread_clone.load(Ordering::SeqCst) {
415-
break
416-
}
427+
thread::spawn(move || loop {
428+
thread_notifier.notify();
429+
if exit_thread_clone.load(Ordering::SeqCst) {
430+
break;
417431
}
418432
});
419433

@@ -424,7 +438,7 @@ mod tests {
424438
// available.
425439
loop {
426440
if persistence_notifier.get_future().wait_timeout(Duration::from_millis(100)) {
427-
break
441+
break;
428442
}
429443
}
430444

@@ -434,7 +448,7 @@ mod tests {
434448
// are available.
435449
loop {
436450
if !persistence_notifier.get_future().wait_timeout(Duration::from_millis(100)) {
437-
break
451+
break;
438452
}
439453
}
440454
}
@@ -494,7 +508,9 @@ mod tests {
494508
};
495509
let callback = Arc::new(AtomicBool::new(false));
496510
let callback_ref = Arc::clone(&callback);
497-
future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
511+
future.register_callback(Box::new(move || {
512+
assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))
513+
}));
498514

499515
assert!(!callback.load(Ordering::SeqCst));
500516
complete_future(&future.state);
@@ -519,7 +535,9 @@ mod tests {
519535

520536
let callback = Arc::new(AtomicBool::new(false));
521537
let callback_ref = Arc::clone(&callback);
522-
future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
538+
future.register_callback(Box::new(move || {
539+
assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))
540+
}));
523541

524542
assert!(callback.load(Ordering::SeqCst));
525543
assert!(future.state.lock().unwrap().callbacks.is_empty());
@@ -530,17 +548,27 @@ mod tests {
530548
// compared to a raw VTable). Instead, we have to write out a lot of boilerplate to build a
531549
// waker, which we do here with a trivial Arc<AtomicBool> data element to track woke-ness.
532550
const WAKER_V_TABLE: RawWakerVTable = RawWakerVTable::new(waker_clone, wake, wake_by_ref, drop);
533-
unsafe fn wake_by_ref(ptr: *const ()) { let p = ptr as *const Arc<AtomicBool>; assert!(!(*p).fetch_or(true, Ordering::SeqCst)); }
534-
unsafe fn drop(ptr: *const ()) { let p = ptr as *mut Arc<AtomicBool>; let _freed = Box::from_raw(p); }
535-
unsafe fn wake(ptr: *const ()) { wake_by_ref(ptr); drop(ptr); }
551+
unsafe fn wake_by_ref(ptr: *const ()) {
552+
let p = ptr as *const Arc<AtomicBool>;
553+
assert!(!(*p).fetch_or(true, Ordering::SeqCst));
554+
}
555+
unsafe fn drop(ptr: *const ()) {
556+
let p = ptr as *mut Arc<AtomicBool>;
557+
let _freed = Box::from_raw(p);
558+
}
559+
unsafe fn wake(ptr: *const ()) {
560+
wake_by_ref(ptr);
561+
drop(ptr);
562+
}
536563
unsafe fn waker_clone(ptr: *const ()) -> RawWaker {
537564
let p = ptr as *const Arc<AtomicBool>;
538565
RawWaker::new(Box::into_raw(Box::new(Arc::clone(&*p))) as *const (), &WAKER_V_TABLE)
539566
}
540567

541568
fn create_waker() -> (Arc<AtomicBool>, Waker) {
542569
let a = Arc::new(AtomicBool::new(false));
543-
let waker = unsafe { Waker::from_raw(waker_clone((&a as *const Arc<AtomicBool>) as *const ())) };
570+
let waker =
571+
unsafe { Waker::from_raw(waker_clone((&a as *const Arc<AtomicBool>) as *const ())) };
544572
(a, waker)
545573
}
546574

@@ -564,14 +592,20 @@ mod tests {
564592
assert!(!woken.load(Ordering::SeqCst));
565593

566594
let (second_woken, second_waker) = create_waker();
567-
assert_eq!(Pin::new(&mut second_future).poll(&mut Context::from_waker(&second_waker)), Poll::Pending);
595+
assert_eq!(
596+
Pin::new(&mut second_future).poll(&mut Context::from_waker(&second_waker)),
597+
Poll::Pending
598+
);
568599
assert!(!second_woken.load(Ordering::SeqCst));
569600

570601
complete_future(&future.state);
571602
assert!(woken.load(Ordering::SeqCst));
572603
assert!(second_woken.load(Ordering::SeqCst));
573604
assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
574-
assert_eq!(Pin::new(&mut second_future).poll(&mut Context::from_waker(&second_waker)), Poll::Ready(()));
605+
assert_eq!(
606+
Pin::new(&mut second_future).poll(&mut Context::from_waker(&second_waker)),
607+
Poll::Ready(())
608+
);
575609
}
576610

577611
#[test]
@@ -714,8 +748,12 @@ mod tests {
714748
let callback_b = Arc::new(AtomicBool::new(false));
715749
let callback_a_ref = Arc::clone(&callback_a);
716750
let callback_b_ref = Arc::clone(&callback_b);
717-
notifier_a.get_future().register_callback(Box::new(move || assert!(!callback_a_ref.fetch_or(true, Ordering::SeqCst))));
718-
notifier_b.get_future().register_callback(Box::new(move || assert!(!callback_b_ref.fetch_or(true, Ordering::SeqCst))));
751+
notifier_a.get_future().register_callback(Box::new(move || {
752+
assert!(!callback_a_ref.fetch_or(true, Ordering::SeqCst))
753+
}));
754+
notifier_b.get_future().register_callback(Box::new(move || {
755+
assert!(!callback_b_ref.fetch_or(true, Ordering::SeqCst))
756+
}));
719757
assert!(callback_a.load(Ordering::SeqCst) ^ callback_b.load(Ordering::SeqCst));
720758

721759
// If we now notify both notifiers again, the other callback will fire, completing the
@@ -740,14 +778,23 @@ mod tests {
740778

741779
// Test that simply polling a future twice doesn't result in two pending `Waker`s.
742780
let mut future_a = notifier.get_future();
743-
assert_eq!(Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)), Poll::Pending);
781+
assert_eq!(
782+
Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)),
783+
Poll::Pending
784+
);
744785
assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 1);
745-
assert_eq!(Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)), Poll::Pending);
786+
assert_eq!(
787+
Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)),
788+
Poll::Pending
789+
);
746790
assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 1);
747791

748792
// If we poll a second future, however, that will store a second `Waker`.
749793
let mut future_b = notifier.get_future();
750-
assert_eq!(Pin::new(&mut future_b).poll(&mut Context::from_waker(&create_waker().1)), Poll::Pending);
794+
assert_eq!(
795+
Pin::new(&mut future_b).poll(&mut Context::from_waker(&create_waker().1)),
796+
Poll::Pending
797+
);
751798
assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 2);
752799

753800
// but when we drop the `Future`s, the pending Wakers will also be dropped.
@@ -758,13 +805,22 @@ mod tests {
758805

759806
// Further, after polling a future twice, if the notifier is woken all Wakers are dropped.
760807
let mut future_a = notifier.get_future();
761-
assert_eq!(Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)), Poll::Pending);
808+
assert_eq!(
809+
Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)),
810+
Poll::Pending
811+
);
762812
assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 1);
763-
assert_eq!(Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)), Poll::Pending);
813+
assert_eq!(
814+
Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)),
815+
Poll::Pending
816+
);
764817
assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 1);
765818
notifier.notify();
766819
assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 0);
767-
assert_eq!(Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)), Poll::Ready(()));
820+
assert_eq!(
821+
Pin::new(&mut future_a).poll(&mut Context::from_waker(&create_waker().1)),
822+
Poll::Ready(())
823+
);
768824
assert_eq!(future_state.lock().unwrap().std_future_callbacks.len(), 0);
769825
}
770826
}

0 commit comments

Comments
 (0)