authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-20 10:52:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-21 05:37:51+02:00
logf97c91ddb5ef600fce294c1bbd28e3f6f2185e5e
tree61bfcaf1af398c32314632975f1b850366aa1a6f
parente4abdf5a133ac4822644da1dabd5437ac751d78d

std.Thread: don't spin


1 files changed, 2 insertions(+), 21 deletions(-)

lib/std/Thread.zig+2-21
...@@ -911,18 +911,9 @@ const WasiThreadImpl = struct {...@@ -911,18 +911,9 @@ const WasiThreadImpl = struct {
911 allocator.free(self.thread.memory);911 allocator.free(self.thread.memory);
912 }912 }
913913
914 var spin: u8 = 10;
915 while (true) {914 while (true) {
916 const tid = self.thread.tid.load(.seq_cst);915 const tid = self.thread.tid.load(.seq_cst);
917 if (tid == 0) {916 if (tid == 0) break;
918 break;
919 }
920
921 if (spin > 0) {
922 spin -= 1;
923 std.atomic.spinLoopHint();
924 continue;
925 }
926917
927 const result = asm (918 const result = asm (
928 \\ local.get %[ptr]919 \\ local.get %[ptr]
...@@ -1514,18 +1505,9 @@ const LinuxThreadImpl = struct {...@@ -1514,18 +1505,9 @@ const LinuxThreadImpl = struct {
1514 fn join(self: Impl) void {1505 fn join(self: Impl) void {
1515 defer posix.munmap(self.thread.mapped);1506 defer posix.munmap(self.thread.mapped);
15161507
1517 var spin: u8 = 10;
1518 while (true) {1508 while (true) {
1519 const tid = self.thread.child_tid.load(.seq_cst);1509 const tid = self.thread.child_tid.load(.seq_cst);
1520 if (tid == 0) {1510 if (tid == 0) break;
1521 break;
1522 }
1523
1524 if (spin > 0) {
1525 spin -= 1;
1526 std.atomic.spinLoopHint();
1527 continue;
1528 }
15291511
1530 switch (linux.E.init(linux.futex_4arg(1512 switch (linux.E.init(linux.futex_4arg(
1531 &self.thread.child_tid.raw,1513 &self.thread.child_tid.raw,
...@@ -1616,7 +1598,6 @@ test "setName, getName" {...@@ -1616,7 +1598,6 @@ test "setName, getName" {
1616}1598}
16171599
1618test {1600test {
1619 // Doesn't use testing.refAllDecls() since that would pull in the compileError spinLoopHint.
1620 _ = Futex;1601 _ = Futex;
1621 _ = ResetEvent;1602 _ = ResetEvent;
1622 _ = Mutex;1603 _ = Mutex;