xiaoxiang781216 commented on code in PR #17642:
URL: https://github.com/apache/nuttx/pull/17642#discussion_r2663414545
##########
sched/hrtimer/hrtimer_process.c:
##########
@@ -69,63 +70,69 @@
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;
+ uint64_t period = 0;
+ 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);
+ period = 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 (period > 0 && hrtimer->expired == expired)
Review Comment:
After more thought, let hrtimer->func return the next timeout introduce the
huge confusion, it's better to:
1. change hrtimer->func return type from uint64_t to void
2. hrtimer->func need call hrtimer_start manually to form period time
BTW, to help hrtimer user generate the exact period timer, it's better to
pass the current expired value to hrtimer->func:
```
typedef CODE void (*hrtimer_cb)(FAR hrtimer_t *hrtimer, uint64_t expired);
```
--
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]