]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys_common/thread_info.rs
Rollup merge of #58906 - Nemo157:generator-state-debug-info, r=Zoxc
[rust.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> where F: FnOnce(&mut ThreadInfo) -> R {
16         THREAD_INFO.try_with(move |c| {
17             if c.borrow().is_none() {
18                 *c.borrow_mut() = Some(ThreadInfo {
19                     stack_guard: None,
20                     thread: Thread::new(None),
21                 })
22             }
23             f(c.borrow_mut().as_mut().unwrap())
24         }).ok()
25     }
26 }
27
28 pub fn current_thread() -> Option<Thread> {
29     ThreadInfo::with(|info| info.thread.clone())
30 }
31
32 pub fn stack_guard() -> Option<Guard> {
33     ThreadInfo::with(|info| info.stack_guard.clone()).and_then(|o| o)
34 }
35
36 pub fn set(stack_guard: Option<Guard>, thread: Thread) {
37     THREAD_INFO.with(|c| assert!(c.borrow().is_none()));
38     THREAD_INFO.with(move |c| *c.borrow_mut() = Some(ThreadInfo{
39         stack_guard,
40         thread,
41     }));
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 }