zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit f1610f6c1ddbb5c2b42d2ecac433b8eff11d5a3a (tree)
parent abf959a0c90418c383cc4f10242dbcc2a2e974fd
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Mon, 27 May 2019 12:16:32 -0400

nanosleep: move windows logic to std.time

Diffstat:
Mstd/os.zig | 13-------------
Mstd/time.zig | 8++++++++
2 files changed, 8 insertions(+), 13 deletions(-)

diff --git a/std/os.zig b/std/os.zig @@ -2315,19 +2315,6 @@ pub fn realpathW(pathname: [*]const u16, out_buffer: *[MAX_PATH_BYTES]u8) RealPa /// Spurious wakeups are possible and no precision of timing is guaranteed. pub fn nanosleep(seconds: u64, nanoseconds: u64) void { - if (windows.is_the_target and !builtin.link_libc) { - // TODO https://github.com/ziglang/zig/issues/1284 - const small_s = math.cast(windows.DWORD, seconds) catch math.maxInt(windows.DWORD); - const ms_from_s = math.mul(windows.DWORD, small_s, std.time.ms_per_s) catch math.maxInt(windows.DWORD); - - const ns_per_ms = std.time.ns_per_s / std.time.ms_per_s; - const big_ms_from_ns = nanoseconds / ns_per_ms; - const ms_from_ns = math.cast(windows.DWORD, big_ms_from_ns) catch math.maxInt(windows.DWORD); - - const ms = math.add(windows.DWORD, ms_from_s, ms_from_ns) catch math.maxInt(windows.DWORD); - windows.kernel32.Sleep(ms); - return; - } var req = timespec{ .tv_sec = math.cast(isize, seconds) catch math.maxInt(isize), .tv_nsec = math.cast(isize, nanoseconds) catch math.maxInt(isize), diff --git a/std/time.zig b/std/time.zig @@ -3,11 +3,19 @@ const std = @import("std.zig"); const assert = std.debug.assert; const testing = std.testing; const os = std.os; +const math = std.math; pub const epoch = @import("time/epoch.zig"); /// Spurious wakeups are possible and no precision of timing is guaranteed. pub fn sleep(nanoseconds: u64) void { + if (os.windows.is_the_target) { + const ns_per_ms = ns_per_s / ms_per_s; + const big_ms_from_ns = nanoseconds / ns_per_ms; + const ms = math.cast(os.windows.DWORD, big_ms_from_ns) catch math.maxInt(os.windows.DWORD); + os.windows.kernel32.Sleep(ms); + return; + } const s = nanoseconds / ns_per_s; const ns = nanoseconds % ns_per_s; std.os.nanosleep(s, ns);