您的位置:首页 > 其它

内核定时器:

2015-11-21 18:14 344 查看


内核定时器:

未来的某个时间点执行提前设置的某个任务函数。

涉及到的核心头文件:

#include <linux/timer.h>

涉及到的结构体:

struct timer_list {

/*

* All fields that change during normal runtime grouped to the

* same cacheline

*/

struct list_head entry;

unsigned long expires; //未来的定时时间点,参考时间是jiffies

struct tvec_base *base;



void (*function)(unsigned long); //定时到达时要执行的处理函数

unsigned long data; //定时处理函数需要的参数



int slack;



#ifdef CONFIG_TIMER_STATS

int start_pid;

void *start_site;

char start_comm[16];

#endif

#ifdef CONFIG_LOCKDEP

struct lockdep_map lockdep_map;

#endif

};

使用步骤:

1. 实例化定时器对象

方法1,

struct timer_list timer;

init_timer(&timer);

timer.expires = jiffies + xxx;

timer.function = service_timer;

timer.data = (unsigned long)dev;



方法2,

struct timer_list timer;

setup_timer(&timer, service_timer, (unsigned long)dev);

timer.expires = jiffies + xxx;

方法3,

DEFINE_TIMER(timer, service_timer, jiffies + xxx, (unsigned long)dev);



2. 向内核注册定时器并启动

add_timer(&timer);

3. 如果需要(周期性的来定时)

mod_timer(&timer, jiffies + xx);

4. 在驱动模块的出口移除定时器

//在对称多处理器对应的系统中使用

del_timer_sync(&timer);

//单核使用

del_timer(&timer);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: