wangchdo commented on code in PR #17642:
URL: https://github.com/apache/nuttx/pull/17642#discussion_r2663471450
##########
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:
Agree.
Using void return value is what I thought of at the beginning. I changed to
add return value according to review comments and since then my handling of
return value has been challenged by Fix-Point constantly...
Anyway, I will remove the return value and update the state-machine diagram
accordingly, thanks for reminding this better solution to eliminate confusion.
I will also pass the current expired value to hritmer->func, this is not
what I though of at the beginning, thanks for your suggestion.
--
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]