Skip to content

Commit 9c527c8

Browse files
committed
Add CondVar::wait_{timeout_,}while to debug_sync
These are useful, but we previously couldn't use them due to our MSRV. Now that we can, we should use them, so we expose them via our normal debug_sync wrappers.
1 parent 09160db commit 9c527c8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lightning/src/sync/debug_sync.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ impl Backtrace { fn new() -> Backtrace { Backtrace {} } }
2626

2727
pub type LockResult<Guard> = Result<Guard, ()>;
2828

29+
pub struct WaitTimeoutResult(bool);
30+
impl WaitTimeoutResult {
31+
pub fn timed_out(&self) -> bool { self.0 }
32+
}
33+
2934
pub struct Condvar {
3035
inner: StdCondvar,
3136
}
@@ -40,12 +45,27 @@ impl Condvar {
4045
self.inner.wait(guard.into_inner()).map(|lock| MutexGuard { mutex, lock }).map_err(|_| ())
4146
}
4247

48+
pub fn wait_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, guard: MutexGuard<'a, T>, condition: F)
49+
-> LockResult<MutexGuard<'a, T>> {
50+
let mutex: &'a Mutex<T> = guard.mutex;
51+
self.inner.wait_while(guard.into_inner(), condition).map(|lock| MutexGuard { mutex, lock })
52+
.map_err(|_| ())
53+
}
54+
4355
#[allow(unused)]
4456
pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
4557
let mutex = guard.mutex;
4658
self.inner.wait_timeout(guard.into_inner(), dur).map(|(lock, _)| (MutexGuard { mutex, lock }, ())).map_err(|_| ())
4759
}
4860

61+
#[allow(unused)]
62+
pub fn wait_timeout_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, guard: MutexGuard<'a, T>, dur: Duration, condition: F)
63+
-> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> {
64+
let mutex = guard.mutex;
65+
self.inner.wait_timeout_while(guard.into_inner(), dur, condition).map_err(|_| ())
66+
.map(|(lock, e)| (MutexGuard { mutex, lock }, WaitTimeoutResult(e.timed_out())))
67+
}
68+
4969
pub fn notify_all(&self) { self.inner.notify_all(); }
5070
}
5171

0 commit comments

Comments
 (0)