Skip to main content.

5.7.6. How to delay execution

bad: burning CPU cycles (don't use)

#define DELAY_MS 100
unsigned long j = jiffies + DELAY_MS * HZ / 1000;

while (time_before(jiffies, j1))
      cpu_relax();

slightly better but still wasteful:

while (time_before(jiffies, j1))
      schedule();

recommended for small (busy waiting) delays

void ndelay(unsigned long nsecs);
void udelay(unsigned long usecs);
void mdelay(unsigned long msecs);

better: sleep instead of busy waiting if possible:

void msleep(unsigned int ms);

/* 
 * interruptible version (for waitqueues)
 * return 0 or if interrupted remaining time
 */
unsigned long msleep_interruptible(unsigned int ms);

void ssleep(unsigned int sec);

longer delays: use waitqueues!:

wait_queue_head_t wait;
init_waitqueue_head(&wait);

/* condition is always false=0, delay in jiffies  */
wait_event_interruptible_timeout(wait, 0, delay);
5.7.5. Allocating Memory 1. Denx Training Topics 5.7.7. Kernel library functions
Prev Home Next