Measures taken for concurrency in the kernel

Source: Internet
Author: User
During multi-threaded execution, concurrency issues may occur. If the processing is poor, multi-threaded execution may result in data processing errors. SMP (symmetric multi-processor) and kernel preemption are two scenarios of multi-thread execution. We raised the issue of protecting shared kernel resources. Below are common kernel methods.

1. spin lock and mutex
First, understand the concept of the critical section. The critical section is the code area for accessing shared resources. Spin lock and mutex (matual exclusion) are two basic mechanisms to protect the kernel.
1) The spin lock ensures that only one thread enters the critical section at the same time. Other threads that want to enter must wait for release. Of course, the thread is not the kernel thread, but the execution thread.

Basic usage: # include <Linux/spinlock. h> spinlock_t mylock = spin_lock_unlocked; spin_lock (& mylock );/*.... critical Section code... */spin_unlock (& mylock);/* release the lock */


2) The mutex does not rotate in situ until it enters an occupied critical section, but changes the current thread to sleep.

 

Basic usage: # include <Linux/mutex. h> static define_mutex (mymutex); mutex_lock (& mymutex );/*.... critical Section code... */mutex_unlock (& mymutex);/* release the mutex */


So how do I choose to use it?
1) if you need to sleep in the critical section, you can only select mutex;
2) only spin locks can be used in interrupt functions;

2. Atomic operations
Atomic operations are used to perform lightweight operations only once. It is often used to modify counters, add values with conditions, and set bits. Atomic Operations ensure that the operations are serialized and do not require locks to protect concurrency.
For specific operation interfaces, see include/ASM-xxx-ARCH/Atomic. h.

3. read/write lock
You can select this option when each execution unit is either read or write but not simultaneously accessed in the critical area.

Basic operation (read): rwlock_t myrwlock = rw_lock_unlocked; read_lock (& myrwlock );/*.... critical Section code... */read_unlock (& myrwlock);/* release the lcok */basic operation (write): rwlock_t myrwlock = rw_lock_unlocked; write_lock (& myrwlock );/*.... critical Section code... */write_unlock (& myrwlock);/* release the lcok */


There are also related Interrupt Processing: read_lock_irqsave (), read_unlock_irqstore (), write_lock_irqsave (), write_lock_irqstore.
1) now, in the new kernel, there is a sequential lock (seqlock) that reads more than writes ).
Basic operations:

 

  do{       read_seqbegin(&xtime_lock);       /* .... Critical Section code ... */   }while(read_seqretry(&xtime_lock,seq);


2) there is another mechanism in the kernel, RCU (read -- copy -- Update ). This mechanism is used to improve the performance when the number of read operations is much higher than that of write operations. The basic idea is that the read thread does not need to be locked, but the write thread needs to become much more complex. First, copy the data and then update the data structure. To ensure the completion of the read operation, the copy will consistently maintain context switching on all RCUs. For more information, see documentation/RCU /*.

4. debugging tools
1) You can configure (config_debug_spinlock) to output common spin lock errors.
2) lockmeter (http://oss.sgi.com/project/lockmeter/) tool can be used for mobile phone lock related statistics.
3) Of course, keep in mind that when a lock is applied, it must be released. deadlock is a common problem.

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.