Skip to main content

luaur_code_gen/functions/
set_perf_log.rs

1use crate::type_aliases::perf_log_fn::PerfLogFn;
2use core::cell::UnsafeCell;
3use core::ffi::c_void;
4
5// Wrapper to permit Sync on UnsafeCell for native-only global state
6#[repr(transparent)]
7struct SyncCell<T: Copy>(UnsafeCell<T>);
8
9// SAFETY: native-only code, caller must ensure no data races
10unsafe impl<T: Copy> Sync for SyncCell<T> {}
11
12impl<T: Copy> SyncCell<T> {
13    const fn new(val: T) -> Self {
14        Self(UnsafeCell::new(val))
15    }
16}
17
18pub(crate) static G_PERF_LOG_CONTEXT: SyncCell<*mut c_void> = SyncCell::new(core::ptr::null_mut());
19pub(crate) static G_PERF_LOG_FN: SyncCell<PerfLogFn> = SyncCell::new(None);
20
21pub fn set_perf_log(context: *mut c_void, log_fn: PerfLogFn) {
22    // SAFETY: native-only context, called before concurrent reads
23    unsafe {
24        *G_PERF_LOG_CONTEXT.0.get() = context;
25        *G_PERF_LOG_FN.0.get() = log_fn;
26    }
27}