use std::os::windows::io::{AsRawHandle, BorrowedHandle, FromRawHandle, OwnedHandle};
use std::time::Instant;
use windows::Win32::Foundation::{HANDLE, WAIT_FAILED, WAIT_OBJECT_0, WAIT_TIMEOUT};
use windows::Win32::System::Threading::{CreateEventW, SetEvent, WaitForMultipleObjects};
use crate::error::{win32_io_error, Error};
#[cfg_attr(
not(feature = "tokio"),
allow(
dead_code,
reason = "only consumers are the async driver and the async raw backend, behind the tokio feature"
)
)]
pub(crate) fn new_cancel_event() -> Result<OwnedHandle, Error> {
owned_event(unsafe { CreateEventW(None, true, false, None) })
}
fn owned_event(created: windows::core::Result<HANDLE>) -> Result<OwnedHandle, Error> {
let h = created.map_err(|e| Error::Io(win32_io_error(e)))?;
Ok(unsafe { OwnedHandle::from_raw_handle(h.0 as _) })
}
#[cfg_attr(
not(feature = "tokio"),
allow(
dead_code,
reason = "only consumers are the async driver and the async raw backend, behind the tokio feature"
)
)]
pub(crate) fn signal_cancel(event: &OwnedHandle) {
let set = unsafe { SetEvent(HANDLE(event.as_raw_handle())) };
debug_assert!(set.is_ok(), "SetEvent on an owned event handle failed: {set:?}");
if !std::thread::panicking() {
assert!(set.is_ok(), "SetEvent on an owned event handle failed: {set:?}");
} else if let Err(e) = &set {
log::error!("SetEvent failed during unwind ({e}); a parked watcher may not release");
}
}
#[cfg_attr(
not(feature = "tokio"),
allow(dead_code, reason = "only consumer is the async driver, behind the tokio feature")
)]
pub(crate) fn wait_process_or_cancel(
process: BorrowedHandle<'_>,
deadline: Option<Instant>,
cancel: &OwnedHandle,
) -> Result<bool, Error> {
#[cfg(feature = "tokio")]
crate::bounded::assert_may_block("waiting for a process to exit or be cancelled");
let handle = HANDLE(process.as_raw_handle());
#[cfg(test)]
if armed_probe::is_armed() {
let already = unsafe { windows::Win32::System::Threading::WaitForSingleObject(handle, 0) };
if already == WAIT_FAILED {
let e = std::io::Error::last_os_error();
debug_assert!(
false,
"armed_probe: non-blocking poll of an identity-verified handle failed: {e}"
);
} else if already != WAIT_OBJECT_0 {
armed_probe::notify_armed_unsignalled();
signal_cancel(cancel);
}
}
let handles = [handle, HANDLE(cancel.as_raw_handle())];
let waited = crate::wait::wait_until(deadline, |ms| unsafe { WaitForMultipleObjects(&handles, false, ms) });
let wait_failed = (waited == WAIT_FAILED).then(std::io::Error::last_os_error);
if waited == WAIT_OBJECT_0 {
Ok(true) } else if waited.0 == WAIT_OBJECT_0.0 + 1 || waited == WAIT_TIMEOUT {
Ok(false) } else if let Some(e) = wait_failed {
Err(Error::Io(e))
} else {
debug_assert!(false, "unexpected WaitForMultipleObjects verdict: {waited:?}");
Err(Error::Io(std::io::Error::other(format!(
"unexpected WaitForMultipleObjects result: {waited:?}"
))))
}
}
#[cfg(test)]
pub(crate) mod armed_probe {
use crate::relayed_probe::{self, Probe};
use std::sync::mpsc::Sender;
pub(crate) struct Armed;
impl Probe for Armed {
type Event = ();
}
pub(crate) type Guard = relayed_probe::Guard<Armed>;
#[cfg_attr(
not(feature = "tokio"),
allow(
dead_code,
reason = "only consumers are the async driver and the async raw backend, behind the tokio feature"
)
)]
pub(crate) fn install(tx: Sender<()>) -> Guard {
relayed_probe::install(tx)
}
pub(crate) fn is_armed() -> bool {
relayed_probe::is_installed::<Armed>()
}
pub(crate) fn notify_armed_unsignalled() {
relayed_probe::notify::<Armed>(());
}
}
#[cfg(test)]
#[path = "cancel_tests.rs"]
mod cancel_tests;