use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
#[derive(Clone, Debug, Default)]
pub struct Ctx {
cancelled: Arc<AtomicBool>,
}
impl Ctx {
pub fn new() -> Self {
Ctx::default()
}
pub fn cancelled(&self) -> bool {
self.cancelled.load(Ordering::Relaxed)
}
pub fn cancel(&self) {
self.cancelled.store(true, Ordering::Relaxed);
}
pub fn raw(&self) -> Arc<AtomicBool> {
Arc::clone(&self.cancelled)
}
}
#[cfg(feature = "http-stack")]
impl Ctx {
pub async fn cancelled_async(&self) {
let raw = self.raw();
tokio::task::yield_now().await;
while !raw.load(Ordering::Relaxed) {
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
}
}
}