This is an automated email from the ASF dual-hosted git repository. archer pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 303bc7411fd0f91cc10a851b24ddff17b4fb2b9d Author: wangchengdong <[email protected]> AuthorDate: Mon Jan 19 14:39:54 2026 +0800 sched/sched: add hrtimer support to drive the scheduler Add hrtimer support to drive the scheduler. Signed-off-by: Chengdong Wang <[email protected]> --- drivers/timers/arch_alarm.c | 4 +- include/nuttx/arch.h | 3 +- sched/sched/sched_timer.c | 93 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 3 deletions(-) diff --git a/drivers/timers/arch_alarm.c b/drivers/timers/arch_alarm.c index 98a49a8e2d4..dbb44a267df 100644 --- a/drivers/timers/arch_alarm.c +++ b/drivers/timers/arch_alarm.c @@ -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; diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h index 4bc63497aae..eaf14260eae 100644 --- a/include/nuttx/arch.h +++ b/include/nuttx/arch.h @@ -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 diff --git a/sched/sched/sched_timer.c b/sched/sched/sched_timer.c index 60913186e9d..5f5c4bc0b50 100644 --- a/sched/sched/sched_timer.c +++ b/sched/sched/sched_timer.c @@ -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 }
