From 4d9547ff2e9377abeb87b53e30580cf1a561f9ce Mon Sep 17 00:00:00 2001 From: Marcio Giaxa Date: Fri, 4 Jan 2019 14:42:45 -0200 Subject: freebsd: implement clock related functions - clock_gettime - clock_getres --- std/c/freebsd.zig | 2 ++ std/os/freebsd/index.zig | 8 ++++++++ std/os/time.zig | 8 ++++---- 3 files changed, 14 insertions(+), 4 deletions(-) (limited to 'std') diff --git a/std/c/freebsd.zig b/std/c/freebsd.zig index eae5516db3..4d5c3810e8 100644 --- a/std/c/freebsd.zig +++ b/std/c/freebsd.zig @@ -22,6 +22,8 @@ pub extern "c" fn openat(fd: c_int, path: ?[*]const u8, flags: c_int) c_int; pub extern "c" fn setgid(ruid: c_uint, euid: c_uint) c_int; pub extern "c" fn setuid(uid: c_uint) c_int; pub extern "c" fn kill(pid: c_int, sig: c_int) c_int; +pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int; +pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int; /// Renamed from `kevent` to `Kevent` to avoid conflict with function name. pub const Kevent = extern struct { diff --git a/std/os/freebsd/index.zig b/std/os/freebsd/index.zig index c122c2d7e1..9bb683f69e 100644 --- a/std/os/freebsd/index.zig +++ b/std/os/freebsd/index.zig @@ -714,6 +714,14 @@ pub fn nanosleep(req: *const timespec, rem: ?*timespec) usize { return errnoWrap(c.nanosleep(req, rem)); } +pub fn clock_gettime(clk_id: i32, tp: *timespec) usize { + return errnoWrap(c.clock_gettime(clk_id, tp)); +} + +pub fn clock_getres(clk_id: i32, tp: *timespec) usize { + return clock_gettime(clk_id, tp); +} + pub fn setuid(uid: u32) usize { return errnoWrap(c.setuid(uid)); } diff --git a/std/os/time.zig b/std/os/time.zig index c3588838bc..42f32f8fee 100644 --- a/std/os/time.zig +++ b/std/os/time.zig @@ -61,7 +61,7 @@ pub fn timestamp() u64 { /// Get the posix timestamp, UTC, in milliseconds pub const milliTimestamp = switch (builtin.os) { Os.windows => milliTimestampWindows, - Os.linux => milliTimestampPosix, + Os.linux, Os.freebsd => milliTimestampPosix, Os.macosx, Os.ios => milliTimestampDarwin, else => @compileError("Unsupported OS"), }; @@ -179,7 +179,7 @@ pub const Timer = struct { debug.assert(err != windows.FALSE); self.start_time = @intCast(u64, start_time); }, - Os.linux => { + Os.linux, Os.freebsd => { //On Linux, seccomp can do arbitrary things to our ability to call // syscalls, including return any errno value it wants and // inconsistently throwing errors. Since we can't account for @@ -215,7 +215,7 @@ pub const Timer = struct { var clock = clockNative() - self.start_time; return switch (builtin.os) { Os.windows => @divFloor(clock * ns_per_s, self.frequency), - Os.linux => clock, + Os.linux, Os.freebsd => clock, Os.macosx, Os.ios => @divFloor(clock * self.frequency.numer, self.frequency.denom), else => @compileError("Unsupported OS"), }; @@ -236,7 +236,7 @@ pub const Timer = struct { const clockNative = switch (builtin.os) { Os.windows => clockWindows, - Os.linux => clockLinux, + Os.linux, Os.freebsd => clockLinux, Os.macosx, Os.ios => clockDarwin, else => @compileError("Unsupported OS"), }; -- cgit v1.2.3