Skip to content

Commit 5724f94

Browse files
committed
f use wait_while, rather than wait
1 parent 758ab06 commit 5724f94

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

lightning/src/util/wakers.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,8 @@ impl Sleeper {
240240
/// Wait until one of the [`Future`]s registered with this [`Sleeper`] has completed.
241241
pub fn wait(&self) {
242242
let (cv, notified_fut_mtx) = self.setup_wait();
243-
let notified_fut = loop {
244-
let mut notified_fut_lck = cv.wait(notified_fut_mtx.lock().unwrap()).unwrap();
245-
if let Some(notified_fut) = notified_fut_lck.take() {
246-
break notified_fut;
247-
}
248-
};
243+
let notified_fut = cv.wait_while(notified_fut_mtx.lock().unwrap(), |fut_opt| fut_opt.is_none())
244+
.unwrap().take().expect("CV wait shouldn't have returned until the notifying future was set");
249245
notified_fut.lock().unwrap().callbacks_made = true;
250246
}
251247

@@ -254,18 +250,14 @@ impl Sleeper {
254250
/// elapsed.
255251
#[cfg(any(test, feature = "std"))]
256252
pub fn wait_timeout(&self, max_wait: Duration) -> bool {
257-
let start_time = Instant::now();
258253
let (cv, notified_fut_mtx) = self.setup_wait();
259-
let notified_fut = loop {
260-
let wait_duration = max_wait.saturating_sub(start_time.elapsed());
261-
if wait_duration == Duration::from_secs(0) { return false; }
262-
match cv.wait_timeout(notified_fut_mtx.lock().unwrap(), wait_duration) {
263-
Ok((mut notified_fut, _)) if notified_fut.is_some() =>
264-
break notified_fut.take().unwrap(),
265-
Ok((notified_fut_lck, _)) => continue,
254+
let notified_fut =
255+
match cv.wait_timeout_while(notified_fut_mtx.lock().unwrap(), max_wait, |fut_opt| fut_opt.is_none()) {
256+
Ok((_, e)) if e.timed_out() => return false,
257+
Ok((mut notified_fut, _)) =>
258+
notified_fut.take().expect("CV wait shouldn't have returned until the notifying future was set"),
266259
Err(_) => panic!("Previous panic while a lock was held led to a lock panic"),
267260
};
268-
};
269261
notified_fut.lock().unwrap().callbacks_made = true;
270262
true
271263
}

0 commit comments

Comments
 (0)