You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by li...@apache.org on 2026/01/26 08:37:20 UTC
(nuttx) 04/11: sched/hrtimer: Add hrtimer_wait to simplify the wait.
This is an automated email from the ASF dual-hosted git repository.
ligd pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 65bc3db6243c3ac4aa8d0f7bc41945cbd24d1bbc
Author: ouyangxiangzhen <ou...@xiaomi.com>
AuthorDate: Wed Jan 7 20:45:01 2026 +0800
sched/hrtimer: Add hrtimer_wait to simplify the wait.
This commit added hrtimer_wait to simplify the wait.
Signed-off-by: ouyangxiangzhen <ou...@xiaomi.com>
---
sched/hrtimer/hrtimer.h | 66 ++++++++++++++++++++++++++++++++++++++++++
sched/hrtimer/hrtimer_cancel.c | 63 ++--------------------------------------
2 files changed, 69 insertions(+), 60 deletions(-)
diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h
index f3d6e80a2aa..42801ff4d8a 100644
--- a/sched/hrtimer/hrtimer.h
+++ b/sched/hrtimer/hrtimer.h
@@ -33,8 +33,18 @@
#include <nuttx/hrtimer.h>
#include <nuttx/seqlock.h>
+#include "sched/sched.h"
+
#ifdef CONFIG_HRTIMER
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Delay used while waiting for a running hrtimer callback to complete */
+
+#define HRTIMER_CANCEL_SYNC_DELAY_MS 5
+
/****************************************************************************
* Public Types
****************************************************************************/
@@ -484,5 +494,61 @@ int hrtimer_cancel_running(FAR hrtimer_t *timer)
return refs;
}
+/****************************************************************************
+ * Name: hrtimer_wait
+ *
+ * Description:
+ * Wait for all references to be released before the timer can be freed.
+ *
+ * Input Parameters:
+ * timer - The hrtimer to be waited.
+ *
+ * Returned Value:
+ * None.
+ *
+ * Assumption:
+ * The periodical hrtimer should be cancelled before calling this function.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_SMP
+static inline_function void hrtimer_wait(FAR hrtimer_t *timer)
+{
+ int cpu;
+
+ DEBUGASSERT(timer && timer->func == NULL);
+
+ /* Wait until all references have been released. */
+
+ for (cpu = 0; cpu < CONFIG_SMP_NCPUS; cpu++)
+ {
+ /* The timer should not be restarted again.
+ * Or there will be ownership invariant violation.
+ */
+
+ DEBUGASSERT(!hrtimer_is_running(timer, cpu));
+
+ /* If sleeping is permitted, yield the CPU briefly to avoid
+ * busy-waiting. Otherwise, spin until the callback completes
+ * and the state becomes inactive.
+ */
+
+ while (hrtimer_is_cancelling(timer, cpu))
+ {
+ if (!up_interrupt_context() && !is_idle_task(this_task()))
+ {
+ nxsched_msleep(HRTIMER_CANCEL_SYNC_DELAY_MS);
+ }
+
+ /* Otherwise, spin-wait is enough. */
+ }
+ }
+
+ /* Finally, we acquire the ownership of this hrtimer. */
+}
+#else
+# define hrtimer_wait(timer) /* Nothing to wait in non-SMP. */
+#endif
+
#endif /* CONFIG_HRTIMER */
#endif /* __SCHED_HRTIMER_HRTIMER_H */
diff --git a/sched/hrtimer/hrtimer_cancel.c b/sched/hrtimer/hrtimer_cancel.c
index 8cf5a322606..bed9011a7f1 100644
--- a/sched/hrtimer/hrtimer_cancel.c
+++ b/sched/hrtimer/hrtimer_cancel.c
@@ -29,53 +29,8 @@
#include <nuttx/clock.h>
#include <errno.h>
-#include "sched/sched.h"
#include "hrtimer/hrtimer.h"
-/****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
-
-/* Delay used while waiting for a running hrtimer callback to complete */
-
-#define HRTIMER_CANCEL_SYNC_DELAY_MS 5
-
-/****************************************************************************
- * Private Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: hrtimer_is_active
- *
- * Description:
- * Check whether a high-resolution timer is currently running on any CPU.
- *
- * Input Parameters:
- * hrtimer - Pointer to the high-resolution timer to check.
- *
- * Returned Value:
- * true - The timer is active on at least one CPU.
- * false - The timer is not active.
- ****************************************************************************/
-#ifdef CONFIG_SMP
-static inline_function bool hrtimer_is_active(FAR hrtimer_t *hrtimer)
-{
- int cpu;
- bool is_active = false;
-
- for (cpu = 0; cpu < CONFIG_SMP_NCPUS; cpu++)
- {
- if (hrtimer_is_cancelling(hrtimer, cpu))
- {
- is_active = true;
- break;
- }
- }
-
- return is_active;
-}
-#endif
-
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -192,21 +147,9 @@ int hrtimer_cancel_sync(FAR hrtimer_t *hrtimer)
ret = hrtimer_cancel(hrtimer);
if (ret > 0)
{
- /* Wait until the timer transitions to the inactive state.
- *
- * If sleeping is permitted, yield the CPU briefly to avoid
- * busy-waiting. Otherwise, spin until the callback completes.
- */
-#ifdef CONFIG_SMP
- while (hrtimer_is_active(hrtimer))
- {
- if (!up_interrupt_context() &&
- !is_idle_task(this_task()))
- {
- nxsched_msleep(HRTIMER_CANCEL_SYNC_DELAY_MS);
- }
- }
-#endif
+ /* Wait until all the timer callbacks finish. */
+
+ hrtimer_wait(hrtimer);
}
return ret;