use std::cell::{Cell, RefCell};
use std::time::Duration;
type OnceHook = Box<dyn FnOnce()>;
type RoundHookOnce = (u32, OnceHook);
type EveryRoundHook = Box<dyn FnMut(u32)>;
thread_local! {
static AWAIT_TIMEOUTS: RefCell<Vec<Option<Duration>>> = const { RefCell::new(Vec::new()) };
static AWAIT_EVENTS: RefCell<Vec<(i16, u32)>> = const { RefCell::new(Vec::new()) };
static BACKOFF_TIMERS: RefCell<Vec<Duration>> = const { RefCell::new(Vec::new()) };
static FORCE_ESRCH_REGISTRATION: Cell<bool> = const { Cell::new(false) };
static ON_BEFORE_REPEEK: RefCell<Option<OnceHook>> = const { RefCell::new(None) };
static ON_ESRCH_REPEEK: RefCell<Option<OnceHook>> = const { RefCell::new(None) };
static ON_KEVENT_ROUND: RefCell<Option<RoundHookOnce>> = const { RefCell::new(None) };
static ON_EVERY_KEVENT_ROUND: RefCell<Option<EveryRoundHook>> = const { RefCell::new(None) };
static FORCED_EINTR: Cell<Option<Duration>> = const { Cell::new(None) };
}
pub(crate) fn record_await_kevent(requested: Option<Duration>) {
AWAIT_TIMEOUTS.with(|v| v.borrow_mut().push(requested));
}
pub(crate) fn await_requested_timeouts() -> Vec<Option<Duration>> {
AWAIT_TIMEOUTS.with(|v| v.borrow().clone())
}
pub(crate) fn record_event(filter: i16, fflags: u32) {
AWAIT_EVENTS.with(|e| e.borrow_mut().push((filter, fflags)));
}
pub(crate) fn take_events() -> Vec<(i16, u32)> {
AWAIT_EVENTS.with(|e| std::mem::take(&mut *e.borrow_mut()))
}
pub(crate) fn record_backoff_timer(interval: Duration) {
BACKOFF_TIMERS.with(|t| t.borrow_mut().push(interval));
}
#[cfg(feature = "tokio")]
pub(crate) fn take_backoff_timers() -> Vec<Duration> {
BACKOFF_TIMERS.with(|t| std::mem::take(&mut *t.borrow_mut()))
}
pub(crate) fn force_proc_registration_esrch_once() -> ForcedOnce {
FORCE_ESRCH_REGISTRATION.with(|f| f.set(true));
ForcedOnce(|| FORCE_ESRCH_REGISTRATION.with(|f| f.set(false)))
}
pub(crate) fn take_forced_esrch_registration() -> bool {
FORCE_ESRCH_REGISTRATION.with(Cell::take)
}
pub(crate) fn on_before_repeek(hook: impl FnOnce() + 'static) -> ForcedOnce {
ON_BEFORE_REPEEK.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
ForcedOnce(|| ON_BEFORE_REPEEK.with(|h| *h.borrow_mut() = None))
}
pub(crate) fn fire_before_repeek() {
if let Some(hook) = ON_BEFORE_REPEEK.with(|h| h.borrow_mut().take()) {
hook();
}
}
pub(crate) fn on_esrch_repeek(hook: impl FnOnce() + 'static) -> ForcedOnce {
ON_ESRCH_REPEEK.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
ForcedOnce(|| ON_ESRCH_REPEEK.with(|h| *h.borrow_mut() = None))
}
pub(crate) fn fire_esrch_repeek() {
if let Some(hook) = ON_ESRCH_REPEEK.with(|h| h.borrow_mut().take()) {
hook();
}
}
pub(crate) fn on_kevent_round(round: u32, hook: impl FnOnce() + 'static) -> ForcedOnce {
ON_KEVENT_ROUND.with(|h| *h.borrow_mut() = Some((round, Box::new(hook))));
ForcedOnce(|| ON_KEVENT_ROUND.with(|h| *h.borrow_mut() = None))
}
pub(crate) fn on_every_kevent_round(hook: impl FnMut(u32) + 'static) -> ForcedOnce {
ON_EVERY_KEVENT_ROUND.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
ForcedOnce(|| ON_EVERY_KEVENT_ROUND.with(|h| *h.borrow_mut() = None))
}
pub(crate) fn force_eintr_once(elapsed: Duration) -> ForcedOnce {
FORCED_EINTR.with(|f| f.set(Some(elapsed)));
ForcedOnce(|| FORCED_EINTR.with(|f| f.set(None)))
}
pub(crate) fn take_forced_eintr() -> Option<Duration> {
FORCED_EINTR.with(Cell::take)
}
pub(crate) fn fire_kevent_round(round: u32) {
if let Some(mut every) = ON_EVERY_KEVENT_ROUND.with(|h| h.borrow_mut().take()) {
every(round);
ON_EVERY_KEVENT_ROUND.with(|h| {
let mut slot = h.borrow_mut();
if slot.is_none() {
*slot = Some(every);
}
});
}
let hook = ON_KEVENT_ROUND.with(|h| {
let mut slot = h.borrow_mut();
match slot.as_ref() {
Some((r, _)) if *r == round => slot.take().map(|(_, hook)| hook),
_ => None,
}
});
if let Some(hook) = hook {
hook();
}
}
pub(crate) fn event_pending(kq: &impl std::os::fd::AsFd) -> bool {
let mut fds = [rustix::event::PollFd::new(kq, rustix::event::PollFlags::IN)];
crate::wait::any_ready_now(&mut fds).expect("poll the wait's kqueue")
}
pub(crate) fn send_signal(pid: u32, signal: libc::c_int) {
let rc = unsafe { libc::kill(pid as libc::pid_t, signal) };
assert_eq!(rc, 0, "kill({pid}, {signal}): {}", std::io::Error::last_os_error());
}
#[must_use = "dropping this immediately disarms the force; bind it for the probe's duration"]
pub(crate) struct ForcedOnce(fn());
impl Drop for ForcedOnce {
fn drop(&mut self) {
(self.0)();
}
}