use std::time::{Duration, Instant};
use crate::error::Error;
use crate::identity::ProcessId;
#[cfg_attr(target_os = "linux", path = "wait/linux.rs")]
#[cfg_attr(target_os = "macos", path = "wait/macos.rs")]
#[cfg_attr(windows, path = "wait/windows.rs")]
pub(crate) mod backend;
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
compile_error!("cosca::wait is implemented only for Linux, macOS, and Windows");
#[cfg(test)]
pub(crate) mod fault {
use std::cell::Cell;
thread_local! {
static FORCE_WATCH_ERROR: Cell<bool> = const { Cell::new(false) };
}
pub(crate) fn set_force_watch_error(on: bool) {
FORCE_WATCH_ERROR.with(|f| f.set(on));
}
pub(crate) fn take_force_watch_error() -> bool {
FORCE_WATCH_ERROR.with(|f| f.replace(false))
}
pub(crate) fn armed() -> bool {
FORCE_WATCH_ERROR.with(|f| f.get())
}
pub(crate) fn forced_watch_error() -> crate::error::Error {
crate::error::Error::Io(std::io::Error::other("forced grace-watch failure (test seam)"))
}
}
pub(crate) fn block_until_exit(id: ProcessId, timeout: Option<Duration>) -> Result<bool, Error> {
#[cfg(test)]
if fault::take_force_watch_error() {
return Err(fault::forced_watch_error());
}
let deadline = timeout.map(|d| Instant::now().checked_add(d));
backend::block_until_exit(id, deadline)
}
pub(crate) fn kill(id: ProcessId) -> Result<(), Error> {
backend::kill(id)
}
pub(crate) fn terminate(id: ProcessId) -> Result<(), Error> {
backend::terminate(id)
}
pub(crate) fn remaining(deadline: Option<Option<Instant>>) -> Option<Duration> {
match deadline {
None | Some(None) => None,
Some(Some(at)) => Some(at.saturating_duration_since(Instant::now())),
}
}
#[cfg(test)]
#[path = "wait_tests.rs"]
mod wait_tests;