use std::sync::atomic::{AtomicBool, AtomicI32, AtomicUsize, Ordering};
pub static STOP: AtomicBool = AtomicBool::new(false);
static RECEIVED: AtomicUsize = AtomicUsize::new(0);
static LAST: AtomicI32 = AtomicI32::new(0);
pub fn install() {
#[cfg(unix)]
{
extern "C" fn handle(signal: libc::c_int) {
STOP.store(true, Ordering::SeqCst);
LAST.store(signal, Ordering::SeqCst);
RECEIVED.fetch_add(1, Ordering::SeqCst);
}
let handler = handle as extern "C" fn(libc::c_int) as libc::sighandler_t;
unsafe {
libc::signal(libc::SIGINT, handler);
libc::signal(libc::SIGTERM, handler);
}
}
}
pub fn requested() -> bool {
STOP.load(Ordering::SeqCst)
}
pub fn received_since(seen: usize) -> Option<(usize, i32)> {
let received = RECEIVED.load(Ordering::SeqCst);
(received > seen).then(|| (received, LAST.load(Ordering::SeqCst)))
}