rat_salsa/tasks/
mod.rs

1//!
2//! Types used for both future tasks and thread tasks.
3//!
4use std::sync::atomic::{AtomicBool, Ordering};
5use std::sync::Arc;
6
7/// Liveness check for background tasks.
8/// Used by both the thread-pool and by the tokio async runner.
9#[derive(Debug, Default, Clone)]
10pub struct Liveness(Arc<AtomicBool>);
11
12impl Liveness {
13    pub fn new() -> Self {
14        Self(Arc::new(AtomicBool::new(false)))
15    }
16
17    pub fn is_alive(&self) -> bool {
18        self.0.load(Ordering::Acquire)
19    }
20
21    pub fn born(&self) {
22        self.0.store(true, Ordering::Release);
23    }
24
25    pub fn dead(&self) {
26        self.0.store(false, Ordering::Release);
27    }
28}
29
30/// Cancel background tasks.
31#[derive(Debug, Default, Clone)]
32pub struct Cancel(Arc<AtomicBool>);
33
34impl Cancel {
35    pub fn new() -> Self {
36        Self(Arc::new(AtomicBool::new(false)))
37    }
38
39    pub fn is_canceled(&self) -> bool {
40        self.0.load(Ordering::Acquire)
41    }
42
43    pub fn cancel(&self) {
44        self.0.store(true, Ordering::Release);
45    }
46}