Skip to content

Commit 88a2cc1

Browse files
committed
posix: declare timespec_to_timeoutms() in posix_clock.h
Provide a single declaration of timespec_to_timeoutms() (which is a private function), in the private header file posix_clock.h . Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent 0d702f3 commit 88a2cc1

File tree

7 files changed

+22
-29
lines changed

7 files changed

+22
-29
lines changed

lib/posix/options/cond.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
LOG_MODULE_REGISTER(pthread_cond, CONFIG_PTHREAD_COND_LOG_LEVEL);
1717

18-
int64_t timespec_to_timeoutms(const struct timespec *abstime);
19-
2018
__pinned_bss
2119
static struct k_condvar posix_cond_pool[CONFIG_MAX_PTHREAD_COND_COUNT];
2220

@@ -168,7 +166,7 @@ int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut)
168166

169167
int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struct timespec *abstime)
170168
{
171-
return cond_wait(cv, mut, K_MSEC((int32_t)timespec_to_timeoutms(abstime)));
169+
return cond_wait(cv, mut, K_MSEC(timespec_to_timeoutms(abstime)));
172170
}
173171

174172
int pthread_cond_init(pthread_cond_t *cvar, const pthread_condattr_t *att)

lib/posix/options/mqueue.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
*
55
* SPDX-License-Identifier: Apache-2.0
66
*/
7+
8+
#include "posix_clock.h"
9+
710
#include <zephyr/kernel.h>
811
#include <errno.h>
912
#include <string.h>
@@ -34,7 +37,6 @@ K_SEM_DEFINE(mq_sem, 1, 1);
3437
/* Initialize the list */
3538
sys_slist_t mq_list = SYS_SLIST_STATIC_INIT(&mq_list);
3639

37-
int64_t timespec_to_timeoutms(const struct timespec *abstime);
3840
static mqueue_object *find_in_list(const char *name);
3941
static int32_t send_message(mqueue_desc *mqd, const char *msg_ptr, size_t msg_len,
4042
k_timeout_t timeout);
@@ -255,9 +257,8 @@ int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
255257
unsigned int msg_prio, const struct timespec *abstime)
256258
{
257259
mqueue_desc *mqd = (mqueue_desc *)mqdes;
258-
int32_t timeout = (int32_t) timespec_to_timeoutms(abstime);
259260

260-
return send_message(mqd, msg_ptr, msg_len, K_MSEC(timeout));
261+
return send_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime)));
261262
}
262263

263264
/**
@@ -286,9 +287,8 @@ int mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
286287
unsigned int *msg_prio, const struct timespec *abstime)
287288
{
288289
mqueue_desc *mqd = (mqueue_desc *)mqdes;
289-
int32_t timeout = (int32_t) timespec_to_timeoutms(abstime);
290290

291-
return receive_message(mqd, msg_ptr, msg_len, K_MSEC(timeout));
291+
return receive_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime)));
292292
}
293293

294294
/**

lib/posix/options/mutex.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* SPDX-License-Identifier: Apache-2.0
66
*/
77

8+
#include "posix_clock.h"
89
#include "posix_internal.h"
910

1011
#include <zephyr/init.h>
@@ -18,8 +19,6 @@ LOG_MODULE_REGISTER(pthread_mutex, CONFIG_PTHREAD_MUTEX_LOG_LEVEL);
1819

1920
static SYS_SEM_DEFINE(lock, 1, 1);
2021

21-
int64_t timespec_to_timeoutms(const struct timespec *abstime);
22-
2322
#define MUTEX_MAX_REC_LOCK 32767
2423

2524
/*
@@ -212,8 +211,7 @@ int pthread_mutex_trylock(pthread_mutex_t *m)
212211
int pthread_mutex_timedlock(pthread_mutex_t *m,
213212
const struct timespec *abstime)
214213
{
215-
int32_t timeout = (int32_t)timespec_to_timeoutms(abstime);
216-
return acquire_mutex(m, K_MSEC(timeout));
214+
return acquire_mutex(m, K_MSEC(timespec_to_timeoutms(abstime)));
217215
}
218216

219217
/**

lib/posix/options/posix_clock.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/*
22
* Copyright (c) 2023, Meta
3+
* Copyright (c) 2025 Tenstorrent AI ULC
34
*
45
* SPDX-License-Identifier: Apache-2.0
56
*/
67

78
#ifndef ZEPHYR_LIB_POSIX_POSIX_CLOCK_H_
89
#define ZEPHYR_LIB_POSIX_POSIX_CLOCK_H_
910

11+
#include <stdint.h>
1012
#include <time.h>
1113

12-
#include <zephyr/kernel.h>
13-
#include <zephyr/posix/posix_types.h>
14+
uint32_t timespec_to_timeoutms(const struct timespec *abstime);
1415

1516
__syscall int __posix_clock_get_base(clockid_t clock_id, struct timespec *ts);
1617

