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 eaa708fa7738849b4cbae7fc0742452db27c2cb6
Author: chao an <[email protected]>
AuthorDate: Wed Oct 28 11:42:21 2026 +0800

    sched/wdog: Fast path watchdog insertion
    
    Avoid scanning the active watchdog list when a timer belongs at the head or
    tail, or when the list is empty. This keeps common ordered insertions out of
    the critical-section linear scan while preserving expiration ordering.
    
    Signed-off-by: chao an <[email protected]>
---
 sched/wdog/wd_start.c | 37 ++++++++++++++++++++++++++++++++-----
 1 file changed, 32 insertions(+), 5 deletions(-)

diff --git a/sched/wdog/wd_start.c b/sched/wdog/wd_start.c
index 36939523054..68f0825832e 100644
--- a/sched/wdog/wd_start.c
+++ b/sched/wdog/wd_start.c
@@ -192,10 +192,41 @@ bool wd_insert(FAR struct wdog_s *wdog, clock_t expired,
   FAR struct wdog_s *curr;
   FAR struct wdog_s *head;
 
+  wdog->func = wdentry;
+  up_getpicbase(&wdog->picbase);
+  wdog->arg = arg;
+  wdog->expired = expired;
+
+  /* Empty list: insert directly as the first active watchdog. */
+
+  if (list_is_empty(&g_wdactivelist))
+    {
+      list_add_head(&g_wdactivelist, &wdog->node);
+      return true;
+    }
+
   /* Traverse the watchdog list */
 
   head = list_first_entry(&g_wdactivelist, struct wdog_s, node);
 
+  /* New earliest expiration: insert at the head and reassess timer. */
+
+  if (!clock_compare(head->expired, expired))
+    {
+      list_add_head(&g_wdactivelist, &wdog->node);
+      return true;
+    }
+
+  curr = list_last_entry(&g_wdactivelist, struct wdog_s, node);
+
+  /* New latest expiration: append at the tail without scanning. */
+
+  if (clock_compare(curr->expired, expired))
+    {
+      list_add_tail(&g_wdactivelist, &wdog->node);
+      return false;
+    }
+
   list_for_every_entry(&g_wdactivelist, curr, struct wdog_s, node)
     {
       /* Until curr->expired has not timed out relative to expired */
@@ -215,11 +246,6 @@ bool wd_insert(FAR struct wdog_s *wdog, clock_t expired,
 
   list_add_before(&curr->node, &wdog->node);
 
-  wdog->func = wdentry;
-  up_getpicbase(&wdog->picbase);
-  wdog->arg = arg;
-  wdog->expired = expired;
-
   /* Return whether the head of the watchdog list has changed. */
 
   return head == curr;
@@ -369,6 +395,7 @@ uint64_t wd_timer(const hrtimer_t *timer, uint64_t expired)
   clock_t  tick = div_const(expired, NSEC_PER_TICK);
   clock_t delay = wd_expiration(tick) - tick;
   uint64_t nsec = TICK2NSEC(delay);
+
   return nsec <= HRTIMER_MAX_DELAY ? nsec : HRTIMER_MAX_DELAY;
 }
 #endif

Reply via email to