-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wait for child processes to finish (#345)
- Loading branch information
Showing
16 changed files
with
465 additions
and
80 deletions.
There are no files selected for viewing
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 @@ | ||
Cargo.lock linguist-generated diff=nodiff |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
tab_spaces = 2 | ||
max_width = 100 |
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,7 @@ | ||
macro_rules! die { | ||
($($arg:tt)*) => {{ | ||
extern crate std; | ||
eprintln!($($arg)*); | ||
process::exit(EXIT_FAILURE) | ||
}}; | ||
} |
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,93 @@ | ||
use common::*; | ||
|
||
use ctrlc; | ||
|
||
pub struct InterruptHandler { | ||
blocks: u32, | ||
interrupted: bool, | ||
} | ||
|
||
impl InterruptHandler { | ||
pub fn install() -> Result<(), ctrlc::Error> { | ||
ctrlc::set_handler(|| InterruptHandler::instance().interrupt()) | ||
} | ||
|
||
fn instance() -> MutexGuard<'static, InterruptHandler> { | ||
lazy_static! { | ||
static ref INSTANCE: Mutex<InterruptHandler> = Mutex::new(InterruptHandler::new()); | ||
} | ||
|
||
match INSTANCE.lock() { | ||
Ok(guard) => guard, | ||
Err(poison_error) => die!( | ||
"{}", | ||
RuntimeError::Internal { | ||
message: format!("interrupt handler mutex poisoned: {}", poison_error), | ||
} | ||
), | ||
} | ||
} | ||
|
||
fn new() -> InterruptHandler { | ||
InterruptHandler { | ||
blocks: 0, | ||
interrupted: false, | ||
} | ||
} | ||
|
||
fn interrupt(&mut self) { | ||
self.interrupted = true; | ||
|
||
if self.blocks > 0 { | ||
return; | ||
} | ||
|
||
Self::exit(); | ||
} | ||
|
||
fn exit() { | ||
process::exit(130); | ||
} | ||
|
||
fn block(&mut self) { | ||
self.blocks += 1; | ||
} | ||
|
||
fn unblock(&mut self) { | ||
if self.blocks == 0 { | ||
die!( | ||
"{}", | ||
RuntimeError::Internal { | ||
message: "attempted to unblock interrupt handler, but handler was not blocked" | ||
.to_string(), | ||
} | ||
); | ||
} | ||
|
||
self.blocks -= 1; | ||
|
||
if self.interrupted { | ||
Self::exit(); | ||
} | ||
} | ||
|
||
pub fn guard<T, F: FnOnce() -> T>(function: F) -> T { | ||
let _guard = InterruptGuard::new(); | ||
function() | ||
} | ||
} | ||
|
||
pub struct InterruptGuard; | ||
|
||
impl InterruptGuard { | ||
fn new() -> InterruptGuard { | ||
InterruptHandler::instance().block(); | ||
InterruptGuard | ||
} | ||
} | ||
|
||
impl Drop for InterruptGuard { | ||
fn drop(&mut self) { | ||
InterruptHandler::instance().unblock(); | ||
} | ||
} |
Oops, something went wrong.