Commit 14e9c7d1f2

Benjamin Feng <contact@fengb.me>
2019-11-20 02:44:19
WASI clock functions
1 parent c3d93cd
Changed files (1)
lib
std
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;
+}