自旋锁


      用在多CPU上的临界资源的保护。
spin_lock_irqsave():
#define spin_lock_irqsave(lock, flags)  _spin_lock_irqsave(lock, flags)
85 unsigned long __lockfunc _spin_lock_irqsave(spinlock_t *lock)
86 {
87     unsigned long flags;
88
89       local_irq_save(flags);   //保存中断标志,并且关闭中断。
90       preempt_disable();
91     spin_acquire(&lock->dep_map, 0, 0, _RET_IP_);
92     /*
93      * On lockdep we dont want the hand-coded irq-enable of
94      * _raw_spin_lock_flags() code, because lockdep assumes
95      * that interrupts are not re-enabled during lock-acquire:
96      */
97 #ifdef CONFIG_LOCKDEP
98     LOCK_CONTENDED(lock, _raw_spin_trylock, _raw_spin_lock);
99 #else
100       _raw_spin_lock_flags(lock, &flags);
101 #endif
102     return flags;
103 }
1、 保存中断标志,并且关闭本地中断。
local_irq_save()-> raw_local_irq_save()-> __raw_local_save_flags() +  raw_local_irq_disable();
81 /*
82  * For spinlocks, etc:
83  */
84 static inline unsigned long   __raw_local_irq_save(void)
85 {  
86     unsigned long flags =   __raw_local_save_flags();
//     __asm__ __volatile__(
//         "pushfl ; popl %0"
//         : "=g" (flags)
//         : /* no input */
//     );  
87    
88       raw_local_irq_disable();          //__asm__ __volatile__("cli" : : : "memory");
89        
90     return flags;
91 }   
2、禁止中断抢占。 preempt_disable().
preempt_disable()
 29 #define preempt_disable() \
30 do { \
31     inc_preempt_count(); \
32     barrier(); \
33 } while (0)
3、加锁 _raw_spin_lock_flags().
60 static inline void __raw_spin_lock(raw_spinlock_t *lock)
61 {
62     asm( __raw_spin_lock_string  : "+m" (lock->slock) : : "memory");
63 }
 23 #define   __raw_spin_lock_string  \
24     "\n1:\t" \
25     LOCK_PREFIX " ; decb %0\n\t" \
26     "jns 3f\n" \
27     "2:\t" \
28     "rep;nop\n\t" \
29     "cmpb $0,%0\n\t" \
30     "jle 2b\n\t" \
31     "jmp 1b\n" \
32     "3:\n\t"

你可能感兴趣的:(自旋锁)