From a10f96e153ea3153d085a975bd382b4d7f8cea44 Mon Sep 17 00:00:00 2001 From: Chris Friedt Date: Wed, 23 Apr 2025 09:09:50 -0400 Subject: [PATCH] posix + tests: use CLOCK_REALTIME where specified by POSIX Use CLOCK_REALTIME for the default clock source throughout the POSIX implementation and tests so that we are consistent with the specification. Signed-off-by: Chris Friedt --- lib/posix/options/cond.c | 2 +- lib/posix/options/mqueue.c | 6 ++++-- lib/posix/options/mutex.c | 2 +- lib/posix/options/pthread.c | 3 ++- lib/posix/options/rwlock.c | 4 ++-- lib/posix/options/semaphore.c | 19 +------------------ tests/lib/c_lib/thrd/src/cnd.c | 2 +- tests/posix/common/src/cond.c | 2 +- tests/posix/rwlocks/src/main.c | 5 +++-- 9 files changed, 16 insertions(+), 29 deletions(-) diff --git a/lib/posix/options/cond.c b/lib/posix/options/cond.c index 40947aa3c45..8c81de5b2f6 100644 --- a/lib/posix/options/cond.c +++ b/lib/posix/options/cond.c @@ -256,7 +256,7 @@ int pthread_condattr_init(pthread_condattr_t *att) return EINVAL; } - attr->clock = CLOCK_MONOTONIC; + attr->clock = CLOCK_REALTIME; attr->initialized = true; return 0; diff --git a/lib/posix/options/mqueue.c b/lib/posix/options/mqueue.c index 23f2ee73ccb..72244a1981d 100644 --- a/lib/posix/options/mqueue.c +++ b/lib/posix/options/mqueue.c @@ -263,7 +263,8 @@ int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len, return -1; } - return send_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime))); + return send_message(mqd, msg_ptr, msg_len, + K_MSEC(timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime))); } /** @@ -298,7 +299,8 @@ int mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len, return -1; } - return receive_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime))); + return receive_message(mqd, msg_ptr, msg_len, + K_MSEC(timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime))); } /** diff --git a/lib/posix/options/mutex.c b/lib/posix/options/mutex.c index 51bd3f53c7c..ff2efc7f655 100644 --- a/lib/posix/options/mutex.c +++ b/lib/posix/options/mutex.c @@ -216,7 +216,7 @@ int pthread_mutex_timedlock(pthread_mutex_t *m, return EINVAL; } - return acquire_mutex(m, K_MSEC(timespec_to_timeoutms(abstime))); + return acquire_mutex(m, K_MSEC(timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime))); } /** diff --git a/lib/posix/options/pthread.c b/lib/posix/options/pthread.c index da79330be17..bc88ea29fe2 100644 --- a/lib/posix/options/pthread.c +++ b/lib/posix/options/pthread.c @@ -1154,7 +1154,8 @@ int pthread_timedjoin_np(pthread_t pthread, void **status, const struct timespec return EINVAL; } - return pthread_timedjoin_internal(pthread, status, K_MSEC(timespec_to_timeoutms(abstime))); + return pthread_timedjoin_internal( + pthread, status, K_MSEC(timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime))); } /** diff --git a/lib/posix/options/rwlock.c b/lib/posix/options/rwlock.c index 8d1a35584f9..39404c9229f 100644 --- a/lib/posix/options/rwlock.c +++ b/lib/posix/options/rwlock.c @@ -211,7 +211,7 @@ int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, return EINVAL; } - if (read_lock_acquire(rwl, timespec_to_timeoutms(abstime)) != 0U) { + if (read_lock_acquire(rwl, timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime)) != 0U) { ret = ETIMEDOUT; } @@ -282,7 +282,7 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, return EINVAL; } - if (write_lock_acquire(rwl, timespec_to_timeoutms(abstime)) != 0U) { + if (write_lock_acquire(rwl, timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime)) != 0U) { ret = ETIMEDOUT; } diff --git a/lib/posix/options/semaphore.c b/lib/posix/options/semaphore.c index d3787862fb4..77a879a8cb9 100644 --- a/lib/posix/options/semaphore.c +++ b/lib/posix/options/semaphore.c @@ -161,29 +161,12 @@ int sem_post(sem_t *semaphore) */ int sem_timedwait(sem_t *semaphore, struct timespec *abstime) { - int32_t timeout; - struct timespec current; - int64_t current_ms, abstime_ms; - if ((abstime == NULL) || !timespec_is_valid(abstime)) { errno = EINVAL; return -1; } - if (clock_gettime(CLOCK_REALTIME, ¤t) < 0) { - return -1; - } - - abstime_ms = (int64_t)_ts_to_ms(abstime); - current_ms = (int64_t)_ts_to_ms(¤t); - - if (abstime_ms <= current_ms) { - timeout = 0; - } else { - timeout = (int32_t)(abstime_ms - current_ms); - } - - if (k_sem_take(semaphore, K_MSEC(timeout))) { + if (k_sem_take(semaphore, K_MSEC(timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime)))) { errno = ETIMEDOUT; return -1; } diff --git a/tests/lib/c_lib/thrd/src/cnd.c b/tests/lib/c_lib/thrd/src/cnd.c index aaf00f9cb7b..82741e352bb 100644 --- a/tests/lib/c_lib/thrd/src/cnd.c +++ b/tests/lib/c_lib/thrd/src/cnd.c @@ -71,7 +71,7 @@ static int test_cnd_thread_fn(void *arg) struct libc_cnd_fixture *const fixture = arg; if (fixture->do_timedwait) { - zassume_ok(clock_gettime(CLOCK_MONOTONIC, &time_point)); + zassume_ok(clock_gettime(CLOCK_REALTIME, &time_point)); timespec_add_ms(&time_point, WAIT_TIME_MS); res = cnd_timedwait(&fixture->cond, &fixture->mutex, &time_point); } else { diff --git a/tests/posix/common/src/cond.c b/tests/posix/common/src/cond.c index b648ac0abd0..94de124c375 100644 --- a/tests/posix/common/src/cond.c +++ b/tests/posix/common/src/cond.c @@ -55,7 +55,7 @@ ZTEST(cond, test_pthread_condattr) zassert_ok(pthread_condattr_init(&att)); zassert_ok(pthread_condattr_getclock(&att, &clock_id), "pthread_condattr_getclock failed"); - zassert_equal(clock_id, CLOCK_MONOTONIC, "clock attribute not set correctly"); + zassert_equal(clock_id, CLOCK_REALTIME, "clock attribute not set correctly"); zassert_ok(pthread_condattr_setclock(&att, CLOCK_REALTIME), "pthread_condattr_setclock failed"); diff --git a/tests/posix/rwlocks/src/main.c b/tests/posix/rwlocks/src/main.c index 7e0fd101f02..aaf859663bf 100644 --- a/tests/posix/rwlocks/src/main.c +++ b/tests/posix/rwlocks/src/main.c @@ -86,8 +86,9 @@ ZTEST(posix_rw_locks, test_rw_lock) usleep(USEC_PER_MSEC); LOG_DBG("Parent thread acquiring WR lock again"); - time.tv_sec = 2; - time.tv_nsec = 0; + zassert_ok(clock_gettime(CLOCK_REALTIME, &time)); + time.tv_sec += 2; + ret = pthread_rwlock_timedwrlock(&rwlock, &time); if (ret) { zassert_ok(pthread_rwlock_wrlock(&rwlock), "Failed to acquire write lock");