feat: add resettable cancellation token
This commit is contained in:
parent
4c826923a5
commit
7b79dd69b4
1 changed files with 34 additions and 0 deletions
34
server/src/cancellation.rs
Normal file
34
server/src/cancellation.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
|
|
||||||
|
pub struct ResetCancelToken {
|
||||||
|
is_canceled: Arc<AtomicBool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ResetCancelToken {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
is_canceled: Arc::new(AtomicBool::new(false)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_canceled(&self) -> bool {
|
||||||
|
self.is_canceled.load(Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cancel(&self) {
|
||||||
|
self.is_canceled.store(true, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn reset(&self) {
|
||||||
|
self.is_canceled.store(false, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Clone for ResetCancelToken {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
is_canceled: self.is_canceled.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue