use std::time::Duration;
use crate::error::Error;
#[cfg(unix)]
use crate::os_process::drive::pause_end;
use crate::os_process::drive::{wait_ended, Control, Next, ProcessExit, RunOutcome, Step, Waitable};
use crate::os_process::{OsProcess, WaitFor};
fn enter() -> Result<(), Error> {
#[cfg(test)]
if crate::wait::fault::take_force_watch_error() {
return Err(crate::wait::fault::forced_watch_error());
}
Ok(())
}
pub(crate) async fn exited_held(p: &dyn OsProcess, timeout: Option<Duration>) -> Result<bool, Error> {
enter()?;
exited(p, absolute(timeout)).await
}
async fn exited(p: &dyn OsProcess, deadline: Option<std::time::Instant>) -> Result<bool, Error> {
Ok(matches!(
run(&mut ProcessExit(p), deadline).await?,
RunOutcome::Done(())
))
}
fn absolute(timeout: Option<Duration>) -> Option<std::time::Instant> {
#[cfg(unix)]
return timeout.and_then(crate::tokio::wait::deadline_from);
#[cfg(windows)]
return timeout.and_then(crate::wait::deadline_from);
}
enum Act<'a> {
Wait(WaitFor<'a>),
Pause(Duration),
}
pub(crate) async fn run<W: Waitable>(
w: &mut W,
deadline: Option<std::time::Instant>,
) -> Result<RunOutcome<W::Output>, Error> {
let mut control = Control::new();
loop {
control.before_look(remaining(deadline));
let step = w.look()?;
let next = control.after_look(step.looked(), remaining(deadline));
let act = match (next, step) {
(Next::Done, Step::Done(out)) => return Ok(RunOutcome::Done(out)),
(Next::DeadlinePassed, _) => return Ok(RunOutcome::DeadlinePassed),
(Next::Look, _) => continue,
(Next::Pause(step), _) => Act::Pause(step),
(Next::Wait, Step::Wait(wait_for)) => Act::Wait(wait_for),
(next, _) => unreachable!("{next:?} answers a look that found something else"),
};
match act {
Act::Pause(step) => pause(step, deadline).await?,
Act::Wait(wait_for) => {
let waited = wait(&wait_for, deadline).await;
control.waited(wait_ended(w, waited)?);
}
}
}
}
#[cfg(unix)]
async fn pause(step: Duration, deadline: Option<std::time::Instant>) -> Result<(), Error> {
let now = crate::tokio::wait::tokio_now();
let Some(end) = pause_end(now, step, deadline) else {
return Ok(());
};
#[cfg(test)]
crate::wait::block_probe::record_pause(end.saturating_duration_since(now));
_ = crate::tokio::wait::arm_at(end, std::future::pending::<()>()).await;
Ok(())
}
#[cfg(windows)]
async fn pause(_step: Duration, _deadline: Option<std::time::Instant>) -> Result<(), Error> {
debug_assert!(false, "a Windows Waitable asked for a recheck");
Err(Error::Io(std::io::Error::other(
"a Windows Waitable asked for a recheck, which no Windows wait has",
)))
}
fn remaining(deadline: Option<std::time::Instant>) -> Option<Duration> {
#[cfg(unix)]
return crate::wait::remaining_at(deadline, crate::tokio::wait::tokio_now());
#[cfg(windows)]
return crate::wait::remaining(deadline);
}
#[cfg(unix)]
async fn wait(wait_for: &WaitFor<'_>, deadline: Option<std::time::Instant>) -> Result<bool, Error> {
match deadline {
Some(at) => match crate::tokio::wait::arm_at(at, watch(wait_for)).await {
Some(woke) => woke.map(|()| true),
None => Ok(false),
},
None => watch(wait_for).await.map(|()| true),
}
}
#[cfg(unix)]
async fn watch(wait_for: &WaitFor<'_>) -> Result<(), Error> {
let WaitFor::Readable(held) = wait_for;
let fd = held.try_clone_to_owned().map_err(Error::Io)?;
#[cfg(target_os = "linux")]
return crate::tokio::wait::watch_pidfd(fd).await;
#[cfg(target_os = "macos")]
{
use std::os::fd::AsFd;
use ::tokio::io::unix::AsyncFd;
use ::tokio::io::Interest;
use crate::tokio::wait::watch_readable;
let afd = AsyncFd::with_interest(fd, Interest::READABLE).map_err(Error::Io)?;
watch_readable(&afd, |fd| {
crate::os_process::poll::is_readable(fd.as_fd())
.map(|readable| readable.then_some(()))
.map_err(Error::Io)
})
.await
}
}
#[cfg(windows)]
async fn wait(wait_for: &WaitFor<'_>, deadline: Option<std::time::Instant>) -> Result<bool, Error> {
use crate::os_process::windows::registered_wait::{wait_any, Woke};
let WaitFor::Signalled(process) = wait_for;
let woke = wait_any(std::iter::once(*process), deadline).await.map_err(Error::Io)?;
Ok(woke == Woke::Signalled)
}
#[cfg(test)]
#[path = "drive_tests.rs"]
pub(crate) mod drive_tests;