use std::sync::{Condvar, Mutex, PoisonError};
use std::time::Duration;
#[derive(Debug, Default)]
pub(crate) struct ShutdownSignal {
requested: Mutex<bool>,
changed: Condvar,
}
impl ShutdownSignal {
pub(crate) fn is_requested(&self) -> bool {
*self.requested.lock().unwrap_or_else(PoisonError::into_inner)
}
pub(crate) fn request(&self) {
*self.requested.lock().unwrap_or_else(PoisonError::into_inner) = true;
self.changed.notify_all();
}
pub(crate) fn wait_timeout(&self, duration: Duration) {
let requested = self.requested.lock().unwrap_or_else(PoisonError::into_inner);
let _ = self
.changed
.wait_timeout_while(requested, duration, |requested| !*requested)
.map_err(PoisonError::into_inner);
}
}
#[cfg(test)]
#[path = "shutdown_tests.rs"]
mod tests;