wasi_virt_layer_cli/
ctrlc_handler.rs1use std::sync::{Arc, Mutex, OnceLock};
2
3type CleanupFn = Box<dyn Fn() + Send + Sync>;
4
5static CLEANUPS: OnceLock<Arc<Mutex<Vec<CleanupFn>>>> = OnceLock::new();
6
7pub fn init() {
9 CLEANUPS.get_or_init(|| {
10 let cleanups = Arc::new(Mutex::new(Vec::<CleanupFn>::new()));
11 let cleanups_clone = cleanups.clone();
12
13 let _ = ctrlc::set_handler(move || {
15 if let Ok(guards) = cleanups_clone.lock() {
16 for guard in guards.iter().rev() {
17 guard();
18 }
19 }
20 std::process::exit(130);
21 });
22
23 cleanups
24 });
25}
26
27pub fn register(f: impl Fn() + Send + Sync + 'static) {
29 init(); if let Some(cleanups) = CLEANUPS.get() {
31 cleanups.lock().unwrap().push(Box::new(f));
32 }
33}