views:

113

answers:

1

Hi,

I would like to know why spin locks are used instead of semaphores inside an interrupt handler.

Thanks & Regards,

Mousey

+5  A: 

Semaphores cause tasks to sleep on contention, which is unacceptable for interrupt handlers. Basically, for such a short and fast task (interrupt handling) the work carried out by the semaphore is overkill. Also, spinlocks can't be held by more than one task.

Michael Foukarakis
@mfukar I know the reason but I didnt understand why double acquire deadlocks can occur because of sleeping and not while using spin locks
mousey
Also I would like to know why spin_trylock() is needed ?
mousey
@mousey: double-acquire deadlocks cannot occur with spinlocks, because if a spinlock is shared with an interrupt handler all lockers of the spinlock must use the interrupt-disabling variant of the lock function.
caf
@caf who shares spin lock with an interrupt handler ? Can you please explain. Similarly mutexes can be done in the same way right. we can disable all the interrupts.
mousey
@mousey: `spin_trylock()` is commonly used where the critical section is optional, and can be skipped.
Michael Foukarakis
@mousey: Someone shares a spin lock with an interrupt handler if they access the same shared data that the interrupt handler does. Mutexes cannot disable interrupts, since if you disable interrupts and sleep, you will never wake up!
caf
@caf Now I got it. Thank you very much.
mousey
Seem to have missed the point that when an interrupts uses a spinlock it is to cater for the case where the spinlock has been taken by software running on another processor - the interrupt handler spins whilst the other processor finishes its access to the guarded resource and releases the spinlock. If an interrupt handler - running with interrupts disabled - hits a locked spinlock held by a thread on the same processor then you have a deadlock. As mentioned above, mutex between a thread and an interrupt handler on the same processor is handled by the thread disabling interrupts.
Dipstick