diff options
Diffstat (limited to 'src/signal.rs')
| -rw-r--r-- | src/signal.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/signal.rs b/src/signal.rs new file mode 100644 index 0000000..8705660 --- /dev/null +++ b/src/signal.rs | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | use std::sync::atomic::{AtomicBool, Ordering}; | ||
| 2 | |||
| 3 | /// Set by the first Ctrl-C. Cleared between turns. | ||
| 4 | pub static CANCEL: AtomicBool = AtomicBool::new(false); | ||
| 5 | |||
| 6 | extern "C" fn handler(_: libc::c_int) { | ||
| 7 | // First Ctrl-C aborts the turn. A second one is taken to mean "I really mean | ||
| 8 | // it" and leaves immediately; `_exit` is async-signal-safe, `exit` is not. | ||
| 9 | if CANCEL.swap(true, Ordering::SeqCst) { | ||
| 10 | unsafe { libc::_exit(130) } | ||
| 11 | } | ||
| 12 | } | ||
| 13 | |||
| 14 | pub fn install() { | ||
| 15 | let handler: extern "C" fn(libc::c_int) = handler; | ||
| 16 | unsafe { | ||
| 17 | libc::signal(libc::SIGINT, handler as libc::sighandler_t); | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | pub fn reset() { | ||
| 22 | CANCEL.store(false, Ordering::SeqCst); | ||
| 23 | } | ||
