-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
68 additions
and
60 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
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 |
---|---|---|
@@ -1,28 +1,30 @@ | ||
use std::cell::RefCell; | ||
use std::sync::{ | ||
atomic::{AtomicUsize, Ordering}, | ||
Arc, | ||
use std::{ | ||
cell::RefCell, | ||
sync::{ | ||
atomic::{AtomicUsize, Ordering}, | ||
Arc, | ||
}, | ||
}; | ||
|
||
/// Blocks current thread until ctrl-c is received | ||
pub async fn block_until_sigint() { | ||
let (ctrlc_send, ctrlc_oneshot) = futures::channel::oneshot::channel(); | ||
let ctrlc_send_c = RefCell::new(Some(ctrlc_send)); | ||
let (ctrlc_send, ctrlc_oneshot) = futures::channel::oneshot::channel(); | ||
let ctrlc_send_c = RefCell::new(Some(ctrlc_send)); | ||
|
||
let running = Arc::new(AtomicUsize::new(0)); | ||
ctrlc::set_handler(move || { | ||
let prev = running.fetch_add(1, Ordering::SeqCst); | ||
if prev == 0 { | ||
println!("Got interrupt, shutting down..."); | ||
// Send sig int in channel to blocking task | ||
if let Some(ctrlc_send) = ctrlc_send_c.try_borrow_mut().unwrap().take() { | ||
ctrlc_send.send(()).expect("Error sending ctrl-c message"); | ||
} | ||
} else { | ||
std::process::exit(0); | ||
} | ||
}) | ||
.expect("Error setting Ctrl-C handler"); | ||
let running = Arc::new(AtomicUsize::new(0)); | ||
ctrlc::set_handler(move || { | ||
let prev = running.fetch_add(1, Ordering::SeqCst); | ||
if prev == 0 { | ||
println!("Got interrupt, shutting down..."); | ||
// Send sig int in channel to blocking task | ||
if let Some(ctrlc_send) = ctrlc_send_c.try_borrow_mut().unwrap().take() { | ||
ctrlc_send.send(()).expect("Error sending ctrl-c message"); | ||
} | ||
} else { | ||
std::process::exit(0); | ||
} | ||
}) | ||
.expect("Error setting Ctrl-C handler"); | ||
|
||
ctrlc_oneshot.await.unwrap(); | ||
ctrlc_oneshot.await.unwrap(); | ||
} |