thread_local! {
static KILL_SWITCH: RefCell<Option<Arc<KillSwitch>>> = const { RefCell::new(None) };
}
use std::{
cell::RefCell,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
};
#[derive(Debug)]
pub(super) struct KillSwitch(AtomicBool);
impl KillSwitch {
pub fn new() -> KillSwitch {
KillSwitch(AtomicBool::new(false))
}
pub fn throw(&self) {
self.0.store(true, Ordering::Relaxed);
}
pub fn is_thrown(&self) -> bool {
self.0.load(Ordering::Relaxed)
}
}
pub(super) fn is_thrown() -> bool {
KILL_SWITCH.with(|ks| {
ks.borrow()
.as_ref()
.expect("no kill switch configured for current thread")
.is_thrown()
})
}
pub(super) fn set(kill_switch: Arc<KillSwitch>) {
KILL_SWITCH.with(|ks| *ks.borrow_mut() = Some(kill_switch));
}