Article Title: synchronization mechanism of Linux kernel-spin lock. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
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 the spin lock is closely related to the architecture. The code is generally implemented through Assembly and defined in files. The actually used interfaces are defined in folders. The spin lock APIs include:
This macro is used to initialize the spin lock x. The spin lock must be initialized before it is actually used. This macro is used to dynamically initialize the specified.
The macro declares a spin lock x and initializes it. The Macro was defined for the first time in 2.6.11 and is not in the previous kernel.
This macro is used to initialize a spin lock statically.
CODE: DEFINE_SPINLOCK (x) is equivalent to spinlock_t x = SPIN_LOCK_UNLOCKEDspin_is_locked (x ))
|
This macro is used to determine whether the spin lock x has been maintained by an execution unit (that is, locked). If yes, true is returned; otherwise, false is returned.
The macro is used to wait for the spin lock x to become not maintained by any execution unit. If no execution unit keeps the spin lock, the macro returns immediately; otherwise, the loop will be there, the spin lock is released until it is retained.
The macro tries its best to get the spin lock. If it can get the lock immediately, it gets the lock and returns the true value. Otherwise, it cannot get the lock immediately and returns false immediately. It does not spin to wait for the lock to be released.
This macro is used to obtain the spin lock. If the lock can be obtained immediately, it will return immediately. Otherwise, it will spin there until the holder of the spin lock is released, it acquires the lock and returns it. In short, only the lock is returned.
CODE:spin_lock_irqsave(lock, flags)
|
When this macro obtains the spin lock, it saves the value of the Flag register to the variable flags and invalidates local interruptions.
[1] [2] Next page