Commit graph

16 commits

Author SHA1 Message Date
ouyangxiangzhen
b685d97199 sched/hrtimer: Simplify the hrtimer.
This commit simplified the hrtimer.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
dac48484ce sched/hrtimer: Update the comments.
This commit updated the comments.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
5bab9fcc7f sched/hrtimer: Add guard timer.
This commit added the guard timer for hrtimer.
The guard timer uses a small memory footprint, offering two main advantages:
- Reduced branches checking for an empty hrtimer queue, simplifying code implementation and improving the performance.
- Additional health monitoring allows the system to enter a safe state in case of time acquisition errors, and supports custom error handling callback functions.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
d1c45f7c4e sched/hrtimer: Fix delay in hrtimer_process.
This commit fixed the overflow check and renamed the period to delay, since the callback return value is the next delay not the period.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
33c6b4115c sched/hrtimer: Simplify the hrtimer_process.
This commit simplified the hrtimer_process.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-02-02 13:26:22 +08:00
ouyangxiangzhen
f1b355f118 sched/hrtimer: Update the comments.
This commit updated the comments.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-26 16:37:10 +08:00
ouyangxiangzhen
a6d64a1a83 sched/hrtimer: Simplify the hrtimer_cancel.
This commit simplified the hrtimer_cancel.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-26 16:37:10 +08:00
ouyangxiangzhen
b2fadb8529 sched/hrtimer: Fix functional correctness issue in synchronization.
On 32-bit platform with 64-bit pointer, read/write to the
`g_hrtimer_running` is not atomic, we should protect it with the
seqcount to ensure the value we read is correct.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-26 16:37:10 +08:00
ouyangxiangzhen
d18fd4640e sched/hrtimer: Fix functional correctness issue in SMP.
Since the hrtimer->expired is not monotonic, it can not be used as the
version. This commit added the ownership encoding to ensure the
invariant that the cancelled the timer can not modify the hrtimer again.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-26 16:37:10 +08:00
ouyangxiangzhen
47ce6590b7 sched/hrtimer: Fixed hrtimer_starttimer.
This commit fixed the functional correctness issues in tick mode
and non-tickless alarm mode.
- In tick mode, we do not need reprogram the timer.
- In non-tickless alarm mode, the up_timer_start receive the relative time as the parameter.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-26 16:37:10 +08:00
wangchengdong
3538d805fc sched/hrtimer: refresh code comments to match implementation
Several code comments in the hrtimer subsystem have become outdated
following recent implementation changes. This patch updates them to
accurately describe the current code behavior and algorithms.

Signed-off-by: Chengdong Wang <wangcd91@gmail.com>
2026-01-26 10:10:57 +08:00
ouyangxiangzhen
a518c28add 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>
2026-01-13 21:41:35 +08:00
wangchengdong
6bffad62de [EXPERIMENTAL] sched/hrtimer: separate SMP logic from hrtimer
Separate SMP-specific logic from the hrtimer core to improve
    performance and maintainability.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2026-01-08 22:47:16 +08:00
ouyangxiangzhen
6bd9ef6992 sched/hrtimer: Rename the callback type to hrtimer_entry_t.
This commit renamed the callback type to `hrtimer_entry_t`, aligning with
the `wdentry_t` in wdog..

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
2026-01-08 22:26:11 +08:00
wangchengdong
1020bc74e8 [EXPERIMENTAL]sched/hrtimr: Allow running/armed hrtimer to be restarted
Allow running/armed hrtimer to be restarted to
  fix hrtimer bug: #17567

Co-authored-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2026-01-06 23:14:04 +08:00
wangchengdong
ed1cb6f9b8 sched/hrtimer: Add high-resolution timer support for NuttX
NuttX provides the wdog module to implement timers for the scheduler.
While it is lightweight and efficient, wdog only supports tick-level timers,
typically in milliseconds. Although the tick duration can be configured,
setting it to microsecond or nanosecond resolution is not practical.
Doing so may cause an interrupt storm, where the CPU is constantly
occupied handling tick interrupts.

To address this limitation, a new hrtimer module is introduced.
It coexists with the wdog module, providing both tick-level timers
for wdog and high-resolution timers (nanosecond-level) directly to users.

Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
2025-12-18 18:36:28 +08:00