commit 0edccc1079003e2c96f5c808fdaf00d465e6e004 (tree)
parent f63cd9194c81459d113a57dc7e8ce357f8029728
Author: Pat Tullmann <pat.github@tullmann.org>
Date: Tue, 5 Aug 2025 17:14:59 -0700
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.
Diffstat:
2 files changed, 37 insertions(+), 65 deletions(-)
diff --git a/lib/std/Thread.zig b/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;
+}
diff --git a/lib/std/posix/test.zig b/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;