use std::cell::{Cell, RefCell};
use std::io;
use std::sync::mpsc::{Receiver, Sender};
use super::unlocked::Unlocked;
#[derive(Debug, Clone, Copy)]
pub(crate) enum ForcedWait {
ExitSeen,
DeadlinePassed,
Gone,
Errno(i32),
Panic,
}
impl ForcedWait {
pub(super) fn into_result(self) -> io::Result<Unlocked> {
match self {
ForcedWait::ExitSeen => Ok(Unlocked::ExitSeen),
ForcedWait::DeadlinePassed => Ok(Unlocked::DeadlinePassed),
ForcedWait::Gone => Ok(Unlocked::Gone),
ForcedWait::Errno(e) => Err(io::Error::from_raw_os_error(e)),
ForcedWait::Panic => panic!("forced panic in the holder's unlocked wait (test seam)"),
}
}
}
pub(crate) struct ParkGate {
reached: Sender<()>,
release: Receiver<()>,
}
pub(crate) fn park_gate() -> (ParkGate, Receiver<()>, Sender<()>) {
let (reached_tx, reached_rx) = std::sync::mpsc::channel();
let (release_tx, release_rx) = std::sync::mpsc::channel();
(
ParkGate {
reached: reached_tx,
release: release_rx,
},
reached_rx,
release_tx,
)
}
type BlockHook = Box<dyn FnOnce(Option<std::time::Duration>)>;
thread_local! {
static PARK: RefCell<Option<ParkGate>> = const { RefCell::new(None) };
static PARK_AFTER_REAP: RefCell<Option<ParkGate>> = const { RefCell::new(None) };
static FORCED_WAIT: Cell<Option<ForcedWait>> = const { Cell::new(None) };
static PANIC_AFTER_RELOCK: Cell<bool> = const { Cell::new(false) };
static ON_CONDVAR_BLOCK: RefCell<Option<BlockHook>> = const { RefCell::new(None) };
#[cfg(windows)]
static FORCE_DUPLICATE_HANDLE_ERROR: Cell<bool> = const { Cell::new(false) };
}
#[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 park_in_unlocked_wait(gate: ParkGate) -> Forced {
PARK.with(|p| *p.borrow_mut() = Some(gate));
Forced(|| PARK.with(|p| *p.borrow_mut() = None))
}
pub(super) fn park_if_armed() {
if let Some(gate) = PARK.with(|p| p.borrow_mut().take()) {
_ = gate.reached.send(());
_ = gate.release.recv();
}
}
pub(crate) fn park_after_reap_recorded_on(gate: ParkGate) -> Forced {
PARK_AFTER_REAP.with(|p| *p.borrow_mut() = Some(gate));
Forced(|| PARK_AFTER_REAP.with(|p| *p.borrow_mut() = None))
}
pub(super) fn park_after_reap_recorded() {
if let Some(gate) = PARK_AFTER_REAP.with(|p| p.borrow_mut().take()) {
_ = gate.reached.send(());
_ = gate.release.recv();
}
}
pub(crate) fn force_unlocked_wait(wait: ForcedWait) -> Forced {
FORCED_WAIT.with(|f| f.set(Some(wait)));
Forced(|| FORCED_WAIT.with(|f| f.set(None)))
}
pub(super) fn take_forced_unlocked_wait() -> Option<ForcedWait> {
FORCED_WAIT.with(Cell::take)
}
pub(crate) fn panic_after_relock_once() -> Forced {
PANIC_AFTER_RELOCK.with(|f| f.set(true));
Forced(|| PANIC_AFTER_RELOCK.with(|f| f.set(false)))
}
pub(super) fn panic_after_relock_if_armed() {
if PANIC_AFTER_RELOCK.with(Cell::take) {
panic!("forced panic just after the holder's relock (test seam)");
}
}
pub(crate) fn on_condvar_block(hook: impl FnOnce() + 'static) -> Forced {
on_condvar_block_with(move |_| hook())
}
pub(crate) fn on_condvar_block_with(hook: impl FnOnce(Option<std::time::Duration>) + 'static) -> Forced {
ON_CONDVAR_BLOCK.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
Forced(|| ON_CONDVAR_BLOCK.with(|h| *h.borrow_mut() = None))
}
pub(super) fn before_condvar_block(remaining: Option<std::time::Duration>) {
if let Some(hook) = ON_CONDVAR_BLOCK.with(|h| h.borrow_mut().take()) {
hook(remaining);
}
}
#[cfg(windows)]
pub(crate) fn force_duplicate_handle_error_once() -> Forced {
FORCE_DUPLICATE_HANDLE_ERROR.with(|f| f.set(true));
Forced(|| FORCE_DUPLICATE_HANDLE_ERROR.with(|f| f.set(false)))
}
#[cfg(windows)]
pub(super) fn take_forced_duplicate_handle_error() -> bool {
FORCE_DUPLICATE_HANDLE_ERROR.with(Cell::take)
}