#[doc(hidden)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Step {
BeforeLane,
BeforePasses,
BeforeReserve,
Reserved,
RecycleDraining,
RecycleDrained,
AnswersWait,
AnswersYield,
AnswerPosted,
PumpPiece,
FillPiece,
FeedBlocking,
StreamSubmit,
StreamCompleted,
StreamBisected,
CollectRetry,
RecycleRewritten,
PlanHeaders,
}
#[cfg(feature = "sim-hooks")]
static HOOK: std::sync::RwLock<Option<fn(Step)>> = std::sync::RwLock::new(None);
#[cfg(feature = "sim-hooks")]
#[doc(hidden)]
pub fn install(hook: Option<fn(Step)>) {
*HOOK
.write()
.unwrap_or_else(std::sync::PoisonError::into_inner) = hook;
}
#[cfg(feature = "sim-hooks")]
std::thread_local! {
static UNSEEN: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
#[cfg(feature = "sim-hooks")]
#[doc(hidden)]
pub fn unseen<R>(f: impl FnOnce() -> R) -> R {
struct Restore(bool);
impl Drop for Restore {
fn drop(&mut self) {
UNSEEN.with(|u| u.set(self.0));
}
}
let _restore = Restore(UNSEEN.with(|u| u.replace(true)));
f()
}
#[cfg(feature = "sim-hooks")]
std::thread_local! {
static NO_FLUSH_THREADS: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
}
#[cfg(feature = "sim-hooks")]
#[doc(hidden)]
pub fn without_flush_threads<R>(f: impl FnOnce() -> R) -> R {
struct Restore(bool);
impl Drop for Restore {
fn drop(&mut self) {
NO_FLUSH_THREADS.with(|u| u.set(self.0));
}
}
let _restore = Restore(NO_FLUSH_THREADS.with(|u| u.replace(true)));
f()
}
#[inline]
pub(crate) fn flush_threads() -> bool {
#[cfg(feature = "sim-hooks")]
{
!NO_FLUSH_THREADS.with(std::cell::Cell::get)
}
#[cfg(not(feature = "sim-hooks"))]
{
true
}
}
#[inline]
pub(crate) fn seen() -> bool {
#[cfg(feature = "sim-hooks")]
{
!UNSEEN.with(std::cell::Cell::get)
}
#[cfg(not(feature = "sim-hooks"))]
{
true
}
}
#[inline]
pub(crate) fn step(s: Step) {
#[cfg(feature = "sim-hooks")]
{
let hook = *HOOK
.read()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if let Some(hook) = hook {
hook(s);
}
}
#[cfg(not(feature = "sim-hooks"))]
let _unused = s;
}
#[cfg(all(test, not(loom), feature = "sim-hooks"))]
pub(crate) mod lib_tests {
use std::sync::{Mutex, PoisonError};
use std::thread::ThreadId;
use super::Step;
type Action = Box<dyn FnOnce() + Send>;
static PENDING: Mutex<Vec<(Step, ThreadId, Action)>> = Mutex::new(Vec::new());
pub(crate) fn at(step: Step, act: Action) {
PENDING
.lock()
.unwrap_or_else(PoisonError::into_inner)
.push((step, std::thread::current().id(), act));
super::install(Some(dispatch));
}
pub(crate) fn clear() {
let me = std::thread::current().id();
PENDING
.lock()
.unwrap_or_else(PoisonError::into_inner)
.retain(|(_, t, _)| *t != me);
}
fn dispatch(step: Step) {
let me = std::thread::current().id();
let act = {
let mut p = PENDING.lock().unwrap_or_else(PoisonError::into_inner);
p.iter()
.position(|(s, t, _)| *s == step && *t == me)
.map(|i| p.remove(i).2)
};
if let Some(act) = act {
act();
}
}
}