authorgravatar for kbutcher6200@gmail.comkprotty <kbutcher6200@gmail.com> 2019-11-24 20:28:29-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-26 20:40:28-05:00
log056b5a26c960923d7afe9807255a32b85d99cacc
tree088c11c0eb63ad90ea25077b84cb9d7ec12050e1
parentca2d566ec85bee81396f64325844cc760b3cf870
signaturelock-open Commit is signed but in an unrecognized format.

ResetEvent: get abstime based on std.time


1 files changed, 14 insertions(+), 6 deletions(-)

lib/std/reset_event.zig+14-6
......@@ -333,12 +333,20 @@ const PosixEvent = struct {
333333 return false;
334334
335335 var ts: os.timespec = undefined;
336 var ts_ptr = &ts;
337336 if (timeout) |timeout_ns| {
338 var tv: os.timeval = undefined;
339 assert(c.gettimeofday(&tv, null) == 0);
340 ts.tv_sec = tv.tv_sec + @intCast(isize, timeout_ns / time.ns_per_s);
341 ts.tv_nsec = (tv.tv_usec * time.microsecond) + @intCast(isize, timeout_ns % time.ns_per_s);
337 var timeout_abs = timeout_ns;
338 if (comptime std.Target.current.isDarwin()) {
339 var tv: os.darwin.timeval = undefined;
340 assert(os.darwin.gettimeofday(&tv, null) == 0);
341 timeout_abs += @intCast(u64, tv.tv_sec) * time.second;
342 timeout_abs += @intCast(u64, tv.tv_usec) * time.microsecond;
343 } else {
344 os.clock_gettime(os.CLOCK_REALTIME, &ts) catch unreachable;
345 timeout_abs += @intCast(u64, ts.tv_sec) * time.second;
346 timeout_abs += @intCast(u64, ts.tv_nsec);
347 }
348 ts.tv_sec = @intCast(@typeOf(ts.tv_sec), @divFloor(timeout_abs, time.second));
349 ts.tv_nsec = @intCast(@typeOf(ts.tv_nsec), @mod(timeout_abs, time.second));
342350 }
343351
344352 var dummy_value: u32 = undefined;
......@@ -348,7 +356,7 @@ const PosixEvent = struct {
348356 while (self.state == wait_token) {
349357 const rc = switch (timeout == null) {
350358 true => c.pthread_cond_wait(&self.cond, &self.mutex),
351 else => c.pthread_cond_timedwait(&self.cond, &self.mutex, ts_ptr),
359 else => c.pthread_cond_timedwait(&self.cond, &self.mutex, &ts),
352360 };
353361 // TODO: rc appears to be the positive error code making os.errno() always return 0 on linux
354362 switch (std.math.max(@as(c_int, os.errno(rc)), rc)) {