1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! A simple crate to catch signals and set a boolean flag for later use.
//!
//! This crate doesn't create threads behind the scene.
//!
//! [![Crates.io Version](https://img.shields.io/crates/v/signalbool.svg)](https://crates.io/crates/signalbool)
//! [![GitHub stars](https://img.shields.io/github/stars/lilydjwg/rust-signalbool.svg?style=social&label=Star)](https://github.com/lilydjwg/rust-signalbool)
//!
//! # Example
//!
//! Here is a program that sleeps until it receives three `SIGINT` signals.
//!
//! ```
//! extern crate signalbool;
//! extern crate nix;
//! 
//! use nix::unistd::sleep;
//! 
//! fn main() {
//!   let mut sb = signalbool::SignalBool::new(
//!     &[signalbool::Signal::SIGINT], signalbool::Flag::Interrupt,
//!   ).unwrap();
//!   let mut count = 0;
//!     
//!   loop {
//!     sleep(10);
//!     if sb.caught() {
//!       println!("Caught SIGINT.");
//!       count += 1;
//!       sb.reset();
//!       if count == 3 {
//!         break;
//!       }
//!     }
//!   }
//! }
//! ```

#[cfg(not(windows))] mod unix;
#[cfg(not(windows))] use unix as imp;
#[cfg(windows)] mod windows;
#[cfg(windows)] use windows as imp;

pub use imp::Signal;

/// A struct that catches specified signals and sets its internal flag to `true`.
///
/// Note: any previously-registered signal handlers will be lost.
#[cfg(windows)]
pub struct SignalBool;
#[cfg(not(windows))]
pub struct SignalBool(usize);

/// flag controlling the restarting behavior.
///
/// Note that most functions in `std` ignore `EINTR` and continue their operations.
///
/// See manpage [`signal(7)`](http://man7.org/linux/man-pages/man7/signal.7.html) for details.
pub enum Flag {
  /// Blocking syscalls will be interrupted.
  #[cfg(not(windows))]
  Interrupt,
  /// Use SA_RESTART so that syscalls don't get interrupted.
  Restart,
}