lib/posix/options/pthread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* SPDX-License-Identifier: Apache-2.0
66
*/
77

8+
#include "posix_clock.h"
89
#include "posix_internal.h"
910
#include "pthread_sched.h"
1011

@@ -81,7 +82,6 @@ BUILD_ASSERT((PTHREAD_CANCEL_ENABLE == 0 || PTHREAD_CANCEL_DISABLE == 0) &&
8182
BUILD_ASSERT(CONFIG_POSIX_PTHREAD_ATTR_STACKSIZE_BITS + CONFIG_POSIX_PTHREAD_ATTR_GUARDSIZE_BITS <=
8283
32);
8384

84-
int64_t timespec_to_timeoutms(const struct timespec *abstime);
8585
static void posix_thread_recycle(void);
8686

8787
__pinned_data

lib/posix/options/rwlock.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include "posix_clock.h"
78
#include "posix_internal.h"
89

910
#include <zephyr/init.h>
@@ -27,9 +28,8 @@ struct posix_rwlockattr {
2728
bool pshared: 1;
2829
};
2930

30-
int64_t timespec_to_timeoutms(const struct timespec *abstime);
31-
static uint32_t read_lock_acquire(struct posix_rwlock *rwl, int32_t timeout);
32-
static uint32_t write_lock_acquire(struct posix_rwlock *rwl, int32_t timeout);
31+
static uint32_t read_lock_acquire(struct posix_rwlock *rwl, uint32_t timeout);
32+
static uint32_t write_lock_acquire(struct posix_rwlock *rwl, uint32_t timeout);
3333

3434
LOG_MODULE_REGISTER(pthread_rwlock, CONFIG_PTHREAD_RWLOCK_LOG_LEVEL);
3535

@@ -198,22 +198,19 @@ int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock)
198198
int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
199199
const struct timespec *abstime)
200200
{
201-
int32_t timeout;
202201
uint32_t ret = 0U;
203202
struct posix_rwlock *rwl;
204203

205204
if (abstime->tv_nsec < 0 || abstime->tv_nsec > NSEC_PER_SEC) {
206205
return EINVAL;
207206
}
208207

209-
timeout = (int32_t) timespec_to_timeoutms(abstime);
210-
211208
rwl = get_posix_rwlock(*rwlock);
212209
if (rwl == NULL) {
213210
return EINVAL;
214211
}
215212

216-
if (read_lock_acquire(rwl, timeout) != 0U) {
213+
if (read_lock_acquire(rwl, timespec_to_timeoutms(abstime)) != 0U) {
217214
ret = ETIMEDOUT;
218215
}
219216

@@ -271,22 +268,19 @@ int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock)
271268
int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
272269
const struct timespec *abstime)
273270
{
274-
int32_t timeout;
275271
uint32_t ret = 0U;
276272
struct posix_rwlock *rwl;
277273

278274
if (abstime->tv_nsec < 0 || abstime->tv_nsec > NSEC_PER_SEC) {
279275
return EINVAL;
280276
}
281277

282-
timeout = (int32_t) timespec_to_timeoutms(abstime);
283-
284278
rwl = get_posix_rwlock(*rwlock);
285279
if (rwl == NULL) {
286280
return EINVAL;
287281
}
288282

289-
if (write_lock_acquire(rwl, timeout) != 0U) {
283+
if (write_lock_acquire(rwl, timespec_to_timeoutms(abstime)) != 0U) {
290284
ret = ETIMEDOUT;
291285
}
292286

@@ -345,7 +339,7 @@ int pthread_rwlock_unlock(pthread_rwlock_t *rwlock)
345339
return 0;
346340
}
347341

348-
static uint32_t read_lock_acquire(struct posix_rwlock *rwl, int32_t timeout)
342+
static uint32_t read_lock_acquire(struct posix_rwlock *rwl, uint32_t timeout)
349343
{
350344
uint32_t ret = 0U;
351345

@@ -360,7 +354,7 @@ static uint32_t read_lock_acquire(struct posix_rwlock *rwl, int32_t timeout)
360354
return ret;
361355
}
362356

363-
static uint32_t write_lock_acquire(struct posix_rwlock *rwl, int32_t timeout)
357+
static uint32_t write_lock_acquire(struct posix_rwlock *rwl, uint32_t timeout)
364358
{
365359
uint32_t ret = 0U;
366360
int64_t elapsed_time, st_time = k_uptime_get();

lib/posix/options/timespec_to_timeout.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include "posix_clock.h"
8+
79
#include <zephyr/kernel.h>
810
#include <ksched.h>
911
#include <zephyr/posix/time.h>
1012

11-
int64_t timespec_to_timeoutms(const struct timespec *abstime)
13+
uint32_t timespec_to_timeoutms(const struct timespec *abstime)
1214
{
1315
int64_t milli_secs, secs, nsecs;
1416
struct timespec curtime;

0 commit comments

Comments
 (0)