| ... | @@ -333,12 +333,20 @@ const PosixEvent = struct { | ... | @@ -333,12 +333,20 @@ const PosixEvent = struct { |
| 333 | return false; | 333 | return false; |
| 334 | | 334 | |
| 335 | var ts: os.timespec = undefined; | 335 | var ts: os.timespec = undefined; |
| 336 | var ts_ptr = &ts; | | |
| 337 | if (timeout) |timeout_ns| { | 336 | if (timeout) |timeout_ns| { |
| 338 | var tv: os.timeval = undefined; | 337 | var timeout_abs = timeout_ns; |
| 339 | assert(c.gettimeofday(&tv, null) == 0); | 338 | if (comptime std.Target.current.isDarwin()) { |
| 340 | ts.tv_sec = tv.tv_sec + @intCast(isize, timeout_ns / time.ns_per_s); | 339 | var tv: os.darwin.timeval = undefined; |
| 341 | ts.tv_nsec = (tv.tv_usec * time.microsecond) + @intCast(isize, timeout_ns % time.ns_per_s); | 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)); |
| 342 | } | 350 | } |
| 343 | | 351 | |
| 344 | var dummy_value: u32 = undefined; | 352 | var dummy_value: u32 = undefined; |
| ... | @@ -348,7 +356,7 @@ const PosixEvent = struct { | ... | @@ -348,7 +356,7 @@ const PosixEvent = struct { |
| 348 | while (self.state == wait_token) { | 356 | while (self.state == wait_token) { |
| 349 | const rc = switch (timeout == null) { | 357 | const rc = switch (timeout == null) { |
| 350 | true => c.pthread_cond_wait(&self.cond, &self.mutex), | 358 | 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), |
| 352 | }; | 360 | }; |
| 353 | // TODO: rc appears to be the positive error code making os.errno() always return 0 on linux | 361 | // TODO: rc appears to be the positive error code making os.errno() always return 0 on linux |
| 354 | switch (std.math.max(@as(c_int, os.errno(rc)), rc)) { | 362 | switch (std.math.max(@as(c_int, os.errno(rc)), rc)) { |