Skip to content

Commit 9ff91f1

Browse files
committed
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>
1 parent ce4942b commit 9ff91f1

File tree

9 files changed

+16
-29
lines changed

9 files changed

+16
-29
lines changed

lib/posix/options/cond.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ int pthread_condattr_init(pthread_condattr_t *att)
256256
return EINVAL;
257257
}
258258

259-
attr->clock = CLOCK_MONOTONIC;
259+
attr->clock = CLOCK_REALTIME;
260260
attr->initialized = true;
261261

262262
return 0;

lib/posix/options/mqueue.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
263263
return -1;
264264
}
265265

266-
return send_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime)));
266+
return send_message(mqd, msg_ptr, msg_len,
267+
K_MSEC(timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime)));
267268
}
268269

269270
/**
@@ -298,7 +299,8 @@ int mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
298299
return -1;
299300
}
300301

301-
return receive_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime)));
302+
return receive_message(mqd, msg_ptr, msg_len,
303+
K_MSEC(timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime)));
302304
}
303305

304306
/**

lib/posix/options/mutex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ int pthread_mutex_timedlock(pthread_mutex_t *m,
216216
return EINVAL;
217217
}
218218

219-
return acquire_mutex(m, K_MSEC(timespec_to_timeoutms(abstime)));
219+
return acquire_mutex(m, K_MSEC(timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime)));
220220
}
221221

222222
/**

lib/posix/options/pthread.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,8 @@ int pthread_timedjoin_np(pthread_t pthread, void **status, const struct timespec
11541154
return EINVAL;
11551155
}
11561156

1157-
return pthread_timedjoin_internal(pthread, status, K_MSEC(timespec_to_timeoutms(abstime)));
1157+
return pthread_timedjoin_internal(
1158+
pthread, status, K_MSEC(timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime)));
11581159
}
11591160

11601161
/**

lib/posix/options/rwlock.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
211211
return EINVAL;
212212
}
213213

214-
if (read_lock_acquire(rwl, timespec_to_timeoutms(abstime)) != 0U) {
214+
if (read_lock_acquire(rwl, timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime)) != 0U) {
215215
ret = ETIMEDOUT;
216216
}
217217

@@ -282,7 +282,7 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
282282
return EINVAL;
283283
}
284284

285-
if (write_lock_acquire(rwl, timespec_to_timeoutms(abstime)) != 0U) {
285+
if (write_lock_acquire(rwl, timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime)) != 0U) {
286286
ret = ETIMEDOUT;
287287
}
288288

lib/posix/options/semaphore.c

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,29 +161,12 @@ int sem_post(sem_t *semaphore)
161161
*/
162162
int sem_timedwait(sem_t *semaphore, struct timespec *abstime)
163163
{
164-
int32_t timeout;
165-
struct timespec current;
166-
int64_t current_ms, abstime_ms;
167-
168164
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
169165
errno = EINVAL;
170166
return -1;
171167
}
172168

173-
if (clock_gettime(CLOCK_REALTIME, &current) < 0) {
174-
return -1;
175-
}
176-
177-
abstime_ms = (int64_t)_ts_to_ms(abstime);
178-
current_ms = (int64_t)_ts_to_ms(&current);
179-
180-
if (abstime_ms <= current_ms) {
181-
timeout = 0;
182-
} else {
183-
timeout = (int32_t)(abstime_ms - current_ms);
184-
}
185-
186-
if (k_sem_take(semaphore, K_MSEC(timeout))) {
169+
if (k_sem_take(semaphore, K_MSEC(timespec_to_clock_timeoutms(CLOCK_REALTIME, abstime)))) {
187170
errno = ETIMEDOUT;
188171
return -1;
189172
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static int test_cnd_thread_fn(void *arg)
7171
struct libc_cnd_fixture *const fixture = arg;
7272

7373
if (fixture->do_timedwait) {
74-
zassume_ok(clock_gettime(CLOCK_MONOTONIC, &time_point));
74+
zassume_ok(clock_gettime(CLOCK_REALTIME, &time_point));
7575
timespec_add_ms(&time_point, WAIT_TIME_MS);
7676
res = cnd_timedwait(&fixture->cond, &fixture->mutex, &time_point);
7777
} else {

tests/posix/common/src/cond.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ ZTEST(cond, test_pthread_condattr)
5555
zassert_ok(pthread_condattr_init(&att));
5656

5757
zassert_ok(pthread_condattr_getclock(&att, &clock_id), "pthread_condattr_getclock failed");
58-
zassert_equal(clock_id, CLOCK_MONOTONIC, "clock attribute not set correctly");
58+
zassert_equal(clock_id, CLOCK_REALTIME, "clock attribute not set correctly");
5959

6060
zassert_ok(pthread_condattr_setclock(&att, CLOCK_REALTIME),
6161
"pthread_condattr_setclock failed");

tests/posix/rwlocks/src/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ ZTEST(posix_rw_locks, test_rw_lock)
8686
usleep(USEC_PER_MSEC);
8787
LOG_DBG("Parent thread acquiring WR lock again");
8888

89-
time.tv_sec = 2;
90-
time.tv_nsec = 0;
89+
zassert_ok(clock_gettime(CLOCK_REALTIME, &time));
90+
time.tv_sec += 2;
91+
9192
ret = pthread_rwlock_timedwrlock(&rwlock, &time);
9293
if (ret) {
9394
zassert_ok(pthread_rwlock_wrlock(&rwlock), "Failed to acquire write lock");

0 commit comments

Comments
 (0)