1 #![allow(dead_code)] // stack_guard isn't used right now on all platforms
2 #![allow(unused_unsafe)] // thread_local with `const {}` triggers this liny
4 use crate::cell::RefCell;
5 use crate::sys::thread::guard::Guard;
6 use crate::thread::Thread;
9 stack_guard: Option<Guard>,
13 thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = const { RefCell::new(None) } }
16 fn with<R, F>(f: F) -> Option<R>
18 F: FnOnce(&mut ThreadInfo) -> R,
21 .try_with(move |thread_info| {
22 let mut thread_info = thread_info.borrow_mut();
23 let thread_info = thread_info.get_or_insert_with(|| ThreadInfo {
25 thread: Thread::new(None),
33 /// Get an address that is unique per running thread.
35 /// This can be used as a non-null usize-sized ID.
36 pub fn current_thread_unique_ptr() -> usize {
37 THREAD_INFO.with(|info| <*const _>::addr(info))
40 pub fn current_thread() -> Option<Thread> {
41 ThreadInfo::with(|info| info.thread.clone())
44 pub fn stack_guard() -> Option<Guard> {
45 ThreadInfo::with(|info| info.stack_guard.clone()).and_then(|o| o)
48 pub fn set(stack_guard: Option<Guard>, thread: Thread) {
49 THREAD_INFO.with(move |thread_info| {
50 let mut thread_info = thread_info.borrow_mut();
51 rtassert!(thread_info.is_none());
52 *thread_info = Some(ThreadInfo { stack_guard, thread });