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>
Determining whether a red-black tree node is the left-mode node using `RB_LEFT(hrtimer, node) == NULL` is functionally incorrect. This is because the left node of any leaf node can also be NULL. For example, in the following rbtree:
5
/ \
3 7
\
4
the left node of the right-most node 7 would also be NULL.
To avoid extra performance overhead to find the left-most node when the
rb-tree changed, this commit used `g_cached_first` to cache the
left-most node.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit simplified the rbtree in hrtimer and provided better
branchless compare function.
Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
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>
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>
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>
Add support for managing hrtimers using a simple list. This approach
is more memory-efficient, as list nodes use less memory, and it is
preferable to an RB-tree when the number of hrtimers is relatively small.
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
Separate SMP-specific logic from the hrtimer core to improve
performance and maintainability.
Signed-off-by: Chengdong Wang <wangchengdong@lixiang.com>
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>
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>