标题:
Linux内核同步机制的自旋锁原理及应用 03
[打印本页]
作者:
samwalton
时间:
2013-8-29 15:08
标题:
Linux内核同步机制的自旋锁原理及应用 03
二、自旋锁综合使用
下面是一个使用的例子,你可以使用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");
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0