authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-01 16:13:51-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-02 14:18:04-08:00
logeb74e23e7ba4f2000bbe4e908bf74c5c9dd7adcc
tree866527741a2cd1b8a707253e31b7f3b59dd2d32a
parentc2d4806d659abf8c4c0ab989eae225303de57af3

std.Io.Threaded: sever dependency on std.Thread Mutex and Condition


1 files changed, 186 insertions(+), 67 deletions(-)

lib/std/Io/Threaded.zig+186-67
......@@ -29,8 +29,8 @@ const ws2_32 = std.os.windows.ws2_32;
2929/// * scanning environment variables on some targets
3030/// * memory-mapping when mmap or equivalent is not available
3131allocator: Allocator,
32mutex: std.Thread.Mutex = .{},
33cond: std.Thread.Condition = .{},
32mutex: Io.Mutex = .init,
33cond: Io.Condition = .init,
3434run_queue: std.SinglyLinkedList = .{},
3535join_requested: bool = false,
3636stack_size: usize,
......@@ -1486,8 +1486,8 @@ var global_single_threaded_instance: Threaded = .init_single_threaded;
14861486pub const global_single_threaded: *Threaded = &global_single_threaded_instance;
14871487
14881488pub fn setAsyncLimit(t: *Threaded, new_limit: Io.Limit) void {
1489 t.mutex.lock();
1490 defer t.mutex.unlock();
1489 mutexLockUncancelable(&t.mutex);
1490 defer mutexUnlock(&t.mutex);
14911491 t.async_limit = new_limit;
14921492}
14931493
......@@ -1508,11 +1508,11 @@ pub fn deinit(t: *Threaded) void {
15081508fn join(t: *Threaded) void {
15091509 if (builtin.single_threaded) return;
15101510 {
1511 t.mutex.lock();
1512 defer t.mutex.unlock();
1511 mutexLockUncancelable(&t.mutex);
1512 defer mutexUnlock(&t.mutex);
15131513 t.join_requested = true;
15141514 }
1515 t.cond.broadcast();
1515 condBroadcast(&t.cond);
15161516 t.wait_group.wait();
15171517}
15181518
......@@ -1574,20 +1574,20 @@ fn worker(t: *Threaded) void {
15741574
15751575 defer t.wait_group.finish();
15761576
1577 t.mutex.lock();
1578 defer t.mutex.unlock();
1577 mutexLockUncancelable(&t.mutex);
1578 defer mutexUnlock(&t.mutex);
15791579
15801580 while (true) {
15811581 while (t.run_queue.popFirst()) |runnable_node| {
1582 t.mutex.unlock();
1582 mutexUnlock(&t.mutex);
15831583 thread.cancel_protection = .unblocked;
15841584 const runnable: *Runnable = @fieldParentPtr("node", runnable_node);
15851585 runnable.startFn(runnable, &thread, t);
1586 t.mutex.lock();
1586 mutexLockUncancelable(&t.mutex);
15871587 t.busy_count -= 1;
15881588 }
15891589 if (t.join_requested) break;
1590 t.cond.wait(&t.mutex);
1590 condWait(&t.cond, &t.mutex);
15911591 }
15921592}
15931593
......@@ -2004,12 +2004,12 @@ fn async(
20042004 },
20052005 };
20062006
2007 t.mutex.lock();
2007 mutexLockUncancelable(&t.mutex);
20082008
20092009 const busy_count = t.busy_count;
20102010
20112011 if (busy_count >= @intFromEnum(t.async_limit)) {
2012 t.mutex.unlock();
2012 mutexUnlock(&t.mutex);
20132013 future.destroy(gpa);
20142014 start(context.ptr, result.ptr);
20152015 return null;
......@@ -2023,7 +2023,7 @@ fn async(
20232023 const thread = std.Thread.spawn(.{ .stack_size = t.stack_size }, worker, .{t}) catch {
20242024 t.wait_group.finish();
20252025 t.busy_count = busy_count;
2026 t.mutex.unlock();
2026 mutexUnlock(&t.mutex);
20272027 future.destroy(gpa);
20282028 start(context.ptr, result.ptr);
20292029 return null;
......@@ -2033,8 +2033,8 @@ fn async(
20332033
20342034 t.run_queue.prepend(&future.runnable.node);
20352035
2036 t.mutex.unlock();
2037 t.cond.signal();
2036 mutexUnlock(&t.mutex);
2037 condSignal(&t.cond);
20382038 return @ptrCast(future);
20392039}
20402040
......@@ -2056,8 +2056,8 @@ fn concurrent(
20562056 };
20572057 errdefer future.destroy(gpa);
20582058
2059 t.mutex.lock();
2060 defer t.mutex.unlock();
2059 mutexLockUncancelable(&t.mutex);
2060 defer mutexUnlock(&t.mutex);
20612061
20622062 const busy_count = t.busy_count;
20632063
......@@ -2080,7 +2080,7 @@ fn concurrent(
20802080
20812081 t.run_queue.prepend(&future.runnable.node);
20822082
2083 t.cond.signal();
2083 condSignal(&t.cond);
20842084 return @ptrCast(future);
20852085}
20862086
......@@ -2101,12 +2101,12 @@ fn groupAsync(
21012101 error.OutOfMemory => return groupAsyncEager(start, context.ptr),
21022102 };
21032103
2104 t.mutex.lock();
2104 mutexLockUncancelable(&t.mutex);
21052105
21062106 const busy_count = t.busy_count;
21072107
21082108 if (busy_count >= @intFromEnum(t.async_limit)) {
2109 t.mutex.unlock();
2109 mutexUnlock(&t.mutex);
21102110 task.destroy(gpa);
21112111 return groupAsyncEager(start, context.ptr);
21122112 }
......@@ -2119,7 +2119,7 @@ fn groupAsync(
21192119 const thread = std.Thread.spawn(.{ .stack_size = t.stack_size }, worker, .{t}) catch {
21202120 t.wait_group.finish();
21212121 t.busy_count = busy_count;
2122 t.mutex.unlock();
2122 mutexUnlock(&t.mutex);
21232123 task.destroy(gpa);
21242124 return groupAsyncEager(start, context.ptr);
21252125 };
......@@ -2136,8 +2136,8 @@ fn groupAsync(
21362136 }, .monotonic);
21372137 t.run_queue.prepend(&task.runnable.node);
21382138
2139 t.mutex.unlock();
2140 t.cond.signal();
2139 mutexUnlock(&t.mutex);
2140 condSignal(&t.cond);
21412141}
21422142fn groupAsyncEager(
21432143 start: *const fn (context: *const anyopaque) Io.Cancelable!void,
......@@ -2201,8 +2201,8 @@ fn groupConcurrent(
22012201 };
22022202 errdefer task.destroy(gpa);
22032203
2204 t.mutex.lock();
2205 defer t.mutex.unlock();
2204 mutexLockUncancelable(&t.mutex);
2205 defer mutexUnlock(&t.mutex);
22062206
22072207 const busy_count = t.busy_count;
22082208
......@@ -2233,7 +2233,7 @@ fn groupConcurrent(
22332233 }, .monotonic);
22342234 t.run_queue.prepend(&task.runnable.node);
22352235
2236 t.cond.signal();
2236 condSignal(&t.cond);
22372237}
22382238
22392239fn groupAwait(userdata: ?*anyopaque, type_erased: *Io.Group, initial_token: *anyopaque) Io.Cancelable!void {
......@@ -3838,8 +3838,8 @@ fn fileStatWindows(userdata: ?*anyopaque, file: File) File.StatError!File.Stat {
38383838
38393839fn systemBasicInformation(t: *Threaded) ?*const windows.SYSTEM_BASIC_INFORMATION {
38403840 if (!t.system_basic_information.initialized.load(.acquire)) {
3841 t.mutex.lock();
3842 defer t.mutex.unlock();
3841 mutexLockUncancelable(&t.mutex);
3842 defer mutexUnlock(&t.mutex);
38433843
38443844 switch (windows.ntdll.NtQuerySystemInformation(
38453845 .SystemBasicInformation,
......@@ -14299,10 +14299,9 @@ const Wsa = struct {
1429914299};
1430014300
1430114301fn initializeWsa(t: *Threaded) error{ NetworkDown, Canceled }!void {
14302 const t_io = io(t);
1430314302 const wsa = &t.wsa;
14304 try wsa.mutex.lock(t_io);
14305 defer wsa.mutex.unlock(t_io);
14303 try mutexLock(&wsa.mutex);
14304 defer mutexUnlock(&wsa.mutex);
1430614305 switch (wsa.status) {
1430714306 .uninitialized => {
1430814307 var wsa_data: ws2_32.WSADATA = undefined;
......@@ -14373,8 +14372,8 @@ const WindowsEnvironStrings = struct {
1437314372};
1437414373
1437514374fn scanEnviron(t: *Threaded) void {
14376 t.mutex.lock();
14377 defer t.mutex.unlock();
14375 mutexLockUncancelable(&t.mutex);
14376 defer mutexUnlock(&t.mutex);
1437814377
1437914378 if (t.environ.initialized) return;
1438014379 t.environ.initialized = true;
......@@ -14729,8 +14728,8 @@ fn spawnPosix(t: *Threaded, options: process.SpawnOptions) process.SpawnError!Sp
1472914728
1473014729fn getDevNullFd(t: *Threaded) !posix.fd_t {
1473114730 {
14732 t.mutex.lock();
14733 defer t.mutex.unlock();
14731 mutexLockUncancelable(&t.mutex);
14732 defer mutexUnlock(&t.mutex);
1473414733 if (t.null_file.fd != -1) return t.null_file.fd;
1473514734 }
1473614735 const mode: u32 = 0;
......@@ -14741,8 +14740,8 @@ fn getDevNullFd(t: *Threaded) !posix.fd_t {
1474114740 .SUCCESS => {
1474214741 syscall.finish();
1474314742 const fresh_fd: posix.fd_t = @intCast(rc);
14744 t.mutex.lock(); // Another thread might have won the race.
14745 defer t.mutex.unlock();
14743 mutexLockUncancelable(&t.mutex); // Another thread might have won the race.
14744 defer mutexUnlock(&t.mutex);
1474614745 if (t.null_file.fd != -1) {
1474714746 posix.close(fresh_fd);
1474814747 return t.null_file.fd;
......@@ -15402,8 +15401,8 @@ fn processSpawnWindows(userdata: ?*anyopaque, options: process.SpawnOptions) pro
1540215401
1540315402fn getCngHandle(t: *Threaded) Io.RandomSecureError!windows.HANDLE {
1540415403 {
15405 t.mutex.lock();
15406 defer t.mutex.unlock();
15404 mutexLockUncancelable(&t.mutex);
15405 defer mutexUnlock(&t.mutex);
1540715406 if (t.random_file.handle) |handle| return handle;
1540815407 }
1540915408
......@@ -15437,8 +15436,8 @@ fn getCngHandle(t: *Threaded) Io.RandomSecureError!windows.HANDLE {
1543715436 )) {
1543815437 .SUCCESS => {
1543915438 syscall.finish();
15440 t.mutex.lock(); // Another thread might have won the race.
15441 defer t.mutex.unlock();
15439 mutexLockUncancelable(&t.mutex); // Another thread might have won the race.
15440 defer mutexUnlock(&t.mutex);
1544215441 if (t.random_file.handle) |prev_handle| {
1544315442 windows.CloseHandle(fresh_handle);
1544415443 return prev_handle;
......@@ -15458,8 +15457,8 @@ fn getCngHandle(t: *Threaded) Io.RandomSecureError!windows.HANDLE {
1545815457
1545915458fn getNulHandle(t: *Threaded) !windows.HANDLE {
1546015459 {
15461 t.mutex.lock();
15462 defer t.mutex.unlock();
15460 mutexLockUncancelable(&t.mutex);
15461 defer mutexUnlock(&t.mutex);
1546315462 if (t.null_file.handle) |handle| return handle;
1546415463 }
1546515464
......@@ -15505,8 +15504,8 @@ fn getNulHandle(t: *Threaded) !windows.HANDLE {
1550515504 )) {
1550615505 .SUCCESS => {
1550715506 syscall.finish();
15508 t.mutex.lock(); // Another thread might have won the race.
15509 defer t.mutex.unlock();
15507 mutexLockUncancelable(&t.mutex); // Another thread might have won the race.
15508 defer mutexUnlock(&t.mutex);
1551015509 if (t.null_file.handle) |prev_handle| {
1551115510 windows.CloseHandle(fresh_handle);
1551215511 return prev_handle;
......@@ -16551,15 +16550,15 @@ fn random(userdata: ?*anyopaque, buffer: []u8) void {
1655116550}
1655216551
1655316552fn randomMainThread(t: *Threaded, buffer: []u8) void {
16554 t.mutex.lock();
16555 defer t.mutex.unlock();
16553 mutexLockUncancelable(&t.mutex);
16554 defer mutexUnlock(&t.mutex);
1655616555
1655716556 if (!t.csprng.isInitialized()) {
1655816557 @branchHint(.unlikely);
1655916558 var seed: [Csprng.seed_len]u8 = undefined;
1656016559 {
16561 t.mutex.unlock();
16562 defer t.mutex.lock();
16560 mutexUnlock(&t.mutex);
16561 defer mutexLockUncancelable(&t.mutex);
1656316562
1656416563 const prev = swapCancelProtection(t, .blocked);
1656516564 defer _ = swapCancelProtection(t, prev);
......@@ -16744,8 +16743,8 @@ fn randomSecure(userdata: ?*anyopaque, buffer: []u8) Io.RandomSecureError!void {
1674416743
1674516744fn getRandomFd(t: *Threaded) Io.RandomSecureError!posix.fd_t {
1674616745 {
16747 t.mutex.lock();
16748 defer t.mutex.unlock();
16746 mutexLockUncancelable(&t.mutex);
16747 defer mutexUnlock(&t.mutex);
1674916748
1675016749 if (t.random_file.fd == -2) return error.EntropyUnavailable;
1675116750 if (t.random_file.fd != -1) return t.random_file.fd;
......@@ -16785,8 +16784,8 @@ fn getRandomFd(t: *Threaded) Io.RandomSecureError!posix.fd_t {
1678516784 .SUCCESS => {
1678616785 syscall.finish();
1678716786 if (!statx.mask.TYPE) return error.EntropyUnavailable;
16788 t.mutex.lock(); // Another thread might have won the race.
16789 defer t.mutex.unlock();
16787 mutexLockUncancelable(&t.mutex); // Another thread might have won the race.
16788 defer mutexUnlock(&t.mutex);
1679016789 if (t.random_file.fd >= 0) {
1679116790 posix.close(fd);
1679216791 return t.random_file.fd;
......@@ -16813,8 +16812,8 @@ fn getRandomFd(t: *Threaded) Io.RandomSecureError!posix.fd_t {
1681316812 switch (posix.errno(fstat_sym(fd, &stat))) {
1681416813 .SUCCESS => {
1681516814 syscall.finish();
16816 t.mutex.lock(); // Another thread might have won the race.
16817 defer t.mutex.unlock();
16815 mutexLockUncancelable(&t.mutex); // Another thread might have won the race.
16816 defer mutexUnlock(&t.mutex);
1681816817 if (t.random_file.fd >= 0) {
1681916818 posix.close(fd);
1682016819 return t.random_file.fd;
......@@ -16878,13 +16877,13 @@ const parking_futex = struct {
1687816877 /// avoid a race.
1687916878 num_waiters: std.atomic.Value(u32),
1688016879 /// Protects `waiters`.
16881 mutex: std.Thread.Mutex,
16880 mutex: Io.Mutex,
1688216881 waiters: std.DoublyLinkedList,
1688316882
1688416883 /// Prevent false sharing between buckets.
1688516884 _: void align(std.atomic.cache_line) = {},
1688616885
16887 const init: Bucket = .{ .num_waiters = .init(0), .mutex = .{}, .waiters = .{} };
16886 const init: Bucket = .{ .num_waiters = .init(0), .mutex = .init, .waiters = .{} };
1688816887 };
1688916888
1689016889 const Waiter = struct {
......@@ -16947,8 +16946,8 @@ const parking_futex = struct {
1694716946 var status_buf: std.atomic.Value(Thread.Status) = undefined;
1694816947
1694916948 {
16950 bucket.mutex.lock();
16951 defer bucket.mutex.unlock();
16949 mutexLockUncancelable(&bucket.mutex);
16950 defer mutexUnlock(&bucket.mutex);
1695216951
1695316952 _ = bucket.num_waiters.fetchAdd(1, .acquire);
1695416953
......@@ -17017,8 +17016,8 @@ const parking_futex = struct {
1701717016 .parked => {
1701817017 // We saw a timeout and updated our own status from `.parked` to `.none`. It is
1701917018 // our responsibility to remove `waiter` from `bucket`.
17020 bucket.mutex.lock();
17021 defer bucket.mutex.unlock();
17019 mutexLockUncancelable(&bucket.mutex);
17020 defer mutexUnlock(&bucket.mutex);
1702217021 bucket.waiters.remove(&waiter.node);
1702317022 assert(bucket.num_waiters.fetchSub(1, .monotonic) > 0);
1702417023 },
......@@ -17057,8 +17056,8 @@ const parking_futex = struct {
1705717056 // of the critical section. This forms a singly-linked list of waiters using `Waiter.node.next`.
1705817057 var waking_head: ?*std.DoublyLinkedList.Node = null;
1705917058 {
17060 bucket.mutex.lock();
17061 defer bucket.mutex.unlock();
17059 mutexLockUncancelable(&bucket.mutex);
17060 defer mutexUnlock(&bucket.mutex);
1706217061
1706317062 var num_removed: u32 = 0;
1706417063 var it = bucket.waiters.first;
......@@ -17113,8 +17112,8 @@ const parking_futex = struct {
1711317112
1711417113 fn removeCanceledWaiter(waiter: *Waiter) void {
1711517114 const bucket = bucketForAddress(waiter.address);
17116 bucket.mutex.lock();
17117 defer bucket.mutex.unlock();
17115 mutexLockUncancelable(&bucket.mutex);
17116 defer mutexUnlock(&bucket.mutex);
1711817117 bucket.waiters.remove(&waiter.node);
1711917118 assert(bucket.num_waiters.fetchSub(1, .monotonic) > 0);
1712017119 waiter.done.store(true, .release); // potentially invalidates `waiter.*`
......@@ -18102,3 +18101,123 @@ fn eventSet(event: *Io.Event) void {
1810218101 .waiting => Thread.futexWake(@ptrCast(event), std.math.maxInt(u32)),
1810318102 }
1810418103}
18104
18105/// Same as `Io.Condition.broadcast` but avoids the VTable.
18106fn condBroadcast(cond: *Io.Condition) void {
18107 var prev_state = cond.state.load(.monotonic);
18108 while (prev_state.waiters > prev_state.signals) {
18109 @branchHint(.unlikely);
18110 prev_state = cond.state.cmpxchgWeak(prev_state, .{
18111 .waiters = prev_state.waiters,
18112 .signals = prev_state.waiters,
18113 }, .release, .monotonic) orelse {
18114 // Update the epoch to tell the waiting threads that there are new signals for them.
18115 // Note that a waiting thread could miss a take if *exactly* (1<<32)-1 wakes happen
18116 // between it observing the epoch and sleeping on it, but this is extraordinarily
18117 // unlikely due to the precise number of calls required.
18118 _ = cond.epoch.fetchAdd(1, .release); // `.release` to ensure ordered after `state` update
18119 Thread.futexWake(&cond.epoch.raw, prev_state.waiters - prev_state.signals);
18120 return;
18121 };
18122 }
18123}
18124
18125/// Same as `Io.Condition.signal` but avoids the VTable.
18126fn condSignal(cond: *Io.Condition) void {
18127 var prev_state = cond.state.load(.monotonic);
18128 while (prev_state.waiters > prev_state.signals) {
18129 @branchHint(.unlikely);
18130 prev_state = cond.state.cmpxchgWeak(prev_state, .{
18131 .waiters = prev_state.waiters,
18132 .signals = prev_state.signals + 1,
18133 }, .release, .monotonic) orelse {
18134 // Update the epoch to tell the waiting threads that there are new signals for them.
18135 // Note that a waiting thread could miss a take if *exactly* (1<<32)-1 wakes happen
18136 // between it observing the epoch and sleeping on it, but this is extraordinarily
18137 // unlikely due to the precise number of calls required.
18138 _ = cond.epoch.fetchAdd(1, .release); // `.release` to ensure ordered after `state` update
18139 Thread.futexWake(&cond.epoch.raw, 1);
18140 return;
18141 };
18142 }
18143}
18144
18145/// Same as `Io.Condition.waitUncancelable` but avoids the VTable.
18146fn condWait(cond: *Io.Condition, mutex: *Io.Mutex) void {
18147 var epoch = cond.epoch.load(.acquire); // `.acquire` to ensure ordered before state load
18148
18149 {
18150 const prev_state = cond.state.fetchAdd(.{ .waiters = 1, .signals = 0 }, .monotonic);
18151 assert(prev_state.waiters < std.math.maxInt(u16)); // overflow caused by too many waiters
18152 }
18153
18154 mutexUnlock(mutex);
18155 defer mutexLockUncancelable(mutex);
18156
18157 while (true) {
18158 Thread.futexWaitUncancelable(&cond.epoch.raw, epoch, null);
18159
18160 epoch = cond.epoch.load(.acquire); // `.acquire` to ensure ordered before `state` laod
18161
18162 var prev_state = cond.state.load(.monotonic);
18163 while (prev_state.signals > 0) {
18164 prev_state = cond.state.cmpxchgWeak(prev_state, .{
18165 .waiters = prev_state.waiters - 1,
18166 .signals = prev_state.signals - 1,
18167 }, .acquire, .monotonic) orelse {
18168 // We successfully consumed a signal.
18169 return;
18170 };
18171 }
18172 }
18173}
18174
18175/// Same as `Io.Mutex.lockUncancelable` but avoids the VTable.
18176fn mutexLock(m: *Io.Mutex) Io.Cancelable!void {
18177 const initial_state = m.state.cmpxchgWeak(
18178 .unlocked,
18179 .locked_once,
18180 .acquire,
18181 .monotonic,
18182 ) orelse {
18183 @branchHint(.likely);
18184 return;
18185 };
18186 if (initial_state == .contended) {
18187 try Thread.futexWait(@ptrCast(&m.state.raw), @intFromEnum(Io.Mutex.State.contended), null);
18188 }
18189 while (m.state.swap(.contended, .acquire) != .unlocked) {
18190 try Thread.futexWait(@ptrCast(&m.state.raw), @intFromEnum(Io.Mutex.State.contended), null);
18191 }
18192}
18193
18194/// Same as `Io.Mutex.lockUncancelable` but avoids the VTable.
18195fn mutexLockUncancelable(m: *Io.Mutex) void {
18196 const initial_state = m.state.cmpxchgWeak(
18197 .unlocked,
18198 .locked_once,
18199 .acquire,
18200 .monotonic,
18201 ) orelse {
18202 @branchHint(.likely);
18203 return;
18204 };
18205 if (initial_state == .contended) {
18206 Thread.futexWaitUncancelable(@ptrCast(&m.state.raw), @intFromEnum(Io.Mutex.State.contended), null);
18207 }
18208 while (m.state.swap(.contended, .acquire) != .unlocked) {
18209 Thread.futexWaitUncancelable(@ptrCast(&m.state.raw), @intFromEnum(Io.Mutex.State.contended), null);
18210 }
18211}
18212
18213/// Same as `Io.Mutex.unlock` but avoids the VTable.
18214fn mutexUnlock(m: *Io.Mutex) void {
18215 switch (m.state.swap(.unlocked, .release)) {
18216 .unlocked => unreachable,
18217 .locked_once => {},
18218 .contended => {
18219 @branchHint(.unlikely);
18220 Thread.futexWake(@ptrCast(&m.state.raw), 1);
18221 },
18222 }
18223}