二、自旋锁综合使用
下面是一个使用的例子,你可以使用source insight查到它
/* never called when PTRS_PER_PMD > 1 */
void pgd_dtor(void *pgd, kmem_cache_t *cache, unsigned long unused)
{
unsigned long flags; /* can be called from interrupt context */
spin_lock_irqsave(&pgd_lock, flags); 枷锁
pgd_list_del(pgd);
spin_unlock_irqrestore(&pgd_lock, flags); 释放
}
中断枷锁
#define spin_lock_irqsave(lock, flags) _spin_lock_irqsave(lock, flags)
分析
unsigned long __lockfunc _spin_lock_irqsave(spinlock_t *lock)
{
unsigned long flags;
local_irq_save(flags); 将寄存器存入flags,并关中断
preempt_disable(); 抢占锁
_raw_spin_lock_flags(lock, flags); 枷锁
return flags;
}
EXPORT_SYMBOL(_spin_lock_irqsave);
继续
/* 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.解锁
二、自旋锁综合使用
下面是一个使用的例子,你可以使用source insight查到它
/* never called when PTRS_PER_PMD > 1 */
void pgd_dtor(void *pgd, kmem_cache_t *cache, unsigned long unused)
{
unsigned long flags; /* can be called from interrupt context */
spin_lock_irqsave(&pgd_lock, flags); 枷锁
pgd_list_del(pgd);
spin_unlock_irqrestore(&pgd_lock, flags); 释放
}
中断枷锁
#define spin_lock_irqsave(lock, flags) _spin_lock_irqsave(lock, flags)
分析
unsigned long __lockfunc _spin_lock_irqsave(spinlock_t *lock)
{
unsigned long flags;
local_irq_save(flags); 将寄存器存入flags,并关中断
preempt_disable(); 抢占锁
_raw_spin_lock_flags(lock, flags); 枷锁
return flags;
}
EXPORT_SYMBOL(_spin_lock_irqsave);
继续
/* 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.解锁
举报