Spin lock in Linux kernel function sleep_on_common

Source: Internet
Author: User

Note that the first call is spin_lock_irqsave (), but then the release of the lock uses spin_unlock ()

The second time to obtain the spin, it is required to use the spin _ lock_irq (), but the release lock uses the spin _unlock_irqrestore (), why?

Static long _ sched

Sleep_on_common (wait_queue_head_t * q, int state, long timeout)
{
Unsigned long flags;
Wait_queue_t wait;
Init_waitqueue_entry (& wait, current );
_ Set_current_state (State );
Spin_lock_irqsave (& Q-> lock, flags );
_ Add_wait_queue (Q, & wait );
Spin_unlock (& Q-> lock );
Timeout = schedule_timeout (timeout );
Spin_lock_irq (& Q-> lock );
_ Remove_wait_queue (Q, & wait );
Spin_unlock_irqrestore (& Q-> lock, flags );
Return timeout;
}
Explanation:

Call spin_lock_irqsave () for the first time, get the spin lock to disable interruption, and save the previous interruption status to flags. Then call _ add_wait_queue () you can safely Add a waiting event to the waiting queue. Then, call spin_unlock () to release the spin lock, but the previous interruption status is not restored.

Then, the current process schedule_timeout () is called to make the CPU (Scheduled) Wait.

After the event arrives, the task is activated again, and then the code after schedule_timeout () is executed, that is, the second spin lock spin_lock_irq (). For details about the spin_lock_irq, we can ensure that there is no other Code to prohibit interruption of the local processor (or, in other words, we can ensure that interruption should be enabled when the spin lock is released: the original statement in ldd3, it is also appropriate to use it here. Therefore, if this sentence is obtained after the execution of spin_lock_irq (), you can safely execute _ remove_wait_queue () delete the previously added wait events from the pending linked list, and then call spin_unlock_irqrestore () to achieve these goals: 1. Release the spin lock; 2. Restore the previously saved interrupt status;

It can be seen that this cleverly restores the process to the interrupted state before it executes the wait event.

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.