Skip to content

posix: clock: implement clock_getres() #70525

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

Merged
merged 3 commits into from
Mar 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion doc/services/portability/posix/option_groups/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ POSIX_TIMERS
:header: API, Supported
:widths: 50,10

clock_getres(),
clock_getres(),yes
clock_gettime(),yes
clock_settime(),yes
nanosleep(),yes
Expand Down
1 change: 1 addition & 0 deletions include/zephyr/posix/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ static inline int32_t _ts_to_ms(const struct timespec *to)
}

int clock_gettime(clockid_t clock_id, struct timespec *ts);
int clock_getres(clockid_t clock_id, struct timespec *ts);
int clock_settime(clockid_t clock_id, const struct timespec *ts);
int clock_getcpuclockid(pid_t pid, clockid_t *clock_id);
/* Timer APIs */
Expand Down
22 changes: 22 additions & 0 deletions lib/posix/options/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,28 @@ int clock_gettime(clockid_t clock_id, struct timespec *ts)
return 0;
}

int clock_getres(clockid_t clock_id, struct timespec *res)
{
BUILD_ASSERT(CONFIG_SYS_CLOCK_TICKS_PER_SEC > 0 &&
CONFIG_SYS_CLOCK_TICKS_PER_SEC <= NSEC_PER_SEC,
"CONFIG_SYS_CLOCK_TICKS_PER_SEC must be > 0 and <= NSEC_PER_SEC");

if (!(clock_id == CLOCK_MONOTONIC || clock_id == CLOCK_REALTIME ||
clock_id == CLOCK_PROCESS_CPUTIME_ID)) {
errno = EINVAL;
return -1;
}

if (res != NULL) {
*res = (struct timespec){
.tv_sec = 0,
.tv_nsec = NSEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC,
};
}

return 0;
}

/**
* @brief Set the time of the specified clock.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/posix/common/src/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,48 @@ ZTEST(clock, test_clock_getcpuclockid)
zassert_equal(ret, EPERM, "POSIX clock_getcpuclock id failed");
}

ZTEST(clock, test_clock_getres)
{
int ret;
struct timespec res;
const struct timespec one_ns = {
.tv_sec = 0,
.tv_nsec = 1,
};

struct arg {
clockid_t clock_id;
struct timespec *res;
int expect;
};

const struct arg args[] = {
/* permuting over "invalid" inputs */
{CLOCK_INVALID, NULL, -1},
{CLOCK_INVALID, &res, -1},
{CLOCK_REALTIME, NULL, 0},
{CLOCK_MONOTONIC, NULL, 0},
{CLOCK_PROCESS_CPUTIME_ID, NULL, 0},

/* all valid inputs */
{CLOCK_REALTIME, &res, 0},
{CLOCK_MONOTONIC, &res, 0},
{CLOCK_PROCESS_CPUTIME_ID, &res, 0},
};

ARRAY_FOR_EACH_PTR(args, arg) {
errno = 0;
res = (struct timespec){0};
ret = clock_getres(arg->clock_id, arg->res);
zassert_equal(ret, arg->expect);
if (ret != 0) {
zassert_equal(errno, EINVAL);
continue;
}
if (arg->res != NULL) {
zassert_true(tp_ge(arg->res, &one_ns));
}
}
}

ZTEST_SUITE(clock, NULL, NULL, NULL, NULL, NULL);