use std::cell::{Cell, RefCell};
use std::time::Duration;
use nix::sys::event::Kqueue;
type RoundHook = Box<dyn FnMut(u32, &Kqueue)>;
type PostEventHook = Box<dyn FnMut(u32)>;
thread_local! {
static ROUND_HOOK: RefCell<Option<RoundHook>> = const { RefCell::new(None) };
static POST_EVENT_HOOK: RefCell<Option<PostEventHook>> = const { RefCell::new(None) };
static KEVENT_CALLS: Cell<u32> = const { Cell::new(0) };
static REQUESTED_TIMEOUTS: RefCell<Vec<Option<Duration>>> = const { RefCell::new(Vec::new()) };
static LAST_EVENT_DATA: Cell<Option<isize>> = const { Cell::new(None) };
static TIMEOUT_OVERRIDE: Cell<Option<Duration>> = const { Cell::new(None) };
static CLAMP_OVERRIDE: Cell<Option<Duration>> = const { Cell::new(None) };
static IN_HOOK: Cell<bool> = const { Cell::new(false) };
static GUARD_ACTIVE: Cell<bool> = const { Cell::new(false) };
}
const REENTRANCY_MESSAGE: &str = "a test hook must not re-enter block_on_kqueue: its own \
RefCell borrow is held for the hook's whole call — drive any nested wait through a \
separate, already-armed kqueue instead (see block_until_marker_ready_again in \
marker_eof_tests)";
fn set_round_hook(hook: impl FnMut(u32, &Kqueue) + 'static) {
ROUND_HOOK.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
}
fn set_post_event_hook(hook: impl FnMut(u32) + 'static) {
POST_EVENT_HOOK.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
}
pub(crate) fn fire_round_hook(round: u32, kq: &Kqueue) {
debug_assert!(!IN_HOOK.with(Cell::get), "{REENTRANCY_MESSAGE}");
IN_HOOK.with(|f| f.set(true));
ROUND_HOOK.with(|h| {
if let Some(hook) = h.borrow_mut().as_mut() {
hook(round, kq);
}
});
IN_HOOK.with(|f| f.set(false));
}
pub(crate) fn fire_post_event_hook(round: u32) {
debug_assert!(!IN_HOOK.with(Cell::get), "{REENTRANCY_MESSAGE}");
IN_HOOK.with(|f| f.set(true));
POST_EVENT_HOOK.with(|h| {
if let Some(hook) = h.borrow_mut().as_mut() {
hook(round);
}
});
IN_HOOK.with(|f| f.set(false));
}
pub(crate) fn kevent_calls() -> u32 {
KEVENT_CALLS.with(Cell::get)
}
pub(crate) fn requested_timeouts() -> Vec<Option<Duration>> {
REQUESTED_TIMEOUTS.with(|v| v.borrow().clone())
}
pub(crate) fn record_kevent_call(requested: Option<Duration>) {
KEVENT_CALLS.with(|c| c.set(c.get() + 1));
REQUESTED_TIMEOUTS.with(|v| v.borrow_mut().push(requested));
}
pub(crate) fn last_event_data() -> Option<isize> {
LAST_EVENT_DATA.with(Cell::get)
}
pub(crate) fn record_event_data(data: isize) {
LAST_EVENT_DATA.with(|c| c.set(Some(data)));
}
pub(crate) fn set_timeout_override(d: Duration) {
TIMEOUT_OVERRIDE.with(|c| c.set(Some(d)));
}
pub(crate) fn take_timeout_override() -> Option<Duration> {
TIMEOUT_OVERRIDE.with(|c| c.take())
}
pub(crate) fn set_clamp_override(d: Duration) {
debug_assert!(
!d.is_zero() && d <= Duration::from_secs(super::KEVENT_MAX_SECS),
"set_clamp_override may only lower the clamp to a positive value, got {d:?}"
);
debug_assert!(
GUARD_ACTIVE.with(Cell::get),
"set_clamp_override needs a live HookGuard: only its Drop resets the override"
);
CLAMP_OVERRIDE.with(|c| c.set(Some(d)));
}
pub(crate) fn clamp_override() -> Option<Duration> {
CLAMP_OVERRIDE.with(Cell::get)
}
fn reset() {
ROUND_HOOK.with(|h| *h.borrow_mut() = None);
POST_EVENT_HOOK.with(|h| *h.borrow_mut() = None);
KEVENT_CALLS.with(|c| c.set(0));
REQUESTED_TIMEOUTS.with(|v| v.borrow_mut().clear());
LAST_EVENT_DATA.with(|c| c.set(None));
TIMEOUT_OVERRIDE.with(|c| c.set(None));
AWAIT_EVENTS.with(|e| e.borrow_mut().clear());
AWAIT_TIMEOUTS.with(|v| v.borrow_mut().clear());
CLAMP_OVERRIDE.with(|c| c.set(None));
IN_HOOK.with(|f| f.set(false));
GUARD_ACTIVE.with(|f| f.set(false));
}
#[must_use]
pub(crate) struct HookGuard {
_private: (),
}
impl HookGuard {
pub(crate) fn install(hook: impl FnMut(u32, &Kqueue) + 'static) -> Self {
debug_assert!(
!GUARD_ACTIVE.with(Cell::get),
"HookGuard::install called while another HookGuard is already active on this \
thread — nesting is not supported"
);
reset();
set_round_hook(hook);
GUARD_ACTIVE.with(|f| f.set(true));
Self { _private: () }
}
pub(crate) fn install_with_post_event(
round_hook: impl FnMut(u32, &Kqueue) + 'static,
post_event_hook: impl FnMut(u32) + 'static,
) -> Self {
let guard = Self::install(round_hook);
set_post_event_hook(post_event_hook);
guard
}
}
impl Drop for HookGuard {
fn drop(&mut self) {
reset();
}
}
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 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 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();
}
}
#[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)();
}
}