Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions drivers/timers/arch_alarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ static void ndelay_accurate(unsigned long nanoseconds)
static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower,
FAR void *arg)
{
#ifdef CONFIG_SCHED_TICKLESS
nxsched_timer_expiration();
#if defined(CONFIG_HRTIMER) || defined(CONFIG_SCHED_TICKLESS)
nxsched_process_timer();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we update all driver and remove nxsched_timer_expiration

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, let me consider about this


#else
clock_t now;

Expand Down Expand Up @@ -380,7 +381,7 @@ int weak_function up_alarm_tick_cancel(FAR clock_t *ticks)
*
****************************************************************************/

#ifdef CONFIG_SCHED_TICKLESS
#if defined(CONFIG_HRTIMER) || defined(CONFIG_SCHED_TICKLESS)
int weak_function up_alarm_start(FAR const struct timespec *ts)
{
int ret = -EAGAIN;
Expand Down
6 changes: 2 additions & 4 deletions include/nuttx/arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -2031,7 +2031,8 @@ int up_alarm_tick_cancel(FAR clock_t *ticks);
*
****************************************************************************/

#if defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM)
#if defined(CONFIG_HRTIMER)|| \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#if defined(CONFIG_HRTIMER)|| \
#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
Expand Down Expand Up @@ -2461,10 +2462,7 @@ void up_ndelay(unsigned long nanoseconds);
*
****************************************************************************/

#ifndef CONFIG_SCHED_TICKLESS
void nxsched_process_timer(void);
#endif

/****************************************************************************
* Name: nxsched_timer_expiration
*
Expand Down
4 changes: 4 additions & 0 deletions sched/init/nx_start.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,10 @@ void nx_start(void)
timer_initialize();
#endif

#ifdef CONFIG_HRTIMER
nxsched_hrtimer_init();
#endif

/* Initialize the signal facility (if in link) */

nxsig_initialize();
Expand Down
5 changes: 2 additions & 3 deletions sched/sched/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ set(SRCS
sched_sysinfo.c
sched_get_stateinfo.c
sched_switchcontext.c
sched_sleep.c)
sched_sleep.c
sched_processtimer.c)

if(DEFINED CONFIG_STACKCHECK_MARGIN)
if(NOT CONFIG_STACKCHECK_MARGIN EQUAL -1)
Expand Down Expand Up @@ -98,8 +99,6 @@ endif()

if(CONFIG_SCHED_TICKLESS)
list(APPEND SRCS sched_timerexpiration.c)
else()
list(APPEND SRCS sched_processtimer.c)
endif()

if(CONFIG_SCHED_CRITMONITOR)
Expand Down
4 changes: 1 addition & 3 deletions sched/sched/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ CSRCS += sched_yield.c sched_rrgetinterval.c sched_foreach.c
CSRCS += sched_lock.c sched_unlock.c sched_lockcount.c
CSRCS += sched_idletask.c sched_self.c sched_get_stackinfo.c sched_get_tls.c
CSRCS += sched_sysinfo.c sched_get_stateinfo.c sched_getcpu.c
CSRCS += sched_switchcontext.c sched_sleep.c
CSRCS += sched_switchcontext.c sched_sleep.c sched_processtimer.c

ifneq ($(CONFIG_STACKCHECK_MARGIN),)
ifneq ($(CONFIG_STACKCHECK_MARGIN),-1)
Expand Down Expand Up @@ -80,8 +80,6 @@ endif

ifeq ($(CONFIG_SCHED_TICKLESS),y)
CSRCS += sched_timerexpiration.c
else
CSRCS += sched_processtimer.c
endif

ifeq ($(CONFIG_SCHED_CRITMONITOR),y)
Expand Down
34 changes: 34 additions & 0 deletions sched/sched/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -607,4 +607,38 @@ static inline_function int nxsched_select_cpu(cpu_set_t affinity)
return cpu;
}
# endif

#ifdef CONFIG_HRTIMER
/****************************************************************************
* Name: nxsched_hrtimer_start
*
* Description:
* (Re)start the scheduler high-resolution timer with a new expiration
* based on the specified tick interval.
*
* Input Parameters:
* ticks - Number of scheduler ticks until expiration.
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/

int nxsched_hrtimer_start(clock_t ticks);

/****************************************************************************
* Name: nxsched_hrtimer_init
*
* Description:
* Initialize the scheduler high-resolution timer (hrtimer) instance
* and arm the first scheduler timer event.
*
* Returned Value:
* Zero on success, or a negative errno value on failure.
*
****************************************************************************/

int nxsched_hrtimer_init(void);
#endif

#endif /* __SCHED_SCHED_SCHED_H */
187 changes: 173 additions & 14 deletions sched/sched/sched_processtimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,108 @@
#include "sched/sched.h"
#include "wdog/wdog.h"
#include "clock/clock.h"
#include "hrtimer/hrtimer.h"

/****************************************************************************
* Private Data
****************************************************************************/

#ifdef CONFIG_HRTIMER

/* Scheduler-owned high-resolution timer instance.
*
* This timer acts as the time source for scheduler-related events:
*
* - Periodic scheduler ticks in non-tickless mode
* - Dynamic expiration points in tickless mode
*
* The timer is initialized lazily to avoid unnecessary setup when
* CONFIG_HRTIMER is enabled but not used immediately.
*/

