diff --git a/crates/irtt-client/src/managed/cancellation.rs b/crates/irtt-client/src/managed/cancellation.rs new file mode 100644 index 0000000..b477290 --- /dev/null +++ b/crates/irtt-client/src/managed/cancellation.rs @@ -0,0 +1,43 @@ +use std::sync::{ + atomic::{AtomicBool, Ordering}, + Arc, +}; + +#[derive(Debug, Clone, Default)] +pub struct CancellationToken { + cancelled: Arc, +} + +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()); + } +}