Synchronization Mechanism of Linux kernel-spin lock

Source: Internet
Author: User
Linux Kernel synchronization mechanism-spin lock-general Linux technology-Linux programming and kernel information. The following is a detailed description. A spin lock can only be held by one executable thread. The spin lock does not cause the caller to sleep. If an execution thread tries to obtain a held spin lock, the thread will keep repeating and waiting, check whether the holder of the spin lock has released the lock. Therefore, the word "Spin" is named.

Because spin locks are usually kept for a very short period of time, it is necessary to choose spin instead of sleep. The efficiency of spin locks is much higher than that of mutex locks.

Semaphores and read/write semaphores are suitable for long periods of time. They can cause the caller to sleep, so they can only be used in the process context (the _ trylock variant can be used in the interrupt context ); the spin lock is suitable for keeping the time very short, because a competing spin lock makes the thread requesting it spin while waiting for re-availability, which is especially a waste of processing time, this is the key of the spin lock, so the spin lock should not be held for a long time. In actual application, the spin lock code has only a few lines, and the time for holding the spin lock generally does not exceed two upstream/downstream switches, because once the thread needs to be switched, it takes at least two times to cut out and cut in. If the time used by the spin lock is much longer than the two context switches, we can make the thread sleep, which loses the significance of designing the spin lock.

If the protected shared resource is accessed only in the context of the process, it is very suitable to use semaphores to protect the shared resource. If the access time to the shared resource is very short, the spin lock can also be used. However, if the protected shared resource needs to interrupt context access (including the bottom half, namely the Interrupt Processing handle and the top half, that is, the Soft Interrupt), the spin lock must be used.

During the spin lock holding period, the preemption fails, while the semaphore and read/write semaphores can be preemptible. The spin lock is required only when the kernel can be preemptible or SMP. In a single CPU and kernel that cannot be preemptible, all operations of the spin lock are null.

To access Shared resources protected by the spin lock, an execution unit must first obtain the lock and release the lock after accessing the shared resources. If no execution unit keeps the lock when obtaining the spin lock, the lock will be obtained immediately. If the lock already has the lock when obtaining the spin lock, the get lock operation will spin there, until the lock is released by the holder of the spin lock.

No matter whether it is a mutex lock or a spin lock, there can be at most one lock at any time. That is to say, at most one execution unit can get the lock at any time. The implementation of spin locks is closely related to the architecture. The code is generally implemented through Assembly, defined in files, and the actually used interfaces are defined in folders. In, the spin lock APIs include:
CODE: spin_lock_init (x)

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.