From 1b7c3e026db797d5851dd9531b6c71b1c893cee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Lach?= Date: Thu, 4 Dec 2025 19:03:35 +0100 Subject: [PATCH 1/2] sys/threads: modify threadsinfo declaration JIRA: RTOS-1187 --- include/sys/threads.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); From 97d5041bde279c485c1166b5c41ec1d6eb679490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Lach?= Date: Fri, 5 Dec 2025 21:36:00 +0100 Subject: [PATCH 2/2] time: handle CLOCK_THREAD_CPUTIME_ID --- include/time.h | 7 ++++--- time/time.c | 13 +++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) 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;