authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-03 20:06:50-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-03 20:46:25-08:00
logd45f9aca14bd36a710293b3d0092039fd91a571b
treeea084f371d318a53665e10f1d8b0decde5b7ce29
parentd1e01e94311ce4423f65d0d102d169ed59f1ca9d

std.Thread: delete Mutex.Recursive

Replaced by the lockStderr functions of std.Io. Trying to make `std.process.stderr_thread_mutex` be a bridge across different Io implementations didn't work in practice.

7 files changed, 39 insertions(+), 100 deletions(-)

lib/std/Io.zig+1-2
......@@ -2160,8 +2160,7 @@ pub const LockedStderr = struct {
21602160
21612161/// For doing application-level writes to the standard error stream.
21622162/// Coordinates also with debug-level writes that are ignorant of Io interface
2163/// and implementations. When this returns, `std.process.stderr_thread_mutex`
2164/// will be locked.
2163/// and implementations.
21652164///
21662165/// See also:
21672166/// * `tryLockStderr`
lib/std/Io/IoUring.zig+1-1
......@@ -10,7 +10,7 @@ const IoUring = std.os.linux.IoUring;
1010
1111/// Must be a thread-safe allocator.
1212gpa: Allocator,
13mutex: std.Thread.Mutex,
13mutex: Io.Mutex,
1414main_fiber_buffer: [@sizeOf(Fiber) + Fiber.max_result_size]u8 align(@alignOf(Fiber)),
1515threads: Thread.List,
1616
lib/std/Io/Kqueue.zig+1-1
......@@ -15,7 +15,7 @@ const posix = std.posix;
1515
1616/// Must be a thread-safe allocator.
1717gpa: Allocator,
18mutex: std.Thread.Mutex,
18mutex: Io.Mutex,
1919main_fiber_buffer: [@sizeOf(Fiber) + Fiber.max_result_size]u8 align(@alignOf(Fiber)),
2020threads: Thread.List,
2121
lib/std/Io/Threaded.zig+34-5
......@@ -67,6 +67,9 @@ stderr_writer: File.Writer = .{
6767},
6868stderr_mode: Io.Terminal.Mode = .no_color,
6969stderr_writer_initialized: bool = false,
70stderr_mutex: Io.Mutex = .init,
71stderr_mutex_locker: std.Thread.Id = Thread.invalid_id,
72stderr_mutex_lock_count: usize = 0,
7073
7174argv0: Argv0,
7275environ: Environ,
......@@ -689,6 +692,13 @@ const Thread = struct {
689692
690693 threadlocal var current: ?*Thread = null;
691694
695 /// A value that does not alias any other thread id.
696 const invalid_id: std.Thread.Id = std.math.maxInt(std.Thread.Id);
697
698 fn currentId() std.Thread.Id {
699 return if (current) |t| t.id else std.Thread.getCurrentId();
700 }
701
692702 /// The thread is neither in a syscall nor entering one, but we want to check for cancelation
693703 /// anyway. If there is a pending cancel request, acknowledge it and return `error.Canceled`.
694704 fn checkCancel() Io.Cancelable!void {
......@@ -13502,15 +13512,29 @@ fn netLookupFallible(
1350213512
1350313513fn lockStderr(userdata: ?*anyopaque, terminal_mode: ?Io.Terminal.Mode) Io.Cancelable!Io.LockedStderr {
1350413514 const t: *Threaded = @ptrCast(@alignCast(userdata));
13505 // Only global mutex since this is Threaded.
13506 process.stderr_thread_mutex.lock();
13515 const current_thread_id = Thread.currentId();
13516
13517 if (@atomicLoad(std.Thread.Id, &t.stderr_mutex_locker, .unordered) != current_thread_id) {
13518 mutexLock(&t.stderr_mutex);
13519 assert(t.stderr_mutex_lock_count == 0);
13520 @atomicStore(std.Thread.Id, &t.stderr_mutex_locker, current_thread_id, .unordered);
13521 }
13522 t.stderr_mutex_lock_count += 1;
13523
1350713524 return initLockedStderr(t, terminal_mode);
1350813525}
1350913526
1351013527fn tryLockStderr(userdata: ?*anyopaque, terminal_mode: ?Io.Terminal.Mode) Io.Cancelable!?Io.LockedStderr {
1351113528 const t: *Threaded = @ptrCast(@alignCast(userdata));
13512 // Only global mutex since this is Threaded.
13513 if (!process.stderr_thread_mutex.tryLock()) return null;
13529 const current_thread_id = Thread.currentId();
13530
13531 if (@atomicLoad(std.Thread.Id, &t.stderr_mutex_locker, .unordered) != current_thread_id) {
13532 if (!t.stderr_mutex.tryLock()) return null;
13533 assert(t.stderr_mutex_lock_count == 0);
13534 @atomicStore(std.Thread.Id, &t.stderr_mutex_locker, current_thread_id, .unordered);
13535 }
13536 t.stderr_mutex_lock_count += 1;
13537
1351413538 return try initLockedStderr(t, terminal_mode);
1351513539}
1351613540
......@@ -13541,7 +13565,12 @@ fn unlockStderr(userdata: ?*anyopaque) void {
1354113565 };
1354213566 t.stderr_writer.interface.end = 0;
1354313567 t.stderr_writer.interface.buffer = &.{};
13544 process.stderr_thread_mutex.unlock();
13568
13569 t.stderr_mutex_lock_count -= 1;
13570 if (t.stderr_mutex_lock_count == 0) {
13571 @atomicStore(std.Thread.Id, &t.stderr_mutex_locker, Thread.invalid_id, .unordered);
13572 mutexUnlock(&t.stderr_mutex);
13573 }
1354513574}
1354613575
1354713576fn processCurrentPath(userdata: ?*anyopaque, buffer: []u8) process.CurrentPathError!usize {
lib/std/Thread.zig+2-12
......@@ -1,6 +1,5 @@
1//! This struct represents a kernel thread, and acts as a namespace for
2//! concurrency primitives that operate on kernel threads. For concurrency
3//! primitives that interact with the I/O interface, see `std.Io`.
1//! This struct represents a kernel thread.
2const Thread = @This();
43
54const builtin = @import("builtin");
65const target = builtin.target;
......@@ -14,13 +13,8 @@ const posix = std.posix;
1413const windows = std.os.windows;
1514const testing = std.testing;
1615
17pub const Mutex = struct {
18 pub const Recursive = @import("Thread/Mutex/Recursive.zig");
19};
20
2116pub const use_pthreads = native_os != .windows and native_os != .wasi and builtin.link_libc;
2217
23const Thread = @This();
2418const Impl = if (native_os == .windows)
2519 WindowsThreadImpl
2620else if (use_pthreads)
......@@ -1604,10 +1598,6 @@ test "setName, getName" {
16041598 thread.join();
16051599}
16061600
1607test {
1608 _ = Mutex;
1609}
1610
16111601fn testIncrementNotify(io: Io, value: *usize, event: *Io.Event) void {
16121602 value.* += 1;
16131603 event.set(io);
lib/std/Thread/Mutex/Recursive.zig deleted-72
......@@ -1,72 +0,0 @@
1//! A synchronization primitive enforcing atomic access to a shared region of
2//! code known as the "critical section".
3//!
4//! Equivalent to `std.Mutex` except it allows the same thread to obtain the
5//! lock multiple times.
6//!
7//! A recursive mutex is an abstraction layer on top of a regular mutex;
8//! therefore it is recommended to use instead `std.Mutex` unless there is a
9//! specific reason a recursive mutex is warranted.
10const Recursive = @This();
11
12const std = @import("../../std.zig");
13const Io = std.Io;
14const assert = std.debug.assert;
15
16mutex: Io.Mutex,
17thread_id: std.Thread.Id,
18lock_count: usize,
19
20pub const init: Recursive = .{
21 .mutex = .init,
22 .thread_id = invalid_thread_id,
23 .lock_count = 0,
24};
25
26/// Acquires the `Mutex` without blocking the caller's thread.
27///
28/// Returns `false` if the calling thread would have to block to acquire it.
29///
30/// Otherwise, returns `true` and the caller should `unlock()` the Mutex to release it.
31pub fn tryLock(r: *Recursive) bool {
32 const current_thread_id = std.Thread.getCurrentId();
33 if (@atomicLoad(std.Thread.Id, &r.thread_id, .unordered) != current_thread_id) {
34 if (!r.mutex.tryLock()) return false;
35 assert(r.lock_count == 0);
36 @atomicStore(std.Thread.Id, &r.thread_id, current_thread_id, .unordered);
37 }
38 r.lock_count += 1;
39 return true;
40}
41
42/// Acquires the `Mutex`, blocking the current thread while the mutex is
43/// already held by another thread.
44///
45/// The `Mutex` can be held multiple times by the same thread.
46///
47/// Once acquired, call `unlock` on the `Mutex` to release it, regardless
48/// of whether the lock was already held by the same thread.
49pub fn lock(r: *Recursive) void {
50 const current_thread_id = std.Thread.getCurrentId();
51 if (@atomicLoad(std.Thread.Id, &r.thread_id, .unordered) != current_thread_id) {
52 Io.Threaded.mutexLock(&r.mutex);
53 assert(r.lock_count == 0);
54 @atomicStore(std.Thread.Id, &r.thread_id, current_thread_id, .unordered);
55 }
56 r.lock_count += 1;
57}
58
59/// Releases the `Mutex` which was previously acquired with `lock` or `tryLock`.
60///
61/// It is undefined behavior to unlock from a different thread that it was
62/// locked from.
63pub fn unlock(r: *Recursive) void {
64 r.lock_count -= 1;
65 if (r.lock_count == 0) {
66 @atomicStore(std.Thread.Id, &r.thread_id, invalid_thread_id, .unordered);
67 Io.Threaded.mutexUnlock(&r.mutex);
68 }
69}
70
71/// A value that does not alias any other thread id.
72const invalid_thread_id: std.Thread.Id = std.math.maxInt(std.Thread.Id);
lib/std/process.zig-7
......@@ -20,13 +20,6 @@ pub const Args = @import("process/Args.zig");
2020pub const Environ = @import("process/Environ.zig");
2121pub const Preopens = @import("process/Preopens.zig");
2222
23/// This is the global, process-wide protection to coordinate stderr writes.
24///
25/// The primary motivation for recursive mutex here is so that a panic while
26/// stderr mutex is held still dumps the stack trace and other debug
27/// information.
28pub var stderr_thread_mutex: std.Thread.Mutex.Recursive = .init;
29
3023/// A standard set of pre-initialized useful APIs for programs to take
3124/// advantage of. This is the type of the first parameter of the main function.
3225/// Applications wanting more flexibility can accept `Init.Minimal` instead.