Linux內核同步機制的自旋鎖原理
繼續(xù)
/* For spinlocks etc */
#define local_irq_save(x)__asm__ __volatile__(pushfl ; popl %0 ; cli:=g (x): /* no input */ :memory)
將標志寄存器的內容放在內存x中。請查看gcc匯編
繼續(xù)
static inline void _raw_spin_lock_flags (spinlock_t *lock, unsigned long flags)
{
#ifdef CONFIG_DEBUG_SPINLOCK
if (unlikely(lock->magic != SPINLOCK_MAGIC)) {
printk(eip: %p , __builtin_return_address(0));
BUG();
}
#endif
__asm__ __volatile__(
spin_lock_string_flags
:=m (lock->slock) : r (flags) : memory);
}
繼續(xù)
#define spin_lock_string_flags
1:
lock ; decb %0 ;lock總線鎖住,原子操作
jns 4f
2:
testl $0x200, %1
jz 3f
sti
3:
rep;nop
cmpb $0, %0
jle 3b
cli
jmp 1b
4:
理解一下大概意思,就可以了。當lock-1后大于等于0就可以關中斷繼續(xù)執(zhí)行了,否則nop空操作。Nop期間,cpu可以執(zhí)行其他任務的代碼。
解鎖
#define spin_unlock_irqrestore(lock, flags)_spin_unlock_irqrestore(lock, flags)
void __lockfunc _spin_unlock_irqrestore(spinlock_t *lock, unsigned long flags)
{
_raw_spin_unlock(lock);
local_irq_restore(flags);
preempt_enable();
}
static inline void _raw_spin_unlock(spinlock_t *lock)
{
#ifdef CONFIG_DEBUG_SPINLOCK
BUG_ON(lock->magic != SPINLOCK_MAGIC);
BUG_ON(!spin_is_locked(lock));
#endif
__asm__ __volatile__(
spin_unlock_string
);
}
Raw赤裸的解鎖,表示最低沉的解鎖原理。
#define spin_unlock_string
xchgb %b0, %1
:=q (oldval), =m (lock->slock)
:0 (oldval) : memory
加1.解鎖
評論