summaryrefslogtreecommitdiffstats
path: root/src/repl/abort.rs
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2023-03-07 11:51:52 +0800
committerGitHub <noreply@github.com>2023-03-07 11:51:52 +0800
commit11dc4d104b21b75d63e10e42ea4ee767c7bd5fdf (patch)
tree338a2a1d9eed095762fd61ee41437aa2b4e3f991 /src/repl/abort.rs
parent1640456049cda9999bb27501af30e05e46b0360d (diff)
downloadaichat-11dc4d104b21b75d63e10e42ea4ee767c7bd5fdf.tar.gz
refactor: optimize ctrl+c/ctrl+d abort handling (#27)
Diffstat (limited to 'src/repl/abort.rs')
-rw-r--r--src/repl/abort.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/repl/abort.rs b/src/repl/abort.rs
new file mode 100644
index 0000000..f76abb6
--- /dev/null
+++ b/src/repl/abort.rs
@@ -0,0 +1,51 @@
+use std::sync::{
+ atomic::{AtomicBool, Ordering},
+ Arc,
+};
+
+pub type SharedAbortSignal = Arc<AbortSignal>;
+
+pub struct AbortSignal {
+ ctrlc: AtomicBool,
+ ctrld: AtomicBool,
+}
+
+impl AbortSignal {
+ pub fn new() -> SharedAbortSignal {
+ Arc::new(Self {
+ ctrlc: AtomicBool::new(false),
+ ctrld: AtomicBool::new(false),
+ })
+ }
+
+ pub fn aborted(&self) -> bool {
+ if self.aborted_ctrlc() {
+ return true;
+ }
+ if self.aborted_ctrld() {
+ return true;
+ }
+ false
+ }
+
+ pub fn aborted_ctrlc(&self) -> bool {
+ self.ctrlc.load(Ordering::SeqCst)
+ }
+
+ pub fn aborted_ctrld(&self) -> bool {
+ self.ctrld.load(Ordering::SeqCst)
+ }
+
+ pub fn reset(&self) {
+ self.ctrlc.store(false, Ordering::SeqCst);
+ self.ctrld.store(false, Ordering::SeqCst);
+ }
+
+ pub fn set_ctrlc(&self) {
+ self.ctrlc.store(true, Ordering::SeqCst);
+ }
+
+ pub fn set_ctrld(&self) {
+ self.ctrld.store(true, Ordering::SeqCst);
+ }
+}