authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-31 03:28:50+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-09-06 20:03:12+02:00
logf35015575ef7ef5b493cf217f5e9c7152d6b6658
tree71d78aca61e666fad0c4f3825eef7996efe46abf
parent804319799586961052a9e4f283ad94d5018ce5fa
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

std.time: Use clock_nanosleep() to implement sleep() on Linux.

This fixes the function for riscv32 where the old nanosleep() is not available. clock_nanosleep() has been available since Linux 2.6 and glibc 2.1 anyway.

1 files changed, 28 insertions(+), 0 deletions(-)

lib/std/time.zig+28
......@@ -50,6 +50,34 @@ pub fn sleep(nanoseconds: u64) void {
5050
5151 const s = nanoseconds / ns_per_s;
5252 const ns = nanoseconds % ns_per_s;
53
54 // Newer kernel ports don't have old `nanosleep()` and `clock_nanosleep()` has been around
55 // since Linux 2.6 and glibc 2.1 anyway.
56 if (builtin.os.tag == .linux) {
57 const linux = std.os.linux;
58
59 var req: linux.timespec = .{
60 .sec = std.math.cast(linux.time_t, s) orelse std.math.maxInt(linux.time_t),
61 .nsec = std.math.cast(linux.time_t, ns) orelse std.math.maxInt(linux.time_t),
62 };
63 var rem: linux.timespec = undefined;
64
65 while (true) {
66 switch (linux.E.init(linux.clock_nanosleep(.MONOTONIC, .{ .ABSTIME = false }, &req, &rem))) {
67 .SUCCESS => return,
68 .INTR => {
69 req = rem;
70 continue;
71 },
72 .FAULT,
73 .INVAL,
74 .OPNOTSUPP,
75 => unreachable,
76 else => return,
77 }
78 }
79 }
80
5381 posix.nanosleep(s, ns);
5482}
5583