use std::time::Duration;
use parking_lot::{Condvar, Mutex};
#[derive(Debug)]
pub struct ExitNotifier {
slot: Mutex<Option<i32>>,
cond: Condvar,
}
impl ExitNotifier {
pub fn new() -> Self {
ExitNotifier { slot: Mutex::new(None), cond: Condvar::new() }
}
pub fn notify_exit(&self, status: i32) {
let mut slot = self.slot.lock();
*slot = Some(status);
self.cond.notify_all();
}
pub fn wait(&self, timeout: Option<Duration>) -> Option<i32> {
let mut slot = self.slot.lock();
if slot.is_some() {
return *slot;
}
match timeout {
Some(t) => {
if self.cond.wait_for(&mut slot, t).timed_out() {
None
} else {
*slot
}
}
None => {
self.cond.wait(&mut slot);
*slot
}
}
}
}