use std::sync::{Arc, Mutex, OnceLock};
type CleanupFn = Box<dyn Fn() + Send + Sync>;
static CLEANUPS: OnceLock<Arc<Mutex<Vec<CleanupFn>>>> = OnceLock::new();
pub fn init() {
CLEANUPS.get_or_init(|| {
let cleanups = Arc::new(Mutex::new(Vec::<CleanupFn>::new()));
let cleanups_clone = cleanups.clone();
let _ = ctrlc::set_handler(move || {
if let Ok(guards) = cleanups_clone.lock() {
for guard in guards.iter().rev() {
guard();
}
}
std::process::exit(130);
});
cleanups
});
}
pub fn register(f: impl Fn() + Send + Sync + 'static) {
init(); if let Some(cleanups) = CLEANUPS.get() {
cleanups.lock().unwrap().push(Box::new(f));
}
}