Linux kernel timer example
Notes about timer in Linux kernel.内容主要转载自Timer Example。
1. Prerequisite
jiffies记录了系统启动以来,经过了多少tick。
一个tick代表多长时间,在内核的CONFIG_HZ中定义。比如CONFIG_HZ=200,则一个jiffies对应5ms时间。所以内核基于jiffies的定时器精度也是5ms。
2. Timer API
A timer is represented in the kernel as an instance of timer_list:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct timer_list {
/*
* All fields that change during normal runtime grouped to the
* same cacheline
*/
struct hlist_node entry;
unsigned long expires;
void (*function)(struct timer_list *);
u32 flags;
struct lockdep_map lockdep_map;
};
expires is an absolute value in jiffies. entry is a doubley linked list, and a callback function.
3. Timer setup initialization
The following are steps to initialize timers:
Setting up the timer: Set up the timer, feeding the user-defined callback function.
1
2
3
4
5
6
7
8
9
10
11
12/**
* timer_setup - prepare a timer for first use
* @timer: the timer in question
* @callback: the function to call when timer expires
* @flags: any TIMER_* flags
*
* Regular timer initialization should use either DEFINE_TIMER() above,
* or timer_setup(). For timers on the stack, timer_setup_on_stack() must
* be used and must be balanced with a call to destroy_timer_on_stack().
*/
__init_timer((timer), (callback), (flags))Setting the expiration time: When the timer is initialized, we need to set its expiration before the callback gets fired:
1
int mod_timer(struct timer_list *timer, unsigned long expires);
Releasing the timer: When you are done with the timer, it needs to be released:
1
2int del_timer(struct timer_list * timer);
int del_timer_sync(struct timer_list *timer);
del_timer() return 0 on an inactive timer, and return 1 on an active timer, del_timer_sync waits for the handler to finish its execution, even those that may happen on another CPU. You should not hold a lock preventing the handler’s completion, otherwise it will result in a dead lock. You should release the timer in the module cleanup routine. You can independently check whether the timer is running or not:1
int timer_pending(const struct timer_list * timer);
4. Example
1 |
|
1 | $ dmesg |
参考资料: