spin_lock, spin_lock_irq and spin_lock_irqsave
2017-12-15 00:00
344 查看
spin lock
spin lock has three variants: spin_lock, spin_irq and spin_irqsave. I am not going to talk the internal details, but give the hard rule to choose which to use. spin lock is used to protect the short/quick critical section. A critical section could be in process context or in interrupt handler. The *_irq and *_irqsave ones are for cases when the contended data could be accessed from both process context and interrupt hander.spin_lock_irqsave
If the data could be accessed from process context or interrupt context, usually you should use spin_lock_irqsave version like:spinlock_t mr_lock = SPIN_LOCK_UNLOCKED; unsigned long flags; spin_lock_irqsave(&mr_lock, flags); /* critical section ... */ spin_unlock_irqrestore(&mr_lock, flags);
The use of spin_lock_irqsave() will disable interrupts locally and provide the spinlock on SMP. This covers both interrupt and SMP concurrency issues. With a call to spin_unlock_irqrestore(), interrupts are restored to the state when the lock was acquired.
spin_lock_irq
Different with *_irqsave, spin_lock_irq will lock and unconditionally disables the IRQ, and spin_unlock_irq unlocks and re-enable the IRQ as below:spinlock_t mr_lock = SPIN_LOCK_UNLOCKED; spin_lock_irq(&mr_lock); /* critical section ... */ spin_unlock_irq(&mr_lock);
The problem is that you may enable the IRQ unexpectedly when the IRQ was already disabled before spin_lock_irq.
spin_lock
Only when you are sure the data will only be accessed in process context, you're safe to use spin_lock without bothering with enabling/disabling the interrupt.相关文章推荐
- spin_lock_irq和spin_lock_irqsave
- Linux内核spin_lock、spin_lock_irq、spin_lock_irqsave
- Linux内核spin_lock、spin_lock_irq 和 spin_lock_irqsave
- mutex and spin_lock_irq
- 从spin_lock到spin_lock_irqsave
- [内核同步]自旋锁spin_lock、spin_lock_irq 和 spin_lock_irqsave 分析
- Linux内核spin_lock与spin_lock_irq分析
- 自旋锁spin_lock、spin_lock_irq 和 spin_lock_irqsave 分析
- spin_lock_irqsave ----由bluesleep module 带来的tip
- 为什么我的spin_lock_irqsave()没有锁住时钟中断?
- spin_lock 和 spin_lock_irqsave
- 【转】那些情况该使用它们spin_lock到spin_lock_irqsave
- spin_lock、spin_lock_irq、spin_lock_irqsave区别
- spin_lock & spin_lock_irqsave
- 嵌入式 Linux内核spin_lock与spin_lock_irq以及spin_lock_irqsave分析
- spin_lock和spin_lock_irqsave
- Processing , Mutex and SpinLock
- spin_lock、spin_lock_irq、spin_lock_irqsave区别
- spin_lock, spin_lock_irq, spin_lock_irqsave 的使用场景
- [内核同步]自旋锁spin_lock、spin_lock_irq 和 spin_lock_irqsave 分析