authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-19 16:48:24+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-03-21 09:56:05+01:00
log36c390bf1484a9a549759663a2bbb9bbe82f6706
treec86e5c8f5074c7a95cdeca42e2f1172273c21a46
parentb4d134a0d2ee238cab70f1e967b2172edbec1b41

Zcu: correctly handle tid when main thread recurses

Resolves: https://codeberg.org/ziglang/zig/issues/31380

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

src/Zcu/PerThread.zig+9-3
......@@ -69,19 +69,26 @@ pub const Id = if (InternPool.single_threaded) enum {
6969 /// will likely involve significant changes to the `InternPool` implementation.
7070 var available_tids: std.ArrayList(Id) = .empty;
7171 threadlocal var recursive_depth: usize = 0;
72 threadlocal var recursive_tid: Id = .main;
72 threadlocal var recursive_tid: Id = undefined;
7373
7474 pub fn allocate(arena: Allocator, n: usize) Allocator.Error!void {
7575 assert(available_tids.items.len == 0);
7676 try available_tids.ensureTotalCapacityPrecise(arena, n - 1);
7777 for (1..n) |tid| available_tids.appendAssumeCapacity(@enumFromInt(tid));
78 switch (build_options.io_mode) {
79 .threaded => {
80 // Called from the main thread, so mark ourselves as such.
81 recursive_depth = 1;
82 recursive_tid = .main;
83 },
84 .evented => {},
85 }
7886 }
7987 pub fn acquire(io: std.Io) Id {
8088 switch (build_options.io_mode) {
8189 .threaded => {
8290 recursive_depth += 1;
8391 if (recursive_depth > 1) {
84 assert(recursive_tid != .main);
8592 return recursive_tid;
8693 }
8794 },
......@@ -106,7 +113,6 @@ pub const Id = if (InternPool.single_threaded) enum {
106113 assert(recursive_tid == tid);
107114 recursive_depth -= 1;
108115 if (recursive_depth > 0) return;
109 recursive_tid = .main;
110116 },
111117 .evented => {},
112118 }