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.
18 // This field holds the known bounds of the stack in (lo, hi)
19 // form. Not all threads necessarily know their precise bounds,
20 // hence this is optional.
21 stack_bounds: (uint, uint),
26 thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(None) }
29 fn with<R>(f: |&mut ThreadInfo| -> R) -> R {
30 if THREAD_INFO.destroyed() {
31 panic!("Use of std::thread::Thread::current() is not possible after \
32 the thread's local data has been destroyed");
35 THREAD_INFO.with(|c| {
36 if c.borrow().is_none() {
37 *c.borrow_mut() = Some(ThreadInfo {
40 thread: NewThread::new(None),
43 f(c.borrow_mut().as_mut().unwrap())
48 pub fn current_thread() -> Thread {
49 ThreadInfo::with(|info| info.thread.clone())
52 pub fn stack_guard() -> uint {
53 ThreadInfo::with(|info| info.stack_guard)
56 pub fn set(stack_bounds: (uint, uint), stack_guard: uint, thread: Thread) {
57 THREAD_INFO.with(|c| assert!(c.borrow().is_none()));
58 THREAD_INFO.with(move |c| *c.borrow_mut() = Some(ThreadInfo{
59 stack_bounds: stack_bounds,
60 stack_guard: stack_guard,
65 // a hack to get around privacy restrictions; implemented by `std::thread::Thread`
67 fn new(name: Option<String>) -> Self;