Commit 52f0bd7f authored by Luca Barbato's avatar Luca Barbato Committed by Luca Barbato
Browse files

Add signal support

So the cli can exit cleanly on sigterm.
parent 3a86ba8b
......@@ -40,6 +40,7 @@ ivf = { path = "ivf/", optional = true }
rayon = "1.0"
bincode = "1.1"
arrayvec = "0.4.10"
signal-hook = { version = "0.1.9", optional = true }
[target.'cfg(target_arch = "x86_64")'.build-dependencies]
nasm-rs = { git = "https://github.com/tdaede/nasm-rs", branch="msvc-OUT", optional = true }
......
......@@ -31,6 +31,8 @@ struct Source<D: Decoder> {
limit: usize,
count: usize,
input: D,
#[cfg(feature = "signal-hook")]
exit_requested: Arc<std::sync::atomic::AtomicBool>,
}
impl<D: Decoder> Source<D> {
......@@ -39,6 +41,14 @@ impl<D: Decoder> Source<D> {
ctx.flush();
return;
}
#[cfg(feature = "signal-hook")] {
if self.exit_requested.load(std::sync::atomic::Ordering::SeqCst) {
ctx.flush();
return;
}
}
match self.input.read_frame(&video_info) {
Ok(frame) => {
match video_info.bit_depth {
......@@ -183,6 +193,38 @@ fn main() {
y4m_dec.read_frame().expect("Skipped more frames than in the input");
}
#[cfg(feature = "signal-hook")]
let exit_requested = {
use std::sync::atomic::*;
let e = Arc::new(AtomicBool::from(false));
fn setup_signal(sig: i32, e: Arc<AtomicBool>) {
unsafe {
signal_hook::register(sig, move || {
if e.load(Ordering::SeqCst) {
std::process::exit(128 + sig);
}
e.store(true, Ordering::SeqCst);
eprintln!("\rExit requested, flushing.\n");
}).expect("Cannot register the signal hooks");
}
}
setup_signal(signal_hook::SIGTERM, e.clone());
setup_signal(signal_hook::SIGQUIT, e.clone());
setup_signal(signal_hook::SIGINT, e.clone());
e
};
#[cfg(feature = "signal-hook")]
let mut source = Source {
limit: cli.limit,
input: y4m_dec,
count: 0,
exit_requested
};
#[cfg(not(feature = "signal-hook"))]
let mut source = Source { limit: cli.limit, input: y4m_dec, count: 0 };
if video_info.bit_depth == 8 {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment