Linux Kernel synchronization method analysis-(below)

Source: Internet
Author: User
Spin locks are available in two forms: Full locks and read/write locks. First, let's take a look at the complete lock.

First, create a new spin lock with a simple declaration. This can be initialized by calling spin_lock_init. Each variable shown in listing 5 implements the same result.

Listing 5. Create and initialize the spin lock spinlock_t my_spinlock = spin_lock_unlocked; ...... Or ...... define_spinlock (my_spinlock); ...... spin_lock_init (& my_spinlock );

After the spin lock is defined, a large number of locked variables can be used. Each variable is used in different contexts.

The variables spin_lock and spin_unlock are shown in Listing 6. This is the simplest variable. It does not disable interruption, but contains all the memory barriers (memory barrier ). This variable assumes that there is no interaction between the interrupt handler and the lock.

Listing 6. spin lock and unlock functions: spin_lock (& my_spinlock); // critical sectionspin_unlock (& my_spinlock );

Next are irqsave and irqrestore pairs, as shown in listing 7. The spin_lock_irqsave function requires a spin lock and interrupts are disabled on the local processor (in SMP scenarios. The spin_unlock_irqrestore function releases the spin lock and restores the interrupt (through the flags parameter.

Listing 7. spin lock variables, where the local CPU interrupt is disabled, such as spin_lock_irqsave (& my_spinlock, flags); // critical sectionspin_unlock_irqrestore (& my_spinlock, flags );

One of the less secure variants of spin_lock_irqrestore/spin_unlock_irq is. I recommend that you do not use this variant because it assumes the interruption status.

Finally, if the kernel thread shares data through the bottom half method, another variation of the spin lock can be used.Bottom halfThis method can delay the operation in the device driver until it is interrupted. This spin lock disables soft interruptions on the local CPU. This prevents softirq, tasklet, and bottom half from running on the local CPU. This variation is shown in listing 8.

Listing 8. Implementation of the bottom-half interaction in the spin lock function: spin_lock_bh (& my_spinlock); // critical sectionspin_unlock_bh (& my_spinlock );

Read/write lock

In many cases, data access is performed by a large number of reads and a small number of write operations (Reading data is more common than writing data ). Read/write locks are created to support this model. This model is interesting because it allows multiple threads to access the same data at the same time, but only one thread can write data at the same time. If the thread that executes the write operation holds this lock, the critical segment cannot be read by other threads. If a thread that executes the read operation holds this lock, multiple read threads can enter the critical segment. Listing 9 demonstrates this model.

Listing 9. read/write spin lock function rwlock_t my_rwlock; rwlock_init (& my_rwlock); write_lock (& my_rwlock); // critical section -- can read and writewrite_unlock (& my_rwlock ); read_lock (& my_rwlock); // critical section -- can read onlyread_unlock (& my_rwlock );

The read/write spin locks are modified for bottom half and interrupt request (IRQ) based on the lock requirements. Obviously, if you are using the original read/write lock, use this spin lock according to the standard spin lock usage, without distinguishing between the read thread and the write thread.

Kernel mutex lock

In the kernel, mutex locks can be used to implement semaphore behavior. Kernel mutex is implemented on the atomic API, but this is invisible to kernel users. Mutex lock is simple, but some rules must be kept in mind. Only one task can hold a mutex lock at a time, and only this task can unlock the mutex lock. Mutex locks cannot be recursively locked or unlocked, and they may not be used in interactive context. However, mutex locks are faster and more compact than the current kernel semaphore options, so if they meet your needs, they will be your wise choice.

You can use the define_mutex macro to create and initialize mutex. This creates a new mutex lock and initializes its structure. You can view this implementation in./Linux/include/Linux/mutex. h.

The mutex lock API provides five functions: three of them are used for locking, one for unlocking, and the other for testing the mutex lock. First, let's take a look at the locking function. You can use the first function mutex_trylock to immediately lock the lock and control the mutex lock when it is unavailable. This function is shown in listing 10.

Listing 10. Try to use mutex_trylock to obtain the mutex lock ret = mutex_trylock (& my_mutex); If (Ret! = 0) {// got the lock !} Else {// did not get the lock}

To wait for the lock, you can call mutex_lock. This call is returned when the mutex lock is available. Otherwise, it will sleep before the mutex lock is available. In either case, when the control is returned, the caller holds a mutex lock. Finally, the caller uses mutex_lock_interruptible when sleep. In this case, the function may return-eintr. The two calls are shown in listing 11.

Listing 11. Locking a mutex lock that may be in sleep state mutex_lock (& my_mutex); // lock is now held by the caller. If (mutex_lock_interruptible (& my_mutex )! = 0) {// interrupted by a signal, no mutex held}

When a mutex lock is locked, it must be unlocked. This is done by the mutex_unlock function. This function cannot be called from the interrupt context. Finally, you can call mutex_is_locked to check the status of the mutex lock. This call is actually compiled into an inline function. If the mutex lock is held (locked), 1 is returned; otherwise, 0 is returned. Listing 12 demonstrates these functions.

Listing 12. Use mutex_is_locked to test mutex_unlock (& my_mutex); If (mutex_is_locked (& my_mutex) = 0) {// mutex is unlocked}

The mutex lock API has its own limitations because it is based on the atomic API. However, it is highly efficient and can be used if it meets your needs.

Big kernel lock)

Finally, let's take a look at the big kernel lock (BLK ). Its usage in the kernel is getting smaller and smaller, but there are still some reserved usage. BKL makes multi-processor Linux possible, but the fine-grained (finer-grained) Lock is slowly replacing BKL. BKL is provided through the lock_kernel and unlock_kernel functions. For more information, see./Linux/lib/kernel_lock.c.

 

Linux has extraordinary performance, and the locking method is the same. The atomic lock not only provides a locking mechanism, but also provides arithmetic or bitwise operations. The spin lock provides a locking mechanism (mainly used in SMP), and The read/write spin lock allows multiple read threads and only one write thread obtains the given lock. Finally, mutex lock is a new locking mechanism that provides a simple API built on the atom. Whatever you need, Linux provides a locking solution to protect your data.

 

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.