This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 90e95515826936e850fd72d8bd8c250557567644 Author: Marco Casaroli <[email protected]> AuthorDate: Fri Oct 23 10:49:51 2026 +0200 libs/libc, include/nuttx: Convert clock() to ticks where ticks are used. times(), the user-space work queues and work_timeleft() took the value of clock() as a number of system ticks. clock() counts in CLOCKS_PER_SEC units, which is the tick rate today. Add CLOCK2TICK() to <nuttx/clock.h>: it converts a clock() value to system ticks, and it is the value itself while CLOCKS_PER_SEC equals CLK_TCK. Use it in times() and in the user-space work queues. work_timeleft() uses clock_systime_ticks() in the kernel and in a flat build, where the kernel work queues keep their time, and CLOCK2TICK() in user space. The generated code of times() and of the work queues does not change. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- include/nuttx/clock.h | 12 ++++++++++++ include/nuttx/wqueue.h | 6 +++++- libs/libc/unistd/lib_times.c | 4 +++- libs/libc/wqueue/work_queue.c | 4 ++-- libs/libc/wqueue/work_usrthread.c | 2 +- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/include/nuttx/clock.h b/include/nuttx/clock.h index fd7e14e6a91..bcfd16eda0b 100644 --- a/include/nuttx/clock.h +++ b/include/nuttx/clock.h @@ -205,6 +205,18 @@ #define TICK2HSEC(tick) div_const_roundnearest(tick, TICK_PER_HSEC) #define TICK2SEC(tick) div_const_roundnearest(tick, TICK_PER_SEC) +/* CLOCK2TICK converts a value of clock() (CLOCKS_PER_SEC units) to system + * ticks (CLK_TCK units). It is for code that cannot call + * clock_systime_ticks(), such as user space in a protected or kernel + * build. + */ + +#if CLOCKS_PER_SEC == CLK_TCK +# define CLOCK2TICK(clk) (clk) +#else +# define CLOCK2TICK(clk) ((clk) / (CLOCKS_PER_SEC / CLK_TCK)) +#endif + /* MSEC2SEC */ #define MSEC2SEC(msec) div_const(msec, MSEC_PER_SEC) diff --git a/include/nuttx/wqueue.h b/include/nuttx/wqueue.h index 97becc82d79..9a4bed5bf2c 100644 --- a/include/nuttx/wqueue.h +++ b/include/nuttx/wqueue.h @@ -578,7 +578,11 @@ int work_cancel_sync_wq(FAR struct kwork_wqueue_s *wqueue, * ****************************************************************************/ -#define work_timeleft(work) ((work)->qtime - clock()) +#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__) +# define work_timeleft(work) ((work)->qtime - clock_systime_ticks()) +#else +# define work_timeleft(work) ((work)->qtime - CLOCK2TICK(clock())) +#endif /**************************************************************************** * Name: lpwork_boostpriority diff --git a/libs/libc/unistd/lib_times.c b/libs/libc/unistd/lib_times.c index f6f51f8cefe..b06aab62917 100644 --- a/libs/libc/unistd/lib_times.c +++ b/libs/libc/unistd/lib_times.c @@ -29,6 +29,8 @@ #include <sys/times.h> #include <time.h> +#include <nuttx/clock.h> + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -59,5 +61,5 @@ clock_t times(FAR struct tms *buffer) } memset(buffer, 0, sizeof(*buffer)); - return clock(); + return CLOCK2TICK(clock()); } diff --git a/libs/libc/wqueue/work_queue.c b/libs/libc/wqueue/work_queue.c index bc10986ead8..9bc0bb0001d 100644 --- a/libs/libc/wqueue/work_queue.c +++ b/libs/libc/wqueue/work_queue.c @@ -117,11 +117,11 @@ static int work_qqueue(FAR struct usr_wqueue_s *wqueue, } else if (delay > 0) { - work->qtime = clock() + delay + 1; + work->qtime = CLOCK2TICK(clock()) + delay + 1; } else { - work->qtime = clock(); + work->qtime = CLOCK2TICK(clock()); } /* Insert the work into the wait queue sorted by the expired time. */ diff --git a/libs/libc/wqueue/work_usrthread.c b/libs/libc/wqueue/work_usrthread.c index f74632c2e5d..c0a8a7cf6d6 100644 --- a/libs/libc/wqueue/work_usrthread.c +++ b/libs/libc/wqueue/work_usrthread.c @@ -149,7 +149,7 @@ static pthread_addr_t work_pthread(pthread_addr_t arg) * zero will always execute immediately. */ - tick = clock(); + tick = CLOCK2TICK(clock()); /* Is this delay work ready? */
