asimov_dataset_cli/
context.rs

1// This is free and unencumbered software released into the public domain.
2
3use std::sync::Arc;
4
5use crossbeam::atomic::AtomicCell;
6
7pub fn new_cancel_context() -> (Context, Canceller) {
8    let val = Arc::new(AtomicCell::new(false));
9
10    (
11        Context {
12            cancelled: val.clone(),
13        },
14        Canceller {
15            cancelled: val.clone(),
16        },
17    )
18}
19
20#[derive(Clone)]
21pub struct Context {
22    cancelled: Arc<AtomicCell<bool>>,
23}
24
25impl Context {
26    #[inline]
27    pub fn is_cancelled(&self) -> bool {
28        self.cancelled.load()
29    }
30}
31
32#[derive(Clone)]
33pub struct Canceller {
34    cancelled: Arc<AtomicCell<bool>>,
35}
36
37impl Canceller {
38    #[inline]
39    pub fn cancel(&self) {
40        self.cancelled.store(true);
41    }
42}