]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys_common/thread_info.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys_common / thread_info.rs
1 #![allow(dead_code)] // stack_guard isn't used right now on all platforms
2
3 use crate::cell::RefCell;
4 use crate::sys::thread::guard::Guard;
5 use crate::thread::Thread;
6
7 struct ThreadInfo {
8 stack_guard: Option<Guard>,
9 thread: Thread,
10 }
11
12 thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(None) }
13
14 impl ThreadInfo {
15 fn with<R, F>(f: F) -> Option<R>
16 where
17 F: FnOnce(&mut ThreadInfo) -> R,
18 {
19 THREAD_INFO
20 .try_with(move |c| {
21 if c.borrow().is_none() {
22 *c.borrow_mut() =
23 Some(ThreadInfo { stack_guard: None, thread: Thread::new(None) })
24 }
25 f(c.borrow_mut().as_mut().unwrap())
26 })
27 .ok()
28 }
29 }
30
31 pub fn current_thread() -> Option<Thread> {
32 ThreadInfo::with(|info| info.thread.clone())
33 }
34
35 pub fn stack_guard() -> Option<Guard> {
36 ThreadInfo::with(|info| info.stack_guard.clone()).and_then(|o| o)
37 }
38
39 pub fn set(stack_guard: Option<Guard>, thread: Thread) {
40 THREAD_INFO.with(|c| assert!(c.borrow().is_none()));
41 THREAD_INFO.with(move |c| *c.borrow_mut() = Some(ThreadInfo { stack_guard, thread }));
42 }
43
44 pub fn reset_guard(stack_guard: Option<Guard>) {
45 THREAD_INFO.with(move |c| c.borrow_mut().as_mut().unwrap().stack_guard = stack_guard);
46 }