Skip to content

Commit 812ca02

Browse files
committed
Simplify calls that use the panic_handler
1 parent c7a3172 commit 812ca02

File tree

3 files changed

+9
-36
lines changed

3 files changed

+9
-36
lines changed

rayon-core/src/broadcast/mod.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::job::{ArcJob, StackJob};
22
use crate::registry::{Registry, WorkerThread};
33
use crate::scope::ScopeLatch;
4-
use crate::unwind;
54
use std::fmt;
65
use std::marker::PhantomData;
76
use std::sync::Arc;
@@ -131,12 +130,7 @@ where
131130
let job = ArcJob::new({
132131
let registry = Arc::clone(registry);
133132
move || {
134-
match unwind::halt_unwinding(|| BroadcastContext::with(&op)) {
135-
Ok(()) => {}
136-
Err(err) => {
137-
registry.handle_panic(err);
138-
}
139-
}
133+
registry.catch_unwind(|| BroadcastContext::with(&op));
140134
registry.terminate(); // (*) permit registry to terminate now
141135
}
142136
});

rayon-core/src/registry.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::{
88
ErrorKind, ExitHandler, PanicHandler, StartHandler, ThreadPoolBuildError, ThreadPoolBuilder,
99
};
1010
use crossbeam_deque::{Injector, Steal, Stealer, Worker};
11-
use std::any::Any;
1211
use std::cell::Cell;
1312
use std::collections::hash_map::DefaultHasher;
1413
use std::fmt;
@@ -332,19 +331,14 @@ impl Registry {
332331
self.thread_infos.len()
333332
}
334333

335-
pub(super) fn handle_panic(&self, err: Box<dyn Any + Send>) {
336-
match self.panic_handler {
337-
Some(ref handler) => {
338-
// If the customizable panic handler itself panics,
339-
// then we abort.
340-
let abort_guard = unwind::AbortIfPanic;
334+
pub(super) fn catch_unwind(&self, f: impl FnOnce()) {
335+
if let Err(err) = unwind::halt_unwinding(f) {
336+
// If there is no handler, or if that handler itself panics, then we abort.
337+
let abort_guard = unwind::AbortIfPanic;
338+
if let Some(ref handler) = self.panic_handler {
341339
handler(err);
342340
mem::forget(abort_guard);
343341
}
344-
None => {
345-
// Default panic handler aborts.
346-
let _ = unwind::AbortIfPanic; // let this drop.
347-
}
348342
}
349343
}
350344

@@ -880,12 +874,7 @@ unsafe fn main_loop(
880874

881875
// Inform a user callback that we started a thread.
882876
if let Some(ref handler) = registry.start_handler {
883-
match unwind::halt_unwinding(|| handler(index)) {
884-
Ok(()) => {}
885-
Err(err) => {
886-
registry.handle_panic(err);
887-
}
888-
}
877+
registry.catch_unwind(|| handler(index));
889878
}
890879

891880
let my_terminate_latch = &registry.thread_infos[index].terminate;
@@ -908,12 +897,7 @@ unsafe fn main_loop(
908897

909898
// Inform a user callback that we exited a thread.
910899
if let Some(ref handler) = registry.exit_handler {
911-
match unwind::halt_unwinding(|| handler(index)) {
912-
Ok(()) => {}
913-
Err(err) => {
914-
registry.handle_panic(err);
915-
}
916-
}
900+
registry.catch_unwind(|| handler(index));
917901
// We're already exiting the thread, there's nothing else to do.
918902
}
919903
}

rayon-core/src/spawn/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,7 @@ where
9494
HeapJob::new({
9595
let registry = Arc::clone(registry);
9696
move || {
97-
match unwind::halt_unwinding(func) {
98-
Ok(()) => {}
99-
Err(err) => {
100-
registry.handle_panic(err);
101-
}
102-
}
97+
registry.catch_unwind(func);
10398
registry.terminate(); // (*) permit registry to terminate now
10499
}
105100
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy