This is an automated email from the ASF dual-hosted git repository.

jiuzhudong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 7b67055150b9ff8e2bf9959d6d4490a81fc84178
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Mon Jan 26 16:46:42 2026 +0800

    sched/hrtimer: inline hrtimer_start.
    
    This commit inlined the `hrtimer_start` to allow the compiler to optimize 
at least 1 branch in
    hrtimer_start.
    
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 include/nuttx/hrtimer.h       | 18 ++++++++++++++++--
 sched/hrtimer/hrtimer_start.c | 25 +++++--------------------
 2 files changed, 21 insertions(+), 22 deletions(-)

diff --git a/include/nuttx/hrtimer.h b/include/nuttx/hrtimer.h
index f416cbb18d4..8f6e12365d5 100644
--- a/include/nuttx/hrtimer.h
+++ b/include/nuttx/hrtimer.h
@@ -207,9 +207,23 @@ int hrtimer_cancel_sync(FAR hrtimer_t *hrtimer);
  *   OK on success; a negated errno value on failure.
  ****************************************************************************/
 
+int hrtimer_start_absolute(FAR hrtimer_t *hrtimer, hrtimer_entry_t func,
+                           uint64_t expired);
+
+static inline_function
 int hrtimer_start(FAR hrtimer_t *hrtimer, hrtimer_entry_t func,
-                  uint64_t expired,
-                  enum hrtimer_mode_e mode);
+                  uint64_t expired, enum hrtimer_mode_e mode)
+{
+  /* In most cases, the mode can be evaluated at compile time.
+   * The compiler will optimize the code to avoid the branch.
+   */
+
+  uint64_t next_expired = mode == HRTIMER_MODE_ABS ? expired :
+                          clock_systime_nsec() +
+                          (expired <= HRTIMER_MAX_DELAY ?
+                           expired : HRTIMER_MAX_DELAY);
+  return hrtimer_start_absolute(hrtimer, func, next_expired);
+}
 
 /****************************************************************************
  * Name: hrtimer_gettime
diff --git a/sched/hrtimer/hrtimer_start.c b/sched/hrtimer/hrtimer_start.c
index 8d26a9fafed..8c876ad6093 100644
--- a/sched/hrtimer/hrtimer_start.c
+++ b/sched/hrtimer/hrtimer_start.c
@@ -36,18 +36,17 @@
  ****************************************************************************/
 
 /****************************************************************************
- * Name: hrtimer_start
+ * Name: hrtimer_start_absolute
  *
  * Description:
  *   Start a high-resolution timer to expire after a specified duration
- *   in nanoseconds, either as an absolute or relative time.
+ *   in nanoseconds.
  *
  * Input Parameters:
  *   hrtimer - Pointer to the hrtimer structure.
  *   func    - Expiration callback function.
  *   expired - Expiration time in nanoseconds. Interpretation
  *             depends on mode.
- *   mode    - Timer mode (HRTIMER_MODE_ABS or HRTIMER_MODE_REL).
  *
  * Returned Value:
  *   OK (0) on success, or a negated errno value on failure.
@@ -60,26 +59,12 @@
  *     nanoseconds from the current time.
  ****************************************************************************/
 
-int hrtimer_start(FAR hrtimer_t *hrtimer, hrtimer_entry_t func,
-                  uint64_t expired,
-                  enum hrtimer_mode_e mode)
+int hrtimer_start_absolute(FAR hrtimer_t *hrtimer, hrtimer_entry_t func,
+                           uint64_t expired)
 {
-  uint64_t next_expired;
   irqstate_t flags;
   int ret = OK;
 
-  /* Compute absolute expiration time */
-
-  if (mode == HRTIMER_MODE_ABS)
-    {
-      next_expired = expired;
-    }
-  else
-    {
-      expired = expired <= HRTIMER_MAX_DELAY ? expired : HRTIMER_MAX_DELAY;
-      next_expired = clock_systime_nsec() + expired;
-    }
-
   DEBUGASSERT(hrtimer != NULL);
 
   /* Acquire the lock and seize the ownership of the hrtimer queue. */
@@ -96,7 +81,7 @@ int hrtimer_start(FAR hrtimer_t *hrtimer, hrtimer_entry_t 
func,
     }
 
   hrtimer->func    = func;
-  hrtimer->expired = next_expired;
+  hrtimer->expired = expired;
 
   /* Insert the timer into the hrtimer queue. */
 

Reply via email to