二、内核定时器
用到的数据结构
struct timer_list {
struct list_head entry;
unsigned long expires;//定时器时间设置一般为jiffies + n(0
void (*function)(unsigned long);//定时器处理函数
unsigned long data;//定时器私有数据
struct tvec_base *base;
#ifdef CONFIG_TIMER_STATS
void *start_site;
char start_comm[16];
int start_pid;
#endif
#ifdef CONFIG_LOCKDEP
struct lockdep_map lockdep_map;
#endif
};
用到的函数:
#define init_timer(timer) init_timer_key((timer), NULL, NULL)
void init_timer_key(struct timer_list *timer,
const char *name,
struct lock_class_key *key)
{
debug_init(timer);
__init_timer(timer, name, key);
}
初始化一个定时器
void add_timer(struct timer_list *timer)
{
BUG_ON(timer_pending(timer));
mod_timer(timer, timer->expires);
}
该函数用于添加一个定时器
代码如下:
#include
#include
#include
#include
strcut timer_list timed;
static void exec_timer(unsigned long arg)
{
printf("do someting else\n");
...
timed.expires = jiffies + 128;
add_timer(&timed);//再次添加一个定时器
}
static int xx_init()
{
...
init_timer(&time);
timed.function = exec_timer;
timed.expires = jiffies + 128;
timed.data = (unsigned long)xx_datap;
...
}