authorgravatar for pat.github@tullmann.orgPat Tullmann <pat.github@tullmann.org> 2025-08-05 17:14:59-07:00
committergravatar for pat.github@tullmann.orgPat Tullmann <pat.github@tullmann.org> 2025-09-09 22:06:20-07:00
log0edccc1079003e2c96f5c808fdaf00d465e6e004
tree54dd7254cab88f8b52bca27778829ae4411b835b
parentf63cd9194c81459d113a57dc7e8ce357f8029728

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.

2 files changed, 37 insertions(+), 65 deletions(-)

lib/std/Thread.zig+37
......@@ -1637,3 +1637,40 @@ test detach {
16371637 event.wait();
16381638 try std.testing.expectEqual(value, 1);
16391639}
1640
1641test "Thread.getCpuCount" {
1642 if (native_os == .wasi) return error.SkipZigTest;
1643
1644 const cpu_count = try Thread.getCpuCount();
1645 try std.testing.expect(cpu_count >= 1);
1646}
1647
1648fn testThreadIdFn(thread_id: *Thread.Id) void {
1649 thread_id.* = Thread.getCurrentId();
1650}
1651
1652test "Thread.getCurrentId" {
1653 if (builtin.single_threaded) return error.SkipZigTest;
1654
1655 var thread_current_id: Thread.Id = undefined;
1656 const thread = try Thread.spawn(.{}, testThreadIdFn, .{&thread_current_id});
1657 thread.join();
1658 try std.testing.expect(Thread.getCurrentId() != thread_current_id);
1659}
1660
1661test "thread local storage" {
1662 if (builtin.single_threaded) return error.SkipZigTest;
1663
1664 const thread1 = try Thread.spawn(.{}, testTls, .{});
1665 const thread2 = try Thread.spawn(.{}, testTls, .{});
1666 try testTls();
1667 thread1.join();
1668 thread2.join();
1669}
1670
1671threadlocal var x: i32 = 1234;
1672fn testTls() !void {
1673 if (x != 1234) return error.TlsBadStartValue;
1674 x += 1;
1675 if (x != 1235) return error.TlsBadEndValue;
1676}
lib/std/posix/test.zig-65
......@@ -7,7 +7,6 @@ const expectError = testing.expectError;
77const fs = std.fs;
88const mem = std.mem;
99const elf = std.elf;
10const Thread = std.Thread;
1110const linux = std.os.linux;
1211
1312const a = std.testing.allocator;
......@@ -446,70 +445,6 @@ test "readlinkat" {
446445 try expect(mem.eql(u8, "file.txt", read_link));
447446}
448447
449fn testThreadIdFn(thread_id: *Thread.Id) void {
450 thread_id.* = Thread.getCurrentId();
451}
452
453test "Thread.getCurrentId" {
454 if (builtin.single_threaded) return error.SkipZigTest;
455
456 var thread_current_id: Thread.Id = undefined;
457 const thread = try Thread.spawn(.{}, testThreadIdFn, .{&thread_current_id});
458 thread.join();
459 try expect(Thread.getCurrentId() != thread_current_id);
460}
461
462test "spawn threads" {
463 if (builtin.single_threaded) return error.SkipZigTest;
464
465 var shared_ctx: i32 = 1;
466
467 const thread1 = try Thread.spawn(.{}, start1, .{});
468 const thread2 = try Thread.spawn(.{}, start2, .{&shared_ctx});
469 const thread3 = try Thread.spawn(.{}, start2, .{&shared_ctx});
470 const thread4 = try Thread.spawn(.{}, start2, .{&shared_ctx});
471
472 thread1.join();
473 thread2.join();
474 thread3.join();
475 thread4.join();
476
477 try expect(shared_ctx == 4);
478}
479
480fn start1() u8 {
481 return 0;
482}
483
484fn start2(ctx: *i32) u8 {
485 _ = @atomicRmw(i32, ctx, AtomicRmwOp.Add, 1, AtomicOrder.seq_cst);
486 return 0;
487}
488
489test "cpu count" {
490 if (native_os == .wasi) return error.SkipZigTest;
491
492 const cpu_count = try Thread.getCpuCount();
493 try expect(cpu_count >= 1);
494}
495
496test "thread local storage" {
497 if (builtin.single_threaded) return error.SkipZigTest;
498
499 const thread1 = try Thread.spawn(.{}, testTls, .{});
500 const thread2 = try Thread.spawn(.{}, testTls, .{});
501 try testTls();
502 thread1.join();
503 thread2.join();
504}
505
506threadlocal var x: i32 = 1234;
507fn testTls() !void {
508 if (x != 1234) return error.TlsBadStartValue;
509 x += 1;
510 if (x != 1235) return error.TlsBadEndValue;
511}
512
513448test "getrandom" {
514449 var buf_a: [50]u8 = undefined;
515450 var buf_b: [50]u8 = undefined;