authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-08 21:00:36+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-08 21:00:36+01:00
logb0570b807ff24a41d5080fe090c4e12f423dd78d
tree886b889ab8d1c12093299afa2f2dc5648a21ba39
parent1face9ad781f3e162c709da0cab38391707b9696
parent130fc7ef751e665de14b440a3b5a3ee75d2312e0

Merge pull request 'std.Io.Threaded: clock_nanosleep is not linux-only' (#30746) from clock_nanosleep into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30746

2 files changed, 25 insertions(+), 13 deletions(-)

lib/std/Io/Threaded.zig+6-8
...@@ -9664,14 +9664,12 @@ fn nowWasi(clock: Io.Clock) Io.Clock.Error!Io.Timestamp {...@@ -9664,14 +9664,12 @@ fn nowWasi(clock: Io.Clock) Io.Clock.Error!Io.Timestamp {
9664fn sleep(userdata: ?*anyopaque, timeout: Io.Timeout) Io.SleepError!void {9664fn sleep(userdata: ?*anyopaque, timeout: Io.Timeout) Io.SleepError!void {
9665 const t: *Threaded = @ptrCast(@alignCast(userdata));9665 const t: *Threaded = @ptrCast(@alignCast(userdata));
9666 if (use_parking_sleep) return parking_sleep.sleep(timeout);9666 if (use_parking_sleep) return parking_sleep.sleep(timeout);
9667 switch (native_os) {9667 if (native_os == .wasi) return sleepWasi(t, timeout);
9668 .wasi => return sleepWasi(t, timeout),9668 if (@TypeOf(posix.system.clock_nanosleep) != void) return sleepPosix(timeout);
9669 .linux => return sleepLinux(timeout),9669 return sleepNanosleep(t, timeout);
9670 else => return sleepPosix(t, timeout),
9671 }
9672}9670}
96739671
9674fn sleepLinux(timeout: Io.Timeout) Io.SleepError!void {9672fn sleepPosix(timeout: Io.Timeout) Io.SleepError!void {
9675 const clock_id: posix.clockid_t = clockToPosix(switch (timeout) {9673 const clock_id: posix.clockid_t = clockToPosix(switch (timeout) {
9676 .none => .awake,9674 .none => .awake,
9677 .duration => |d| d.clock,9675 .duration => |d| d.clock,
...@@ -9685,7 +9683,7 @@ fn sleepLinux(timeout: Io.Timeout) Io.SleepError!void {...@@ -9685,7 +9683,7 @@ fn sleepLinux(timeout: Io.Timeout) Io.SleepError!void {
9685 var timespec: posix.timespec = timestampToPosix(deadline_nanoseconds);9683 var timespec: posix.timespec = timestampToPosix(deadline_nanoseconds);
9686 const syscall: Syscall = try .start();9684 const syscall: Syscall = try .start();
9687 while (true) {9685 while (true) {
9688 switch (std.os.linux.errno(std.os.linux.clock_nanosleep(clock_id, .{ .ABSTIME = switch (timeout) {9686 switch (posix.errno(posix.system.clock_nanosleep(clock_id, .{ .ABSTIME = switch (timeout) {
9689 .none, .duration => false,9687 .none, .duration => false,
9690 .deadline => true,9688 .deadline => true,
9691 } }, &timespec, &timespec))) {9689 } }, &timespec, &timespec))) {
...@@ -9737,7 +9735,7 @@ fn sleepWasi(t: *Threaded, timeout: Io.Timeout) Io.SleepError!void {...@@ -9737,7 +9735,7 @@ fn sleepWasi(t: *Threaded, timeout: Io.Timeout) Io.SleepError!void {
9737 syscall.finish();9735 syscall.finish();
9738}9736}
97399737
9740fn sleepPosix(t: *Threaded, timeout: Io.Timeout) Io.SleepError!void {9738fn sleepNanosleep(t: *Threaded, timeout: Io.Timeout) Io.SleepError!void {
9741 const t_io = ioBasic(t);9739 const t_io = ioBasic(t);
9742 const sec_type = @typeInfo(posix.timespec).@"struct".fields[0].type;9740 const sec_type = @typeInfo(posix.timespec).@"struct".fields[0].type;
9743 const nsec_type = @typeInfo(posix.timespec).@"struct".fields[1].type;9741 const nsec_type = @typeInfo(posix.timespec).@"struct".fields[1].type;
lib/std/c.zig+19-5
...@@ -10588,7 +10588,7 @@ pub const socket = switch (native_os) {...@@ -10588,7 +10588,7 @@ pub const socket = switch (native_os) {
1058810588
10589pub const socketpair = switch (native_os) {10589pub const socketpair = switch (native_os) {
10590 // https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/#unsupported\unavailable:10590 // https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/#unsupported\unavailable:
10591 .windows => void,10591 .windows => {},
10592 else => private.socketpair,10592 else => private.socketpair,
10593};10593};
1059410594
...@@ -10775,11 +10775,10 @@ pub extern "c" fn recvfrom(...@@ -10775,11 +10775,10 @@ pub extern "c" fn recvfrom(
10775) if (native_os == .windows) c_int else isize;10775) if (native_os == .windows) c_int else isize;
1077610776
10777pub const recvmsg = switch (native_os) {10777pub const recvmsg = switch (native_os) {
10778 // Windows: Technically, a form of recvmsg() exists for Windows, but the10778 // Technically, a form of recvmsg() exists for Windows, but the user has to
10779 // user has to install some kind of callback for it. I'm not sure if/how10779 // install some kind of callback for it.
10780 // we can map this to normal recvmsg() interface use.
10781 // https://learn.microsoft.com/en-us/windows/win32/api/mswsock/nc-mswsock-lpfn_wsarecvmsg10780 // https://learn.microsoft.com/en-us/windows/win32/api/mswsock/nc-mswsock-lpfn_wsarecvmsg
10782 .windows => void,10781 .windows => {},
10783 else => private.recvmsg,10782 else => private.recvmsg,
10784};10783};
1078510784
...@@ -11087,6 +11086,20 @@ pub extern "c" fn pthread_getthreadid_np() c_int;...@@ -11087,6 +11086,20 @@ pub extern "c" fn pthread_getthreadid_np() c_int;
11087pub extern "c" fn pthread_set_name_np(thread: pthread_t, name: [*:0]const u8) void;11086pub extern "c" fn pthread_set_name_np(thread: pthread_t, name: [*:0]const u8) void;
11088pub extern "c" fn pthread_get_name_np(thread: pthread_t, name: [*:0]u8, len: usize) void;11087pub extern "c" fn pthread_get_name_np(thread: pthread_t, name: [*:0]u8, len: usize) void;
1108911088
11089pub const TIMER = switch (native_os) {
11090 .linux, .emscripten => std.os.linux.TIMER,
11091 .openbsd, .netbsd, .wasi, .windows, .freebsd => packed struct(u32) {
11092 ABSTIME: bool,
11093 _: u31 = 0,
11094 },
11095 else => void,
11096};
11097
11098pub const clock_nanosleep = switch (native_os) {
11099 .linux, .emscripten, .netbsd, .wasi, .windows, .freebsd => private.clock_nanosleep,
11100 else => {},
11101};
11102
11090// OS-specific bits. These are protected from being used on the wrong OS by11103// OS-specific bits. These are protected from being used on the wrong OS by
11091// comptime assertions inside each OS-specific file.11104// comptime assertions inside each OS-specific file.
1109211105
...@@ -11476,6 +11489,7 @@ const private = struct {...@@ -11476,6 +11489,7 @@ const private = struct {
11476 extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;11489 extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
11477 extern "c" fn msync(addr: *align(page_size) const anyopaque, len: usize, flags: c_int) c_int;11490 extern "c" fn msync(addr: *align(page_size) const anyopaque, len: usize, flags: c_int) c_int;
11478 extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;11491 extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
11492 extern "c" fn clock_nanosleep(clockid: clockid_t, flags: TIMER, t: *const timespec, remain: ?*timespec) c_int;
11479 extern "c" fn pipe2(fds: *[2]fd_t, flags: O) c_int;11493 extern "c" fn pipe2(fds: *[2]fd_t, flags: O) c_int;
11480 extern "c" fn readdir(dir: *DIR) ?*dirent;11494 extern "c" fn readdir(dir: *DIR) ?*dirent;
11481 extern "c" fn realpath(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;11495 extern "c" fn realpath(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8;