use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::sync::Notify;
#[derive(Debug, Clone, Default)]
pub struct CancelToken {
inner: Arc<Inner>,
}
#[derive(Debug, Default)]
struct Inner {
flag: AtomicBool,
notify: Notify,
}
impl CancelToken {
pub fn new() -> Self {
Self::default()
}
pub fn cancel(&self) {
self.inner.flag.store(true, Ordering::SeqCst);
self.inner.notify.notify_waiters();
}
pub fn is_cancelled(&self) -> bool {
self.inner.flag.load(Ordering::SeqCst)
}
pub async fn cancelled(&self) {
loop {
let notified = self.inner.notify.notified();
if self.is_cancelled() {
return;
}
notified.await;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn cancelled_resolves_after_cancel() {
let t = CancelToken::new();
assert!(!t.is_cancelled());
let t2 = t.clone();
let h = tokio::spawn(async move { t2.cancelled().await });
tokio::task::yield_now().await;
t.cancel();
h.await.unwrap();
assert!(t.is_cancelled());
t.cancelled().await;
}
}