一、自旋锁
自旋就是自己连续的循环等待。如果你有抱着你的爱人旋转的经历,那么你应该知道一件事情,为了安全,你不能旋转太久,你的爱人如果头昏,也想你早日释放。是的,自旋的缺点,就是它频繁的循环直到等待锁的释放,将它用于可以快速完成的代码中才好。
自旋不能抢占,但能中断。
相关话题:SMP和cpu。多个cpu和单个cpu。很多书说自旋锁只能在多处理机中使用,这是不正确的。
首先定义
- Spinlock_t lock;
对不起,我只能找到arm平台的锁了
- /*
- * ARMv6 Spin-locking.
- *
- * We (exclusively) read the old value, and decrement it. If it
- * hits zero, we may have won the lock, so we try (exclusively)
- * storing it.
- *
- * Unlocked value: 0
- * Locked value: 1
- */
- typedef struct {
- volatile unsigned int lock;
- #ifdef CONFIG_PREEMPT
- unsigned int break_lock;
- #endif
- } spinlock_t;
补上x86平台
- #define SPINLOCK_MAGIC 0x1D244B3C
- typedef struct {
- unsigned long magic;
- volatile unsigned long lock;
- volatile unsigned int babble;
- const char *module; // 所属模块
- char *owner;
- int oline;
- } spinlock_t;
Lock为0时可以用,1是等待。0像锁孔,当没有钥匙插进去时,它才可以插进去
怎么初始化呢?
- #define spin_lock_init(x)
- do {
- (x)->magic = SPINLOCK_MAGIC;
- (x)->lock = 0; ;0初始化,表示可用
- (x)->babble = 5;
- (x)->module = __FILE__;
- (x)->owner = NULL;
- (x)->oline = 0;
- } while (0)
定义一个自旋锁的方法很有意思,
- Spinlock_t lock=?????
可以通过spin_lock
Spin_lock_irqsave 来调用自旋锁,后者不允许中断。前者有可能在上锁中发生中断。
还有spin_trylock 这是一个绝不妥协的函数,它不等待。
恢复为spin_unlock
Spin_unlock_irqrestore
考查下面代码
- #define spin_lock_irqsave(lock, flags) _spin_lock_irqsave(lock, flags)
- #define _spin_lock_irqsave(lock, flags)
- do {
- local_irq_save(flags); 保存中断请求标志
- preempt_disable(); 不允许抢占
- _raw_spin_lock(lock);
- __acquire(lock);
- } while (0)
二、自旋锁综合使用
下面是一个使用的例子,你可以使用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.解锁
你也许还要参考linux内核分析的书籍,才能领会更多。