use std::fmt;
use std::fmt::Debug;
use std::sync::Condvar as StdCondvar;
use std::sync::MutexGuard;
use std::sync::WaitTimeoutResult;
use std::time::Duration;
static CONDVAR_POISONED: &str = "condvar is poisoned";
#[derive(Default)]
pub struct Condvar {
std: StdCondvar,
}
impl Condvar {
pub const fn new() -> Condvar {
Condvar {
std: StdCondvar::new(),
}
}
pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
self.std.wait(guard).expect(CONDVAR_POISONED)
}
pub fn wait_while<'a, T, F>(&self, guard: MutexGuard<'a, T>, condition: F) -> MutexGuard<'a, T>
where
F: FnMut(&mut T) -> bool,
{
self.std
.wait_while(guard, condition)
.expect(CONDVAR_POISONED)
}
pub fn wait_timeout<'a, T>(
&self,
guard: MutexGuard<'a, T>,
dur: Duration,
) -> (MutexGuard<'a, T>, WaitTimeoutResult) {
self.std.wait_timeout(guard, dur).expect(CONDVAR_POISONED)
}
pub fn wait_timeout_while<'a, T, F>(
&self,
guard: MutexGuard<'a, T>,
dur: Duration,
condition: F,
) -> (MutexGuard<'a, T>, WaitTimeoutResult)
where
F: FnMut(&mut T) -> bool,
{
self.std
.wait_timeout_while(guard, dur, condition)
.expect(CONDVAR_POISONED)
}
pub fn notify_one(&self) {
self.std.notify_one();
}
pub fn notify_all(&self) {
self.std.notify_all();
}
}
impl Debug for Condvar {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(&self.std, formatter)
}
}