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