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.