From 947a333ec3c741668024a9a7cb3002cca082a0b2 Mon Sep 17 00:00:00 2001 From: Tatsuyuki Ishi Date: Tue, 23 Jul 2024 00:06:05 +0900 Subject: [PATCH] util/u_queue: Replace relative time wait hack with u_cnd_monotonic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove the gross hack. The hack was broken too, because it incorrectly added abs_time (a timestamp) to the now (another timestamp). Reviewed-by: Marek Olšák Part-of: --- src/util/u_queue.c | 39 ++++++++++++--------------------------- src/util/u_queue.h | 3 ++- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/src/util/u_queue.c b/src/util/u_queue.c index 750dd2015a6..026e1582a22 100644 --- a/src/util/u_queue.c +++ b/src/util/u_queue.c @@ -31,6 +31,7 @@ #include "util/os_time.h" #include "util/u_string.h" #include "util/u_thread.h" +#include "util/timespec.h" #include "u_process.h" #if defined(__linux__) @@ -160,7 +161,7 @@ util_queue_fence_signal(struct util_queue_fence *fence) { mtx_lock(&fence->mutex); fence->signalled = true; - cnd_broadcast(&fence->cond); + u_cnd_monotonic_broadcast(&fence->cond); mtx_unlock(&fence->mutex); } @@ -169,7 +170,7 @@ _util_queue_fence_wait(struct util_queue_fence *fence) { mtx_lock(&fence->mutex); while (!fence->signalled) - cnd_wait(&fence->cond, &fence->mutex); + u_cnd_monotonic_wait(&fence->cond, &fence->mutex); mtx_unlock(&fence->mutex); } @@ -177,31 +178,15 @@ bool _util_queue_fence_wait_timeout(struct util_queue_fence *fence, int64_t abs_timeout) { - /* This terrible hack is made necessary by the fact that we really want an - * internal interface consistent with os_time_*, but cnd_timedwait is spec'd - * to be relative to the TIME_UTC clock. - */ - int64_t rel = abs_timeout - os_time_get_nano(); + struct timespec ts; + timespec_from_nsec(&ts, abs_timeout); - if (rel > 0) { - struct timespec ts; - - timespec_get(&ts, TIME_UTC); - - ts.tv_sec += abs_timeout / (1000*1000*1000); - ts.tv_nsec += abs_timeout % (1000*1000*1000); - if (ts.tv_nsec >= (1000*1000*1000)) { - ts.tv_sec++; - ts.tv_nsec -= (1000*1000*1000); - } - - mtx_lock(&fence->mutex); - while (!fence->signalled) { - if (cnd_timedwait(&fence->cond, &fence->mutex, &ts) != thrd_success) - break; - } - mtx_unlock(&fence->mutex); + mtx_lock(&fence->mutex); + while (!fence->signalled) { + if (u_cnd_monotonic_timedwait(&fence->cond, &fence->mutex, &ts) != thrd_success) + break; } + mtx_unlock(&fence->mutex); return fence->signalled; } @@ -211,7 +196,7 @@ util_queue_fence_init(struct util_queue_fence *fence) { memset(fence, 0, sizeof(*fence)); (void) mtx_init(&fence->mutex, mtx_plain); - cnd_init(&fence->cond); + u_cnd_monotonic_init(&fence->cond); fence->signalled = true; } @@ -232,7 +217,7 @@ util_queue_fence_destroy(struct util_queue_fence *fence) mtx_lock(&fence->mutex); mtx_unlock(&fence->mutex); - cnd_destroy(&fence->cond); + u_cnd_monotonic_destroy(&fence->cond); mtx_destroy(&fence->mutex); } #endif diff --git a/src/util/u_queue.h b/src/util/u_queue.h index 2d5ef91bc91..a1b26994365 100644 --- a/src/util/u_queue.h +++ b/src/util/u_queue.h @@ -35,6 +35,7 @@ #include +#include "cnd_monotonic.h" #include "simple_mtx.h" #include "util/futex.h" #include "util/list.h" @@ -124,7 +125,7 @@ util_queue_fence_is_signalled(struct util_queue_fence *fence) */ struct util_queue_fence { mtx_t mutex; - cnd_t cond; + struct u_cnd_monotonic cond; int signalled; };