use std::time::Duration;
use lock_api::{MutexGuard, RawMutex};
pub trait RawCondvar {
type RawMutex: RawMutex;
fn new() -> Self;
fn wait<T, M>(&self, mutex_guard: &mut MutexGuard<'_, Self::RawMutex, T>);
fn wait_for<T, M>(
&self,
mutex_guard: &mut MutexGuard<'_, Self::RawMutex, T>,
timeout: Duration,
) -> WaitTimeoutResult;
fn notify_one(&self);
fn notify_all(&self);
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WaitTimeoutResult {
timed_out: bool,
}
impl WaitTimeoutResult {
pub fn new(timed_out: bool) -> Self {
Self { timed_out }
}
pub fn timed_out(&self) -> bool {
self.timed_out
}
}
impl RawCondvar for parking_lot_rt::Condvar {
type RawMutex = parking_lot_rt::RawMutex;
fn new() -> Self {
parking_lot_rt::Condvar::new()
}
fn wait<T, M>(&self, mutex_guard: &mut MutexGuard<'_, Self::RawMutex, T>) {
self.wait(mutex_guard);
}
fn wait_for<T, M>(
&self,
mutex_guard: &mut MutexGuard<'_, Self::RawMutex, T>,
timeout: Duration,
) -> WaitTimeoutResult {
WaitTimeoutResult::new(self.wait_for(mutex_guard, timeout).timed_out())
}
fn notify_one(&self) {
self.notify_one();
}
fn notify_all(&self) {
self.notify_all();
}
}
#[cfg(feature = "parking_lot")]
impl RawCondvar for parking_lot::Condvar {
type RawMutex = parking_lot::RawMutex;
fn new() -> Self {
parking_lot::Condvar::new()
}
fn wait<T, M>(&self, mutex_guard: &mut MutexGuard<'_, Self::RawMutex, T>) {
self.wait(mutex_guard);
}
fn wait_for<T, M>(
&self,
mutex_guard: &mut MutexGuard<'_, Self::RawMutex, T>,
timeout: Duration,
) -> WaitTimeoutResult {
WaitTimeoutResult::new(self.wait_for(mutex_guard, timeout).timed_out())
}
fn notify_one(&self) {
self.notify_one();
}
fn notify_all(&self) {
self.notify_all();
}
}