Make the thread_state machinery behave the same across debug and opt builds.

I don't need this per se, but it seems like a footgun for the methods to
return incorrect information depending on the build type. I don't see anywhere
where the overhead would be at all significant.

MozReview-Commit-ID: G1qyUFhI0aB
This commit is contained in:
Bobby Holley 2017-09-19 10:19:49 -07:00
parent 4f98a3f56a
commit b0d1cde558

View file

@ -2,11 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Supports dynamic assertions in debug builds about what sort of thread is //! Supports dynamic assertions in about what sort of thread is running and
//! running and what state it's in. //! what state it's in.
//!
//! In release builds, `get` returns 0. All of the other functions inline
//! away to nothing.
#![deny(missing_docs)] #![deny(missing_docs)]
@ -37,20 +34,13 @@ macro_rules! thread_types ( ( $( $fun:ident = $flag:ident ; )* ) => (
} }
$( $(
#[cfg(debug_assertions)]
#[allow(missing_docs)] #[allow(missing_docs)]
pub fn $fun(self) -> bool { pub fn $fun(self) -> bool {
self.contains($flag) self.contains($flag)
} }
#[cfg(not(debug_assertions))]
#[allow(missing_docs)]
pub fn $fun(self) -> bool {
true
}
)* )*
} }
#[cfg(debug_assertions)]
static TYPES: &'static [ThreadState] = static TYPES: &'static [ThreadState] =
&[ $( $flag ),* ]; &[ $( $flag ),* ];
)); ));
@ -60,7 +50,6 @@ thread_types! {
is_layout = LAYOUT; is_layout = LAYOUT;
} }
#[cfg(debug_assertions)]
mod imp { mod imp {
use std::cell::RefCell; use std::cell::RefCell;
use super::{TYPES, ThreadState}; use super::{TYPES, ThreadState};
@ -89,14 +78,14 @@ mod imp {
}); });
// Exactly one of the thread type flags should be set. // Exactly one of the thread type flags should be set.
assert_eq!(1, TYPES.iter().filter(|&&ty| state.contains(ty)).count()); debug_assert_eq!(1, TYPES.iter().filter(|&&ty| state.contains(ty)).count());
state state
} }
/// Enter into a given temporary state. Panics if re-entring. /// Enter into a given temporary state. Panics if re-entring.
pub fn enter(x: ThreadState) { pub fn enter(x: ThreadState) {
let state = get(); let state = get();
assert!(!state.intersects(x)); debug_assert!(!state.intersects(x));
STATE.with(|ref k| { STATE.with(|ref k| {
*k.borrow_mut() = Some(state | x); *k.borrow_mut() = Some(state | x);
}) })
@ -105,19 +94,9 @@ mod imp {
/// Exit a given temporary state. /// Exit a given temporary state.
pub fn exit(x: ThreadState) { pub fn exit(x: ThreadState) {
let state = get(); let state = get();
assert!(state.contains(x)); debug_assert!(state.contains(x));
STATE.with(|ref k| { STATE.with(|ref k| {
*k.borrow_mut() = Some(state & !x); *k.borrow_mut() = Some(state & !x);
}) })
} }
} }
#[cfg(not(debug_assertions))]
#[allow(missing_docs)]
mod imp {
use super::ThreadState;
#[inline(always)] pub fn initialize(_: ThreadState) { }
#[inline(always)] pub fn get() -> ThreadState { ThreadState::empty() }
#[inline(always)] pub fn enter(_: ThreadState) { }
#[inline(always)] pub fn exit(_: ThreadState) { }
}