diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2018-07-26 13:13:07 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2018-07-27 10:22:23 +0200 |
| commit | c3da5dc5e0d0c6614f48c2d1178d58ff1e47809c (patch) | |
| tree | 968d12d9315a1d0872a15a0ff912921a41b97105 /executor/common_linux.h | |
| parent | 4dcfea28eb0ef438c338983c63c9405554b83510 (diff) | |
executor: simplify event_timedwait
We always have current_time_ms in event_timedwait
so use it instead of manual clock_gettime calls
which tend to be bulkier.
Diffstat (limited to 'executor/common_linux.h')
| -rw-r--r-- | executor/common_linux.h | 24 |
1 files changed, 9 insertions, 15 deletions
diff --git a/executor/common_linux.h b/executor/common_linux.h index 5f3eec25f..ad653f396 100644 --- a/executor/common_linux.h +++ b/executor/common_linux.h @@ -51,26 +51,20 @@ static int event_isset(event_t* ev) return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE); } -static int event_timedwait(event_t* ev, uint64 timeout_ms) +static int event_timedwait(event_t* ev, uint64 timeout) { - struct timespec ts; - if (clock_gettime(CLOCK_MONOTONIC, &ts)) - fail("clock_gettime failed"); - const uint64 kNsPerSec = 1000 * 1000 * 1000; - uint64 start_ns = (uint64)ts.tv_sec * kNsPerSec + (uint64)ts.tv_nsec; - uint64 now_ns = start_ns; - uint64 timeout_ns = timeout_ms * 1000 * 1000; + uint64 start = current_time_ms(); + uint64 now = start; for (;;) { - uint64 remain_ns = timeout_ns - (now_ns - start_ns); - ts.tv_sec = remain_ns / kNsPerSec; - ts.tv_nsec = remain_ns % kNsPerSec; + uint64 remain = timeout - (now - start); + struct timespec ts; + ts.tv_sec = remain / 1000; + ts.tv_nsec = (remain % 1000) * 1000 * 1000; syscall(SYS_futex, &ev->state, FUTEX_WAIT, 0, &ts); if (__atomic_load_n(&ev->state, __ATOMIC_RELAXED)) return 1; - if (clock_gettime(CLOCK_MONOTONIC, &ts)) - fail("clock_gettime failed"); - now_ns = (uint64)ts.tv_sec * kNsPerSec + (uint64)ts.tv_nsec; - if (now_ns - start_ns > timeout_ns) + now = current_time_ms(); + if (now - start > timeout) return 0; } } |
