sched/sched: add hrtimer support to drive the scheduler

Add hrtimer support to drive the scheduler.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
This commit is contained in:
wangchengdong
2026-01-19 14:39:54 +08:00
committed by archer
parent 9d8f0ace40
commit 303bc7411f
3 changed files with 97 additions and 3 deletions
+2 -2
View File
@@ -126,7 +126,7 @@ static void ndelay_accurate(unsigned long nanoseconds)
static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower,
FAR void *arg)
{
#ifdef CONFIG_SCHED_TICKLESS
#if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_HRTIMER)
nxsched_process_timer();
#else
clock_t now;
@@ -388,7 +388,7 @@ int weak_function up_alarm_tick_cancel(FAR clock_t *ticks)
*
****************************************************************************/
#ifdef CONFIG_SCHED_TICKLESS
#if defined(CONFIG_SCHED_TICKLESS) || defined(CONFIG_HRTIMER)
int weak_function up_alarm_start(FAR const struct timespec *ts)
{
int ret = -EAGAIN;
+2 -1
View File
@@ -2051,7 +2051,8 @@ int up_alarm_tick_cancel(FAR clock_t *ticks);
*
****************************************************************************/
#if defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM)
#if defined(CONFIG_HRTIMER) || \
defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM)
int up_alarm_start(FAR const struct timespec *ts);
int up_alarm_tick_start(clock_t ticks);
#endif
+93
View File
@@ -31,6 +31,82 @@
#include <errno.h>
#include "sched/sched.h"
#include "hrtimer/hrtimer.h"
/****************************************************************************
* Private Data
****************************************************************************/
/* High-resolution timer used to drive the scheduler tick.
*
* In non-tickless mode, this timer periodically generates a scheduler
* tick with interval NSEC_PER_TICK.
*
* In tickless mode, the timer is still used, but the callback does not
* request automatic re-arming.
*/
#ifdef CONFIG_HRTIMER
static hrtimer_t g_sched_hrtimer;
static bool g_sched_hrtimer_started = false;
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxsched_hrtimer_callback
*
* Description:
* High-resolution timer callback used to drive the scheduler.
*
* This callback is invoked when the scheduler hrtimer expires.
* It advances the scheduler time base by calling
* nxsched_process_tick().
*
* Input Parameters:
* hrtimer - Pointer to the expired hrtimer instance
* expired - Expiration time in nanoseconds
*
* Returned Value:
* In non-tickless mode:
* Returns the next expiration interval (NSEC_PER_TICK),
* causing the hrtimer to be re-armed periodically.
*
* In tickless mode:
* Returns 0 to indicate that the timer should not be
* automatically restarted.
*
****************************************************************************/
#ifdef CONFIG_HRTIMER
static uint64_t
nxsched_hrtimer_callback(FAR const struct hrtimer_s *hrtimer,
uint64_t expired)
{
UNUSED(hrtimer);
UNUSED(expired);
/* Advance scheduler time and process time slice expiration */
nxsched_process_tick();
#ifndef CONFIG_SCHED_TICKLESS
/* Periodic tick mode: re-arm timer with fixed tick interval */
return NSEC_PER_TICK;
#else
/* Tickless mode controls the next wakeup explicitly */
return 0;
#endif
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxsched_process_timer
@@ -47,5 +123,22 @@
void nxsched_process_timer(void)
{
#ifdef CONFIG_HRTIMER
/* Process all expired high-resolution timers */
if (g_sched_hrtimer_started == false)
{
g_sched_hrtimer_started = true;
hrtimer_start(&g_sched_hrtimer,
nxsched_hrtimer_callback,
NSEC_PER_TICK,
HRTIMER_MODE_REL);
}
hrtimer_process(hrtimer_gettime());
#else
/* Fallback: process one scheduler tick */
nxsched_process_tick();
#endif
}