spin_lock_irqsave 与 spin_lock_irq [英] spin_lock_irqsave vs spin_lock_irq

查看:18
本文介绍了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?

推荐答案

如果在您的代码开始锁定之前已经禁用了中断,则当您调用 spin_unlock_irq 时,您将在可能不需要的情况下强制重新启用中断方式.相反,如果您还通过 spin_lock_irqsave 将当前中断启用状态保存在 flags 中,则尝试在释放锁定后重新启用具有相同 flags 的中断,该函数只会恢复之前的状态(因此不一定会启用中断).

If interrupts are already disabled before your code starts locking, when you call spin_unlock_irq you will forcibly re-enable interrupts in a potentially unwanted manner. If instead you also save the current interrupt enable state in flags through spin_lock_irqsave, attempting to re-enable interrupts with the same flags after releasing the lock, the function will just restore the previous state (thus not necessarily enabling interrupts).

spin_lock_irqsave 示例:

spinlock_t mLock = SPIN_LOCK_UNLOCK;
unsigned long flags;

spin_lock_irqsave(&mLock, flags); // Save the state of interrupt enable in flags and then disable interrupts
// Critical section
spin_unlock_irqrestore(&mLock, flags); // Return to the previous state saved 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 interrupts are already disabled
// Critical section
spin_unlock_irq(&mLock); // Could result in an unwanted interrupt re-enable...

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

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