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

Fixes zephyrproject-rtos#59954

Signed-off-by: Jai Arora <infolinesoni@gmail.com>
  • Loading branch information
jaiiarora committed Jan 1, 2024
1 parent e67b12e commit 8aac8f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions include/zephyr/posix/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ extern "C" {
#define CLOCK_REALTIME 1
#endif

#ifndef CLOCK_PROCESS_CPUTIME_ID
#define CLOCK_PROCESS_CPUTIME_ID 2
#endif

#ifndef CLOCK_MONOTONIC
#define CLOCK_MONOTONIC 4
#endif
Expand All @@ -88,6 +92,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
22 changes: 22 additions & 0 deletions lib/posix/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <zephyr/posix/sys/time.h>
#include <zephyr/internal/syscall_handler.h>
#include <zephyr/spinlock.h>
#include <zephyr/posix/unistd.h>

/*
* `k_uptime_get` returns a timestamp based on an always increasing
Expand Down Expand Up @@ -195,3 +196,24 @@ 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_PROCESS_CPUTIME_ID
*clock_id = CLOCK_PROCESS_CPUTIME_ID;
return 0;
#endif

/* We don't have a timer for that. */
return ENOENT;
}

0 comments on commit 8aac8f3

Please sign in to comment.