Skip to content

Commit

Permalink
Simplify calls that use the panic_handler
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Nov 16, 2022
1 parent c7a3172 commit 812ca02
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 36 deletions.
8 changes: 1 addition & 7 deletions rayon-core/src/broadcast/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
}
});
Expand Down
30 changes: 7 additions & 23 deletions rayon-core/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -332,19 +331,14 @@ impl Registry {
self.thread_infos.len()
}

pub(super) fn handle_panic(&self, err: Box<dyn Any + Send>) {
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.
}
}
}

Expand Down Expand Up @@ -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 = &registry.thread_infos[index].terminate;
Expand All @@ -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.
}
}
Expand Down
7 changes: 1 addition & 6 deletions rayon-core/src/spawn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
})
Expand Down

0 comments on commit 812ca02

Please sign in to comment.