~linux/Documentation/spinlocks.txt
多核心處理 critical section 的方式,safe but slow(disable interrupts)
67 See
68
69 spinlock_t xxx_lock = SPIN_LOCK_UNLOCKED;
70
71
72 unsigned long flags;
73
74 spin_lock_irqsave(&xxx_lock, flags);
75 ... critical section here ..
76 spin_unlock_irqrestore(&xxx_lock, flags);
單核心處理 critical section 的方式:
115
116 spin_lock_irqsave(flags);
117 .. critical section ..
118 spin_unlock_irqrestore(flags);
119
reader/writer 處理 shared data 的鎖定方式:
149 rwlock_t xxx_lock = RW_LOCK_UNLOCKED;
150
151
152 unsigned long flags;
153
154 read_lock_irqsave(&xxx_lock, flags);
155 .. critical section that only reads the info ...
156 read_unlock_irqrestore(&xxx_lock, flags);
157
158 write_lock_irqsave(&xxx_lock, flags);
159 .. read and write exclusive access to the info ...
160 write_unlock_irqrestore(&xxx_lock, flags);
如果確定不會跑在 interrupt handler 中(否則會重複鎖定造成 dead lock),
可以用簡化的 spin_lock API 較快
192 spin_lock(&lock);
193 ...
194 spin_unlock(&lock);
因此 read lock 等可以用簡化的非 irq safe spinlock,但 write lock 則必須
使用 irq safe 的 spin lock
沒有留言:
張貼留言