]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/common/thread_info.rs
Add verbose option to rustdoc in order to fix problem with --version
[rust.git] / src / libstd / sys / common / thread_info.rs
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.
4 //
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.
10
11 use core::prelude::*;
12
13 use thread::Thread;
14 use cell::RefCell;
15 use string::String;
16
17 struct ThreadInfo {
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),
22     stack_guard: uint,
23     thread: Thread,
24 }
25
26 thread_local! { static THREAD_INFO: RefCell<Option<ThreadInfo>> = RefCell::new(None) }
27
28 impl ThreadInfo {
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");
33         }
34
35         THREAD_INFO.with(|c| {
36             if c.borrow().is_none() {
37                 *c.borrow_mut() = Some(ThreadInfo {
38                     stack_bounds: (0, 0),
39                     stack_guard: 0,
40                     thread: NewThread::new(None),
41                 })
42             }
43             f(c.borrow_mut().as_mut().unwrap())
44         })
45     }
46 }
47
48 pub fn current_thread() -> Thread {
49     ThreadInfo::with(|info| info.thread.clone())
50 }
51
52 pub fn stack_guard() -> uint {
53     ThreadInfo::with(|info| info.stack_guard)
54 }
55
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,
61         thread: thread,
62     }));
63 }
64
65 // a hack to get around privacy restrictions; implemented by `std::thread::Thread`
66 pub trait NewThread {
67     fn new(name: Option<String>) -> Self;
68 }