use std::time::Duration;
use parking_lot::{Condvar, Mutex};
pub struct Signal {
mutex: Mutex<bool>,
cond: Condvar,
}
impl Signal {
pub fn new() -> Self {
Self {
mutex: Mutex::new(false),
cond: Condvar::new(),
}
}
pub fn notify(&self) {
let mut flag = self.mutex.lock();
*flag = true;
self.cond.notify_all();
}
pub fn wait(&self) {
let mut flag = self.mutex.lock();
while !*flag {
self.cond.wait(&mut flag);
}
}
pub fn wait_timeout(&self, timeout: Duration) -> bool {
let mut flag = self.mutex.lock();
if *flag {
return true;
}
self.cond.wait_for(&mut flag, timeout);
*flag
}
}