authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-30 21:52:26-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-04-30 21:52:26-04:00
logbb0c5f7a2f816709a0ec5af7099057fdfbfad04a
tree3d5fb6d6a3de329464e56a5face49e785e76e807
parent1fabd6bbf355eb6111c4ed88be97f1ec2bf7b1ae
parentd395ed2d403aa0ff57c493052e3675de23292424
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2392 from shritesh/wasi_clock

wasi: implement timestamp

2 files changed, 23 insertions(+), 0 deletions(-)

std/os/time.zig+13
......@@ -7,6 +7,7 @@ const testing = std.testing;
77const windows = std.os.windows;
88const linux = std.os.linux;
99const darwin = std.os.darwin;
10const wasi = std.os.wasi;
1011const posix = std.os.posix;
1112
1213pub const epoch = @import("epoch.zig");
......@@ -64,9 +65,21 @@ pub const milliTimestamp = switch (builtin.os) {
6465 Os.windows => milliTimestampWindows,
6566 Os.linux, Os.freebsd, Os.netbsd => milliTimestampPosix,
6667 Os.macosx, Os.ios => milliTimestampDarwin,
68 Os.wasi => milliTimestampWasi,
6769 else => @compileError("Unsupported OS"),
6870};
6971
72fn milliTimestampWasi() u64 {
73 var ns: wasi.timestamp_t = undefined;
74
75 // TODO: Verify that precision is ignored
76 const err = wasi.clock_time_get(wasi.CLOCK_REALTIME, 1, &ns);
77 debug.assert(err == wasi.ESUCCESS);
78
79 const ns_per_ms = 1000;
80 return @divFloor(ns, ns_per_ms);
81}
82
7083fn milliTimestampWindows() u64 {
7184 //FileTime has a granularity of 100 nanoseconds
7285 // and uses the NTFS/Windows epoch
std/os/wasi/core.zig+10
......@@ -1,18 +1,28 @@
1pub const clockid_t = u32;
12pub const errno_t = u16;
23pub const exitcode_t = u32;
34pub const fd_t = u32;
45pub const signal_t = u8;
6pub const timestamp_t = u64;
57
68pub const ciovec_t = extern struct {
79 buf: [*]const u8,
810 buf_len: usize,
911};
1012
13pub const CLOCK_REALTIME: clockid_t = 0;
14pub const CLOCK_MONOTONIC: clockid_t = 1;
15pub const CLOCK_PROCESS_CPUTIME_ID: clockid_t = 2;
16pub const CLOCK_THREAD_CPUTIME_ID: clockid_t = 3;
17
1118pub const SIGABRT: signal_t = 6;
1219
1320pub extern "wasi_unstable" fn args_get(argv: [*][*]u8, argv_buf: [*]u8) errno_t;
1421pub extern "wasi_unstable" fn args_sizes_get(argc: *usize, argv_buf_size: *usize) errno_t;
1522
23pub extern "wasi_unstable" fn clock_res_get(clock_id: clockid_t, resolution: *timestamp_t) errno_t;
24pub extern "wasi_unstable" fn clock_time_get(clock_id: clockid_t, precision: timestamp_t, timestamp: *timestamp_t) errno_t;
25
1626pub extern "wasi_unstable" fn environ_get(environ: [*]?[*]u8, environ_buf: [*]u8) errno_t;
1727pub extern "wasi_unstable" fn environ_sizes_get(environ_count: *usize, environ_buf_size: *usize) errno_t;
1828