feat(managed): implement thread-safe cancellation token
This commit is contained in:
1 parent
3f7afd4f7a
commit
d9846b7868
1 file changed
+43
@@ -0,0 +1,43 @@
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct CancellationToken {
|
||||
cancelled: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl CancellationToken {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn cancel(&self) {
|
||||
self.cancelled.store(true, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
pub fn is_cancelled(&self) -> bool {
|
||||
self.cancelled.load(Ordering::SeqCst)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn token_starts_uncancelled() {
|
||||
assert!(!CancellationToken::new().is_cancelled());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancel_is_visible_through_clones() {
|
||||
let token = CancellationToken::new();
|
||||
let clone = token.clone();
|
||||
|
||||
token.cancel();
|
||||
|
||||
assert!(clone.is_cancelled());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user