sched/hrtimer: Add hrtimer_wait to simplify the wait.

This commit added hrtimer_wait to simplify the wait.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit is contained in:
ouyangxiangzhen 2026-01-07 20:45:01 +08:00 committed by GUIDINGLI
parent b2fadb8529
commit 65bc3db624
2 changed files with 69 additions and 60 deletions

View file

@ -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 */

View file

@ -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;