1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(dead_code)] // stack_guard isn't used right now on all platforms
14 use sys::thread::guard::Guard;
18 stack_guard: Option<Guard>,
22 thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(None) }
25 fn with<R, F>(f: F) -> Option<R> where F: FnOnce(&mut ThreadInfo) -> R {
26 THREAD_INFO.try_with(move |c| {
27 if c.borrow().is_none() {
28 *c.borrow_mut() = Some(ThreadInfo {
30 thread: Thread::new(None),
33 f(c.borrow_mut().as_mut().unwrap())
38 pub fn current_thread() -> Option<Thread> {
39 ThreadInfo::with(|info| info.thread.clone())
42 pub fn stack_guard() -> Option<Guard> {
43 ThreadInfo::with(|info| info.stack_guard.clone()).and_then(|o| o)
46 pub fn set(stack_guard: Option<Guard>, thread: Thread) {
47 THREAD_INFO.with(|c| assert!(c.borrow().is_none()));
48 THREAD_INFO.with(move |c| *c.borrow_mut() = Some(ThreadInfo{
54 pub fn reset_guard(stack_guard: Option<Guard>) {
55 THREAD_INFO.with(move |c| c.borrow_mut().as_mut().unwrap().stack_guard = stack_guard);