diff --git a/include/sys/threads.h b/include/sys/threads.h index 04d47197..3b0cfba7 100644 --- a/include/sys/threads.h +++ b/include/sys/threads.h @@ -83,7 +83,7 @@ static inline int beginthread(void (*start)(void *), unsigned int priority, void } -extern int threadsinfo(int n, threadinfo_t *info); +extern int threadsinfo(int *tid, unsigned int flags, int n, threadinfo_t *info); extern int priority(int priority); diff --git a/include/time.h b/include/time.h index ce30af5c..9bf91aeb 100644 --- a/include/time.h +++ b/include/time.h @@ -26,9 +26,10 @@ #define CLOCKS_PER_SEC 1000000 -#define CLOCK_MONOTONIC 0 -#define CLOCK_MONOTONIC_RAW 1 -#define CLOCK_REALTIME 2 +#define CLOCK_MONOTONIC (0U) +#define CLOCK_MONOTONIC_RAW (1U) +#define CLOCK_REALTIME (2U) +#define CLOCK_THREAD_CPUTIME_ID (3U) #ifdef __cplusplus diff --git a/time/time.c b/time/time.c index a05178ad..bb4eadd3 100644 --- a/time/time.c +++ b/time/time.c @@ -14,6 +14,7 @@ */ #include +#include #include #include #include @@ -92,21 +93,29 @@ time_t time(time_t *tp) int clock_gettime(clockid_t clk_id, struct timespec *tp) { time_t now, offs; + int tid; + threadinfo_t info; if (tp == NULL) { errno = EINVAL; return -1; } - if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW) { + if (clk_id != CLOCK_REALTIME && clk_id != CLOCK_MONOTONIC && clk_id != CLOCK_MONOTONIC_RAW && clk_id != CLOCK_THREAD_CPUTIME_ID) { errno = EINVAL; return -1; } gettime(&now, &offs); - if (clk_id == CLOCK_REALTIME) + if (clk_id == CLOCK_REALTIME) { now += offs; + } + else if (clk_id == CLOCK_THREAD_CPUTIME_ID) { + tid = gettid(); + threadsinfo(&tid, PH_THREADINFO_CPUTIME, 1, &info); + now = info.cpuTime; + } tp->tv_sec = now / (1000 * 1000); now -= tp->tv_sec * 1000 * 1000;