| ... | ... | @@ -50,6 +50,34 @@ pub fn sleep(nanoseconds: u64) void { |
| 50 | 50 | |
| 51 | 51 | const s = nanoseconds / ns_per_s; |
| 52 | 52 | 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 | |
| 53 | 81 | posix.nanosleep(s, ns); |
| 54 | 82 | } |
| 55 | 83 | |