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; #ifdef CONFIG_LOCKDEP struct lockdep_map lockdep_map; #endif };
/** * @TIMER_DEFERRABLE: A deferrable timer will work normally when the * system is busy, but will not cause a CPU to come out of idle just * to service it; instead, the timer will be serviced when the CPU * eventually wakes up with a subsequent non-deferrable timer. * * @TIMER_IRQSAFE: An irqsafe timer is executed with IRQ disabled and * it's safe to wait for the completion of the running instance from * IRQ handlers, for example, by calling del_timer_sync(). * * Note: The irq disabled callback execution is a special case for * workqueue locking issues. It's not meant for executing random crap * with interrupts disabled. Abuse is monitored! * * @TIMER_PINNED: A pinned timer will not be affected by any timer * placement heuristics (like, NOHZ) and will always expire on the CPU * on which the timer was enqueued. * * Note: Because enqueuing of timers can migrate the timer from one * CPU to another, pinned timers are not guaranteed to stay on the * initialy selected CPU. They move to the CPU on which the enqueue * function is invoked via mod_timer() or add_timer(). If the timer * should be placed on a particular CPU, then add_timer_on() has to be * used. */ #define TIMER_CPUMASK 0x0003FFFF #define TIMER_MIGRATING 0x00040000 #define TIMER_BASEMASK (TIMER_CPUMASK | TIMER_MIGRATING) #define TIMER_DEFERRABLE 0x00080000 #define TIMER_PINNED 0x00100000 #define TIMER_IRQSAFE 0x00200000 #define TIMER_INIT_FLAGS (TIMER_DEFERRABLE | TIMER_PINNED | TIMER_IRQSAFE) #define TIMER_ARRAYSHIFT 22 #define TIMER_ARRAYMASK 0xFFC00000struct 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; #ifdef CONFIG_LOCKDEP struct lockdep_map lockdep_map; #endif };
/** * 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(). */ #define timer_setup(timer, callback, flags) \ __init_timer((timer), (callback), (flags)) extern int mod_timer(struct timer_list *timer, unsigned long expires);
第一个参数是一个指向定时器的指针,第二个函数是回调函数,第三个参数是定时器的flags
注意,调用timer_setup并不会开始计时,只是初始化而已
开启定时器
调用add_timer之后,定时器就开始计时了
void add_timer(struct timer_list *timer)
修改定时器
使用mod_timer来修改定时器的超时时间,当调用后,定时器也会开始计时
int mod_timer(struct timer_list *timer, unsigned long expires);
[ 174.376654] In /home/liode/nfs_dir/tmp/timer_test_1.c tiemr_test_init (efault) [ 176.429446] in timer_func [ 178.477630] in timer_func [ 180.525649] in timer_func [ 182.573641] in timer_func [ 184.621682] in timer_func [ 186.669393] in timer_func [ 188.717528] in timer_func [ 190.765575] in timer_func [ 192.813704] in timer_func [ 194.861760] in timer_func [ 196.909732] in timer_func [ 198.957690] in timer_func [ 201.005811] in timer_func [ 203.053696] in timer_func [ 205.101819] in timer_func [ 207.149806] in timer_func [ 209.197712] in timer_func [ 211.245846] in timer_func [ 213.293587] in timer_func [ 215.334934] in timer_func [ 217.369458] in timer_func [ 217.428013] In /home/liode/nfs_dir/tmp/timer_test_1.c tiemr_test_exit (efault)