Why spinlocks don't work in uniprocessor (unicore) systems?

前端 未结 5 1995
生来不讨喜
生来不讨喜 2020-12-23 23:55

I know that spinlocks work with spining, different kernel paths exist and Kernels are preemptive, so why spinlocks don\'t work in uniprocessor systems? (for example, in Linu

5条回答
  •  被撕碎了的回忆
    2020-12-24 00:15

    there are different versions of spinlock:

    spin_lock_irqsave(&xxx_lock, flags);
    ... critical section here ..
    spin_unlock_irqrestore(&xxx_lock, flags);
    

    In Uni processor spin_lock_irqsave() should be used when data needs to shared between process context and interrupt context, as in this case IRQ also gets disabled. spin_lock_irqsave() work under all circumstances, but partly because they are safe they are also fairly slow. However, in case data needs to be protected across different CPUs then it is better to use below versions, these are cheaper ones as IRQs dont get disabled in this case:

    spin_lock(&lock);
    ...
    spin_unlock(&lock);
    

    In uniprocessor systems calling spin_lock_irqsave(&xxx_lock, flags); has the same effect as disabling interrupts which will provide the needed interrupt concurrency protection without unneeded SMP protection. However, in multiprocessor systems this covers both interrupt and SMP concurrency issues.

提交回复
热议问题