Skip to content

Commit

Permalink
posix: implement clock_getcpuclockid function
Browse files Browse the repository at this point in the history
implements clock_getcpuclockid function and
adds zephyr test.

Fixes zephyrproject-rtos#59954

Signed-off-by: Jai Arora <infolinesoni@gmail.com>
  • Loading branch information
jaiiarora committed Dec 28, 2023
1 parent 87d056b commit f7a4377
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/zephyr/posix/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ int clock_gettime(clockid_t clock_id, struct timespec *ts);
__syscall int clock_gettime(clockid_t clock_id, struct timespec *ts);
#endif /* CONFIG_ARCH_POSIX */
int clock_settime(clockid_t clock_id, const struct timespec *ts);
int clock_getcpuclockid(pid_t pid, clockid_t *clock_id);
/* Timer APIs */
int timer_create(clockid_t clockId, struct sigevent *evp, timer_t *timerid);
int timer_delete(timer_t timerid);
Expand Down
25 changes: 25 additions & 0 deletions lib/posix/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,28 @@ int gettimeofday(struct timeval *tv, void *tz)

return res;
}

/**
* @brief Get current real time.
*
* See IEEE 1003.1
*/
int clock_getcpuclockid(pid_t pid, clockid_t *clock_id)
{
/* We don't allow any process ID but our own. */
if (pid != 0 && pid != getpid())
return EPERM;

#ifdef CLOCK_MONOTONIC
*clock_id = CLOCK_MONOTONIC;
return 0;
#else
#ifdef CLOCK_REALTIME
*clock_id = CLOCK_REALTIME;
return 0;
#endif
#endif

/* We don't have a timer for that. */
return ENOENT;
}
8 changes: 8 additions & 0 deletions tests/posix/common/src/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

ZTEST(posix_apis, test_clock)
{
int ret;
int64_t nsecs_elapsed, secs_elapsed;
struct timespec ts, te;
clockid_t clock_id;

printk("POSIX clock APIs\n");

Expand All @@ -40,6 +42,12 @@ ZTEST(posix_apis, test_clock)
zassert_equal(secs_elapsed, SLEEP_SECONDS,
"POSIX clock API test failed");

ret = clock_getcpuclockid(0, &clock_id);
zassert_equal(ret, 0, "POSIX clock_getcpuclock id failed");

ret = clock_getcpuclockid((pid_t)2482, &clock_id);
zassert_equal(ret, EPERM, "POSIX clock_getcpuclock id failed");

printk("POSIX clock APIs test done\n");
}

Expand Down

0 comments on commit f7a4377

Please sign in to comment.