| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use crate::{in_shutdown, Interrupted, Interruptee}; |
| 6 |
use rusqlite::{Connection, InterruptHandle}; |
| 7 |
use std::fmt; |
| 8 |
use std::sync::{ |
| 9 |
atomic::{AtomicUsize, Ordering}, |
| 10 |
Arc, |
| 11 |
}; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
pub struct SqlInterruptHandle { |
| 27 |
db_handle: InterruptHandle, |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
interrupt_counter: Arc<AtomicUsize>, |
| 32 |
} |
| 33 |
|
| 34 |
impl SqlInterruptHandle { |
| 35 |
#[inline] |
| 36 |
pub fn new(conn: &Connection) -> Self { |
| 37 |
Self { |
| 38 |
db_handle: conn.get_interrupt_handle(), |
| 39 |
interrupt_counter: Arc::new(AtomicUsize::new(0)), |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#[inline] |
| 47 |
pub fn begin_interrupt_scope(&self) -> Result<SqlInterruptScope, Interrupted> { |
| 48 |
if in_shutdown() { |
| 49 |
Err(Interrupted) |
| 50 |
} else { |
| 51 |
Ok(SqlInterruptScope::new(Arc::clone(&self.interrupt_counter))) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
#[inline] |
| 57 |
pub fn interrupt(&self) { |
| 58 |
self.interrupt_counter.fetch_add(1, Ordering::Relaxed); |
| 59 |
self.db_handle.interrupt(); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
impl fmt::Debug for SqlInterruptHandle { |
| 64 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 65 |
f.debug_struct("SqlInterruptHandle") |
| 66 |
.field( |
| 67 |
"interrupt_counter", |
| 68 |
&self.interrupt_counter.load(Ordering::Relaxed), |
| 69 |
) |
| 70 |
.finish() |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
#[derive(Debug)] |
| 79 |
pub struct SqlInterruptScope { |
| 80 |
start_value: usize, |
| 81 |
interrupt_counter: Arc<AtomicUsize>, |
| 82 |
} |
| 83 |
|
| 84 |
impl SqlInterruptScope { |
| 85 |
fn new(interrupt_counter: Arc<AtomicUsize>) -> Self { |
| 86 |
let start_value = interrupt_counter.load(Ordering::Relaxed); |
| 87 |
Self { |
| 88 |
start_value, |
| 89 |
interrupt_counter, |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
pub fn dummy() -> Self { |
| 97 |
Self::new(Arc::new(AtomicUsize::new(0))) |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
#[inline] |
| 102 |
pub fn was_interrupted(&self) -> bool { |
| 103 |
self.interrupt_counter.load(Ordering::Relaxed) != self.start_value |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
#[inline] |
| 108 |
pub fn err_if_interrupted(&self) -> Result<(), Interrupted> { |
| 109 |
if self.was_interrupted() { |
| 110 |
Err(Interrupted) |
| 111 |
} else { |
| 112 |
Ok(()) |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
impl Interruptee for SqlInterruptScope { |
| 118 |
#[inline] |
| 119 |
fn was_interrupted(&self) -> bool { |
| 120 |
self.was_interrupted() |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
impl AsRef<SqlInterruptHandle> for SqlInterruptHandle { |
| 126 |
fn as_ref(&self) -> &SqlInterruptHandle { |
| 127 |
self |
| 128 |
} |
| 129 |
} |