authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-06-11 15:14:06-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-06-12 17:09:07-07:00
log55a9ea250cf2aad58f3c4eb49ac6ee8d2b6f5cff
treec2f20b3dc137fec4907f825de35c8208b19dc574
parent0cef727e59d7b0c34756c09f64cbfe4490dcc3e7

std.debug: lock stderr mutex when panicking

The doc comments for this global said: "Locked to avoid interleaving panic messages from multiple threads." Huh? There's already a mutex for that, it's the stderr mutex. Lock that one instead.

1 files changed, 6 insertions(+), 9 deletions(-)

lib/std/debug.zig+6-9
...@@ -447,9 +447,6 @@ pub fn panicExtra(...@@ -447,9 +447,6 @@ pub fn panicExtra(
447/// The counter is incremented/decremented atomically.447/// The counter is incremented/decremented atomically.
448var panicking = std.atomic.Value(u8).init(0);448var panicking = std.atomic.Value(u8).init(0);
449449
450// Locked to avoid interleaving panic messages from multiple threads.
451var panic_mutex = std.Thread.Mutex{};
452
453/// Counts how many times the panic handler is invoked by this thread.450/// Counts how many times the panic handler is invoked by this thread.
454/// This is used to catch and handle panics triggered by the panic handler.451/// This is used to catch and handle panics triggered by the panic handler.
455threadlocal var panic_stage: usize = 0;452threadlocal var panic_stage: usize = 0;
...@@ -474,8 +471,8 @@ pub fn panicImpl(trace: ?*const std.builtin.StackTrace, first_trace_addr: ?usize...@@ -474,8 +471,8 @@ pub fn panicImpl(trace: ?*const std.builtin.StackTrace, first_trace_addr: ?usize
474471
475 // Make sure to release the mutex when done472 // Make sure to release the mutex when done
476 {473 {
477 panic_mutex.lock();474 lockStdErr();
478 defer panic_mutex.unlock();475 defer unlockStdErr();
479476
480 const stderr = io.getStdErr().writer();477 const stderr = io.getStdErr().writer();
481 if (builtin.single_threaded) {478 if (builtin.single_threaded) {
...@@ -2604,8 +2601,8 @@ fn handleSegfaultPosix(sig: i32, info: *const posix.siginfo_t, ctx_ptr: ?*anyopa...@@ -2604,8 +2601,8 @@ fn handleSegfaultPosix(sig: i32, info: *const posix.siginfo_t, ctx_ptr: ?*anyopa
2604 _ = panicking.fetchAdd(1, .seq_cst);2601 _ = panicking.fetchAdd(1, .seq_cst);
26052602
2606 {2603 {
2607 panic_mutex.lock();2604 lockStdErr();
2608 defer panic_mutex.unlock();2605 defer unlockStdErr();
26092606
2610 dumpSegfaultInfoPosix(sig, code, addr, ctx_ptr);2607 dumpSegfaultInfoPosix(sig, code, addr, ctx_ptr);
2611 }2608 }
...@@ -2680,8 +2677,8 @@ fn handleSegfaultWindowsExtra(...@@ -2680,8 +2677,8 @@ fn handleSegfaultWindowsExtra(
2680 _ = panicking.fetchAdd(1, .seq_cst);2677 _ = panicking.fetchAdd(1, .seq_cst);
26812678
2682 {2679 {
2683 panic_mutex.lock();2680 lockStdErr();
2684 defer panic_mutex.unlock();2681 defer unlockStdErr();
26852682
2686 dumpSegfaultInfoWindows(info, msg, label);2683 dumpSegfaultInfoWindows(info, msg, label);
2687 }2684 }