自旋锁和互斥锁的区别是,自旋锁不会引起睡眠,所以可用于不能休眠的代码中(如IRQ)
自旋锁保持期间抢占失效,而信号量保持期间可以被抢占
定义
spinlock_t lock;
init
#define spin_lock_init(_lock)
do {
spinlock_check(_lock);
raw_spin_lock_init(&(_lock)->rlock);
} while (0)
获得
static inline void spin_lock(spinlock_t *lock)
static inline int spin_trylock(spinlock_t *lock)
spin_lock:获得锁以后,立即返回,否则,它将自旋在那里,直到该自旋锁的保持者释放
spin_trylock:获得锁失败,立即返回假
释放
static inline void spin_unlock(spinlock_t *lock)
举例
spinlock_t lock;
spin_lock_init(&lock)
...
critical section
...
spin_unlock(&lock)
其他
尽管自旋锁可以保证临界区不受别的CPU和本CPU抢占进程打扰,但是得到锁的代码在执行临界区的时候,还可能受到中断和底半部的影响
static inline void spin_lock_irq(spinlock_t *lock)
#define spin_lock_irqsave(lock, flags)
static inline void spin_lock_bh(spinlock_t *lock)
static inline void spin_unlock_irq(spinlock_t *lock)
static inline void spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
static inline void spin_unlock_bh(spinlock_t *lock)
spin_lock_irq() = spin_lock() + local_irq_disable()
spin_lock_irqsave() = spin_lock() + local_irq_save()
spin_lock_bh() = spin_lock() + local_bh_disable()
spin_unlock_irq() = spin_unlock() + local_irq_enable()
spin_unlock_irqrestore() = spin_unlock() + local_irq_restore()
spin_unlock_bh = spin_unlock() + local_bh_enable()local_irq_disable:关中断
local_irq_save:关中断并保存状态字
local_bh_disable:关底半部
local_irq_enable:开中断
local_irq_restore:开中断并恢复状态字
local_bh_enable:开底半部
在任何情况下使用spin_lock_irq都是安全的。因为它既禁止本地中断,又禁止内核抢占
spin_lock比spin_lock_irq速度快,但是它并不是任何情况下都是安全的
spin_lock_irqsave
- 保存本地中断状态
- 关闭本地中断
- 获取自旋锁
spin_unlock_irqrestore
- 释放锁
- 恢复本地中断
注:读写自旋锁待补充