fixed signale handling
This commit is contained in:
@@ -5,6 +5,7 @@ extern crate chan_signal;
|
||||
use std::io::prelude::*;
|
||||
use std::fs::File;
|
||||
use std::thread;
|
||||
use std::thread::JoinHandle;
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -67,15 +68,18 @@ fn get_value(port: &str) -> String {
|
||||
fn main() {
|
||||
let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]);
|
||||
let (sdone, rdone) = chan::sync(0);
|
||||
let (spanic, rpanic) = chan::sync(0);
|
||||
let worker = thread::Builder::new()
|
||||
.name("worker".to_string())
|
||||
.spawn(move || run(rdone))
|
||||
.expect("Create worker thread failed");
|
||||
let watchdog = thread::Builder::new()
|
||||
.name("watchdog".to_string())
|
||||
.spawn(move || watchdog(worker, spanic))
|
||||
.expect("Create watchdog thread failed");
|
||||
|
||||
chan_select! {
|
||||
default => {
|
||||
worker.join().expect("Worker thread paniced");
|
||||
},
|
||||
rpanic.recv() => { },
|
||||
signal.recv() -> signal => {
|
||||
#[cfg(debug_assertions)]
|
||||
println!("received signal: {:?}", signal);
|
||||
@@ -83,6 +87,12 @@ fn main() {
|
||||
sdone.send(());
|
||||
}
|
||||
}
|
||||
|
||||
watchdog.join().expect("Watchdog thread paniced");
|
||||
}
|
||||
|
||||
fn watchdog(thread: JoinHandle<()>, sdone: chan::Sender<()>) {
|
||||
thread.join().expect("Thread paniced");
|
||||
}
|
||||
|
||||
fn run(rdone: chan::Receiver<()>) {
|
||||
|
||||
Reference in New Issue
Block a user