Timer

#include <linux/delay.h>

There are several functions that can be used to wait for some amount of time. They all start with one character that describes the unit of time that must be given as parameter: s for secons, m for milliseconds, u for microseconds, and n for nanoseconds.

{m,n,u}delay(t)

Spin the processor for the specified interval. These functions can even be used in interrupt handlers but they should not be used if the desired delay is greater than a few milliseconds.

{s,m}sleep(t)

Put the current process to sleep for the specified interval.

#include <linux/timer.h>

A timer can call a function at a specific point in time. Unit of time is 'jiffies', a counter that is monotonically increasing.

 void my_timer(unsigned long data) {}
 struct timer_list;
 init_timer(&timer_list);
 timer_list.function = my_timer;
 timer_list.data = arg_for_my_timer;
 timer_list.expires = jiffies + msecs_to_jiffies(msecs);
add_timer(&timer_list)

activate a timer

del_timer(&timer_list)

deactivate timer

del_timer_sync(&timer_list)

same as del_timer, but wait if a timer is still running

The timer is one-shot: it is automatically deactivated when the timer function gets executed. Both del_timer functions return 0 if the function was started and 1 if the timer is still active.

Be careful, the timer function is executed in interrupt context (by the softirq system). So it must not access the current pointer and must not sleep.