sched/hrtimer: Add callback pointer in hrtimer_start.

The hrtimer is designed to replace the wdog timer in future, so it
should have the callback function in its API. The evaluation results showed there is no any
performance degradation since the CPU pipeline can hide the latency.
This commit also decoupled the rb-tree implementation with the pending
state checking.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit is contained in:
ouyangxiangzhen
2026-01-07 15:17:56 +08:00
committed by GUIDINGLI
parent cd6a978ba9
commit a518c28add
4 changed files with 10 additions and 13 deletions
+3 -7
View File
@@ -118,12 +118,7 @@ extern "C"
* None * None
****************************************************************************/ ****************************************************************************/
static inline_function #define hrtimer_init(hrtimer) memset(hrtimer, 0, sizeof(hrtimer_t))
void hrtimer_init(FAR hrtimer_t *hrtimer, hrtimer_cb func)
{
memset(hrtimer, 0, sizeof(hrtimer_t));
hrtimer->func = func;
}
/**************************************************************************** /****************************************************************************
* Name: hrtimer_cancel * Name: hrtimer_cancel
@@ -184,6 +179,7 @@ int hrtimer_cancel_sync(FAR hrtimer_t *hrtimer);
* *
* Input Parameters: * Input Parameters:
* hrtimer - Timer instance to start * hrtimer - Timer instance to start
* func - Expiration callback function
* expired - Expiration time in nanoseconds * expired - Expiration time in nanoseconds
* mode - HRTIMER_MODE_ABS or HRTIMER_MODE_REL * mode - HRTIMER_MODE_ABS or HRTIMER_MODE_REL
* *
@@ -191,7 +187,7 @@ int hrtimer_cancel_sync(FAR hrtimer_t *hrtimer);
* OK on success; a negated errno value on failure. * OK on success; a negated errno value on failure.
****************************************************************************/ ****************************************************************************/
int hrtimer_start(FAR hrtimer_t *hrtimer, int hrtimer_start(FAR hrtimer_t *hrtimer, hrtimer_entry_t func,
uint64_t expired, uint64_t expired,
enum hrtimer_mode_e mode); enum hrtimer_mode_e mode);
+2 -5
View File
@@ -198,10 +198,7 @@ RB_PROTOTYPE(hrtimer_tree_s, hrtimer_node_s, entry, hrtimer_compare);
static inline_function bool hrtimer_is_armed(FAR hrtimer_t *hrtimer) static inline_function bool hrtimer_is_armed(FAR hrtimer_t *hrtimer)
{ {
/* RB-tree root has NULL parent, so root must be checked explicitly */ return hrtimer->func != NULL;
return RB_PARENT(&hrtimer->node, entry) != NULL ||
RB_ROOT(&g_hrtimer_tree) == &hrtimer->node;
} }
/**************************************************************************** /****************************************************************************
@@ -217,7 +214,7 @@ static inline_function void hrtimer_remove(FAR hrtimer_t *hrtimer)
/* Explicitly clear parent to mark the timer as unarmed */ /* Explicitly clear parent to mark the timer as unarmed */
RB_PARENT(&hrtimer->node, entry) = NULL; hrtimer->func = NULL;
} }
/**************************************************************************** /****************************************************************************
+1
View File
@@ -139,6 +139,7 @@ void hrtimer_process(uint64_t now)
DEBUGASSERT(hrtimer->expired > period); DEBUGASSERT(hrtimer->expired > period);
hrtimer->func = func;
hrtimer_insert(hrtimer); hrtimer_insert(hrtimer);
} }
+4 -1
View File
@@ -44,6 +44,7 @@
* *
* Input Parameters: * Input Parameters:
* hrtimer - Pointer to the hrtimer structure. * hrtimer - Pointer to the hrtimer structure.
* func - Expiration callback function.
* expired - Expiration time in nanoseconds. Interpretation * expired - Expiration time in nanoseconds. Interpretation
* depends on mode. * depends on mode.
* mode - Timer mode (HRTIMER_MODE_ABS or HRTIMER_MODE_REL). * mode - Timer mode (HRTIMER_MODE_ABS or HRTIMER_MODE_REL).
@@ -59,7 +60,7 @@
* nanoseconds from the current time. * nanoseconds from the current time.
****************************************************************************/ ****************************************************************************/
int hrtimer_start(FAR hrtimer_t *hrtimer, int hrtimer_start(FAR hrtimer_t *hrtimer, hrtimer_entry_t func,
uint64_t expired, uint64_t expired,
enum hrtimer_mode_e mode) enum hrtimer_mode_e mode)
{ {
@@ -77,6 +78,8 @@ int hrtimer_start(FAR hrtimer_t *hrtimer,
hrtimer_remove(hrtimer); hrtimer_remove(hrtimer);
} }
hrtimer->func = func;
/* Compute absolute expiration time */ /* Compute absolute expiration time */
if (mode == HRTIMER_MODE_ABS) if (mode == HRTIMER_MODE_ABS)