Skip to content

Commit

Permalink
Add ctrlc for proper CTRL-C signal handling
Browse files Browse the repository at this point in the history
  • Loading branch information
svenstaro committed Jan 21, 2018
1 parent 79b7071 commit 49587d6
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 15 deletions.
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ regex = "0.2.5"

[target.'cfg(not(target_os = "emscripten"))'.dependencies]
clap = "2.29"
ctrlc = "3.1"

[target.'cfg(target_os = "emscripten")'.dependencies]
emscripten-sys = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion src/bootlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn run(appconfig: &AppConfig) {
println!();
csleep(sleep_length);

if appconfig.is_time_to_quit() {
if appconfig.should_exit() {
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn run(appconfig: &AppConfig) {

csleep(sleep_length);

if appconfig.is_time_to_quit() {
if appconfig.should_exit() {
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub fn run(appconfig: &AppConfig) {
let sleep_length = rng.gen_range(30, 200);
csleep(sleep_length);

if appconfig.is_time_to_quit() {
if appconfig.should_exit() {
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/composer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn run(appconfig: &AppConfig) {

csleep(sleep_length);

if appconfig.is_time_to_quit() {
if appconfig.should_exit() {
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cryptomining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn run(appconfig: &AppConfig) {
}
csleep(sleep_length);

if appconfig.is_time_to_quit() {
if appconfig.should_exit() {
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn run(appconfig: &AppConfig) {
pb.add(bytes_per_sleep);
csleep(sleep_millis);

if appconfig.is_time_to_quit() {
if appconfig.should_exit() {
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/kernel_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn run(appconfig: &AppConfig) {
println!("{}", line);
csleep(sleep_length);

if appconfig.is_time_to_quit() {
if appconfig.should_exit() {
return;
}
}
Expand Down
19 changes: 18 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#[macro_use]
extern crate clap;

#[cfg(not(target_os = "emscripten"))]
extern crate ctrlc;

#[cfg(target_os = "emscripten")]
extern crate emscripten_sys;

Expand Down Expand Up @@ -50,6 +53,14 @@ lazy_static! {
static ref COMPOSERS_LIST: Vec<&'static str> = COMPOSERS.lines().collect();
}

#[cfg(not(target_os = "emscripten"))]
use std::sync::atomic::AtomicBool;

#[cfg(not(target_os = "emscripten"))]
lazy_static! {
static ref CTRLC_PRESSED: AtomicBool = AtomicBool::new(false);
}

fn main() {
Paint::enable_windows_ascii();

Expand Down Expand Up @@ -85,6 +96,11 @@ fn main() {
}
process::exit(0);
}

use std::sync::atomic::Ordering;
ctrlc::set_handler(move || {
CTRLC_PRESSED.store(true, Ordering::SeqCst);
}).expect("Error setting Ctrl-C handler");
}

let mut rng = thread_rng();
Expand All @@ -104,7 +120,8 @@ fn main() {
#[cfg(not(target_os = "emscripten"))]
{
use std::process;
if appconfig.is_time_to_quit() {
if appconfig.should_exit() {
println!("Saving work to disk...");
process::exit(0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/memdump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn run(appconfig: &AppConfig) {
let row_delay = rng.gen_range(100, 200);
csleep(row_delay);

if appconfig.is_time_to_quit() {
if appconfig.should_exit() {
return;
}
println!();
Expand Down
20 changes: 14 additions & 6 deletions src/parse_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,24 @@ pub struct AppConfig {

impl AppConfig {
/// Check whether it's time to stop running.
pub fn is_time_to_quit(&self) -> bool {
pub fn should_exit(&self) -> bool {
// Check whether CTRL-C was pressed.
#[cfg(not(target_os = "emscripten"))]
{
use std::sync::atomic::Ordering;
use CTRLC_PRESSED;
if CTRLC_PRESSED.load(Ordering::SeqCst) {
return true;
}
}

// Check if maximum running time is exceeded.
if let Some(ea) = self.exit_after {
if self.started_at.elapsed() > *ea {
true
} else {
false
return true;
}
} else {
false
}
return false;
}
}

Expand Down

0 comments on commit 49587d6

Please sign in to comment.