aboutsummaryrefslogtreecommitdiffstats
path: root/src/signal.rs
blob: 8705660918613af4c95266b70446eb0af1f9a327 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use std::sync::atomic::{AtomicBool, Ordering};

/// Set by the first Ctrl-C. Cleared between turns.
pub static CANCEL: AtomicBool = AtomicBool::new(false);

extern "C" fn handler(_: libc::c_int) {
    // First Ctrl-C aborts the turn. A second one is taken to mean "I really mean
    // it" and leaves immediately; `_exit` is async-signal-safe, `exit` is not.
    if CANCEL.swap(true, Ordering::SeqCst) {
        unsafe { libc::_exit(130) }
    }
}

pub fn install() {
    let handler: extern "C" fn(libc::c_int) = handler;
    unsafe {
        libc::signal(libc::SIGINT, handler as libc::sighandler_t);
    }
}

pub fn reset() {
    CANCEL.store(false, Ordering::SeqCst);
}