canic_memory/macros/
runtime.rs

1/// Run `$body` during process start-up using `ctor`.
2///
3/// The macro expands to a `ctor` hook so eager TLS initializers can register
4/// their work before any canister lifecycle hooks execute. Prefer wrapping
5/// the body in a separate function for larger initializers to keep the hook
6/// simple.
7#[macro_export]
8macro_rules! eager_init {
9    ($body:block) => {
10        #[ $crate::__reexports::ctor::ctor(anonymous, crate_path = $crate::__reexports::ctor) ]
11        fn __canic_eager_init() {
12            $body
13        }
14    };
15}
16
17/// Declare a thread-local static and schedule an eager initialization touch.
18///
19/// Expands to a `thread_local!` block and ensures the TLS slot is accessed
20/// during the eager-init phase so subsequent calls observe a fully
21/// initialized value.
22#[macro_export]
23macro_rules! eager_static {
24    ($vis:vis static $name:ident : $ty:ty = $init:expr;) => {
25        thread_local! {
26            $vis static $name: $ty = $init;
27        }
28
29        $crate::eager_init!({
30            // Capture the TLS accessor and register a closure that forces initialization.
31            $crate::runtime::defer_tls_initializer(|| {
32                $name.with(|_| {});
33            });
34        });
35    };
36}