Linux 内核定时器使用 二 高精度定时器 hrtimer 的用例

之前介绍了timer_list内核定时器,它的精度在毫秒级别,再高一点它就无能为力了,所幸内核提供了高精度定时器 hrtimer。

源文件在linux/kernel/hrtimer.c中。接口简单。下面介绍一下相关接口

1. 定时器定义与绑定超时回调函数
static struct hrtimer timer;
 
/* 设置回调函数 */
timer.function = hrtimer_hander;
 
2. 定时器初始化
/*
 *  参数timer是hrtimer指针,
 *  参数clock_id有如下常用几种选项:
 *  CLOCK_REALTIME    //实时时间,如果系统时间变了,定时器也会变
 *  CLOCK_MONOTONIC    //递增时间,不受系统影响
 *  参数mode有如下几种选项:
 *     HRTIMER_MODE_ABS = 0x0,        /* 绝对模式 */
    HRTIMER_MODE_REL = 0x1,        /* 相对模式 */
    HRTIMER_MODE_PINNED = 0x02,    /* 和CPU绑定 */
    HRTIMER_MODE_ABS_PINNED = 0x02, /* 第一种和第三种的结合 */
    HRTIMER_MODE_REL_PINNED = 0x03, /* 第二种和第三种的结合 */
 */
void hrtimer_init(struct hrtimer *timer, clockid_t clock_id, enum hrtimer_mode mode);
 
3. 定时器启动
/*
 * 参数timer是hrtimer指针
 * 参数tim是时间,可以使用ktime_set()函数设置时间,
 * 参数mode和初始化的mode参数一致
 */
hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode);
 
4. 设置时间
/*
 * 单位为秒和纳秒组合
 */
ktime_t ktime_set(const long secs, const unsigned long nsecs);
 
/* 设置超时时间,当定时器超时后可以用该函数设置下一次超时时间 */
hrtimer_forward_now(struct hrtimer *timer, ktime_t interval)
 
5. 注意事项:
定时器超时后会调用回调函数,回调函数结构类似这样:
enum hrtimer_restart        (*function)(struct hrtimer *);
 
enum hrtimer_restart {
    HRTIMER_NORESTART,    /* 不重启定时器 */
    HRTIMER_RESTART,    /* 重启定时器 */
};
在回调函数返回前要手动设置下一次超时时间。
另外,回调函数执行时间不宜过长,因为是在中断上下文中,如果有什么任务的话,最好使用工作队列等机制。
 
6. 关闭定时器
int hrtimer_cancel(struct hrtimer *timer);
 
简单用例:
/*
 *  Description : 高精度定时器用例
 *  Author : mason
 *  Date   : 201808
 */
 
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/hrtimer.h>
#include <linux/jiffies.h>
 
static struct hrtimer timer;
ktime_t kt;
 
/* 定时器回调函数 */
static enum hrtimer_restart  hrtimer_hander(struct hrtimer *timer)
{
    printk("hrtimer up\r\n");
 
    /* 设置下次过期时间 */
    kt = ktime_set(3,0);    
    hrtimer_forward_now(timer, kt);
 
    /* 该参数将重新启动定时器 */    
    return HRTIMER_RESTART;  
}
 
static int __init hrtimer_demo_init(void)
{
    printk("hello hrtimer \r\n");
 
    kt = ktime_set(1,10);
 
    /* hrtimer初始化 */
    hrtimer_init(&timer,CLOCK_MONOTONIC,HRTIMER_MODE_REL);
 
    /* hrtimer启动 */
    hrtimer_start(&timer,kt,HRTIMER_MODE_REL);
 
    /* 设置回调函数 */
    timer.function = hrtimer_hander;
 
    return 0;
}
 
static void __exit hrtimer_demo_exit(void)
{
    /* hrtimer注销 */
    hrtimer_cancel(&timer);
    printk("bye hrtimer\r\n");
}
 
 
module_init(hrtimer_demo_init);
module_exit(hrtimer_demo_exit);
MODULE_LICENSE("GPL");
 
参考文档 :

1. Linux 下定时器的实现方式分析

https://www.ibm.com/developerworks/cn/linux/l-cn-timers/

2. hrtimer高精度定时器的简单使用【学习笔记】

https://www.cnblogs.com/zzb-Dream-90Time/p/7084916.html
--------------------- 
作者:mrsonko 
来源:CSDN 
原文:https://blog.csdn.net/fuyuande/article/details/82193600 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值