From 9733534cf4cac16793c5089318ec64441e75fcf5 Mon Sep 17 00:00:00 2001 From: Desour Date: Sun, 6 Oct 2024 17:13:13 +0200 Subject: [PATCH] posix implementation: use correct clock --- src/threading/ipc_channel.cpp | 24 +++++++++++++++++------- src/threading/ipc_channel.h | 1 + 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/threading/ipc_channel.cpp b/src/threading/ipc_channel.cpp index 4e9fe8a727f63..73ef7b4d7c96d 100644 --- a/src/threading/ipc_channel.cpp +++ b/src/threading/ipc_channel.cpp @@ -126,7 +126,7 @@ static void post(IPCChannelBuffer *buf) noexcept #elif defined(IPC_CHANNEL_IMPLEMENTATION_POSIX) -// timeout is absolute +// timeout is absolute (using cond_clockid) // returns false on timeout static bool wait(IPCChannelBuffer *buf, const struct timespec *timeout) noexcept { @@ -172,18 +172,25 @@ static bool wait_in(IPCChannelEnd::Dir *dir, u64 timeout_ms_abs) struct timespec timeout; struct timespec *timeoutp = nullptr; if (timeout_ms_abs > 0) { -#if defined(IPC_CHANNEL_IMPLEMENTATION_LINUX_FUTEX) // Relative time u64 tnow = porting::getTimeMs(); if (tnow > timeout_ms_abs) return false; - u64 timeout_ms = timeout_ms_abs - tnow; + u64 timeout_ms_rel = timeout_ms_abs - tnow; +#if defined(IPC_CHANNEL_IMPLEMENTATION_LINUX_FUTEX) + timeout.tv_sec = 0; + timeout.tv_nsec = 0; #elif defined(IPC_CHANNEL_IMPLEMENTATION_POSIX) - // Absolute time - u64 timeout_ms = timeout_ms_abs; + // Absolute time, relative to cond_clockid + FATAL_ERROR_IF(clock_gettime(dir->buf_in->cond_clockid, &timeout) < 0, + "clock_gettime failed"); + if (timeout.tv_nsec >= 1000'000'000L) { + timeout.tv_nsec -= 1000'000'000L; + timeout.tv_sec += 1; + } #endif - timeout.tv_sec = timeout_ms / 1000; - timeout.tv_nsec = timeout_ms % 1000 * 1000000UL; + timeout.tv_sec += timeout_ms_rel / 1000; + timeout.tv_nsec += timeout_ms_rel % 1000 * 1000'000L; timeoutp = &timeout; } @@ -225,6 +232,8 @@ IPCChannelBuffer::IPCChannelBuffer() goto error_condattr_setpshared; if (pthread_mutexattr_setpshared(&mutexattr, PTHREAD_PROCESS_SHARED) != 0) goto error_mutexattr_setpshared; + if (pthread_condattr_getclock(&condattr, &cond_clockid) != 0) + goto error_condattr_getclock; if (pthread_cond_init(&cond, &condattr) != 0) goto error_cond_init; if (pthread_mutex_init(&mutex, &mutexattr) != 0) @@ -236,6 +245,7 @@ IPCChannelBuffer::IPCChannelBuffer() error_mutex_init: pthread_cond_destroy(&cond); error_cond_init: +error_condattr_getclock: error_mutexattr_setpshared: error_condattr_setpshared: pthread_mutexattr_destroy(&mutexattr); diff --git a/src/threading/ipc_channel.h b/src/threading/ipc_channel.h index 3f8ae33bd5667..68a570ed8257c 100644 --- a/src/threading/ipc_channel.h +++ b/src/threading/ipc_channel.h @@ -71,6 +71,7 @@ struct IPCChannelBuffer #elif defined(IPC_CHANNEL_IMPLEMENTATION_POSIX) pthread_cond_t cond; + clockid_t cond_clockid; pthread_mutex_t mutex; bool posted = false; // protected by mutex #endif