Commit 0edccc1079

Pat Tullmann <pat.github@tullmann.org>
2025-08-06 02:14:59
Move some Thread tests out of posix/test.zig into Thread.zig
These tests aren't (directly) using Posix APIs, so they don't need to be in posix/test.zig. Put them over with the code and tests in Thread.zig. Since the spawn/join test in the posix code was redundant, just dropped that one.
1 parent f63cd91
Changed files (2)
lib
lib/std/posix/test.zig
@@ -7,7 +7,6 @@ const expectError = testing.expectError;
 const fs = std.fs;
 const mem = std.mem;
 const elf = std.elf;
-const Thread = std.Thread;
 const linux = std.os.linux;
 
 const a = std.testing.allocator;
@@ -446,70 +445,6 @@ test "readlinkat" {
     try expect(mem.eql(u8, "file.txt", read_link));
 }
 
-fn testThreadIdFn(thread_id: *Thread.Id) void {
-    thread_id.* = Thread.getCurrentId();
-}
-
-test "Thread.getCurrentId" {
-    if (builtin.single_threaded) return error.SkipZigTest;
-
-    var thread_current_id: Thread.Id = undefined;
-    const thread = try Thread.spawn(.{}, testThreadIdFn, .{&thread_current_id});
-    thread.join();
-    try expect(Thread.getCurrentId() != thread_current_id);
-}
-
-test "spawn threads" {
-    if (builtin.single_threaded) return error.SkipZigTest;
-
-    var shared_ctx: i32 = 1;
-
-    const thread1 = try Thread.spawn(.{}, start1, .{});
-    const thread2 = try Thread.spawn(.{}, start2, .{&shared_ctx});
-    const thread3 = try Thread.spawn(.{}, start2, .{&shared_ctx});
-    const thread4 = try Thread.spawn(.{}, start2, .{&shared_ctx});
-
-    thread1.join();
-    thread2.join();
-    thread3.join();
-    thread4.join();
-
-    try expect(shared_ctx == 4);
-}
-
-fn start1() u8 {
-    return 0;
-}
-
-fn start2(ctx: *i32) u8 {
-    _ = @atomicRmw(i32, ctx, AtomicRmwOp.Add, 1, AtomicOrder.seq_cst);
-    return 0;
-}
-
-test "cpu count" {
-    if (native_os == .wasi) return error.SkipZigTest;
-
-    const cpu_count = try Thread.getCpuCount();
-    try expect(cpu_count >= 1);
-}
-
-test "thread local storage" {
-    if (builtin.single_threaded) return error.SkipZigTest;
-
-    const thread1 = try Thread.spawn(.{}, testTls, .{});
-    const thread2 = try Thread.spawn(.{}, testTls, .{});
-    try testTls();
-    thread1.join();
-    thread2.join();
-}
-
-threadlocal var x: i32 = 1234;
-fn testTls() !void {
-    if (x != 1234) return error.TlsBadStartValue;
-    x += 1;
-    if (x != 1235) return error.TlsBadEndValue;
-}
-
 test "getrandom" {
     var buf_a: [50]u8 = undefined;
     var buf_b: [50]u8 = undefined;
lib/std/Thread.zig
@@ -1637,3 +1637,40 @@ test detach {
     event.wait();
     try std.testing.expectEqual(value, 1);
 }
+
+test "Thread.getCpuCount" {
+    if (native_os == .wasi) return error.SkipZigTest;
+
+    const cpu_count = try Thread.getCpuCount();
+    try std.testing.expect(cpu_count >= 1);
+}
+
+fn testThreadIdFn(thread_id: *Thread.Id) void {
+    thread_id.* = Thread.getCurrentId();
+}
+
+test "Thread.getCurrentId" {
+    if (builtin.single_threaded) return error.SkipZigTest;
+
+    var thread_current_id: Thread.Id = undefined;
+    const thread = try Thread.spawn(.{}, testThreadIdFn, .{&thread_current_id});
+    thread.join();
+    try std.testing.expect(Thread.getCurrentId() != thread_current_id);
+}
+
+test "thread local storage" {
+    if (builtin.single_threaded) return error.SkipZigTest;
+
+    const thread1 = try Thread.spawn(.{}, testTls, .{});
+    const thread2 = try Thread.spawn(.{}, testTls, .{});
+    try testTls();
+    thread1.join();
+    thread2.join();
+}
+
+threadlocal var x: i32 = 1234;
+fn testTls() !void {
+    if (x != 1234) return error.TlsBadStartValue;
+    x += 1;
+    if (x != 1235) return error.TlsBadEndValue;
+}