1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
5 //! Supports dynamic assertions in about what sort of thread is running and
6 //! what state it's in.
10 use std::cell::RefCell;
13 /// A thread state flag, used for multiple assertions.
14 pub struct ThreadState: u32 {
15 /// Whether we're in a script thread.
17 /// Whether we're in a layout thread.
20 /// Whether we're in a script worker thread (actual web workers), or in
21 /// a layout worker thread.
22 const IN_WORKER = 0x0100;
24 /// Whether the current thread is going through a GC.
29 macro_rules! thread_types ( ( $( $fun:ident = $flag:path ; )* ) => (
31 /// Whether the current thread is a worker thread.
32 pub fn is_worker(self) -> bool {
33 self.contains(ThreadState::IN_WORKER)
37 #[allow(missing_docs)]
38 pub fn $fun(self) -> bool {
46 is_script = ThreadState::SCRIPT;
47 is_layout = ThreadState::LAYOUT;
50 thread_local!(static STATE: RefCell<Option<ThreadState>> = RefCell::new(None));
52 /// Initializes the current thread state.
53 pub fn initialize(x: ThreadState) {
55 if let Some(ref s) = *k.borrow() {
57 panic!("Thread state already initialized as {:?}", s);
60 *k.borrow_mut() = Some(x);
64 /// Initializes the current thread as a layout worker thread.
65 pub fn initialize_layout_worker_thread() {
66 initialize(ThreadState::LAYOUT | ThreadState::IN_WORKER);
69 /// Gets the current thread state.
70 pub fn get() -> ThreadState {
71 let state = STATE.with(|ref k| {
73 None => ThreadState::empty(), // Unknown thread.
81 /// Enters into a given temporary state. Panics if re-entring.
82 pub fn enter(x: ThreadState) {
84 debug_assert!(!state.intersects(x));
86 *k.borrow_mut() = Some(state | x);
90 /// Exits a given temporary state.
91 pub fn exit(x: ThreadState) {
93 debug_assert!(state.contains(x));
95 *k.borrow_mut() = Some(state & !x);