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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use ffi_support::implement_into_ffi_by_pointer;
use interrupt_support::Interruptee;
use rusqlite::InterruptHandle;
use std::sync::{
atomic::{AtomicUsize, Ordering},
Arc,
};
pub struct SqlInterruptHandle {
db_handle: InterruptHandle,
interrupt_counter: Arc<AtomicUsize>,
}
impl SqlInterruptHandle {
pub fn new(
db_handle: InterruptHandle,
interrupt_counter: Arc<AtomicUsize>,
) -> SqlInterruptHandle {
SqlInterruptHandle {
db_handle,
interrupt_counter,
}
}
pub fn interrupt(&self) {
self.interrupt_counter.fetch_add(1, Ordering::SeqCst);
self.db_handle.interrupt();
}
}
implement_into_ffi_by_pointer!(SqlInterruptHandle);
#[derive(Debug)]
pub struct SqlInterruptScope {
start_value: usize,
ptr: Arc<AtomicUsize>,
}
impl SqlInterruptScope {
#[inline]
pub fn new(ptr: Arc<AtomicUsize>) -> Self {
let start_value = ptr.load(Ordering::SeqCst);
Self { start_value, ptr }
}
#[inline]
pub fn err_if_interrupted(&self) -> Result<(), interrupt_support::Interrupted> {
<Self as Interruptee>::err_if_interrupted(self)
}
}
impl Interruptee for SqlInterruptScope {
#[inline]
fn was_interrupted(&self) -> bool {
self.ptr.load(Ordering::SeqCst) != self.start_value
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_sync_send() {
fn is_sync<T: Sync>() {}
fn is_send<T: Send>() {}
is_sync::<SqlInterruptHandle>();
is_send::<SqlInterruptHandle>();
}
}