easy_qjs/cancellation.rs
1use std::{
2 sync::{
3 atomic::{AtomicBool, Ordering},
4 Arc,
5 },
6 time::Instant,
7};
8
9use crate::Cancellation;
10
11impl Cancellation {
12 pub fn new(deadline: Option<Instant>, cancellation: Arc<AtomicBool>) -> Cancellation {
13 Self {
14 deadline,
15 cancellation,
16 }
17 }
18
19 pub fn cancelled(&self) -> bool {
20 self.deadline.map(|d| d <= Instant::now()).unwrap_or(false)
21 || self.cancellation.load(Ordering::Relaxed)
22 }
23}