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 {...@@ -69,19 +69,26 @@ pub const Id = if (InternPool.single_threaded) enum {
69 /// will likely involve significant changes to the `InternPool` implementation.69 /// will likely involve significant changes to the `InternPool` implementation.
70 var available_tids: std.ArrayList(Id) = .empty;70 var available_tids: std.ArrayList(Id) = .empty;
71 threadlocal var recursive_depth: usize = 0;71 threadlocal var recursive_depth: usize = 0;
72 threadlocal var recursive_tid: Id = .main;72 threadlocal var recursive_tid: Id = undefined;
7373
74 pub fn allocate(arena: Allocator, n: usize) Allocator.Error!void {74 pub fn allocate(arena: Allocator, n: usize) Allocator.Error!void {
75 assert(available_tids.items.len == 0);75 assert(available_tids.items.len == 0);
76 try available_tids.ensureTotalCapacityPrecise(arena, n - 1);76 try available_tids.ensureTotalCapacityPrecise(arena, n - 1);
77 for (1..n) |tid| available_tids.appendAssumeCapacity(@enumFromInt(tid));77 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 }
78 }86 }
79 pub fn acquire(io: std.Io) Id {87 pub fn acquire(io: std.Io) Id {
80 switch (build_options.io_mode) {88 switch (build_options.io_mode) {
81 .threaded => {89 .threaded => {
82 recursive_depth += 1;90 recursive_depth += 1;
83 if (recursive_depth > 1) {91 if (recursive_depth > 1) {
84 assert(recursive_tid != .main);
85 return recursive_tid;92 return recursive_tid;
86 }93 }
87 },94 },
...@@ -106,7 +113,6 @@ pub const Id = if (InternPool.single_threaded) enum {...@@ -106,7 +113,6 @@ pub const Id = if (InternPool.single_threaded) enum {
106 assert(recursive_tid == tid);113 assert(recursive_tid == tid);
107 recursive_depth -= 1;114 recursive_depth -= 1;
108 if (recursive_depth > 0) return;115 if (recursive_depth > 0) return;
109 recursive_tid = .main;
110 },116 },
111 .evented => {},117 .evented => {},
112 }118 }