use std::{sync::OnceLock, thread};
use tracing::{error, info};
static DETECTION_THREAD: OnceLock<thread::JoinHandle<()>> = OnceLock::new();
pub fn try_spawn_deadlock_detection() {
DETECTION_THREAD.get_or_init(|| {
thread::spawn(move || {
info!("Deadlock detection thread started");
loop {
thread::sleep(std::time::Duration::from_secs(10));
let deadlocks = parking_lot::deadlock::check_deadlock();
if deadlocks.is_empty() {
continue;
}
error!("{} deadlocks detected", deadlocks.len());
for (i, threads) in deadlocks.iter().enumerate() {
error!("Deadlock #{i}");
for t in threads {
error!("Thread Id {:#?}", t.thread_id());
error!("{:#?}", t.backtrace());
}
}
}
})
});
}