Why do we have interrupts in spinlock?

Do we really turn interrupts off in spinlock_acquire and then turn them on only when release spinlock? Does it mean that there will be no context switches between acquiring and releasing? What for do we do it?

This how the functions look likeL

void spinlock_acquire(struct spinlock *splk)
     splraise(IPL_NONE, IPL_HIGH);

void spinlock_release(struct spinlock *splk)
     spllower(IPL_HIGH, IPL_NONE);


By definition, spinlock holds the cpu until it is acquired. It is like spinlock is per_cpu as it is holding a cpu. Of course, it does not mean that it is acquired by all threads that will be scheduled on the cpu. Actually, since there is no interrupt, there is no context switch and hence there is only one thread held to the cpu untill we call spinlock_release.

There are also some comments regarding this in code. if we don’t have a disable_interrupts in spinlock_acquire and multiple acquire are called from same cpu, it will be something wrong.
I am not sure if this something is deadlock or some other problem ?


  • Get the lock.

  • First disable interrupts (otherwise, if we get a timer interrupt we

  • might come back to this lock and deadlock), then use a machine-level

  • atomic operation to wait for the lock to be free.



spinlock_acquire(struct spinlock *splk)