Commit 4d9547ff2e

Marcio Giaxa <i@mgxm.me>
2019-01-04 17:42:45
freebsd: implement clock related functions
- clock_gettime - clock_getres
1 parent 1e781a3
Changed files (3)
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 {
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));
 }
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"),
     };