use super::{LockResult, MutexGuard};
use crate::rt;
use std::sync::PoisonError;
use std::time::Duration;
#[derive(Debug)]
pub struct Condvar {
object: rt::Condvar,
}
#[derive(Debug)]
pub struct WaitTimeoutResult(bool);
impl Condvar {
pub fn new() -> Condvar {
Condvar {
object: rt::Condvar::new(),
}
}
pub fn wait<'a, T>(&self, mut guard: MutexGuard<'a, T>) -> LockResult<MutexGuard<'a, T>> {
guard.unborrow();
self.object.wait(guard.rt());
guard.reborrow();
Ok(guard)
}
pub fn wait_timeout<'a, T>(
&self,
guard: MutexGuard<'a, T>,
_dur: Duration,
) -> LockResult<(MutexGuard<'a, T>, WaitTimeoutResult)> {
self.wait(guard)
.map(|guard| (guard, WaitTimeoutResult(false)))
.map_err(|err| PoisonError::new((err.into_inner(), WaitTimeoutResult(false))))
}
pub fn notify_one(&self) {
self.object.notify_one();
}
pub fn notify_all(&self) {
self.object.notify_all();
}
}
impl WaitTimeoutResult {
pub fn timed_out(&self) -> bool {
self.0
}
}