authorgravatar for contact@fengb.meBenjamin Feng <contact@fengb.me> 2019-11-19 19:44:19-06:00
committergravatar for contact@fengb.meBenjamin Feng <contact@fengb.me> 2019-11-19 19:44:19-06:00
log14e9c7d1f2f9a1b583237626e6b834c8e86d6cf7
treee1ae0e9e972a03157387f6b2650bba22f71045ed
parentc3d93cd9f2a6ebc4e7104435a8bf24213eb4ba34

WASI clock functions


1 files changed, 32 insertions(+), 0 deletions(-)

lib/std/os/wasi.zig+32
...@@ -76,3 +76,35 @@ pub extern "wasi_unstable" fn sched_yield() errno_t;...@@ -76,3 +76,35 @@ pub extern "wasi_unstable" fn sched_yield() errno_t;
76pub 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;76pub 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;
77pub 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;77pub 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;
78pub extern "wasi_unstable" fn sock_shutdown(sock: fd_t, how: sdflags_t) errno_t;78pub extern "wasi_unstable" fn sock_shutdown(sock: fd_t, how: sdflags_t) errno_t;
79
80/// Get the errno from a syscall return value, or 0 for no error.
81pub fn getErrno(r: usize) usize {
82 const signed_r = @bitCast(isize, r);
83 return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0;
84}
85
86pub fn clock_getres(clock_id: i32, res: *timespec) errno_t {
87 var ts: timestamp_t = undefined;
88 const err = clock_res_get(@bitCast(u32, clock_id), &ts);
89 if (err != 0) {
90 return err;
91 }
92 res.* = .{
93 .tv_sec = @intCast(i64, ts / 1000000000),
94 .tv_nsec = @intCast(isize, ts % 1000000000),
95 };
96 return 0;
97}
98
99pub fn clock_gettime(clock_id: i32, tp: *timespec) errno_t {
100 var ts: timestamp_t = undefined;
101 const err = clock_time_get(@bitCast(u32, clock_id), 1, &ts);
102 if (err != 0) {
103 return err;
104 }
105 tp.* = .{
106 .tv_sec = @intCast(i64, ts / 1000000000),
107 .tv_nsec = @intCast(isize, ts % 1000000000),
108 };
109 return 0;
110}