Spin lock spin_lock

Source: Internet
Author: User

 

1. Most of the locks are implemented by a mechanism called spin lock, which can be used in unsleep code.

For example, if a spin lock is a mutually exclusive device, there can be only two values: "Lock" and "unlock ".

It is often implemented as a single bit in an integer. Code testing related to getting a special lock

If the lock is available, the lock bit is set and the code continues to enter the critical section. On the contrary, as shown in figure

If the lock has been acquired by others, the Code enters a compact loop and checks the lock repeatedly until it changes

Available. This loop is the "Spin" part of the spin lock.
2. the inclusion file required by the spin lock primitive is <Linux/spinlock. h>. An actual lock has a type.
Spinlock_t
-------------------------------------------------------------
Operations related to spin locks
1. Define the spin lock
Spinlock_t lock;
2. initialize the spin lock
Spin_lock_init (Lock); this macro is used to dynamically initialize the spin lock.
3. before entering a critical section, your code must obtain the required lock.
Spin_lock (Lock); If the lock can be obtained immediately, it returns immediately; otherwise, it will spin there

Until the lock is released. Note that all spin lock waits are, because of their characteristics, not

Can be interrupted. Once you call the spin_lock, you will spin until the lock becomes available.
Spin_trylock (Lock); the macro tries to get the spin lock. If it can be obtained immediately, it gets the lock and returns

Returns true. Otherwise, the system returns false immediately.
4. Release a lock you have obtained
Spin_unlock (Lock );
------------------------------------------------------------
1. The core rule applied to the spin lock is required by any code. It is atomic when holding the spin lock.

Not sleep; in fact, it cannot give up the processor for any reason, except for service interruptions (and sometimes

It won't work at this time)
2. The spin lock is generally used in this way.
Spinlock_t lock;
Spin_lock_init (& lock );
Spin_lock (& lock);/* Get the spin lock to protect the critical section */
/* Critical section */
Spin_unlock (& lock );
3. The spin lock is mainly applicable to scenarios where SMP or single CPU but the kernel can be preemptible.

System, spin lock degrades to empty operation.
4. The spin lock ensures that the critical zone is not disturbed by other CPU or preemptible processes in the current CPU.

When executing the critical section, the code path may also be affected by interruptions and the bottom half.
The complete understanding of the spin lock API requires an understanding of Interrupt Processing and related concepts.
In fact, there are four functions that can lock a spin lock.
Void spin_lock (spinlock_t * Lock );
Void spin_lock_irqsave (spinlock_t * Lock, unsigned long flags );
Void spin_lock_irq (spinlock_t * Lock );
Void spin_lock_bh (spinlock_t * Lock
There are also four methods to release a spin lock; the one you use must correspond to the function you used to obtain the lock.
Void spin_unlock (spinlock_t * Lock );
Void spin_unlock_irqrestore (spinlock_t * Lock, unsigned long flags );
Void spin_unlock_irq (spinlock_t * Lock );
Void spin_unlock_bh (spinlock_t * Lock );
5. Pay attention to the following issues when using spin locks:
1) The spin lock is actually a waiting lock. When the lock cannot be notified, the CPU continuously executes "test and set" the lock until it is available to obtain the lock, the CPU does not do any useful work while waiting for the spin lock, just waiting. Therefore, it is reasonable to use the spin lock only when the lock occupation time is very short.
2) spin locks may cause system deadlocks. (Recursive use of spin locks)
3) functions that may cause process scheduling cannot be called during spin lock.

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.