Delaying Execution
Using the timer interrupt and the value of jiffies
, it’s
easy to generate time intervals that are multiples of the timer tick,
but for smaller delays, the programmer must resort to software loops,
which are introduced last in this section.
Although I’ll show you all the fancy techniques, I think it’s best to become familiar with timing issues by first looking at simple code, though the first implementations I’m going to show are not the best ones.
Long Delays
If you want to delay execution by a multiple of the clock tick or you don’t require strict precision (for example, if you want to delay an integer number of seconds), the easiest implementation (and the most brain-dead) is the following, also known as busy waiting:
unsigned long j = jiffies + jit_delay * HZ; while (jiffies < j) /* nothing */;
This kind of implementation should definitively be avoided.[19] I’m showing it here because on occasion you might want to run this code to understand better the internals of other code (I’ll suggest how to test using busy waiting towards the end of this chapter).
But let’s look at how this code works. The loop is guaranteed
to work because jiffies
is declared as volatile
by the kernel headers and therefore is reread any time some C code accesses it. Though ``correct,'' this busy loop completely locks the computer for the duration of the delay; the scheduler never interrupts a process that is running in kernel space. Since the kernel is non-reentrant in the current implementation, ...
Get Linux Device Drivers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.