canic_core/macros/timer.rs
1//! Perf-instrumented timer helpers that auto-label with module + function name.
2//!
3//! These macros wrap [`TimerOps`](crate::ops::ic::timer::TimerOps) so callers can
4//! schedule work without manually threading labels. Labels are constructed
5//! as `module_path!()::function_name`.
6
7///
8/// timer
9/// Schedule a one-shot timer with an auto-generated label.
10///
11/// # Examples
12/// - `timer!(Duration::from_secs(5), do_cleanup);`
13/// - `timer!(Duration::ZERO, my_task, arg1, arg2);`
14///
15#[macro_export]
16macro_rules! timer {
17 ($delay:expr, $func:path $(, $($args:tt)*)? ) => {{
18 let label = concat!(module_path!(), "::", stringify!($func));
19 $crate::ops::ic::timer::TimerOps::set($delay, label, $func($($($args)*)?))
20 }};
21}
22
23///
24/// timer_guarded
25/// Schedule a one-shot timer if none is already scheduled for the slot.
26/// Returns true when a new timer was scheduled.
27///
28/// # Examples
29/// - `timer_guarded!(MY_TIMER, Duration::from_secs(5), do_cleanup);`
30/// - `timer_guarded!(MY_TIMER, Duration::ZERO, my_task, arg1, arg2);`
31///
32#[macro_export]
33macro_rules! timer_guarded {
34 ($slot:path, $delay:expr, $func:path $(, $($args:tt)*)? ) => {{
35 let label = concat!(module_path!(), "::", stringify!($func));
36 $crate::ops::ic::timer::TimerOps::set_guarded(
37 &$slot,
38 $delay,
39 label,
40 $func($($($args)*)?),
41 )
42 }};
43}
44
45///
46/// timer_interval
47/// Schedule a repeating timer with an auto-generated label.
48///
49/// # Examples
50/// - `timer_interval!(Duration::from_secs(60), heartbeat);`
51/// - `timer_interval!(Duration::from_secs(10), tick, state.clone());`
52///
53#[macro_export]
54macro_rules! timer_interval {
55 ($interval:expr, $func:path $(, $($args:tt)*)? ) => {{
56 let label = concat!(module_path!(), "::", stringify!($func));
57 $crate::ops::ic::timer::TimerOps::set_interval(
58 $interval,
59 label,
60 move || $func($($($args)*)?),
61 )
62 }};
63}
64
65///
66/// timer_interval_guarded
67/// Schedule an init timer that installs a repeating timer for the slot.
68/// Returns true when a new timer was scheduled.
69///
70/// # Examples
71/// - `timer_interval_guarded!(MY_TIMER, Duration::ZERO, init_task; Duration::from_secs(60), tick);`
72/// - `timer_interval_guarded!(MY_TIMER, Duration::from_secs(2), init; Duration::from_secs(10), tick, state.clone());`
73///
74#[macro_export]
75macro_rules! timer_interval_guarded {
76 (
77 $slot:path,
78 $init_delay:expr,
79 $init_func:path $(, $($init_args:tt)*)?
80 ;
81 $interval:expr,
82 $tick_func:path $(, $($tick_args:tt)*)?
83 ) => {{
84 let init_label = concat!(module_path!(), "::", stringify!($init_func));
85 let tick_label = concat!(module_path!(), "::", stringify!($tick_func));
86
87 $crate::ops::ic::timer::TimerOps::set_guarded_interval(
88 &$slot,
89 $init_delay,
90 init_label,
91 move || $init_func($($($init_args)*)?),
92 $interval,
93 tick_label,
94 move || $tick_func($($($tick_args)*)?),
95 )
96 }};
97}