2023.7.15 linux 高分定时器 hrtimer

High-resolution timers (hrtimers)  高分辨率计时器 纳秒级的,内核配置:CONFIG_HIGH_RES_TIMERS   ktime_t 

头文件:    #include <linux/hrtimer.h>

初始化:   void hrtimer_init(struct hrtimer *timer, clockid_t which_clock, enum hrtimer_mode mode);

which_clock?

1,CLOCK_REALTIME: This selects the real-time time—that is, the wall time. If the system time changes, it can affect this timer.

2,CLOCK_MONOTONIC: This is an incremental time, not affected by system changes. However, it stops incrementing when the system goes to sleep or suspends.

3,CLOCK_BOOTTIME: The running time of the system. Similar to CLOCK_ MONOTONIC, the difference is that it includes sleep time. When suspended, it will still increase.

mode?

 enum hrtimer_restart callback(struct hrtimer *h);   这个callback的返回值就2个:HRTIMER_NORESTART 或者HRTIMER_RESTART 前一种类似单次任务,后类似定期任务

高分计时器前移: u64 hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval) ;now是从什么时间开始,interval 是 前移多少。

 从现在now开始前移有一个专门的函数:u64 hrtimer_forward_now(struct hrtimer *timer, ktime_t interval)  ;

启动计时器: int hrtimer_start(struct hrtimer *timer, ktime_t time, const enum hrtimer_mode mode);  #mode represents the timer expiry mode, and it should be either HRTIMER_MODE_ABS for an absolute time value or HRTIMER_MODE_REL for a time value relative to now.   time就是到期时间、

ktime_t 与秒 毫秒 纳秒的转换函数

ktime_t ktime_set(const s64 secs, const unsigned long nsecs);

ktime_t ns_to_ktime(u64 ns);

ktime_t ms_to_ktime(u64 ms);

s64 ktime_to_ns(const ktime_t kt);

s64 ktime_to_us(const ktime_t kt);

ktime_t ktime_sub(const ktime_t lhs, const ktime_t rhs);

ktime_t ktime_sub(const ktime_t lhs, const ktime_t rhs);

ktime_t ktime_add(const ktime_t add1, const ktime_t add2);

ktime_t ktime_add_ns(const ktime_t kt, u64 nsec);

停止或取消定时器:

int hrtimer_cancel(struct hrtimer *timer);

int hrtimer_try_to_cancel(struct hrtimer *timer);

如果timer已经是inactive,都返回0;try函数如果在timer pending期间cancel成功返回1,若已经到期执行callback则失败返回-1;hrtimer_cancel()会等待callback执行完毕再返回

检测回调函数是否在运行。。。。。      int hrtimer_callback_running(struct hrtimer *timer);

 

 

posted @   杨大茄子  阅读(295)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示