Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize thread::panicking() #56469

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions src/libstd/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,46 @@ fn default_hook(info: &PanicInfo) {
}
}


#[cfg(not(test))]
#[doc(hidden)]
#[unstable(feature = "update_panic_count", issue = "0")]
pub fn update_panic_count(amt: isize) -> usize {
use cell::Cell;
thread_local! { static PANIC_COUNT: Cell<usize> = Cell::new(0) }

PANIC_COUNT.with(|c| {
fn with_panic_count<R>(f: impl FnOnce(&Cell<usize>) -> R) -> R {
// Use `static mut` on wasm32 if there are no atomics.
#[cfg(all(target_arch = "wasm32", not(target_feature = "atomics")))]
{
static mut PANIC_COUNT: Cell<usize> = Cell::new(0);
unsafe { f(&PANIC_COUNT) }
}

// Use fast `#[thread_local]` on platforms that support it.
#[cfg(all(
target_thread_local,
not(all(target_arch = "wasm32", not(target_feature = "atomics"))),
))]
{
#[thread_local]
static PANIC_COUNT: Cell<usize> = Cell::new(0);
f(&PANIC_COUNT)
}

// Fall back to regular `thread_local!` in all other cases.
#[cfg(all(
not(target_thread_local),
not(all(target_arch = "wasm32", not(target_feature = "atomics"))),
))]
{
thread_local! { static PANIC_COUNT: Cell<usize> = Cell::new(0) }
PANIC_COUNT.with(f)
}
}

with_panic_count(|c| {
let next = (c.get() as isize + amt) as usize;
c.set(next);
return next
next
})
}

Expand Down