use std::cell::{Cell, RefCell};
use std::io;
use crate::os_process::child::PeekOutcome;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum HolderStep {
FinalPeek,
#[cfg_attr(
not(target_os = "macos"),
allow(dead_code, reason = "only macOS takes a second peek")
)]
SecondPeek,
}
type StepHook = Box<dyn FnOnce()>;
struct RegisteredHook {
id: u64,
step: HolderStep,
hook: StepHook,
}
thread_local! {
static FORCED_PEEK: RefCell<std::collections::VecDeque<io::Result<PeekOutcome>>> = const { RefCell::new(std::collections::VecDeque::new()) };
#[cfg(target_os = "macos")]
static FORCED_EMPTY_REAP: Cell<bool> = const { Cell::new(false) };
#[cfg(target_os = "macos")]
static FORCED_SI_CODE: Cell<Option<i32>> = const { Cell::new(None) };
static STEPS: RefCell<Vec<HolderStep>> = const { RefCell::new(Vec::new()) };
static STEP_HOOKS: RefCell<Vec<RegisteredHook>> = const { RefCell::new(Vec::new()) };
static NEXT_HOOK_ID: Cell<u64> = const { Cell::new(0) };
}
#[must_use = "dropping this immediately disarms the force; bind it for the probe's duration"]
pub(crate) struct Forced(fn());
impl Drop for Forced {
fn drop(&mut self) {
(self.0)();
}
}
pub(crate) fn force_peek_once(result: io::Result<PeekOutcome>) -> Forced {
force_peeks([result])
}
pub(crate) fn force_peeks(results: impl IntoIterator<Item = io::Result<PeekOutcome>>) -> Forced {
FORCED_PEEK.with(|f| *f.borrow_mut() = results.into_iter().collect());
Forced(|| FORCED_PEEK.with(|f| f.borrow_mut().clear()))
}
#[track_caller]
pub(crate) fn assert_peeks_exhausted() {
let left = FORCED_PEEK.with(|f| f.borrow().len());
assert_eq!(left, 0, "{left} forced peek(s) were never consumed");
}
pub(crate) fn take_forced_peek() -> Option<io::Result<PeekOutcome>> {
FORCED_PEEK.with(|f| f.borrow_mut().pop_front())
}
#[cfg(target_os = "macos")]
pub(crate) fn force_empty_reap_once() -> Forced {
FORCED_EMPTY_REAP.with(|f| f.set(true));
Forced(|| FORCED_EMPTY_REAP.with(|f| f.set(false)))
}
#[cfg(target_os = "macos")]
pub(crate) fn take_forced_empty_reap() -> bool {
FORCED_EMPTY_REAP.with(Cell::take)
}
#[cfg(target_os = "macos")]
pub(crate) fn force_consuming_record_once(si_code: i32) -> Forced {
FORCED_SI_CODE.with(|f| f.set(Some(si_code)));
Forced(|| FORCED_SI_CODE.with(|f| f.set(None)))
}
#[cfg(target_os = "macos")]
pub(crate) fn take_forced_si_code() -> Option<i32> {
FORCED_SI_CODE.with(Cell::take)
}
pub(crate) fn step(step: HolderStep) {
STEPS.with(|s| s.borrow_mut().push(step));
let hook = STEP_HOOKS.with(|h| {
let mut hooks = h.borrow_mut();
hooks.iter().position(|h| h.step == step).map(|i| hooks.remove(i).hook)
});
if let Some(hook) = hook {
hook();
}
}
pub(crate) fn holder_steps() -> Vec<HolderStep> {
STEPS.with(|s| std::mem::take(&mut *s.borrow_mut()))
}
#[must_use = "dropping this immediately removes the hook; bind it for the probe's duration"]
pub(crate) struct StepHookGuard(u64);
impl Drop for StepHookGuard {
fn drop(&mut self) {
STEP_HOOKS.with(|h| h.borrow_mut().retain(|hook| hook.id != self.0));
}
}
pub(crate) fn on_holder_step(step: HolderStep, hook: impl FnOnce() + 'static) -> StepHookGuard {
let id = NEXT_HOOK_ID.with(|n| n.replace(n.get() + 1));
STEP_HOOKS.with(|h| {
h.borrow_mut().push(RegisteredHook {
id,
step,
hook: Box::new(hook),
});
});
StepHookGuard(id)
}