wangchdo commented on code in PR #17573:
URL: https://github.com/apache/nuttx/pull/17573#discussion_r2647358464
##########
sched/hrtimer/hrtimer_process.c:
##########
@@ -100,27 +99,81 @@ void hrtimer_process(uint64_t now)
hrtimer->state = HRTIMER_STATE_RUNNING;
+#ifdef CONFIG_SMP
+ hrtimer->cpus++;
+
+ /* cpus is a running reference counter and must never wrap */
+
+ DEBUGASSERT(hrtimer->cpus != 0);
+#endif
+
+ /* Leave critical section before invoking the callback */
+
spin_unlock_irqrestore(&g_hrtimer_spinlock, flags);
- /* Invoke the timer callback */
+ /* Execute the timer callback */
period = hrtimer->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 */
+#ifdef CONFIG_SMP
+ hrtimer->cpus--;
+#endif
- hrtimer->state = HRTIMER_STATE_INACTIVE;
- }
- else
+ switch (hrtimer->state)
{
- /* Restart the periodic timer */
-
- hrtimer->expired += period;
- hrtimer->state = HRTIMER_STATE_ARMED;
- RB_INSERT(hrtimer_tree_s, &g_hrtimer_tree, &hrtimer->node);
+ case HRTIMER_STATE_RUNNING:
+ {
+ /* Timer callback completed normally */
+
+ if (period > 0)
+ {
+ /* Periodic timer: re-arm with the next expiration */
+
+ hrtimer->expired += period;
+ hrtimer->state = HRTIMER_STATE_ARMED;
+
+ RB_INSERT(hrtimer_tree_s, &g_hrtimer_tree,
+ &hrtimer->node);
+ }
+ else
+ {
+ /* One-shot timer: deactivate when last instance ends */
+#ifdef CONFIG_SMP
+ if (hrtimer->cpus == 0)
+ {
+#endif
+ hrtimer->state = HRTIMER_STATE_INACTIVE;
+#ifdef CONFIG_SMP
+ }
+#endif
Review Comment:
Done
##########
sched/hrtimer/hrtimer_process.c:
##########
@@ -100,27 +99,81 @@ void hrtimer_process(uint64_t now)
hrtimer->state = HRTIMER_STATE_RUNNING;
+#ifdef CONFIG_SMP
+ hrtimer->cpus++;
+
+ /* cpus is a running reference counter and must never wrap */
+
+ DEBUGASSERT(hrtimer->cpus != 0);
+#endif
+
+ /* Leave critical section before invoking the callback */
+
spin_unlock_irqrestore(&g_hrtimer_spinlock, flags);
- /* Invoke the timer callback */
+ /* Execute the timer callback */
period = hrtimer->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 */
+#ifdef CONFIG_SMP
+ hrtimer->cpus--;
+#endif
- hrtimer->state = HRTIMER_STATE_INACTIVE;
- }
- else
+ switch (hrtimer->state)
{
- /* Restart the periodic timer */
-
- hrtimer->expired += period;
- hrtimer->state = HRTIMER_STATE_ARMED;
- RB_INSERT(hrtimer_tree_s, &g_hrtimer_tree, &hrtimer->node);
+ case HRTIMER_STATE_RUNNING:
+ {
+ /* Timer callback completed normally */
+
+ if (period > 0)
+ {
+ /* Periodic timer: re-arm with the next expiration */
+
+ hrtimer->expired += period;
+ hrtimer->state = HRTIMER_STATE_ARMED;
+
+ RB_INSERT(hrtimer_tree_s, &g_hrtimer_tree,
+ &hrtimer->node);
+ }
+ else
+ {
+ /* One-shot timer: deactivate when last instance ends */
+#ifdef CONFIG_SMP
+ if (hrtimer->cpus == 0)
+ {
+#endif
+ hrtimer->state = HRTIMER_STATE_INACTIVE;
+#ifdef CONFIG_SMP
+ }
+#endif
+ }
+
+ break;
+ }
+
+ case HRTIMER_STATE_CANCELED:
+ {
+ /* Timer was canceled during callback execution */
+
+#ifdef CONFIG_SMP
Review Comment:
Done
##########
sched/sched/sched_processtimer.c:
##########
@@ -44,11 +44,139 @@
#include "sched/sched.h"
#include "wdog/wdog.h"
#include "clock/clock.h"
+#include "hrtimer/hrtimer.h"
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#ifdef CONFIG_HRTIMER
+
+/* High-resolution timer instance used by the scheduler.
+ *
+ * This timer is responsible for driving scheduler time events,
+ * including periodic ticks in non-tickless mode or dynamic
+ * expirations in tickless mode.
+ */
+
+static hrtimer_t g_nxsched_hrtimer;
+
+/* Indicates whether the scheduler high-resolution timer has been
+ * initialized. Initialization is performed lazily on first use.
+ */
+
+static bool g_sched_hrtimer_inited = false;
/****************************************************************************
* Private Functions
****************************************************************************/
+static void nxsched_process_tick(void);
+
+/****************************************************************************
+ * Name: nxsched_hrtimer_callback
+ *
+ * Description:
+ * Callback function invoked when the scheduler high-resolution timer
+ * expires.
+ *
+ * The callback behavior depends on scheduler configuration:
+ *
+ * - CONFIG_SCHED_TICKLESS:
+ * The callback computes the current time and delegates scheduling
+ * decisions to nxsched_tick_expiration().
+ *
+ * - !CONFIG_SCHED_TICKLESS:
+ * The callback processes a single scheduler tick and returns the
+ * interval until the next tick.
+ *
+ * Input Parameters:
+ * hrtimer - Pointer to the expired high-resolution timer instance.
+ *
+ * Returned Value:
+ * In non-tickless mode, returns the number of nanoseconds until the
+ * next expiration.
+ * In tickless mode, the return value is ignored.
+ *
+ ****************************************************************************/
+
+static uint32_t nxsched_hrtimer_callback(FAR hrtimer_t *hrtimer)
+{
+#ifdef CONFIG_SCHED_TICKLESS
+ uint64_t now = hrtimer_gettime();
+
+ /* Notify the scheduler of the current time in ticks */
+
+ nxsched_tick_expiration(NSEC2TICK(now));
+#else
+
+ /* Request the next tick after a fixed interval */
+
+ hrtimer_start(&g_nxsched_hrtimer,
+ g_nxsched_hrtimer.expired + NSEC_PER_TICK,
+ HRTIMER_MODE_ABS);
+
+ /* Process one periodic scheduler tick */
+
+ nxsched_process_tick();
+
+ return 0;
+#endif
+}
+
+/****************************************************************************
+ * Name: nxsched_process_hrtimer
+ *
+ * Description:
+ * Process scheduler-related high-resolution timer events.
+ *
+ * This function performs lazy initialization of the scheduler's
+ * high-resolution timer on first invocation. Once initialized, it
+ * processes any expired high-resolution timer events.
+ *
+ * This function is expected to be called from architecture-specific
+ * timer handling code.
+ *
+ * Input Parameters:
+ * None.
+ *
+ * Returned Value:
+ * None.
+ *
+ ****************************************************************************/
+
+static void nxsched_process_hrtimer(void)
+{
+ uint64_t now = hrtimer_gettime();
Review Comment:
Done
--
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]