Skip to content

Commit 4b90816

Browse files
M-Moawadnashif
authored andcommitted
posix: Map CLOCK_REALTIME and CLOCK_MONOTONIC to Zephyr clock IDs
Some toolchains may define CLOCK_REALTIME and CLOCK_MONOTONIC in their libc headers with values that differ from Zephyr's internal SYS_CLOCK_REALTIME and SYS_CLOCK_MONOTONIC. To ensure consistent behavior across all boards and toolchains, Introduce a helper function to map CLOCK_REALTIME and CLOCK_MONOTONIC to Zephyr's internal clock IDs (SYS_CLOCK_REALTIME and SYS_CLOCK_MONOTONIC). This prevents mismatched clock IDs being passed to the kernel, avoiding invalid clockid errors when using functions like clock_gettime(). Signed-off-by: Mohamed Moawad <[email protected]>
1 parent daaacd2 commit 4b90816

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

include/zephyr/sys/clock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,9 @@ static inline bool sys_timepoint_expired(k_timepoint_t timepoint)
378378
/** @cond INTERNAL_HIDDEN */
379379
/* forward declaration as workaround for time.h */
380380
struct timespec;
381+
382+
/* Convert a POSIX clock (cast to int) to a sys_clock identifier */
383+
int sys_clock_from_clockid(int clock_id);
381384
/** @endcond INTERNAL_HIDDEN */
382385

383386
/**

lib/os/clock.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ static void timespec_from_ticks(uint64_t ticks, struct timespec *ts)
5050
};
5151
}
5252

53+
int sys_clock_from_clockid(int clock_id)
54+
{
55+
switch (clock_id) {
56+
#if defined(CLOCK_REALTIME) || defined(_POSIX_C_SOURCE)
57+
case (int)CLOCK_REALTIME:
58+
return SYS_CLOCK_REALTIME;
59+
#endif
60+
#if defined(CLOCK_MONOTONIC) || defined(_POSIX_MONOTONIC_CLOCK)
61+
case (int)CLOCK_MONOTONIC:
62+
return SYS_CLOCK_MONOTONIC;
63+
#endif
64+
default:
65+
return -EINVAL;
66+
}
67+
}
68+
5369
int sys_clock_gettime(int clock_id, struct timespec *ts)
5470
{
5571
if (!is_valid_clock_id(clock_id)) {

lib/posix/options/clock.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int clock_gettime(clockid_t clock_id, struct timespec *ts)
1818
{
1919
int ret;
2020

21-
ret = sys_clock_gettime((int)clock_id, ts);
21+
ret = sys_clock_gettime(sys_clock_from_clockid((int)clock_id), ts);
2222
if (ret < 0) {
2323
errno = -ret;
2424
return -1;
@@ -61,7 +61,7 @@ int clock_settime(clockid_t clock_id, const struct timespec *tp)
6161
{
6262
int ret;
6363

64-
ret = sys_clock_settime((int)clock_id, tp);
64+
ret = sys_clock_settime(sys_clock_from_clockid((int)clock_id), tp);
6565
if (ret < 0) {
6666
errno = -ret;
6767
return -1;

lib/posix/options/clock_selection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
2626
return -1;
2727
}
2828

29-
ret = sys_clock_nanosleep((int)clock_id, flags, rqtp, rmtp);
29+
ret = sys_clock_nanosleep(sys_clock_from_clockid((int)clock_id), flags, rqtp, rmtp);
3030
if (ret < 0) {
3131
errno = -ret;
3232
return -1;

lib/posix/options/timespec_to_timeout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ uint32_t timespec_to_timeoutms(int clock_id, const struct timespec *abstime)
1717
{
1818
struct timespec curtime;
1919

20-
if (sys_clock_gettime(clock_id, &curtime) < 0) {
20+
if (sys_clock_gettime(sys_clock_from_clockid(clock_id), &curtime) < 0) {
2121
return 0;
2222
}
2323

0 commit comments

Comments
 (0)