Skip to content

Commit a1b5a1b

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 2fab887 commit a1b5a1b

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lightning/src/sync/debug_sync.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use std::sync::RwLockReadGuard as StdRwLockReadGuard;
1212
use std::sync::RwLockWriteGuard as StdRwLockWriteGuard;
1313
use std::sync::Condvar as StdCondvar;
1414

15+
pub use std::sync::WaitTimeoutResult;
16+
1517
use crate::prelude::HashMap;
1618

1719
use super::{LockTestExt, LockHeldState};
@@ -40,12 +42,27 @@ impl Condvar {
4042
self.inner.wait(guard.into_inner()).map(|lock| MutexGuard { mutex, lock }).map_err(|_| ())
4143
}
4244

45+
pub fn wait_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, guard: MutexGuard<'a, T>, condition: F)
46+
-> LockResult<MutexGuard<'a, T>> {
47+
let mutex: &'a Mutex<T> = guard.mutex;
48+
self.inner.wait_while(guard.into_inner(), condition).map(|lock| MutexGuard { mutex, lock })
49+
.map_err(|_| ())
50+
}
51+
4352
#[allow(unused)]
4453
pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
4554
let mutex = guard.mutex;
4655
self.inner.wait_timeout(guard.into_inner(), dur).map(|(lock, _)| (MutexGuard { mutex, lock }, ())).map_err(|_| ())
4756
}
4857

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

lightning/src/sync/nostd_sync.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ pub type LockResult<Guard> = Result<Guard, ()>;
88

99
pub struct Condvar {}
1010

11+
pub struct WaitTimeoutResult(bool);
12+
impl WaitTimeoutResult {
13+
pub fn timed_out(&self) -> bool { self.0 }
14+
}
15+
1116
impl Condvar {
1217
pub fn new() -> Condvar {
1318
Condvar { }
@@ -22,6 +27,22 @@ impl Condvar {
2227
Ok((guard, ()))
2328
}
2429

30+
pub fn wait_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, mut guard: MutexGuard<'a, T>, mut condition: F)
31+
-> LockResult<MutexGuard<'a, T>> {
32+
assert!(!condition(&mut *guard));
33+
Ok(guard)
34+
}
35+
36+
#[allow(unused)]
37+
pub fn wait_timeout_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, mut guard: MutexGuard<'a, T>, dur: Duration, mut condition: F)
38+
-> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> {
39+
if condition(&mut *guard) {
40+
Ok((guard, WaitTimeoutResult(true)))
41+
} else {
42+
Ok((guard, WaitTimeoutResult(false)))
43+
}
44+
}
45+
2546
pub fn notify_all(&self) {}
2647
}
2748

0 commit comments

Comments
 (0)