From 14e9c7d1f2f9a1b583237626e6b834c8e86d6cf7 Mon Sep 17 00:00:00 2001 From: Benjamin Feng Date: Tue, 19 Nov 2019 19:44:19 -0600 Subject: [PATCH] WASI clock functions --- lib/std/os/wasi.zig | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/std/os/wasi.zig b/lib/std/os/wasi.zig index fce29ef0b3..7f030510ed 100644 --- a/lib/std/os/wasi.zig +++ b/lib/std/os/wasi.zig @@ -76,3 +76,35 @@ pub extern "wasi_unstable" fn sched_yield() errno_t; pub extern "wasi_unstable" fn sock_recv(sock: fd_t, ri_data: *const iovec_t, ri_data_len: usize, ri_flags: riflags_t, ro_datalen: *usize, ro_flags: *roflags_t) errno_t; pub extern "wasi_unstable" fn sock_send(sock: fd_t, si_data: *const ciovec_t, si_data_len: usize, si_flags: siflags_t, so_datalen: *usize) errno_t; pub extern "wasi_unstable" fn sock_shutdown(sock: fd_t, how: sdflags_t) errno_t; + +/// Get the errno from a syscall return value, or 0 for no error. +pub fn getErrno(r: usize) usize { + const signed_r = @bitCast(isize, r); + return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0; +} + +pub fn clock_getres(clock_id: i32, res: *timespec) errno_t { + var ts: timestamp_t = undefined; + const err = clock_res_get(@bitCast(u32, clock_id), &ts); + if (err != 0) { + return err; + } + res.* = .{ + .tv_sec = @intCast(i64, ts / 1000000000), + .tv_nsec = @intCast(isize, ts % 1000000000), + }; + return 0; +} + +pub fn clock_gettime(clock_id: i32, tp: *timespec) errno_t { + var ts: timestamp_t = undefined; + const err = clock_time_get(@bitCast(u32, clock_id), 1, &ts); + if (err != 0) { + return err; + } + tp.* = .{ + .tv_sec = @intCast(i64, ts / 1000000000), + .tv_nsec = @intCast(isize, ts % 1000000000), + }; + return 0; +}