Skip to content

Commit

Permalink
Make std::panicking::panic_count::is_zero inline and move the slow …
Browse files Browse the repository at this point in the history
…path into a separate cold function.
  • Loading branch information
eduardosm committed Jun 24, 2020
1 parent f03cf99 commit 771a1d8
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/libstd/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,25 @@ pub mod panic_count {
LOCAL_PANIC_COUNT.with(|c| c.get())
}

#[inline]
pub fn is_zero() -> bool {
if GLOBAL_PANIC_COUNT.load(Ordering::Relaxed) == 0 {
// Fast path: if `GLOBAL_PANIC_COUNT` is zero, all threads
// (including the current one) will have `LOCAL_PANIC_COUNT`
// equal to zero, so TLS access can be avoided.
true
} else {
LOCAL_PANIC_COUNT.with(|c| c.get() == 0)
is_zero_slow_path()
}
}

// Slow path is in a separate function to reduce the amount of code
// inlined from `is_zero`.
#[inline(never)]
#[cold]
fn is_zero_slow_path() -> bool {
LOCAL_PANIC_COUNT.with(|c| c.get() == 0)
}
}

#[cfg(test)]
Expand Down Expand Up @@ -350,6 +359,7 @@ pub unsafe fn r#try<R, F: FnOnce() -> R>(f: F) -> Result<R, Box<dyn Any + Send>>
}

/// Determines whether the current thread is unwinding because of panic.
#[inline]
pub fn panicking() -> bool {
!panic_count::is_zero()
}
Expand Down

0 comments on commit 771a1d8

Please sign in to comment.