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


The following commit(s) were added to refs/heads/master by this push:
     new 3122150b1f1 !include/time.h: Make CLOCKS_PER_SEC 1000000 as XSI 
requires.
3122150b1f1 is described below

commit 3122150b1f1919710b6aa2c592abad98ef755db0
Author: Marco Casaroli <[email protected]>
AuthorDate: Tue Oct 20 20:53:35 2026 +0200

    !include/time.h: Make CLOCKS_PER_SEC 1000000 as XSI requires.
    
    CLOCKS_PER_SEC was the system tick rate (1000000 / CONFIG_USEC_PER_TICK,
    100 by default), and clock() returned system ticks.  XSI requires
    CLOCKS_PER_SEC to be 1000000, so clock() counts microseconds.  The tick
    rate is a different thing: it is CLK_TCK, the unit of times() and of
    sysconf(_SC_CLK_TCK).
    
    Define CLOCKS_PER_SEC as 1000000 and make clock() return microseconds.
    times() and sysconf(_SC_CLK_TCK) keep their values in CLK_TCK units.
    
    BREAKING CHANGE: CLOCKS_PER_SEC is 1000000 and clock() counts
    microseconds.  Code that uses CLOCKS_PER_SEC as the number of system
    ticks per second (for example, to convert seconds into a timeout for
    wd_start() or nxsig_usleep()) must use CLK_TCK or TICK_PER_SEC instead.
    Code that uses a clock() value as a number of ticks (for example
    TICK2MSEC(clock() - start)) must use clock_systime_ticks() in the
    kernel, CLOCK2TICK(clock()) in user space, or convert a difference of
    clock() values with CLOCK2MSEC(), CLOCK2USEC() or CLOCKS_PER_SEC.
    Code that divides clock() by CLOCKS_PER_SEC needs no change.  Programs
    compiled with the old header must be compiled again.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/time.h      | 17 +++++++----------
 sched/clock/clock.c |  7 +++++--
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/include/time.h b/include/time.h
index 244b070d549..325ebb49224 100644
--- a/include/time.h
+++ b/include/time.h
@@ -37,24 +37,21 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Clock tick of the system (frequency Hz).
- *
- * NOTE: This symbolic name CLK_TCK has been removed from the standard.
- *       It is replaced with CLOCKS_PER_SEC.  Both are defined here.
- *
- * The default value is 100Hz, but this default setting can be overridden by
- * defining the clock interval in microseconds as CONFIG_USEC_PER_TICK in the
- * board configuration file.
+/* Clock tick of the system (frequency Hz): the unit of times() and
+ * sysconf(_SC_CLK_TCK).  The default value is 100Hz; CONFIG_USEC_PER_TICK
+ * sets the clock interval in microseconds.
  */
 
 #ifdef CONFIG_USEC_PER_TICK
 #  define CLK_TCK                 (1000000/CONFIG_USEC_PER_TICK)
-#  define CLOCKS_PER_SEC          (1000000/CONFIG_USEC_PER_TICK)
 #else
 #  define CLK_TCK                 (100)
-#  define CLOCKS_PER_SEC          (100)
 #endif
 
+/* The unit of clock(): XSI requires one microsecond */
+
+#define CLOCKS_PER_SEC            (1000000)
+
 /* CLOCK_REALTIME refers to the standard time source.  For most
  * implementations, the standard time source is the system timer interrupt.
  * However, if the platform supports an RTC, then the standard time source
diff --git a/sched/clock/clock.c b/sched/clock/clock.c
index 5d3d478049a..d4a49b34918 100644
--- a/sched/clock/clock.c
+++ b/sched/clock/clock.c
@@ -54,11 +54,14 @@
  *   None
  *
  * Returned Value:
- *   The system time in units of clock ticks is returned.
+ *   The system time in microseconds (CLOCKS_PER_SEC units) is returned.
  *
  ****************************************************************************/
 
 clock_t clock(void)
 {
-  return clock_systime_ticks();
+  struct timespec ts;
+
+  clock_systime_timespec(&ts);
+  return (clock_t)ts.tv_sec * CLOCKS_PER_SEC + ts.tv_nsec / NSEC_PER_USEC;
 }

Reply via email to