Skip to content

Commit cbfa612

Browse files
committed
Simplify on_panic callback handling
The registration of `panicking::on_panic` as a general-purpose callback is overcomplicated and can fail. Instead, invoking it explicitly removes the need for locking and paves the way for further improvements.
1 parent f07f4ef commit cbfa612

File tree

1 file changed

+5
-16
lines changed
  • src/libstd/sys/common/unwind

1 file changed

+5
-16
lines changed

src/libstd/sys/common/unwind/mod.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,12 @@ pub fn begin_unwind<M: Any + Send>(msg: M, file_line: &(&'static str, u32)) -> !
244244
#[inline(never)] #[cold] // this is the slow path, please never inline this
245245
fn begin_unwind_inner(msg: Box<Any + Send>,
246246
file_line: &(&'static str, u32)) -> ! {
247-
// Make sure the default failure handler is registered before we look at the
248-
// callbacks. We also use a raw sys-based mutex here instead of a
249-
// `std::sync` one as accessing TLS can cause weird recursive problems (and
250-
// we don't need poison checking).
251-
unsafe {
252-
static LOCK: Mutex = Mutex::new();
253-
static mut INIT: bool = false;
254-
LOCK.lock();
255-
if !INIT {
256-
register(panicking::on_panic);
257-
INIT = true;
258-
}
259-
LOCK.unlock();
260-
}
247+
let (file, line) = *file_line;
248+
249+
// First, invoke the default panic handler.
250+
panicking::on_panic(&*msg, file, line);
261251

262-
// First, invoke call the user-defined callbacks triggered on thread panic.
252+
// Then, invoke call the user-defined callbacks triggered on thread panic.
263253
//
264254
// By the time that we see a callback has been registered (by reading
265255
// MAX_CALLBACKS), the actual callback itself may have not been stored yet,
@@ -275,7 +265,6 @@ fn begin_unwind_inner(msg: Box<Any + Send>,
275265
0 => {}
276266
n => {
277267
let f: Callback = unsafe { mem::transmute(n) };
278-
let (file, line) = *file_line;
279268
f(&*msg, file, line);
280269
}
281270
}

0 commit comments

Comments
 (0)