Browse Source

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 <cfriedt@tenstorrent.com>
pull/89063/head
Chris Friedt 3 months ago committed by Benjamin Cabé
parent
commit
a10f96e153
  1. 2
      lib/posix/options/cond.c
  2. 6
      lib/posix/options/mqueue.c
  3. 2
      lib/posix/options/mutex.c
  4. 3
      lib/posix/options/pthread.c
  5. 4
      lib/posix/options/rwlock.c
  6. 19
      lib/posix/options/semaphore.c
  7. 2
      tests/lib/c_lib/thrd/src/cnd.c
  8. 2
      tests/posix/common/src/cond.c
  9. 5
      tests/posix/rwlocks/src/main.c

2
lib/posix/options/cond.c

@ -256,7 +256,7 @@ int pthread_condattr_init(pthread_condattr_t *att) @@ -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;

6
lib/posix/options/mqueue.c

@ -263,7 +263,8 @@ int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len, @@ -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, @@ -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)));
}
/**

2
lib/posix/options/mutex.c

@ -216,7 +216,7 @@ int pthread_mutex_timedlock(pthread_mutex_t *m, @@ -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)));
}
/**

3
lib/posix/options/pthread.c

@ -1154,7 +1154,8 @@ int pthread_timedjoin_np(pthread_t pthread, void **status, const struct timespec @@ -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)));
}
/**

4
lib/posix/options/rwlock.c

@ -211,7 +211,7 @@ int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, @@ -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, @@ -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;
}

19
lib/posix/options/semaphore.c

@ -161,29 +161,12 @@ int sem_post(sem_t *semaphore) @@ -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, &current) < 0) {
return -1;
}
abstime_ms = (int64_t)_ts_to_ms(abstime);
current_ms = (int64_t)_ts_to_ms(&current);
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;
}

2
tests/lib/c_lib/thrd/src/cnd.c

@ -71,7 +71,7 @@ static int test_cnd_thread_fn(void *arg) @@ -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 {

2
tests/posix/common/src/cond.c

@ -55,7 +55,7 @@ ZTEST(cond, test_pthread_condattr) @@ -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");

5
tests/posix/rwlocks/src/main.c

@ -86,8 +86,9 @@ ZTEST(posix_rw_locks, test_rw_lock) @@ -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");

Loading…
Cancel
Save