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 db5a48daacf557135b4941b3dab33e5cc900747c Author: ouyangxiangzhen <[email protected]> AuthorDate: Wed Oct 28 20:32:28 2026 +0800 sched/hrtimer: Rework loop exits and running marks in hrtimer_process Bind hrtimer_reprogram() to the "earliest timer has not expired" exit of the processing loop. The tail if (hrtimer) re-tested the emptiness outcome the loop exit had already established; the empty queue now leaves the loop without touching the hardware, and each exit path is one well-predicted branch lighter. The running mark is claimed before the timer is dequeued and released at the end of each processed timer instead of once per batch: the ownership is published before the queue node is touched, and a cancel_sync() waiter is signalled as soon as its own callback completes instead of waiting for the remaining timers of the batch and the final reprogram. Measured on qemu-intel64 with the UP build (where the running marks compile out): unchanged within noise. Assisted-by: mimo-v2.6-pro Signed-off-by: ouyangxiangzhen <[email protected]> --- sched/hrtimer/hrtimer.h | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h index ce1e7f1557d..8c3eeb1db50 100644 --- a/sched/hrtimer/hrtimer.h +++ b/sched/hrtimer/hrtimer.h @@ -686,18 +686,28 @@ static inline_function void hrtimer_process(uint64_t now) * the queue. */ - while ((hrtimer = hrtimer_get_first()) != NULL && - HRTIMER_TIME_BEFORE_EQ(hrtimer->expired, now)) + while ((hrtimer = hrtimer_get_first()) != NULL) { expired = hrtimer->expired; - func = hrtimer->func; - /* Remove the expired timer from the timer queue */ + /* The earliest timer has not expired: arm the hardware for it. + * An empty queue exits the loop without touching the hardware. + */ - hrtimer_remove(hrtimer); + if (!HRTIMER_TIME_BEFORE_EQ(expired, now)) + { + hrtimer_reprogram(expired); + break; + } hrtimer_mark_running(hrtimer, cpu); + func = hrtimer->func; + + /* Remove the expired timer from the timer queue */ + + hrtimer_remove(hrtimer); + /* Leave critical section before invoking the callback */ hrtimer_unlock(flags); @@ -726,17 +736,8 @@ static inline_function void hrtimer_process(uint64_t now) hrtimer->func = func; hrtimer_insert(hrtimer); } - } - hrtimer_unmark_running(cpu); - - /* Arm the hardware for the earliest remaining timer. When the queue - * is empty nothing needs to fire and the hardware is left alone. - */ - - if (hrtimer) - { - hrtimer_reprogram(hrtimer->expired); + hrtimer_unmark_running(cpu); } /* Release the lock and give up the ownership of the hrtimer queue. */
