| ... | ... | @@ -8,82 +8,8 @@ const posix = std.posix; |
| 8 | 8 | |
| 9 | 9 | pub const epoch = @import("time/epoch.zig"); |
| 10 | 10 | |
| 11 | | /// Spurious wakeups are possible and no precision of timing is guaranteed. |
| 12 | | pub fn sleep(nanoseconds: u64) void { |
| 13 | | if (builtin.os.tag == .windows) { |
| 14 | | const big_ms_from_ns = nanoseconds / ns_per_ms; |
| 15 | | const ms = math.cast(windows.DWORD, big_ms_from_ns) orelse math.maxInt(windows.DWORD); |
| 16 | | windows.kernel32.Sleep(ms); |
| 17 | | return; |
| 18 | | } |
| 19 | | |
| 20 | | if (builtin.os.tag == .wasi) { |
| 21 | | const w = std.os.wasi; |
| 22 | | const userdata: w.userdata_t = 0x0123_45678; |
| 23 | | const clock: w.subscription_clock_t = .{ |
| 24 | | .id = .MONOTONIC, |
| 25 | | .timeout = nanoseconds, |
| 26 | | .precision = 0, |
| 27 | | .flags = 0, |
| 28 | | }; |
| 29 | | const in: w.subscription_t = .{ |
| 30 | | .userdata = userdata, |
| 31 | | .u = .{ |
| 32 | | .tag = .CLOCK, |
| 33 | | .u = .{ .clock = clock }, |
| 34 | | }, |
| 35 | | }; |
| 36 | | |
| 37 | | var event: w.event_t = undefined; |
| 38 | | var nevents: usize = undefined; |
| 39 | | _ = w.poll_oneoff(&in, &event, 1, &nevents); |
| 40 | | return; |
| 41 | | } |
| 42 | | |
| 43 | | if (builtin.os.tag == .uefi) { |
| 44 | | const boot_services = std.os.uefi.system_table.boot_services.?; |
| 45 | | const us_from_ns = nanoseconds / ns_per_us; |
| 46 | | const us = math.cast(usize, us_from_ns) orelse math.maxInt(usize); |
| 47 | | _ = boot_services.stall(us); |
| 48 | | return; |
| 49 | | } |
| 50 | | |
| 51 | | const s = nanoseconds / ns_per_s; |
| 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 | | |
| 81 | | posix.nanosleep(s, ns); |
| 82 | | } |
| 83 | | |
| 84 | | test sleep { |
| 85 | | sleep(1); |
| 86 | | } |
| 11 | /// Deprecated: moved to std.Thread.sleep |
| 12 | pub const sleep = std.Thread.sleep; |
| 87 | 13 | |
| 88 | 14 | /// Get a calendar timestamp, in seconds, relative to UTC 1970-01-01. |
| 89 | 15 | /// Precision of timing depends on the hardware and operating system. |
| ... | ... | @@ -155,7 +81,7 @@ test milliTimestamp { |
| 155 | 81 | const margin = ns_per_ms * 50; |
| 156 | 82 | |
| 157 | 83 | const time_0 = milliTimestamp(); |
| 158 | | sleep(ns_per_ms); |
| 84 | std.Thread.sleep(ns_per_ms); |
| 159 | 85 | const time_1 = milliTimestamp(); |
| 160 | 86 | const interval = time_1 - time_0; |
| 161 | 87 | try testing.expect(interval > 0); |
| ... | ... | @@ -359,7 +285,7 @@ test Timer { |
| 359 | 285 | const margin = ns_per_ms * 150; |
| 360 | 286 | |
| 361 | 287 | var timer = try Timer.start(); |
| 362 | | sleep(10 * ns_per_ms); |
| 288 | std.Thread.sleep(10 * ns_per_ms); |
| 363 | 289 | const time_0 = timer.read(); |
| 364 | 290 | try testing.expect(time_0 > 0); |
| 365 | 291 | // Tests should not depend on timings: skip test if outside margin. |