wangchdo commented on code in PR #17642:
URL: https://github.com/apache/nuttx/pull/17642#discussion_r2661670817


##########
sched/hrtimer/hrtimer_process.c:
##########
@@ -69,63 +70,68 @@
 void hrtimer_process(uint64_t now)
 {
   FAR hrtimer_t *hrtimer;
-  uint64_t expired;
-  uint32_t period = 0;
   irqstate_t flags;
+  hrtimer_cb func;
+  uint64_t expired;
+  int cpu = this_cpu();
 
   /* Lock the hrtimer RB-tree to protect access */
 
   flags = spin_lock_irqsave(&g_hrtimer_spinlock);
 
   /* Fetch the earliest active timer */
 
-  hrtimer = (FAR hrtimer_t *)RB_MIN(hrtimer_tree_s, &g_hrtimer_tree);
+  hrtimer = hrtimer_get_first();
 
   while (hrtimer != NULL)
     {
+      func = hrtimer->func;
+
+      /* Ensure the timer callback is valid */
+
+      DEBUGASSERT(func != NULL);
+
+      expired = hrtimer->expired;
+
       /* Check if the timer has expired */
 
-      if (!clock_compare(hrtimer->expired, now))
+      if (!clock_compare(expired, now))
         {
           break;
         }
 
       /* Remove the expired timer from the active tree */
 
-      RB_REMOVE(hrtimer_tree_s, &g_hrtimer_tree, &hrtimer->node);
-
-      /* Ensure the timer callback is valid */
+      hrtimer_remove(hrtimer);
 
-      DEBUGASSERT(hrtimer->func != NULL);
+      g_hrtimer_running[cpu] = hrtimer;
 
-      hrtimer->state = HRTIMER_STATE_RUNNING;
+      /* Leave critical section before invoking the callback */
 
       spin_unlock_irqrestore(&g_hrtimer_spinlock, flags);
 
       /* Invoke the timer callback */
 
-      period = hrtimer->func(hrtimer);
+      expired += func(hrtimer);
+
+      /* Re-enter critical section to update timer state */
 
       flags = spin_lock_irqsave(&g_hrtimer_spinlock);
 
-      if ((hrtimer->state == HRTIMER_STATE_CANCELED) || (period == 0))
-        {
-          /* Timer is canceled or one-shot; mark it inactive */
+      g_hrtimer_running[cpu] = NULL;
 
-          hrtimer->state = HRTIMER_STATE_INACTIVE;
-        }
-      else
+      /* Re-arm periodic timer if not canceled or re-armed concurrently */
+
+      if (hrtimer->expired != UINT64_MAX && hrtimer->expired != expired)

Review Comment:
   OK, I will restore my previous implementation



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to