Skip to main content

luaur_common/methods/
thread_context_thread_context_time_trace.rs

1#[cfg(feature = "luau_enable_time_trace")]
2use crate::functions::create_thread::create_thread;
3use crate::functions::get_global_context::get_global_context;
4use crate::records::thread_context::ThreadContext;
5
6impl ThreadContext {
7    pub fn thread_context() -> Self {
8        #[cfg(not(feature = "luau_enable_time_trace"))]
9        {
10            ThreadContext {
11                global_context: get_global_context(),
12                thread_id: 0,
13                events: Vec::new(),
14                data: Vec::new(),
15            }
16        }
17
18        #[cfg(feature = "luau_enable_time_trace")]
19        {
20            // C++: `ThreadContext() : globalContext(getGlobalContext()) { threadId = createThread(*globalContext, this); }`
21            let global_context = get_global_context();
22            let mut result = ThreadContext {
23                global_context: global_context.clone(),
24                thread_id: 0,
25                events: Vec::new(),
26                data: Vec::new(),
27            };
28
29            // `createThread(*globalContext, this)` — the singleton context guards
30            // its own mutable state behind a Mutex, so a shared `&` suffices.
31            result.thread_id = create_thread(&global_context, &mut result as *mut ThreadContext);
32
33            result
34        }
35    }
36}