Linux内核定时器
一、Linux定时器主要调用接口
1、定时器数据结构
1 struct timer_list {
2 /*
3 * All fields that change during normal runtime grouped to
4 * the same cacheline
5 */
6 struct hlist_node entry;
7 unsigned long expires;
8 void (*function)(unsigned long);
9 unsigned long data;
10 u32 flags;
11 int slack;
12
13 #ifdef CONFIG_TIMER_STATS
14 int start_pid;
15 void *start_site;
16 char start_comm[16];
17 #endif
18 #ifdef CONFIG_LOCKDEP
19 struct lockdep_map lockdep_map;
20 #endif
21 };
2、定时器初始化接口
(1)、init_timer
原型:#define init_timer(timer)
用法:
i、声明定时器
ii、调用init_timer
iii、初始timer字段 :
(2)、TIMER_INITIALIZER
原型:#define TIMER_INITIALIZER(_function, _expires, _data)
用法:
i、初始化定时器
注意:使用TIMER_INITIALIZER初始化定时器,必须在声明定时器变量的同时初始化。
ii、初始化timer字段
使用该方法初始化定时器,因为在调用接口的同时,已经将回调函数等参数传入,可以不用初始化timer字段。但是,在开启定时器的时候,定时器会立即执行。如需要开启定时器后延时一段时间执行,需重新对expires字段赋值。
(3)、DEFINE_TIMER
原型:#define DEFINE_TIMER(_name, _function, _expires, _data)
用法:
i、初始化定时器
ii、初始化timer字段
使用该方法初始化定时器,因为在调用接口的同时,已经将回调函数等参数传入,可以不用初始化timer字段。但是,在开启定时器的时候,定时器会立即执行。如需要开启定时器后延时一段时间执行,需重新对expires字段赋值。
_name字段,不可以是字符串。
(4)、setup_timer
原型:#define setup_timer(timer, fn, data)
用法:
i、初始化定时器
ii、调用setup_timer
ii、初始化timer字段
使用该方法初始化定时器,因为在调用接口的同时,已经将回调函数等参数传入,可以不用初始化timer字段。但是,在开启定时器的时候,定时器会立即执行。如需要开启定时器后延时一段时间执行,需重新对expires字段赋值。
3、定时器开启接口
(1)、add_timer
原型:void add_timer(struct timer_list *timer)
用法:
i、保证定时器的主要字段已经被初始化:expires、function、data等等
ii、add_timer(&MYTIMER);
iii、定时器被激活,根据expires参数执行
(2)、mod_timer
原型:int mod_timer(struct timer_list *timer, unsigned long expires)
用法:
i、保证定时器的主要字段已经被初始化:function、data等等
ii、mod_timer(&MYTIMER,jiffies + TIME_SECOND*HZ);
iii、定时器被激活,根据expires参数执行
iiii、如果timer此时已经被激活,返回1
3、定时器销毁接口
(1)、del_timer.
原型:int del_timer(struct timer_list *timer)
用法:
(2)、del_timer_sync
原型:int del_timer_sync(struct timer_list *timer)
用法:
与 del_timer 相比,此函数会等待定时器服务函数执行完毕,然后删除定时器。
4、完整测试代码
1 #include <linux/init.h>
2 #include <linux/module.h>
3 #include <linux/timer.h>
4
5 #define RUN_TIMES_VALUE 5 //timer func run RUN_TIMES_VALUE times,then exit
6
7 #define TIME_SECOND 5 //restart time
8
9 static void timer_start(void);
10 static void timer_restart(void);
11 static void timer_func(unsigned long param);
12 static void timer_destroy(void);
13
14 /**** init type include 4 kinds ****/
15
16 //#define TIMER_INIT_TYPE_1
17 #define TIMER_INIT_TYPE_2
18 //#define TIMER_INIT_TYPE_3
19 //#define TIMER_INIT_TYPE_4
20
21
22 #ifdef TIMER_INIT_TYPE_1
23 #define TIMER1_PRINT "This is timer 1 func"
24 #define THE_TIMER_NAME1 my_timer1
25 #define MYTIMER THE_TIMER_NAME1
26 #define MY_PRINT TIMER1_PRINT
27 static struct timer_list THE_TIMER_NAME1;
28 #endif
29
30 #ifdef TIMER_INIT_TYPE_2
31 #define TIMER2_PRINT "This is timer 2 func"
32 #define THE_TIMER_NAME2 my_timer2
33 #define MYTIMER THE_TIMER_NAME2
34 #define MY_PRINT TIMER2_PRINT
35 static struct timer_list THE_TIMER_NAME2 = TIMER_INITIALIZER(timer_func,0,((unsigned long)TIMER2_PRINT));
36 #endif
37
38 #ifdef TIMER_INIT_TYPE_3
39 #define TIMER3_PRINT "This is timer 3 func"
40 #define THE_TIMER_NAME3 my_timer3 //不可以是字符串
41 #define MYTIMER THE_TIMER_NAME3
42 #define MY_PRINT TIMER3_PRINT
43 DEFINE_TIMER(THE_TIMER_NAME3,timer_func,0,((unsigned long)TIMER3_PRINT));
44 #endif
45
46 #ifdef TIMER_INIT_TYPE_4
47 #define TIMER4_PRINT "This is timer 4 func"
48 #define THE_TIMER_NAME4 my_timer4
49 #define MYTIMER THE_TIMER_NAME4
50 #define MY_PRINT TIMER4_PRINT
51 static struct timer_list THE_TIMER_NAME4;
52 #endif
53
54
55 /**** destroy type,include 2 kinds ****/
56
57 #define TIMER_DESTROY_TYPE_1
58 //#define TIMER_DESTROY_TYPE_2
59
60 /**** activate timer, include 2 kinds ****/
61 #define TIMER_ACTIVATE_TYPE_1
62 //#define TIMER_ACTIVATE_TYPE_2
63
64
65
66 static void timer_start(void)
67 {
68 int ret = 0;
69 #ifdef TIMER_ACTIVATE_TYPE_1
70 printk("Timer start in type 1 -- add_timer\r\n");
71 add_timer(&MYTIMER);
72 ret = mod_timer(&MYTIMER,jiffies + TIME_SECOND*HZ);
73 printk(KERN_INFO"start -- Ret of mod_timer is %d\n",ret);
74 #endif
75 #ifdef TIMER_ACTIVATE_TYPE_2
76 printk("Timer start in type 2 -- mod_timer\r\n");
77 ret = mod_timer(&MYTIMER,jiffies + TIME_SECOND*HZ);
78 printk(KERN_INFO"start -- Ret of mod_timer is %d\n",ret);
79 #endif
80 }
81 static void timer_restart(void)
82 {
83 int ret = 0;
84 #ifdef TIMER_ACTIVATE_TYPE_1
85 printk("Timer restart in type 1 -- add_timer\r\n");
86 MYTIMER.expires = jiffies + TIME_SECOND*HZ;
87 add_timer(&MYTIMER);
88 #endif
89 #ifdef TIMER_ACTIVATE_TYPE_2
90 printk("Timer restart in type 2 -- mod_timer\r\n");
91 ret = mod_timer(&MYTIMER,jiffies + TIME_SECOND*HZ);
92 printk(KERN_INFO"restart -- Ret of mod_timer is %d\n",ret);
93 #endif
94 }
95
96 static void timer_func(unsigned long param)
97 {
98 static unsigned int counter = 0;
99 char* p_print = (char*)param;
100
101 printk(KERN_INFO"\r\n");
102 if(counter < RUN_TIMES_VALUE)
103 {
104 printk(KERN_INFO"%s %d\r\n",p_print,counter);
105 counter++;
106 timer_restart();
107 }
108 else
109 {
110 printk(KERN_INFO"Run times run out of\r\n");
111 }
112 }
113
114 static void timer_init(void)
115 {
116 printk(KERN_INFO"Alloc a timer\r\n");
117
118 #ifdef TIMER_INIT_TYPE_1
119 printk(KERN_INFO"Select type 1 to init the timer -- init_timer\r\n");
120 init_timer(&MYTIMER);
121 #endif
122
123 #ifdef TIMER_INIT_TYPE_2
124 printk(KERN_INFO"Select type 2 to init the timer -- TIMER_INITIALIZER\r\n");
125 #endif
126
127 #ifdef TIMER_INIT_TYPE_3
128 printk(KERN_INFO"Select type 3 to init the timer -- DEFINE_TIMERr\r\n");
129 #endif
130
131 #ifdef TIMER_INIT_TYPE_4
132 setup_timer(&MYTIMER,timer_func,(unsigned long )MY_PRINT);
133 printk(KERN_INFO"Select type 4 to init the timer -- setup_timer\r\n");
134 #endif
135
136 MYTIMER.expires = jiffies + TIME_SECOND*HZ;
137 MYTIMER.function = timer_func;
138 MYTIMER.data = (unsigned long )MY_PRINT;
139
140
141 }
142 static void timer_destroy(void)
143 {
144
145 #ifdef TIMER_DESTROY_TYPE_1
146 printk(KERN_INFO"Destroy the timer in destroy type 1 -- del_timer\r\n");
147 del_timer(&MYTIMER);
148 #endif
149
150 #ifdef TIMER_DESTROY_TYPE_2
151 printk(KERN_INFO"Destroy the timer in destroy type 2 -- del_timer_sync\r\n");
152 del_timer_sync(&MYTIMER);
153 #endif
154 }
155
156
157 static int __init timer_test_init(void)
158 {
159 printk(KERN_INFO"Enter timer test\r\n");
160 timer_init();
161 timer_start();
162 return 0;
163 }
164
165 static void __exit timer_test_exit(void)
166 {
167 timer_destroy();
168
169 printk(KERN_INFO"Exit timer test\r\n");
170 }
171
172 module_init(timer_test_init);
173 module_exit(timer_test_exit);
174
175 MODULE_LICENSE("GPL");
176 MODULE_AUTHOR("Marvin.xin");
177 MODULE_DESCRIPTION("A Test Module for Timer");
5、同一个定时器函数,服务4个timer
根据定时器函数参数区分不同的定时器
1 #include <linux/init.h>
2 #include <linux/module.h>
3 #include <linux/timer.h>
4
5 #define RUN_TIMES_VALUE 5 //timer func run RUN_TIMES_VALUE times,then exit
6
7 #define TIME_SECOND 5 //restart time
8
9 static void timer_start(void);
10 static void timer_restart(void);
11 static void timer_func(unsigned long param);
12 static void timer_destroy(void);
13
14 /**** init type include 4 kinds ****/
15
16 //#define TIMER_INIT_TYPE_1
17 #define TIMER_INIT_TYPE_2
18 //#define TIMER_INIT_TYPE_3
19 //#define TIMER_INIT_TYPE_4
20
21
22 #ifdef TIMER_INIT_TYPE_1
23 #define TIMER1_PRINT "This is timer 1 func"
24 #define THE_TIMER_NAME1 my_timer1
25 #define MYTIMER THE_TIMER_NAME1
26 #define MY_PRINT TIMER1_PRINT
27 static struct timer_list THE_TIMER_NAME1;
28 #endif
29
30 #ifdef TIMER_INIT_TYPE_2
31 #define TIMER2_PRINT "This is timer 2 func"
32 #define THE_TIMER_NAME2 my_timer2
33 #define MYTIMER THE_TIMER_NAME2
34 #define MY_PRINT TIMER2_PRINT
35 static struct timer_list THE_TIMER_NAME2 = TIMER_INITIALIZER(timer_func,0,((unsigned long)TIMER2_PRINT));
36 #endif
37
38 #ifdef TIMER_INIT_TYPE_3
39 #define TIMER3_PRINT "This is timer 3 func"
40 #define THE_TIMER_NAME3 my_timer3 //不可以是字符串
41 #define MYTIMER THE_TIMER_NAME3
42 #define MY_PRINT TIMER3_PRINT
43 DEFINE_TIMER(THE_TIMER_NAME3,timer_func,0,((unsigned long)TIMER3_PRINT));
44 #endif
45
46 #ifdef TIMER_INIT_TYPE_4
47 #define TIMER4_PRINT "This is timer 4 func"
48 #define THE_TIMER_NAME4 my_timer4
49 #define MYTIMER THE_TIMER_NAME4
50 #define MY_PRINT TIMER4_PRINT
51 static struct timer_list THE_TIMER_NAME4;
52 #endif
53
54
55 /**** destroy type,include 2 kinds ****/
56
57 #define TIMER_DESTROY_TYPE_1
58 //#define TIMER_DESTROY_TYPE_2
59
60 /**** activate timer, include 2 kinds ****/
61 #define TIMER_ACTIVATE_TYPE_1
62 //#define TIMER_ACTIVATE_TYPE_2
63
64
65
66 static void timer_start(void)
67 {
68 int ret = 0;
69 #ifdef TIMER_ACTIVATE_TYPE_1
70 printk("Timer start in type 1 -- add_timer\r\n");
71 add_timer(&MYTIMER);
72 ret = mod_timer(&MYTIMER,jiffies + TIME_SECOND*HZ);
73 printk(KERN_INFO"start -- Ret of mod_timer is %d\n",ret);
74 #endif
75 #ifdef TIMER_ACTIVATE_TYPE_2
76 printk("Timer start in type 2 -- mod_timer\r\n");
77 ret = mod_timer(&MYTIMER,jiffies + TIME_SECOND*HZ);
78 printk(KERN_INFO"start -- Ret of mod_timer is %d\n",ret);
79 #endif
80 }
81 static void timer_restart(void)
82 {
83 int ret = 0;
84 #ifdef TIMER_ACTIVATE_TYPE_1
85 printk("Timer restart in type 1 -- add_timer\r\n");
86 MYTIMER.expires = jiffies + TIME_SECOND*HZ;
87 add_timer(&MYTIMER);
88 #endif
89 #ifdef TIMER_ACTIVATE_TYPE_2
90 printk("Timer restart in type 2 -- mod_timer\r\n");
91 ret = mod_timer(&MYTIMER,jiffies + TIME_SECOND*HZ);
92 printk(KERN_INFO"restart -- Ret of mod_timer is %d\n",ret);
93 #endif
94 }
95
96 static void timer_func(unsigned long param)
97 {
98 static unsigned int counter = 0;
99 char* p_print = (char*)param;
100
101 printk(KERN_INFO"\r\n");
102 if(counter < RUN_TIMES_VALUE)
103 {
104 printk(KERN_INFO"%s %d\r\n",p_print,counter);
105 counter++;
106 timer_restart();
107 }
108 else
109 {
110 printk(KERN_INFO"Run times run out of\r\n");
111 }
112 }
113
114 static void timer_init(void)
115 {
116 printk(KERN_INFO"Alloc a timer\r\n");
117
118 #ifdef TIMER_INIT_TYPE_1
119 printk(KERN_INFO"Select type 1 to init the timer -- init_timer\r\n");
120 init_timer(&MYTIMER);
121 #endif
122
123 #ifdef TIMER_INIT_TYPE_2
124 printk(KERN_INFO"Select type 2 to init the timer -- TIMER_INITIALIZER\r\n");
125 #endif
126
127 #ifdef TIMER_INIT_TYPE_3
128 printk(KERN_INFO"Select type 3 to init the timer -- DEFINE_TIMERr\r\n");
129 #endif
130
131 #ifdef TIMER_INIT_TYPE_4
132 setup_timer(&MYTIMER,timer_func,(unsigned long )MY_PRINT);
133 printk(KERN_INFO"Select type 4 to init the timer -- setup_timer\r\n");
134 #endif
135
136 MYTIMER.expires = jiffies + TIME_SECOND*HZ;
137 MYTIMER.function = timer_func;
138 MYTIMER.data = (unsigned long )MY_PRINT;
139
140
141 }
142 static void timer_destroy(void)
143 {
144
145 #ifdef TIMER_DESTROY_TYPE_1
146 printk(KERN_INFO"Destroy the timer in destroy type 1 -- del_timer\r\n");
147 del_timer(&MYTIMER);
148 #endif
149
150 #ifdef TIMER_DESTROY_TYPE_2
151 printk(KERN_INFO"Destroy the timer in destroy type 2 -- del_timer_sync\r\n");
152 del_timer_sync(&MYTIMER);
153 #endif
154 }
155
156
157 static int __init timer_test_init(void)
158 {
159 printk(KERN_INFO"Enter timer test\r\n");
160 timer_init();
161 timer_start();
162 return 0;
163 }
164
165 static void __exit timer_test_exit(void)
166 {
167 timer_destroy();
168
169 printk(KERN_INFO"Exit timer test\r\n");
170 }
171
172 module_init(timer_test_init);
173 module_exit(timer_test_exit);
174
175 MODULE_LICENSE("GPL");
176 MODULE_AUTHOR("Marvin.xin");
177 MODULE_DESCRIPTION("A Test Module for Timer");