spin_lock on non-preemtive linux kernels

前端 未结 4 1158
忘掉有多难
忘掉有多难 2021-01-14 02:56

I read that on a system with 1 CPU and non preemtive linux kernel (2.6.x) a spin_lock call is equivalent to an empty call, and thus implemented that way.

I can\'t un

4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-14 03:08

    If you were to use spin_lock() on a non-preemptive kernel to shield data against an interrupt handler, you'd deadlock (on a single-processor machine).

    If the interrupt handler runs while other kernel code holds the lock, it will spin forever, as there is no way for the regular kernel code to resume and release the lock.

    Spinlocks can only be used if the lock holder can always run to completion.

    The solution for a lock that might be wanted by an interrupt handler is to use spin_lock_irqsave(), which disables interrupts while the spinlock is held. With 1 cpu, no interrupt handler can run, so there will not be a deadlock. On smp, an interrupt handler might start spinning on another cpu, but since the cpu holding the lock can't be interrupted, the lock will eventually be released.

提交回复
热议问题