extern crate nix;
#[macro_use]
extern crate lazy_static;
use std::sync::{ONCE_INIT, Once, Arc};
use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT};
use nix::sys::signal::{self, SigAction};
static INIT: Once = ONCE_INIT;
static mut SIG_ACTION: Option<SigAction> = None;
lazy_static! {
static ref CLOSED: Arc<AtomicBool> = Arc::new(ATOMIC_BOOL_INIT);
}
extern "C" fn when_exit(_: i32) {
CLOSED.store(true, Ordering::Relaxed);
}
pub struct Closer;
impl Closer {
pub fn init() -> Closer {
INIT.call_once(|| {
let sig_action = signal::SigAction::new(
signal::SigHandler::Handler(when_exit),
signal::SaFlags::empty(),
signal::SigSet::empty(),
);
unsafe {
signal::sigaction(signal::SIGINT, &sig_action).unwrap();
signal::sigaction(signal::SIGTERM, &sig_action).unwrap();
SIG_ACTION = Some(sig_action);
}
});
Closer
}
pub fn is_closed(&self) -> bool {
CLOSED.load(Ordering::Relaxed)
}
pub fn closed(&self) -> Arc<AtomicBool> {
CLOSED.clone()
}
}