Spinlock Flow in Kernel level
A spinlock is a lock which loops for the lock (repeatedly checks for the lock) until it acquires the lock, i.e. It is kind of busy waiting on lock. Spinlocks are supposed to use when the critical section we are going to execute takes very short period of time. i.e. the critical section should not contain any sleeping functions/time taking calls. The above is the introduction for the spin lock, Lets move to the kernel level execution of spinlocks in brief....
When we are going to use the spinlocks we includes linux/spinlock.h header file. But internally it includes some other header files which combinely helps us in implementing spinlocks. The following is the information of specific header files and there role in implementing the spinlocks.
1. asm/spinlock_types.h: Contains the raw_spinlock_t/raw_rwlock_t and the initializers
2. linux/spinlock_types.h: Defines the generic type and initializers
3. asm/spinlock.h: Contains the __raw_spin_*( )/etc. lowlevel implementations, mostly inline assembly code
4. linux/spinlock_api_smp.h: Contains the prototypes for the _spin_*( ) APIs
5. linux/spinlock.h: Builds the final spin_*( ) APIs
Description:
Whats happening here when we call a spinlock call the flow will be like:
architecture indepenedent spinlock calls ---> architecture dependent spinlock calls
struct spinlock_t = {
volatile unsigned long *lock;
}
When we try to spinlock, the kernel will check the lower significant bit of the 'lock' structure member to check whether this bit is set. If the bit is not set then it means the lock is available, So the kernel will set the bit , it means the process that is trying to acquire the spinlock is succeed. The bit set of lock should be done in single instruction cycle (To avoid the conflicts like: two process at a time trying to acquire the lock, then to make sure only one process has acquired the lock and many others), For this purpose we use atomic instructions (The instructions which executes in single instruction cycle). For this we need hardware to support these instructions. Which confirms that to implement the spinlocks we need hardware support along with software(OS) support.
Note: For rwlocks most significant bit of 'lock' will be checked.
RSS Feed
Twitter
Orkut