分析rwlock的结构

分析rwlock的结构  

typedef struct {
volatile unsigned int lock;
} rwlock_t;

rwlock_t tasklist_lock __attribute__((__aligned__((1 << ((5))
)),__section__(".data.cacheline_aligned"))) = (rwlock_t) { 0x01000000 } ;
;0x01000000(RW_LOCK_UNLOCKED)是偏置值

read_lock(&tasklist_lock);
编绎为
lock ; subl $1,tasklist_lock 减1
js 2f 如果为负值表示已被write_lock锁定
1:
.section .text.lock,"ax"
2: pushl % eax
leal tasklist_lock,% eax 取tasklist_lock的地址
call __read_lock_failed
popl % eax
jmp 1b 抓锁成功了
.previous

;semaphore.c
__read_lock_failed:
lock ; incl (% eax) 恢复tasklist_lock的值
1: cmpl $1,(% eax)
js 1b 如果tasklist_lock小于1表示write_unlock尚未解锁
lock ; decl (% eax) 准备再次抓取tasklist_lock
js __read_lock_failed 它又被某个write_lock锁住了,再次循环
ret

read_unlock(&tasklist_lock);
编绎为
lock ; incl tasklist_lock

write_lock_irq(&tasklist_lock);
编绎为
cli
lock ; subl $0x01000000,(tasklist_lock) 减去tasklist_lock的偏置值
jnz 2f 如果非0则说明它被read_lock()或write_lock()锁住了
1:
.section .text.lock,"ax"
2: pushl % eax
leal tasklist_lock,% eax
call __write_lock_failed
popl % eax
jmp 1b
.previous

; semaphore.c
__write_lock_failed:
lock ; addl $0x01000000,(% eax) 恢复tasklist_lock值
1: cmpl $0x01000000,(% eax)
jne 1b 等待解锁
lock ; subl $0x01000000,(% eax) 再次抓锁
jnz __write_lock_failed 如果又被锁住的话再次循环
ret

write_unlock_irq(&tasklist_lock);
编绎为:
lock ; addl $0x01000000,tasklist_lock 恢复偏置值
sti

你可能感兴趣的:(分析rwlock的结构)