Skip to main content

canic_memory/macros/
runtime.rs

1/// Register one eager-init body for execution from the generated lifecycle hooks.
2///
3/// The body runs synchronously after Canic has restored memory, config, and
4/// environment state, but before any zero-delay bootstrap timers fire.
5#[macro_export]
6macro_rules! eager_init {
7    ($body:block) => {
8        macro_rules! __canic_run_registered_eager_init {
9            () => {{
10                if option_env!("CANIC_SKIP_EAGER_INIT").is_none() {
11                    $body
12                }
13            }};
14        }
15    };
16}
17
18/// Declare a thread-local static and schedule an eager initialization touch.
19///
20/// Expands to a `thread_local!` block and ensures the TLS slot is accessed
21/// during the eager-init phase so subsequent calls observe a fully
22/// initialized value.
23#[macro_export]
24macro_rules! eager_static {
25    ($vis:vis static $name:ident : $ty:ty = $init:expr;) => {
26        thread_local! {
27            $vis static $name: $ty = $init;
28        }
29
30        const _: () = {
31            fn __canic_touch_tls() {
32                $name.with(|_| {});
33            }
34
35            #[ $crate::__reexports::ctor::ctor(anonymous, crate_path = $crate::__reexports::ctor) ]
36            fn __canic_register_eager_tls() {
37                $crate::runtime::defer_tls_initializer(__canic_touch_tls);
38            }
39        };
40    };
41}