Skip to content

posix: Map CLOCK_REALTIME and CLOCK_MONOTONIC to Zephyr clock IDs #93075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/zephyr/sys/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ static inline bool sys_timepoint_expired(k_timepoint_t timepoint)
/** @cond INTERNAL_HIDDEN */
/* forward declaration as workaround for time.h */
struct timespec;

/* Convert a POSIX clock (cast to int) to a sys_clock identifier */
int sys_clock_from_clockid(int clock_id);
/** @endcond INTERNAL_HIDDEN */

/**
Expand Down
16 changes: 16 additions & 0 deletions lib/os/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ static void timespec_from_ticks(uint64_t ticks, struct timespec *ts)
};
}

int sys_clock_from_clockid(int clock_id)
{
switch (clock_id) {
#if defined(CLOCK_REALTIME) || defined(_POSIX_C_SOURCE)
case (int)CLOCK_REALTIME:
return SYS_CLOCK_REALTIME;
#endif
#if defined(CLOCK_MONOTONIC) || defined(_POSIX_MONOTONIC_CLOCK)
case (int)CLOCK_MONOTONIC:
return SYS_CLOCK_MONOTONIC;
#endif
default:
return -EINVAL;
}
}

int sys_clock_gettime(int clock_id, struct timespec *ts)
{
if (!is_valid_clock_id(clock_id)) {
Expand Down
4 changes: 2 additions & 2 deletions lib/posix/options/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int clock_gettime(clockid_t clock_id, struct timespec *ts)
{
int ret;

ret = sys_clock_gettime((int)clock_id, ts);
ret = sys_clock_gettime(sys_clock_from_clockid((int)clock_id), ts);
if (ret < 0) {
errno = -ret;
return -1;
Expand Down Expand Up @@ -61,7 +61,7 @@ int clock_settime(clockid_t clock_id, const struct timespec *tp)
{
int ret;

ret = sys_clock_settime((int)clock_id, tp);
ret = sys_clock_settime(sys_clock_from_clockid((int)clock_id), tp);
if (ret < 0) {
errno = -ret;
return -1;
Expand Down
2 changes: 1 addition & 1 deletion lib/posix/options/clock_selection.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
return -1;
}

ret = sys_clock_nanosleep((int)clock_id, flags, rqtp, rmtp);
ret = sys_clock_nanosleep(sys_clock_from_clockid((int)clock_id), flags, rqtp, rmtp);
if (ret < 0) {
errno = -ret;
return -1;
Expand Down
2 changes: 1 addition & 1 deletion lib/posix/options/timespec_to_timeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ uint32_t timespec_to_timeoutms(int clock_id, const struct timespec *abstime)
{
struct timespec curtime;

if (sys_clock_gettime(clock_id, &curtime) < 0) {
if (sys_clock_gettime(sys_clock_from_clockid(clock_id), &curtime) < 0) {
return 0;
}

Expand Down