reifydb_runtime/actor/timers/
mod.rs1use std::{
20 fmt::Debug,
21 sync::{
22 Arc,
23 atomic::{AtomicBool, AtomicU64, Ordering},
24 },
25};
26
27#[cfg(reifydb_target = "native")]
28pub mod scheduler;
29#[cfg(reifydb_target = "wasm")]
30pub(crate) mod wasm;
31
32#[derive(Clone)]
36pub struct TimerHandle {
37 id: u64,
38 cancelled: Arc<AtomicBool>,
39}
40
41impl TimerHandle {
42 pub(crate) fn new(id: u64) -> Self {
43 Self {
44 id,
45 cancelled: Arc::new(AtomicBool::new(false)),
46 }
47 }
48
49 pub fn cancel(&self) -> bool {
54 self.cancelled.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst).is_ok()
55 }
56
57 pub fn is_cancelled(&self) -> bool {
59 self.cancelled.load(Ordering::SeqCst)
60 }
61
62 pub fn id(&self) -> u64 {
64 self.id
65 }
66
67 pub(crate) fn cancelled_flag(&self) -> Arc<AtomicBool> {
69 self.cancelled.clone()
70 }
71}
72
73impl Debug for TimerHandle {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 f.debug_struct("TimerHandle").field("id", &self.id).field("cancelled", &self.is_cancelled()).finish()
76 }
77}
78
79static TIMER_ID_COUNTER: AtomicU64 = AtomicU64::new(0);
81
82pub(crate) fn next_timer_id() -> u64 {
83 TIMER_ID_COUNTER.fetch_add(1, Ordering::Relaxed)
84}