Skip to main content.
Navigation:
DENX
>
Training2
>
LddUsingDelays
Translations:
Edit
|
Attach
|
Raw
|
Ref-By
|
Printable
|
More
Training2
Sections of this site:
DENX Home
|
DULG
|
ELDK-5
|
Know
|
Training
|
U-Boot
|
U-Bootdoc
Topics
Training2 Home
Changes
Index
Search
Go
List of pages in Training2
Search
%SECTION0{name=LddUsingDelays}% How to delay execution * Linux timekeeping: =HZ=, jiffies * sometimes we need to delay execution, for example * short: device initialization, timeouts, ... * longer: need to do something at certain time *bad: burning CPU cycles (don't use)* <verbatim> #define DELAY_MS 100 unsigned long j = jiffies + DELAY_MS * HZ / 1000; while (time_before(jiffies, j1)) cpu_relax(); </verbatim> * =cpu_relax= means _not doing much_. Calls architecture dependent code, actually only a memory barrier on powerpc. *slightly better but still wasteful:* <verbatim> while (time_before(jiffies, j1)) schedule(); </verbatim> * others can run, but system stays loaded (we stay in run queue of the scheduler) *recommended for small (busy waiting) delays* <verbatim> void ndelay(unsigned long nsecs); void udelay(unsigned long usecs); void mdelay(unsigned long msecs); </verbatim> *better: sleep instead of busy waiting if possible:* <verbatim> 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); </verbatim> * Interesting: =msleep(1)= can sleep for up to 20ms [[http://thread.gmane.org/gmane.linux.kernel/555287][lkml thread]]. Proposed solution: implement msleep with hrtimers. *longer delays: use waitqueues!:* <verbatim> wait_queue_head_t wait; init_waitqueue_head(&wait); /* condition is always false=0, delay in jiffies */ wait_event_interruptible_timeout(wait, 0, delay); </verbatim>
5.7.5. Allocating Memory
1. Denx Training Topics
5.7.7. Kernel library functions
Prev
Home
Next