use std::sync::atomic::{AtomicBool, Ordering};
static INTERRUPTED: AtomicBool = AtomicBool::new(false);
pub fn interrupted() -> bool {
INTERRUPTED.load(Ordering::Relaxed)
}
pub fn reset() {
INTERRUPTED.store(false, Ordering::Relaxed);
}
pub fn check() -> crate::error::Result<()> {
if interrupted() {
Err(crate::error::Error::cancelled("interrupted"))
} else {
Ok(())
}
}
#[cfg(unix)]
extern "C" fn handle(_sig: i32) {
INTERRUPTED.store(true, Ordering::Relaxed);
}
#[cfg(unix)]
pub fn install() {
#[repr(C)]
struct SigAction {
sa_handler: extern "C" fn(i32),
sa_mask: [u64; 16],
sa_flags: i32,
sa_restorer: usize,
}
extern "C" {
fn sigaction(signum: i32, act: *const SigAction, oldact: *mut SigAction) -> i32;
}
const SIGINT: i32 = 2;
let act = SigAction {
sa_handler: handle,
sa_mask: [0; 16],
sa_flags: 0,
sa_restorer: 0,
};
unsafe {
sigaction(SIGINT, &act, std::ptr::null_mut());
}
restore_sigpipe();
}
#[cfg(unix)]
pub fn restore_sigpipe() {
extern "C" {
fn signal(signum: i32, handler: usize) -> usize;
}
const SIGPIPE: i32 = 13;
const SIG_DFL: usize = 0;
unsafe {
signal(SIGPIPE, SIG_DFL);
}
}
#[cfg(not(unix))]
pub fn install() {
}
#[cfg(not(unix))]
pub fn restore_sigpipe() {}