Linux kernel driver delay "turn"

Source: Internet
Author: User

transferred from: http://blog.chinaunix.net/uid-24219701-id-3288103.html jiffies counter timer interrupts are generated by the system's timing hardware at regular intervals; this interval is programmed by the kernel at boot time based on the Hz value, which is a system-dependent value, and the value of a kernel counter is incremented each time a clock break occurs. This counter is initialized to 0 at system startup, So it represents the number of ticks since the last boot.

This counter and the utility function to read it are located, though you will often just contain,

#includeunsigned long J, Stamp_1, Stamp_half, Stamp_n;j = jiffies;/* Read the current value * /stamp_1 = j + HZ;/* 1 Second in the future */stamp_half = j + hz/2;/* Half a second * /Stamp_n = j + N * hz/1000;/* n milliseconds */

Busy Waiting

If you want to delay the execution of multiple clock ticks, allowing some negligence in the value, the easiest (albeit not recommended) implementation is a loop that monitors the Jiffy counters. This busy wait implementation often looks like the following code, where J1 is the value of jiffies in the delay timeout:

While (Time_before (jiffies, J1)) {}

timed outThe sub-optimal delay cycle shown so far is done by looking at the Jiffy counter without telling anyone. But the best way to implement a time-lapse, as you might guess, is to ask the kernel to do it for you. There are 2 ways to build a jiffy-based timeout, depending on whether your drive Wait for other events to be moved.

If your driver uses a wait queue to wait for some other event, but you also want to make sure it runs within a certain time period, you can use Wait_event_timeout or wait_event_interruptible_timeout:

#includeLong Wait_event_timeout (wait_queue_head_t Q, condition, long timeout);Long Wait_event_interruptible_timeout (wait_queue_head_t Q, condition, long timeout);

These functions sleep on a given queue, but they return after a timeout (expressed in jiffies). Therefore, they achieve a limited sleep that does not always go to bed. Note that the timeout value represents the number of jiffies to wait, not an absolute time. This value is represented by a signed number, because it is sometimes the result of a subtraction operation, although these functions complain by a PRINTK statement if the timeout value provided is negative. If the timeout is reached, these functions return 0; If the process is awakened by another event, it returns the remaining timeout value in Jiffies. The return value is never negative, even if the delay is greater than the expected value due to the system load.

Wait_event_timeout and Wait_event_interruptible_timeout are designed to have a hardware-driven presence, which can be restored in any way: either someone calls Wake_up on the waiting queue, or times out. This does not apply to Jitqueue because no one is calling wake_up on the waiting queue (after all, no other code knows about it), so this process wakes up when it expires. To accommodate this particular situation, here you want to postpone execution without waiting for a specific event, the kernel provides the Schedule_timeout function, so you can avoid declaring and using an extra waiting queue header:

#includesigned Long Schedule_timeout (signed long timeout);

Here, timeout is the number of jiffies to delay. The return value is 0 unless the function returns (responds to a signal) before the given timeout is lost. Schedule_timeout requests the caller to set the current process state first, so a typical call looks like this:

set_current_state (task_interruptible);schedule_timeout (delay);

The first line calls Set_current_state to set something so that the scheduler does not run the current process again until the timeout resets it back to the task_running state . To get an interruptible delay , use task_uninterruptible instead .

Short delay

When a device driver needs to handle its hardware response time, the latency involved is often up to a few milliseconds.

Kernel functions Ndelay, Udelay, and mdelay are useful for short delays, delaying the execution of a specified number of nanoseconds, microseconds, or milliseconds, respectively. Their prototypes are:

#includevoid Ndelay (unsigned long nsecs);void Udelay (unsigned long usecs);void Mdelay (unsigned long msecs);

There is another way to get a millisecond (and longer) delay without involving a busy wait. The file declares these functions:

void msleep (unsigned int millisecs);unsigned long msleep_interruptible (unsigned int millisecs);void ssleep (unsigned int seconds)

The first 2 functions allow the calling process to enter sleep for a given number of milliseconds. A call to Msleep is non-interruptible; You can ensure that the process sleeps at least a given number of milliseconds. If your driver is in a waiting queue and you want to wake up to interrupt sleep, use msleep_interruptible. The return value from msleep_interruptible is normally 0; If, however, the process is awakened early, the return value is the number of milliseconds remaining in the initial request sleep cycle. The call to Ssleep causes the process to enter a non-interruptible sleep given the number of seconds.

Linux kernel driver delay "turn"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.