In-depth introduction to Concurrency Control for Linux Device Drivers
Source: Internet
Author: User
For more information about concurrency control of Linux Device Drivers-Linux general technology-Linux programming and kernel, see the following. In the driver, when multiple threads access the same resource at the same time (the global variable in the driver is a typical shared resource), it may lead to a "race State ", therefore, we must control the concurrency of shared resources. The most common method to solve Concurrency Control in Linux kernel is spin lock and semaphore (usually used as mutex lock ).
Spin locks are similar to semaphores, but not similar to semaphores. Similar to semaphores, spin locks are similar to semaphores in terms of functions. They are completely different in nature and implementation mechanism, but not in the same category.
The spin lock will not cause the caller to sleep. If the spin lock has been maintained by another execution unit, the caller will keep repeating and check whether the lock holder has released the lock, "Spin" means "in-situ rotation ". The semaphores cause the caller to sleep. They drag the process out of the running queue unless the lock is obtained. This is their "not class ".
However, either a semaphore or a spin lock can have at most one lock at any time point, that is, at most one execution unit can obtain a lock at any time point. This is their "similar ".
In view of the above features of the spin lock and semaphore, the spin lock is suitable for keeping the time very short, it can be used in any context; the semaphore is suitable for holding for a long time, it can only be used in the process context. If the protected shared resource is accessed only in the process context, the shared resource can be protected by semaphores. If the access time to the shared resource is very short, the spin lock is also a good choice. 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.
Semaphore-related APIs mainly include:
Define semaphores
Struct semaphore sem;
Initialize semaphores
Void sema_init (struct semaphore * sem, int val );
This function initializes the semaphores and sets the semaphores sem value to val.
Void init_MUTEX (struct semaphore * sem );
This function is used to initialize a mutex lock, that is, it sets the semaphores sem value to 1, equivalent to sema_init (struct semaphore * sem, 1 );
Void init_MUTEX_LOCKED (struct semaphore * sem );
This function is also used to initialize a mutex lock, but it sets the semaphores sem value to 0, equivalent to sema_init (struct semaphore * sem, 0 );
Obtain semaphores
Void down (struct semaphore * sem );
This function is used to obtain semaphores sem, which can cause sleep and therefore cannot be used in the interrupted context;
Int down_interruptible (struct semaphore * sem );
This function is similar to the down function. The difference is that the down function cannot be interrupted by signals, but down_interruptible can be interrupted by signals;
Int down_trylock (struct semaphore * sem );
This function tries to obtain the semaphores sem. If it can be obtained immediately, it obtains the semaphores and returns 0. Otherwise, a non-zero value is returned. It does not cause the caller to sleep and can be used in the interrupted context.
Release semaphores
Void up (struct semaphore * sem );
This function releases semaphores sem to wake the waiting person.
APIS related to spin locks mainly include:
Define a spin lock
Spinlock_t spin;
Initialize the spin lock
Spin_lock_init (lock)
This macro is used to dynamically initialize the spin lock.
Get spin lock
Spin_lock (lock)
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;
Spin_trylock (lock)
This macro tries to get the spin lock. If it can get the lock immediately, it gets the lock and returns the true value. Otherwise, it immediately returns the false value and does not actually "turn in the original position ";
Release a spin lock
Spin_unlock (lock)
This macro releases the spin lock, which is used in pairs with the spin_trylock or the spin_lock;
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