use std::cell::{Cell, RefCell};
use std::io;
use super::Peek;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum HolderStep {
#[cfg_attr(not(target_os = "linux"), allow(dead_code, reason = "only the Linux holder polls"))]
Poll,
FinalPeek,
Reap,
#[cfg_attr(
not(target_os = "macos"),
allow(dead_code, reason = "only macOS takes a second peek")
)]
SecondPeek,
#[cfg_attr(
not(target_os = "linux"),
allow(dead_code, reason = "only the Linux holder blocks in waitid")
)]
BlockingWaitid,
#[cfg_attr(
not(target_os = "linux"),
allow(dead_code, reason = "only the Linux holder backs off")
)]
Backoff,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ForcedReap {
None,
Errno(i32),
}
type StepHook = Box<dyn FnOnce()>;
#[cfg(target_os = "linux")]
type WaitidObserver = Box<dyn FnMut(u32)>;
struct RegisteredHook {
id: u64,
step: HolderStep,
hook: StepHook,
}
thread_local! {
static FORCED_PEEK: RefCell<std::collections::VecDeque<io::Result<Peek>>> = const { RefCell::new(std::collections::VecDeque::new()) };
static FORCED_REAP: Cell<Option<ForcedReap>> = const { Cell::new(None) };
static FORCED_SI_CODE: Cell<Option<i32>> = const { Cell::new(None) };
#[cfg(target_os = "linux")]
static FORCED_VISIBLE_NONE: Cell<bool> = const { Cell::new(false) };
#[cfg(target_os = "linux")]
static FORCED_VISIBLE_ERRNO: Cell<Option<i32>> = const { Cell::new(None) };
#[cfg(target_os = "linux")]
static WAITID_OBSERVER: RefCell<Option<WaitidObserver>> = const { RefCell::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) };
static SIGNALS: Cell<u32> = 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<Peek>) -> Forced {
force_peeks([result])
}
pub(crate) fn force_peeks(results: impl IntoIterator<Item = io::Result<Peek>>) -> Forced {
FORCED_PEEK.with(|f| *f.borrow_mut() = results.into_iter().collect());
Forced(|| FORCED_PEEK.with(|f| f.borrow_mut().clear()))
}
#[cfg(any(feature = "tokio", target_os = "macos"))]
pub(crate) fn forced_peeks_left() -> usize {
FORCED_PEEK.with(|f| f.borrow().len())
}
#[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<Peek>> {
FORCED_PEEK.with(|f| f.borrow_mut().pop_front())
}
pub(crate) fn force_reap_once(what: ForcedReap) -> Forced {
FORCED_REAP.with(|f| f.set(Some(what)));
Forced(|| FORCED_REAP.with(|f| f.set(None)))
}
pub(crate) fn take_forced_reap() -> Option<ForcedReap> {
FORCED_REAP.with(Cell::take)
}
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)))
}
pub(crate) fn take_forced_si_code() -> Option<i32> {
FORCED_SI_CODE.with(Cell::take)
}
#[cfg(target_os = "linux")]
pub(crate) fn force_visible_none_once() -> Forced {
FORCED_VISIBLE_NONE.with(|f| f.set(true));
Forced(|| FORCED_VISIBLE_NONE.with(|f| f.set(false)))
}
#[cfg(target_os = "linux")]
pub(crate) fn take_forced_visible_none() -> bool {
FORCED_VISIBLE_NONE.with(Cell::take)
}
#[cfg(target_os = "linux")]
#[cfg_attr(
not(all(feature = "tokio", debug_assertions)),
allow(dead_code, reason = "only the async child's debug-build tests force a failed wait")
)]
pub(crate) fn force_visible_errno_once(errno: i32) -> Forced {
FORCED_VISIBLE_ERRNO.with(|f| f.set(Some(errno)));
Forced(|| FORCED_VISIBLE_ERRNO.with(|f| f.set(None)))
}
#[cfg(target_os = "linux")]
pub(crate) fn take_forced_visible_errno() -> Option<i32> {
FORCED_VISIBLE_ERRNO.with(Cell::take)
}
#[cfg(target_os = "linux")]
pub(crate) fn observe_waitid(observer: impl FnMut(u32) + 'static) -> Forced {
WAITID_OBSERVER.with(|o| *o.borrow_mut() = Some(Box::new(observer)));
Forced(|| WAITID_OBSERVER.with(|o| *o.borrow_mut() = None))
}
#[cfg(target_os = "linux")]
pub(crate) fn waitid_called(options: u32) {
let observer = WAITID_OBSERVER.with(|o| o.borrow_mut().take());
if let Some(mut observer) = observer {
observer(options);
WAITID_OBSERVER.with(|o| {
o.borrow_mut().get_or_insert(observer);
});
}
}
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)
}
pub(crate) fn signal_sent() {
SIGNALS.with(|s| s.set(s.get() + 1));
}
pub(crate) fn signals_sent() -> u32 {
SIGNALS.with(Cell::take)
}