use std::sync::{
Arc,
atomic::{AtomicU8, Ordering},
};
const STARTED: u8 = 1;
const CANCELLED: u8 = 1 << 1;
#[derive(Clone, Debug, Default)]
pub struct SimulationCancellationToken {
state: Arc<AtomicU8>,
}
impl SimulationCancellationToken {
pub fn new() -> Self {
Self::default()
}
pub fn cancel(&self) {
self.state.fetch_or(CANCELLED, Ordering::AcqRel);
}
pub fn is_cancelled(&self) -> bool {
self.state.load(Ordering::Acquire) & CANCELLED != 0
}
pub fn has_started(&self) -> bool {
self.state.load(Ordering::Acquire) & STARTED != 0
}
pub(crate) fn mark_started(&self) {
self.state.fetch_or(STARTED, Ordering::Release);
}
}