继续
/* For spinlocks etc */
#define local_irq_save(x) __asm__ __volatile__("pushfl ; popl %0 ; cli":"=g" (x): /* no input */ :"memory")
将标志寄存器的内容放在内存x中。请查看gcc汇编
继续
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");
}
继续
#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就可以关中断继续执行了,否则nop空操作。Nop期间,cpu可以执行其他任务的代码。
解锁
#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.解锁