Previous Next Table of Contents

6. Timers

If you need to start some timer and run your routine at some time later, you'll need to use Linux timers. There are two flavors of timers in Linux, but the old one is deprecated, you should use just the new fast timers. This interface is defined in <linux/timer.h>. Basically, you need struct timerlist somewhere (usually inside of some structure with your data), which you initialize at the beginning with init_timer(). Whenever you want to activate some timer, you make sure the function, data and expires members of the structure are filled in and call add_timer. This will add your timer to the list of pending timers and once current time (measured in jiffies - timer interrupt ticks) will reach or exceed expires, your function will be called with argument data. You can also use del_timer to de-activate some pending timer. The timer handlers are executed in bottom half context of the timer bottom half handler. Note that some kernel subsystems, like scsi, use their own interfaces to add and delete timers. Once a timer expires and calls it's function, it is deactivated, so basically these timers are one-shot. If you need some timer to be active again, you need to set the expires member and call add_timer on it again.


Previous Next Table of Contents