1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! Cooperative cancellation shared by bounded queries and the native
//! `SharedDatabase` coordinator.
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use crate::{PvError, Result};
/// A clonable, thread-safe cancellation signal.
///
/// Cancellation is cooperative: queued shared-database work observes the flag
/// before it starts, and running bounded queries observe it at the same regular
/// checkpoints used for row, memory, and deadline limits.
#[derive(Clone, Debug, Default)]
pub struct CancellationToken {
cancelled: Arc<AtomicBool>,
}
impl CancellationToken {
/// Create a token in the non-cancelled state.
pub fn new() -> Self {
Self::default()
}
/// Request cancellation. Calling this more than once is harmless.
pub fn cancel(&self) {
self.cancelled.store(true, Ordering::Release);
}
/// Whether cancellation has been requested.
pub fn is_cancelled(&self) -> bool {
self.cancelled.load(Ordering::Acquire)
}
pub(crate) fn check(&self) -> Result<()> {
if self.is_cancelled() {
Err(PvError::Cancelled)
} else {
Ok(())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn clones_share_cancellation_state() {
let token = CancellationToken::new();
let clone = token.clone();
assert!(!clone.is_cancelled());
token.cancel();
assert!(clone.is_cancelled());
assert!(matches!(clone.check(), Err(PvError::Cancelled)));
}
}