I would like to know why spin locks are used instead of semaphores inside an interrupt handler.
Whats the problem with semaphore & mutex. And why spinlock needed ?
semaphore
or mutex
in interrupt handlers. The answer is yes and no. you can use the up and unlock, but you can’t use down and lock, as these are blocking calls which put the process to sleep
and we are not supposed to sleep in interrupt handlers.Note that semaphore is not a systemV IPC techniques, its just a synchronization techniques. And there are three functions to acquires the semaphore.
down() : acquire the semaphore and put into un-interruptible state.
down_trylock() : try if lock is available, if lock is not available , don't sleep.
So, what if we want to achieve the synchronization in interrupt handlers ? Use spinlocks.
What spinlocks will do ?
Spinlock is a lock which never yields.
Similar to mutex, it has two operations – lock and unlock.
If the lock is available, process will acquire it and will continue in the critical section and unlock it, once its done. This is similar to mutex
. But, what if lock is not available ? Here, comes the interesting difference. With mutex
, the process will sleep
, until the lock is available. But,
in case of spinlock, it goes into the tight loop, where it continuously checks for a lock, until it becomes available
.