-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new: interrupt-ignore-count command-line option with default value of 3
- Loading branch information
Showing
5 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// std imports | ||
use std::process::exit; | ||
use std::thread::{spawn, JoinHandle}; | ||
use std::time::{Duration, Instant}; | ||
|
||
// third-party imports | ||
use signal_hook::{iterator::Signals, SIGINT}; | ||
|
||
// local imports | ||
use crate::error::*; | ||
|
||
// --- | ||
|
||
pub struct SignalHandler { | ||
signals: Signals, | ||
thread: Option<JoinHandle<()>>, | ||
} | ||
|
||
impl SignalHandler { | ||
pub fn run<F>(max_count: usize, timeout: Duration, f: F) -> Result<()> | ||
where | ||
F: FnOnce() -> Result<()>, | ||
{ | ||
let _guard = Self::new(max_count, timeout)?; | ||
f() | ||
} | ||
|
||
fn new(max_count: usize, timeout: Duration) -> Result<Self> { | ||
let signals = Signals::new(&[SIGINT])?; | ||
let s = signals.clone(); | ||
|
||
let thread = spawn(move || { | ||
let mut count = 0; | ||
let mut ts = Instant::now(); | ||
for signal in &s { | ||
match signal { | ||
SIGINT => { | ||
if count < max_count { | ||
count += 1; | ||
} | ||
let now = Instant::now(); | ||
if now.duration_since(ts) > timeout { | ||
count = 0; | ||
} | ||
if count == max_count { | ||
exit(0x80 + signal); | ||
} | ||
ts = now; | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
}); | ||
|
||
Ok(Self { | ||
signals, | ||
thread: Some(thread), | ||
}) | ||
} | ||
} | ||
|
||
impl Drop for SignalHandler { | ||
fn drop(&mut self) { | ||
self.signals.close(); | ||
if let Some(thread) = self.thread.take() { | ||
thread.join().unwrap(); | ||
} | ||
} | ||
} |