有時候我們希望能在某個時間點執行某些動作,這時候便可以使用timer,在使用timer有些規矩必須被遵守。因為不是user-space來喚起,所以不允許存取user-space,current也就沒有意義。不能休眠,也不准schedule()或者任何有可能休眠的動作都不准。
struct timer_list {
struct list_head entry;
unsigned long expires;
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
};
timer_list必須初始化之後才能使用,您可以選擇init_timer()或TIMER_INITIALIZER(),接著就可以設定expires/callback function/data(參數),並且使用add_timer()將其加入timer中,或者使用del_timer()移除pending中的timer,也可以使用mod_timer()修改或者重新設定timer。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/timer.h>
MODULE_LICENSE("GPL");
struct timer_list brook_timer;
static void callback(unsigned long);
struct data {
int count;
};
static struct data data;
static void callback(unsigned long data)
{
struct data *dp = (struct data*) data;
printk("%s(): %d\n", __FUNCTION__, dp->count++);
mod_timer(&brook_timer, jiffies + 5 * HZ);
}
static int __init init_modules(void)
{
init_timer(&brook_timer);
brook_timer.expires = jiffies + 5 * HZ;
brook_timer.function = &callback;
brook_timer.data = (unsigned long) &data;
add_timer(&brook_timer);
return 0;
}
static void __exit exit_modules(void)
{
del_timer(&brook_timer);
}
module_init(init_modules);
module_exit(exit_modules);
kernel timer最短的間隔是1個jiffies,而且會受到硬體中斷,和其他非同步事件的干擾,所以不適合非常精密的應用。

