From 812ca025aedddea8a4c7d8477146527b71b33e19 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Sat, 11 Jun 2022 10:29:54 -0700 Subject: [PATCH] Simplify calls that use the panic_handler --- rayon-core/src/broadcast/mod.rs | 8 +------- rayon-core/src/registry.rs | 30 +++++++----------------------- rayon-core/src/spawn/mod.rs | 7 +------ 3 files changed, 9 insertions(+), 36 deletions(-) diff --git a/rayon-core/src/broadcast/mod.rs b/rayon-core/src/broadcast/mod.rs index bece9b990..bbf6f3e0d 100644 --- a/rayon-core/src/broadcast/mod.rs +++ b/rayon-core/src/broadcast/mod.rs @@ -1,7 +1,6 @@ use crate::job::{ArcJob, StackJob}; use crate::registry::{Registry, WorkerThread}; use crate::scope::ScopeLatch; -use crate::unwind; use std::fmt; use std::marker::PhantomData; use std::sync::Arc; @@ -131,12 +130,7 @@ where let job = ArcJob::new({ let registry = Arc::clone(registry); move || { - match unwind::halt_unwinding(|| BroadcastContext::with(&op)) { - Ok(()) => {} - Err(err) => { - registry.handle_panic(err); - } - } + registry.catch_unwind(|| BroadcastContext::with(&op)); registry.terminate(); // (*) permit registry to terminate now } }); diff --git a/rayon-core/src/registry.rs b/rayon-core/src/registry.rs index 32185ad60..69c43d95d 100644 --- a/rayon-core/src/registry.rs +++ b/rayon-core/src/registry.rs @@ -8,7 +8,6 @@ use crate::{ ErrorKind, ExitHandler, PanicHandler, StartHandler, ThreadPoolBuildError, ThreadPoolBuilder, }; use crossbeam_deque::{Injector, Steal, Stealer, Worker}; -use std::any::Any; use std::cell::Cell; use std::collections::hash_map::DefaultHasher; use std::fmt; @@ -332,19 +331,14 @@ impl Registry { self.thread_infos.len() } - pub(super) fn handle_panic(&self, err: Box) { - match self.panic_handler { - Some(ref handler) => { - // If the customizable panic handler itself panics, - // then we abort. - let abort_guard = unwind::AbortIfPanic; + pub(super) fn catch_unwind(&self, f: impl FnOnce()) { + if let Err(err) = unwind::halt_unwinding(f) { + // If there is no handler, or if that handler itself panics, then we abort. + let abort_guard = unwind::AbortIfPanic; + if let Some(ref handler) = self.panic_handler { handler(err); mem::forget(abort_guard); } - None => { - // Default panic handler aborts. - let _ = unwind::AbortIfPanic; // let this drop. - } } } @@ -880,12 +874,7 @@ unsafe fn main_loop( // Inform a user callback that we started a thread. if let Some(ref handler) = registry.start_handler { - match unwind::halt_unwinding(|| handler(index)) { - Ok(()) => {} - Err(err) => { - registry.handle_panic(err); - } - } + registry.catch_unwind(|| handler(index)); } let my_terminate_latch = ®istry.thread_infos[index].terminate; @@ -908,12 +897,7 @@ unsafe fn main_loop( // Inform a user callback that we exited a thread. if let Some(ref handler) = registry.exit_handler { - match unwind::halt_unwinding(|| handler(index)) { - Ok(()) => {} - Err(err) => { - registry.handle_panic(err); - } - } + registry.catch_unwind(|| handler(index)); // We're already exiting the thread, there's nothing else to do. } } diff --git a/rayon-core/src/spawn/mod.rs b/rayon-core/src/spawn/mod.rs index dc5725941..827e36e61 100644 --- a/rayon-core/src/spawn/mod.rs +++ b/rayon-core/src/spawn/mod.rs @@ -94,12 +94,7 @@ where HeapJob::new({ let registry = Arc::clone(registry); move || { - match unwind::halt_unwinding(func) { - Ok(()) => {} - Err(err) => { - registry.handle_panic(err); - } - } + registry.catch_unwind(func); registry.terminate(); // (*) permit registry to terminate now } })