Reputation: 21
Can anybody explain exact use of spinlock in case of device driver. I am just confused as in many of the interrupt handler routines i have seen spinlocks. As spinlock are busy waiting. it can make delay in other interrupts.. Please if possible list down some scenarios for device driver in which we can use spinlock
I am new to this field. your help makes some initial boost for me.
Upvotes: 2
Views: 1463
Reputation: 8992
Spinlocks are used in high-Irql scenarios where you cannot give your quantum to a scheduler.
So for example when you want to lock some data structure (outside of interrupts), you acquire a mutex. When somebody else owns the mutex, your thread is put into a 'waiting' list in the scheduler until the mutex is released. Then it receives ownership of the mutex and all other threads accessing it are put to sleep when they try to acquire the mutex.
When you are in an interrupt, you cannot do this. You neither have a thread context, nor should this interrupt be postponed 'indefinitely'. But sometimes, you do need to access shared data inside a Interrupt. When there are more then one CPU present in a system, several interrupts and threads can run at the same time. So you must protect work queues, shared objects, etc., so that they will not get corrupted. To do this, you use spinlocks. Because the policy is to do as few things as possible inside a spinlock-guarded-section, you can expect that there are no problems from spinning on the lock itself for only a couple of cycles, until it gets released by the owner.
So, what to take from this, is that when you access shared data from an interrupt, you have to use a spinlock, because there are no other primitives which can be used.
And as noted, always hold a spinlock only for very small amounts of time.
Upvotes: 4