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.
This commit is contained in:
Matt Corallo 2023-03-20 18:26:39 +00:00
parent 2fab8873f9
commit a1b5a1bba3
2 changed files with 38 additions and 0 deletions

View file

@ -12,6 +12,8 @@ use std::sync::RwLockReadGuard as StdRwLockReadGuard;
use std::sync::RwLockWriteGuard as StdRwLockWriteGuard;
use std::sync::Condvar as StdCondvar;
pub use std::sync::WaitTimeoutResult;
use crate::prelude::HashMap;
use super::{LockTestExt, LockHeldState};
@ -40,12 +42,27 @@ impl Condvar {
self.inner.wait(guard.into_inner()).map(|lock| MutexGuard { mutex, lock }).map_err(|_| ())
}
pub fn wait_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, guard: MutexGuard<'a, T>, condition: F)
-> LockResult<MutexGuard<'a, T>> {
let mutex: &'a Mutex<T> = guard.mutex;
self.inner.wait_while(guard.into_inner(), condition).map(|lock| MutexGuard { mutex, lock })
.map_err(|_| ())
}
#[allow(unused)]
pub fn wait_timeout<'a, T>(&'a self, guard: MutexGuard<'a, T>, dur: Duration) -> LockResult<(MutexGuard<'a, T>, ())> {
let mutex = guard.mutex;
self.inner.wait_timeout(guard.into_inner(), dur).map(|(lock, _)| (MutexGuard { mutex, lock }, ())).map_err(|_| ())
}
#[allow(unused)]
pub fn wait_timeout_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, guard: MutexGuard<'a, T>, dur: Duration, condition: F)
-> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> {
let mutex = guard.mutex;
self.inner.wait_timeout_while(guard.into_inner(), dur, condition).map_err(|_| ())
.map(|(lock, e)| (MutexGuard { mutex, lock }, e))
}
pub fn notify_all(&self) { self.inner.notify_all(); }
}

View file

@ -8,6 +8,11 @@ pub type LockResult<Guard> = Result<Guard, ()>;
pub struct Condvar {}
pub struct WaitTimeoutResult(bool);
impl WaitTimeoutResult {
pub fn timed_out(&self) -> bool { self.0 }
}
impl Condvar {
pub fn new() -> Condvar {
Condvar { }
@ -22,6 +27,22 @@ impl Condvar {
Ok((guard, ()))
}
pub fn wait_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, mut guard: MutexGuard<'a, T>, mut condition: F)
-> LockResult<MutexGuard<'a, T>> {
assert!(!condition(&mut *guard));
Ok(guard)
}
#[allow(unused)]
pub fn wait_timeout_while<'a, T, F: FnMut(&mut T) -> bool>(&'a self, mut guard: MutexGuard<'a, T>, dur: Duration, mut condition: F)
-> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> {
if condition(&mut *guard) {
Ok((guard, WaitTimeoutResult(true)))
} else {
Ok((guard, WaitTimeoutResult(false)))
}
}
pub fn notify_all(&self) {}
}