reifydb_runtime/actor/timers/
mod.rs1use std::{
20 fmt,
21 fmt::Debug,
22 sync::{
23 Arc,
24 atomic::{AtomicBool, AtomicU64, Ordering},
25 },
26};
27
28#[cfg(reifydb_target = "native")]
29pub mod scheduler;
30#[cfg(reifydb_target = "wasi")]
31pub(crate) mod wasi;
32#[cfg(reifydb_target = "wasm")]
33pub(crate) mod wasm;
34
35#[cfg(reifydb_target = "wasi")]
36use wasi::drain_expired_timers as wasi_drain;
37
38#[derive(Clone)]
42pub struct TimerHandle {
43 id: u64,
44 cancelled: Arc<AtomicBool>,
45}
46
47impl TimerHandle {
48 pub(crate) fn new(id: u64) -> Self {
49 Self {
50 id,
51 cancelled: Arc::new(AtomicBool::new(false)),
52 }
53 }
54
55 pub fn cancel(&self) -> bool {
60 self.cancelled.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst).is_ok()
61 }
62
63 pub fn is_cancelled(&self) -> bool {
65 self.cancelled.load(Ordering::SeqCst)
66 }
67
68 pub fn id(&self) -> u64 {
70 self.id
71 }
72
73 pub(crate) fn cancelled_flag(&self) -> Arc<AtomicBool> {
75 self.cancelled.clone()
76 }
77}
78
79impl Debug for TimerHandle {
80 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81 f.debug_struct("TimerHandle").field("id", &self.id).field("cancelled", &self.is_cancelled()).finish()
82 }
83}
84
85static TIMER_ID_COUNTER: AtomicU64 = AtomicU64::new(0);
87
88pub(crate) fn next_timer_id() -> u64 {
89 TIMER_ID_COUNTER.fetch_add(1, Ordering::Relaxed)
90}
91
92#[cfg(reifydb_target = "wasi")]
97pub fn drain_expired_timers() {
98 wasi_drain();
99}
100
101#[cfg(not(reifydb_target = "wasi"))]
103pub fn drain_expired_timers() {}