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;...@@ -7,6 +7,7 @@ const testing = std.testing;
7const windows = std.os.windows;7const windows = std.os.windows;
8const linux = std.os.linux;8const linux = std.os.linux;
9const darwin = std.os.darwin;9const darwin = std.os.darwin;
10const wasi = std.os.wasi;
10const posix = std.os.posix;11const posix = std.os.posix;
1112
12pub const epoch = @import("epoch.zig");13pub const epoch = @import("epoch.zig");
...@@ -64,9 +65,21 @@ pub const milliTimestamp = switch (builtin.os) {...@@ -64,9 +65,21 @@ pub const milliTimestamp = switch (builtin.os) {
64 Os.windows => milliTimestampWindows,65 Os.windows => milliTimestampWindows,
65 Os.linux, Os.freebsd, Os.netbsd => milliTimestampPosix,66 Os.linux, Os.freebsd, Os.netbsd => milliTimestampPosix,
66 Os.macosx, Os.ios => milliTimestampDarwin,67 Os.macosx, Os.ios => milliTimestampDarwin,
68 Os.wasi => milliTimestampWasi,
67 else => @compileError("Unsupported OS"),69 else => @compileError("Unsupported OS"),
68};70};
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
70fn milliTimestampWindows() u64 {83fn milliTimestampWindows() u64 {
71 //FileTime has a granularity of 100 nanoseconds84 //FileTime has a granularity of 100 nanoseconds
72 // and uses the NTFS/Windows epoch85 // and uses the NTFS/Windows epoch
std/os/wasi/core.zig+10
...@@ -1,18 +1,28 @@...@@ -1,18 +1,28 @@
1pub const clockid_t = u32;
1pub const errno_t = u16;2pub const errno_t = u16;
2pub const exitcode_t = u32;3pub const exitcode_t = u32;
3pub const fd_t = u32;4pub const fd_t = u32;
4pub const signal_t = u8;5pub const signal_t = u8;
6pub const timestamp_t = u64;
57
6pub const ciovec_t = extern struct {8pub const ciovec_t = extern struct {
7 buf: [*]const u8,9 buf: [*]const u8,
8 buf_len: usize,10 buf_len: usize,
9};11};
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
11pub const SIGABRT: signal_t = 6;18pub const SIGABRT: signal_t = 6;
1219
13pub extern "wasi_unstable" fn args_get(argv: [*][*]u8, argv_buf: [*]u8) errno_t;20pub extern "wasi_unstable" fn args_get(argv: [*][*]u8, argv_buf: [*]u8) errno_t;
14pub extern "wasi_unstable" fn args_sizes_get(argc: *usize, argv_buf_size: *usize) errno_t;21pub 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
16pub extern "wasi_unstable" fn environ_get(environ: [*]?[*]u8, environ_buf: [*]u8) errno_t;26pub extern "wasi_unstable" fn environ_get(environ: [*]?[*]u8, environ_buf: [*]u8) errno_t;
17pub extern "wasi_unstable" fn environ_sizes_get(environ_count: *usize, environ_buf_size: *usize) errno_t;27pub extern "wasi_unstable" fn environ_sizes_get(environ_count: *usize, environ_buf_size: *usize) errno_t;
1828