1
votes

I understand that we disable interrupts when acquiring spinlocks in interrupt handlers.

I have a doubt as to what happens when a spinlock is held in process context and at that instant an interrupt occurs ?

Can this scenario occur ?

As per my understanding if it so happens that the handler too try to acquire the same lock held in process context , it would keep on spinning for that lock.

So do we always disable interrupts when acquiring spinlock ?

1

1 Answers

-2
votes

Spinning is what spin locks are for.

When the interrupt happens on another CPU, it will spin only until the process-context lock has been unlocked.

When the interrupt happens on the same CPU, it would spin forever. This is why you should disable interrupts when taking a spin lock in process context, and if the same lock is also used by an interrupt handler.

(Disabling interrupts when taking a spin lock in interrupt context typically is not necessary because an interrupt handler cannot be interrupted by itself.)