Skip to content

Commit 2222d4a

Browse files
committed
f use _while variants
1 parent 2a0469e commit 2222d4a

File tree

2 files changed

+9
-32
lines changed

2 files changed

+9
-32
lines changed

lightning/src/sync/debug_sync.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,13 @@ impl Condvar {
4040
Condvar { inner: StdCondvar::new() }
4141
}
4242

43-
pub fn wait<'a, T>(&'a self, guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
44-
let mutex: &'a Mutex<T> = guard.mutex;
45-
self.inner.wait(guard.into_inner()).map(|lock| MutexGuard { mutex, lock }).map_err(|_| ())
46-
}
47-
4843
pub fn wait_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, guard: MutexGuard<'a, T>, condition: F)
4944
-> LockResult<MutexGuard<'a, T>> {
5045
let mutex: &'a Mutex<T> = guard.mutex;
5146
self.inner.wait_while(guard.into_inner(), condition).map(|lock| MutexGuard { mutex, lock })
5247
.map_err(|_| ())
5348
}
5449

55-
#[allow(unused)]
56-
pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
57-
let mutex = guard.mutex;
58-
self.inner.wait_timeout(guard.into_inner(), dur).map(|(lock, _)| (MutexGuard { mutex, lock }, ())).map_err(|_| ())
59-
}
60-
6150
#[allow(unused)]
6251
pub fn wait_timeout_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, guard: MutexGuard<'a, T>, dur: Duration, condition: F)
6352
-> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> {

lightning/src/util/wakers.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +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 = {
244-
let mut notified_fut_lck = notified_fut_mtx.lock().unwrap();
245-
loop {
246-
if let Some(notified_fut) = notified_fut_lck.take() {
247-
break notified_fut;
248-
}
249-
notified_fut_lck = cv.wait(notified_fut_lck).unwrap();
250-
}
251-
};
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");
252245
notified_fut.lock().unwrap().callbacks_made = true;
253246
}
254247

@@ -257,19 +250,14 @@ impl Sleeper {
257250
/// elapsed.
258251
#[cfg(any(test, feature = "std"))]
259252
pub fn wait_timeout(&self, max_wait: Duration) -> bool {
260-
let start_time = Instant::now();
261253
let (cv, notified_fut_mtx) = self.setup_wait();
262-
let notified_fut = {
263-
let mut notified_fut_lck = notified_fut_mtx.lock().unwrap();
264-
loop {
265-
if let Some(notified_fut) = notified_fut_lck.take() {
266-
break notified_fut;
267-
}
268-
let sleep_time = max_wait.checked_sub(start_time.elapsed()).unwrap_or(Duration::from_secs(0));
269-
if sleep_time == Duration::from_secs(0) { return false; }
270-
notified_fut_lck = cv.wait_timeout(notified_fut_lck, max_wait).unwrap().0;
271-
}
272-
};
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"),
259+
Err(_) => panic!("Previous panic while a lock was held led to a lock panic"),
260+
};
273261
notified_fut.lock().unwrap().callbacks_made = true;
274262
true
275263
}

0 commit comments

Comments
 (0)