pub use crate::park::{Mutation, Point};
use crate::pool::Pool;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::{Arc, PoisonError, RwLock};
type Hook = Arc<dyn Fn(Point) + Send + Sync>;
static HOOK: RwLock<Option<Hook>> = RwLock::new(None);
static MUTATION: AtomicU8 = AtomicU8::new(0);
pub fn set_hook(hook: Option<Hook>) {
*HOOK.write().unwrap_or_else(PoisonError::into_inner) = hook;
}
pub fn set_mutation(m: Mutation) {
MUTATION.store(
match m {
Mutation::None => 0,
Mutation::SkipRecheck => 1,
Mutation::NoLockHandoff => 2,
},
Ordering::SeqCst,
);
}
pub(crate) fn mutation() -> Mutation {
match MUTATION.load(Ordering::SeqCst) {
1 => Mutation::SkipRecheck,
2 => Mutation::NoLockHandoff,
_ => Mutation::None,
}
}
pub(crate) fn fire(point: Point) {
let hook = HOOK.read().unwrap_or_else(PoisonError::into_inner).clone();
if let Some(hook) = hook {
hook(point);
}
}
pub fn wake_parked(pool: &Pool) {
pool.awake.wake_parked();
}