DENX . Training2 . LddUsingDelays

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);

----- Revision r1.1 - 26 Mar 2008 - 11:06 - MarkusKlotzbuecher
Copyright © 2002-2021 by DENX Software Engineering