From 43d6289c26c3cd4a63680979d465c18969ece949 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Fri, 24 May 2024 09:54:41 -0500 Subject: [PATCH] Make assert_is_main_thread() simpler to optimize The compiler cannot guarantee that a `static AtomicBool` is always the same initial value, but it can do so for a `const bool`. --- src/threads.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/threads.rs b/src/threads.rs index 5dd19f0d5..6eccd5fc4 100644 --- a/src/threads.rs +++ b/src/threads.rs @@ -24,10 +24,7 @@ impl FloggableDebug for ThreadId {} /// The thread id of the main thread, as set by [`init()`] at startup. static mut MAIN_THREAD_ID: Option = None; /// Used to bypass thread assertions when testing. -#[cfg(not(test))] -static THREAD_ASSERTS_CFG_FOR_TESTING: AtomicBool = AtomicBool::new(false); -#[cfg(test)] -static THREAD_ASSERTS_CFG_FOR_TESTING: AtomicBool = AtomicBool::new(true); +const THREAD_ASSERTS_CFG_FOR_TESTING: bool = cfg!(test); /// This allows us to notice when we've forked. static IS_FORKED_PROC: AtomicBool = AtomicBool::new(false); @@ -111,7 +108,7 @@ fn not_main_thread() -> ! { panic!("Function is not running on the main thread!"); } - if !is_main_thread() && !THREAD_ASSERTS_CFG_FOR_TESTING.load(Ordering::Relaxed) { + if !is_main_thread() && !THREAD_ASSERTS_CFG_FOR_TESTING { not_main_thread(); } } @@ -123,7 +120,7 @@ fn not_background_thread() -> ! { panic!("Function is not allowed to be called on the main thread!"); } - if is_main_thread() && !THREAD_ASSERTS_CFG_FOR_TESTING.load(Ordering::Relaxed) { + if is_main_thread() && !THREAD_ASSERTS_CFG_FOR_TESTING { not_background_thread(); } }