static hrtimer_t g_nxsched_hrtimer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's init to func directly and remove g_sched_hrtimer_inited

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


/****************************************************************************
* Private Functions
****************************************************************************/

static void nxsched_process_tick(void);

/****************************************************************************
* Name: nxsched_hrtimer_callback
*
* Description:
* Callback invoked by the high-resolution timer framework when the
* scheduler timer expires.
*
* Behavior depends on scheduler configuration:
*
* CONFIG_SCHED_TICKLESS:
* - Query current high-resolution time
* - Convert time to scheduler ticks
* - Notify scheduler via nxsched_tick_expiration()
*
* !CONFIG_SCHED_TICKLESS:
* - Re-arm the next periodic tick
* - Process a single scheduler tick
*
* Input Parameters:
* hrtimer - Pointer to the expired high-resolution timer
*
* Returned Value:
* In non-tickless mode, returns the interval until the next expiration.
* In tickless mode, the return value is ignored.
*
****************************************************************************/

static uint64_t
nxsched_hrtimer_callback(FAR hrtimer_t *hrtimer, uint64_t expired)
{
UNUSED(hrtimer);
UNUSED(expired);

#ifdef CONFIG_SCHED_TICKLESS
nxsched_timer_expiration();
#else
nxsched_process_tick();
return NSEC_PER_TICK;
#endif
}

/****************************************************************************
* Name: nxsched_process_hrtimer
*
* Description:
* Entry point for scheduler-related high-resolution timer processing.
*
* Responsibilities:
* - Process expired hrtimer events
* - Perform one-time initialization of the scheduler hrtimer
* - Arm the initial scheduler timer event
*
* This function is expected to be called from architecture-specific
* timer interrupt handling code.
*
* Input Parameters:
* None
*
* Returned Value:
* None
*
****************************************************************************/

static void nxsched_process_hrtimer(void)
{
uint64_t now = hrtimer_gettime();

/* Process any expired high-resolution timers */

hrtimer_process(now);
}
#endif /* CONFIG_HRTIMER */

#ifndef CONFIG_SCHED_TICKLESS

/****************************************************************************
* Name: nxsched_cpu_scheduler
*
Expand Down Expand Up @@ -145,19 +242,7 @@ static inline void nxsched_process_scheduler(void)
#endif

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* System Timer Hooks
*
* These are standard interfaces that are exported by the OS
* for use by the architecture specific logic
*
****************************************************************************/

/****************************************************************************
* Name: nxsched_process_timer
* Name: nxsched_process_tick
*
* Description:
* This function handles system timer events.
Expand All @@ -174,7 +259,7 @@ static inline void nxsched_process_scheduler(void)
*
****************************************************************************/

void nxsched_process_timer(void)
static void nxsched_process_tick(void)
{
#ifdef CONFIG_CLOCK_TIMEKEEPING
/* Process wall time */
Expand Down Expand Up @@ -204,3 +289,77 @@ void nxsched_process_timer(void)
board_timerhook();
#endif
}
#endif

#ifdef CONFIG_HRTIMER

/****************************************************************************
* Name: nxsched_hrtimer_init
*
* Description:
* Initialize the scheduler high-resolution timer (hrtimer) instance
* and arm the first scheduler timer event.
*
* Returned Value:
* Zero on success, or a negative errno value on failure.
*
****************************************************************************/

int nxsched_hrtimer_init(void)
{
/* Initialize the scheduler hrtimer instance */

hrtimer_init(&g_nxsched_hrtimer, nxsched_hrtimer_callback);

/* Start the first scheduler timer event with one tick interval */

return hrtimer_start(&g_nxsched_hrtimer, NSEC_PER_TICK, HRTIMER_MODE_REL);
}

/****************************************************************************
* Name: nxsched_hrtimer_start
*
* Description:
* Start or re-arm the scheduler high-resolution timer.
*
* Input Parameters:
* ticks - Absolute expiration time
*
* Returned Value:
* Zero on success, or a negative errno value on failure.
*
****************************************************************************/

int nxsched_hrtimer_start(clock_t ticks)
{
return hrtimer_start(&g_nxsched_hrtimer, ticks, HRTIMER_MODE_ABS);
}
#endif

/****************************************************************************
* System Timer Hooks
*
* These are standard interfaces that are exported by the OS
* for use by the architecture specific logic
*
****************************************************************************/

void nxsched_process_timer(void)
{
#if defined(CONFIG_HRTIMER)
/* High-resolution timer-based scheduling */

nxsched_process_hrtimer();

#elif defined(CONFIG_SCHED_TICKLESS)
/* Legacy tickless scheduling */

nxsched_timer_expiration();

#else
/* Legacy periodic tick-based scheduling */

nxsched_process_tick();

#endif
}
4 changes: 4 additions & 0 deletions sched/sched/sched_timerexpiration.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,11 @@ static clock_t nxsched_timer_start(clock_t ticks, clock_t interval)
if (interval != CLOCK_MAX)
{
interval = adjust_next_interval(interval);
#ifdef CONFIG_HRTIMER
nxsched_hrtimer_start(ticks + interval);
#else
nxsched_timer_tick_start(ticks, interval);
#endif
}

atomic_set(&g_timer_interval, interval);
Expand Down
Loading