aboutsummaryrefslogtreecommitdiffstats
path: root/executor
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2018-07-26 13:13:07 +0200
committerDmitry Vyukov <dvyukov@google.com>2018-07-27 10:22:23 +0200
commitc3da5dc5e0d0c6614f48c2d1178d58ff1e47809c (patch)
tree968d12d9315a1d0872a15a0ff912921a41b97105 /executor
parent4dcfea28eb0ef438c338983c63c9405554b83510 (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')
-rw-r--r--executor/common.h25
-rw-r--r--executor/common_linux.h24
2 files changed, 19 insertions, 30 deletions
diff --git a/executor/common.h b/executor/common.h
index 9f7552900..5ddfb08c5 100644
--- a/executor/common.h
+++ b/executor/common.h
@@ -260,27 +260,22 @@ static int event_isset(event_t* ev)
return res;
}
-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 timeout_ns = timeout_ms * 1000 * 1000;
+ uint64 start = current_time_ms();
+ uint64 now = start;
pthread_mutex_lock(&ev->mu);
for (;;) {
if (ev->state)
break;
- if (clock_gettime(CLOCK_MONOTONIC, &ts))
- fail("clock_gettime failed");
- uint64 now_ns = (uint64)ts.tv_sec * kNsPerSec + (uint64)ts.tv_nsec;
- if (now_ns - start_ns > timeout_ns)
- break;
- 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;
pthread_cond_timedwait(&ev->cv, &ev->mu, &ts);
+ now = current_time_ms();
+ if (now - start > timeout)
+ break;
}
int res = ev->state;
pthread_mutex_unlock(&ev->mu);
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;
}
}