Spin Lock (Spinlock) explained Classic, thorough

Source: Internet
Author: User

A spin lock is similar to a mutex lock, only the spin lock does not cause the caller to sleep, and if the spin lock has been maintained by another unit of execution, the caller has been looping there to see if the lock has been released by the spin lock, and the word "spin" is thus named.   Since the spin lock user generally keeps the lock time very short, it is necessary to choose spin instead of sleep, and the efficiency of the spin lock is much higher than that of the mutual exclusion lock.   Semaphores and read-write semaphores are suitable for longer periods of time, they can cause callers to sleep, so they can only be used in the process context (_trylock variants can be used in the interrupt context), and spin locks are suitable for keeping a very short time, which can be used in any context. If the protected shared resource is accessible only in the process context, it is appropriate to use semaphores to protect the shared resource, and if the access time to the Common Gateway resource is very short, the spin lock can also be used.   However, if the protected shared resource requires access to the interrupt context (including the bottom half, the interrupt handle and the top half, which is a soft interrupt), the spin lock must be used. The spin lock retention period is preemption failure, while the semaphore and read-write semaphore retention period can be preempted.   The spin lock is only really needed if the kernel can be preempted or SMP, and all operations of the spin lock are empty under a single CPU and not a preemptive kernel. Like a mutex, an execution unit must obtain a lock to access a shared resource that is protected by a spin lock, and the lock must be released after the shared resource has been accessed.   If no unit of execution retains the lock when acquiring a spin lock, the lock is immediately acquired, and if the lock is already retained when the spin lock is acquired, the acquisition of the lock will spin there until the lock is released by the person holding the spin lock.   Whether it is a mutex, or a spin lock, at any time, there can be at most one retention, and it is said that at any time only one execution unit can acquire the lock. The spin lock API has:spin_lock_init (x)This macro is used to initialize the spin lock x. Spin locks must be initialized before they are actually used. This macro is used for dynamic initialization.define_spinlock (x)
The macro declares a spin lock x and initializes it. The macro is defined for the first time in 2.6.11 and does not have the macro in the previous kernel.spin_lock_unlocked
This macro is used to statically initialize a spin lock.define_spinlock (x)Equivalent to spinlock_t x = spin_lock_unlocked spin_is_locked (x)
The macro is used to determine whether a spin lock x has been held by an execution unit (that is, locked) and, if so, returns True, otherwise it returns false.spin_unlock_wait (x)
The macro is used to wait for the spin lock X to become not maintained by any of the execution units, and if no execution unit holds the spin lock, the macro returns immediately, otherwise it loops there until the spin lock is released.Spin_trylock (Lock)
The macro tries to get a spin lock, and if you can get the lock immediately, it gets the lock and returns TRUE, otherwise you cannot get the lock immediately and return to false immediately. It does not spin wait for lock to be released.Spin_lock (Lock)
The macro is used to obtain the spin lock, and if it can get the lock immediately, it returns immediately, otherwise it will spin there until the spin lock's retention is released, and then it gets the lock and returns. In short, it only returns if it obtains a lock.Spin_lock_irqsave (lock, flags)
The macro obtains the spin lock while saving the value of the flag register to the variable flags and fails the local interrupt.SPIN_LOCK_IRQ (Lock)
The macro is similar to Spin_lock_irqsave, except that the macro does not save the value of the flag register.SPIN_LOCK_BH (Lock)
The macro fails the local soft interrupt while getting the spin lock.Spin_unlock (Lock)
The macro releases the spin lock, which is paired with Spin_trylock or Spin_lock. If Spin_trylock returns FALSE, the spin lock is not obtained, so no spin_unlock release is necessary.Spin_unlock_irqrestore (lock, flags)
While the macro frees the spin lock, it also restores the value of the flag register to the value that the variable flags holds. It is used in pairs with spin_lock_irqsave.SPIN_UNLOCK_IRQ (Lock)
This macro frees the spin lock and also enables local interrupts. It is paired with SPIN_LOCK_IRQ applications.SPIN_UNLOCK_BH (Lock)
The macro frees the spin lock while also enabling local soft interrupts. It is used in pairs with SPIN_LOCK_BH.Spin_trylock_irqsave (lock, flags)If the macro obtains a spin lock, it will also save the value of the flag register to the variable flags, and fail the local interrupt, and do nothing if the lock is not obtained.
So if you can get the lock immediately, it's equivalent to Spin_lock_irqsave, and if you can't get the lock, it's equivalent to Spin_trylock. If the macro obtains a spin lock, it needs to be released using Spin_unlock_irqrestore.SPIN_TRYLOCK_IRQ (Lock)The macro is similar to Spin_trylock_irqsave, except that the macro does not save the flag register. If the macro gets a spin lock, you need to use SPIN_UNLOCK_IRQ to release it.SPIN_TRYLOCK_BH (Lock)
If the macro obtains a spin lock, it will also fail the local soft interrupt. If you don't get a lock, it does nothing. Therefore, if the lock is obtained, it is equivalent to SPIN_LOCK_BH, and if it is not locked, it is equivalent to Spin_trylock. If the macro has a spin lock, you need to use SPIN_UNLOCK_BH to release it.Spin_can_lock (Lock)
  This macro is used to determine whether spin lock locks can be locked, and it is actually spin_is_locked. If lock is not locked, it returns true, otherwise, returns false.   The macro is defined for the first time in 2.6.11 and does not have the macro in the previous kernel.   There are several versions of the spin lock and free spin lock, so it is necessary for the reader to know what version of the macro to use to obtain and release the lock. If the protected shared resource is accessed only in process context and soft interrupt context, when accessing a shared resource in a process context, it may be interrupted by a soft interrupt, which may go into the soft interrupt context to access the protected shared resource, so access to the shared resource must be used Spin_lock   _BH and spin_unlock_bh to protect. Of course, the use of SPIN_LOCK_IRQ and SPIN_UNLOCK_IRQ as well as Spin_lock_irqsave and Spin_unlock_irqrestore can also be, they fail the local hard interrupt, the failure of the hard interrupt implicitly also fails the soft interrupt.   But using SPIN_LOCK_BH and SPIN_UNLOCK_BH is the most appropriate, it's faster than the other two.   If the protected shared resource is accessed only in the process context and in the Tasklet or timer context, then you should use the same macros that obtain and release the lock as above, because Tasklet and timer are implemented with soft interrupts. If a protected shared resource is accessed only in one tasklet or timer context, no spin lock protection is required because the same tasklet or timer can only run on one CPU, even in an SMP environment.   In fact, Tasklet has bound the Tasklet to the current CPU when it calls the Tasklet_schedule token that it needs to be scheduled, so the same tasklet is never likely to run on another CPU at the same time. The timer was also set to the current CPU when it was added to the timer queue using Add_timer, so the same timer could never run on another CPU.   Of course, the same tasklet has two instances running on the same CPU at the same time is even more impossible. If a protected shared resource is accessed only in two or more tasklet or timer contexts, access to the shared resource only needs to be protected with spin_lock and Spin_unlock, not using the _BH version, because when the Tasklet or timer runs,  There can be no other tasklet or timer running on the current CPU. If the protected shared resource is accessed only in the context of a soft interrupt (except Tasklet and timer), then thisShared resources need to be protected with spin_lock and Spin_unlock, because the same soft interrupts can be run on different CPUs at the same time.   If a protected shared resource is accessed in two or more soft interrupt contexts, the shared resource certainly needs to be protected with spin_lock and spin_unlock, and different soft interrupts can be run on different CPUs at the same time. If the protected shared resource is in a soft interrupt (including Tasklet and timer) or process context and hard interrupt context access, it may be interrupted by a hard interrupt during a soft interrupt or process context visit, thereby accessing the shared resource in a hard interrupt context, so   You need to use SPIN_LOCK_IRQ and SPIN_UNLOCK_IRQ to protect access to shared resources in a process or soft interrupt context.   And what version is used in the interrupt handling handle, depending on the situation, if only one interrupt handling handle accesses the shared resource, then only Spin_lock and Spin_unlock are needed in the interrupt handling handle to secure access to the shared resource. Because during the execution of an interrupt handling handle, it is not possible to be interrupted by a soft interrupt or process on the same CPU.   However, if you have different interrupt handling handles to access the shared resource, you need to use SPIN_LOCK_IRQ and SPIN_UNLOCK_IRQ in the interrupt handling handle to secure access to the shared resource. In the case of using SPIN_LOCK_IRQ and SPIN_UNLOCK_IRQ, it is entirely possible to replace it with Spin_lock_irqsave and Spin_unlock_irqrestore, which one should be used, depending on the circumstances,   If you can be sure that interrupts are enabled before accessing shared resources, it is better to use SPIN_LOCK_IRQ. Because it's faster than Spin_lock_irqsave, but if you're not sure if it's interrupted, then it's better to use Spin_lock_irqsave and Spin_unlock_irqrestore,   Because it restores the interrupt flag before accessing the shared resource rather than directly disrupting it. Of course, in some cases it is necessary to break the expiration when accessing the shared resource, and it is necessary to interrupt enabling when the access is finished, such as using SPIN_LOCK_IRQ and SPIN_UNLOCK_IRQ best.
Spin_lock is used to block concurrent access to shared resources by the execution unit on different CPUs and to the asynchronous access of shared resources resulting from different process contexts, while interrupt failures and soft interrupt failures are designed to prevent soft outages on the same CPU or to disrupt asynchronous access to shared resources.

Source: http://blog.csdn.net/unbutun/article/details/5730037

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.