1 #![allow(dead_code)] // stack_guard isn't used right now on all platforms
3 use crate::cell
::RefCell
;
4 use crate::sys
::thread
::guard
::Guard
;
5 use crate::thread
::Thread
;
8 stack_guard
: Option
<Guard
>,
12 thread_local
! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(None) }
15 fn with
<R
, F
>(f
: F
) -> Option
<R
>
17 F
: FnOnce(&mut ThreadInfo
) -> R
,
21 if c
.borrow().is_none() {
23 Some(ThreadInfo { stack_guard: None, thread: Thread::new(None) }
)
25 f(c
.borrow_mut().as_mut().unwrap())
31 pub fn current_thread() -> Option
<Thread
> {
32 ThreadInfo
::with(|info
| info
.thread
.clone())
35 pub fn stack_guard() -> Option
<Guard
> {
36 ThreadInfo
::with(|info
| info
.stack_guard
.clone()).and_then(|o
| o
)
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 }
));
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
);