The Linux kernel provides 3 functions for nanosecond, subtle, and millisecond latencies, respectively:
void Ndelay (unsigned long nsecs);
void Udelay (unsigned long usecs);
void Mdelay (unsigned long msecs);
These 3 functions of the delay principle is busy waiting, that is, in the process of delay did not give up the CPU, according to the frequency of the CPU to carry out a certain number of cycles.
In the kernel for the millisecond above the delay, it is best not to use the Mdelay function directly, which will waste CPU resources, for milliseconds above the delay, the kernel provides the following functions:
void msleep (unsigned int millisecs);
unsigned long msleep_interruptible (unsigned int milosecs);
void Ssleep (unsigned int seconds);
Note: The accuracy of msleep similar functions is limited by the system Hz and process scheduling.
1 long delay
In the kernel, an intuitive method of delay is to add the current jiffies of the time set for the delay to the delay time, so that the time of delay can be judged simply by comparing the current jiffies and the set time. For this method, a simple macro is provided in the kernel to determine whether the delay is complete.
Time_after (Jiffies,delay); /* Now if you have not reached the time delay, then return true, otherwise return 0*/
Time_before (jiffies,delay); * If the delay has not been completed, return true, otherwise return 0*/
The following two functions can add the current process to the wait queue to sleep on the waiting queue, and the process will wake up when the timeout occurs:
Sleep_on_timeout (wait_queue_head_t *q, unsigned long timeout);
Interrupt_sleep_on_timeout (wait_queue_head_t *q, unsigned long timeout);