spin_lock_irqsave和spin_lock_irq [英] spin_lock_irqsave vs spin_lock_irq

查看:821
本文介绍了spin_lock_irqsave和spin_lock_irq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SMP机器上,我们必须在中断上下文中使用spin_lock_irqsave而不是spin_lock_irq.

On an SMP machine we must use spin_lock_irqsave and not spin_lock_irq from interrupt context.

我们为什么要保存标志(包含IF)?

Why would we want to save the flags (which contain the IF)?

还有另一个可以打断我们的中断程序吗?

Is there another interrupt routine that could interrupt us?

推荐答案

我对内核是陌生的,但是从罗伯特·洛夫(Robert Love)的书"Linux Kernel Development"(Linux内核开发)中可以了解到,如果在代码开始之前已在处理器上禁用了中断,锁定,当您调用spin_unlock_irq时,您将以错误的方式释放锁定.如果保存标志并用标志释放它,则函数spin_lock_irqsave会将中断返回到其先前的状态.

I am new to the kernel but from what I gather from Robert Love's book "Linux Kernel Development", if interrupts are already disabled on the processor before your code starts locking, when you call spin_unlock_irq you will release the lock in an erroneous manner. If you save the flags and release it with the flags, the function spin_lock_irqsave will just return the interrupt to its previous state.

带有spin_lock_irqsave

spinlock_t mLock = SPIN_LOCK_UNLOCK;
unsigned long flags;

spin_lock_irqsave(&mLock, flags); // save the state, if locked already it is saved in flags
// Critical section
spin_unlock_irqrestore(&mLock, flags); // return to the formally state specified in flags

带有spin_lock_irq(不带irqsave)的示例:

Example with spin_lock_irq( without irqsave ):

spinlock_t mLock = SPIN_LOCK_UNLOCK;
unsigned long flags;

spin_lock_irq(&mLock); // Does not know if already locked
// Critical section
spin_unlock_irq(&mLock); // Could result in an error unlock...

这篇关于spin_lock_irqsave和spin_lock_irq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