|
Locks With LL/SC
Lock: |
LL r1, lock_addr |
/* Normal read miss/ BusRead */ |
|
addi r2, r0, 0x1 |
|
|
SC r2, lock_addr |
/* Possibly upgrade miss */ |
|
beqz r2, Lock |
/* Check if SC succeeded */ |
|
bnez r1, Lock |
/* Check if someone is in CS */ |
- LL/SC is best-suited for test & test & set locks
Lock: |
LL r1, lock_addr |
|
bnez r1, Lock |
|
addi r1, r0, 0x1 |
|
SC r1, lock_addr |
|
beqz r1, Lock |
Fetch & op with LL/SC
Try: |
LL r1, addr |
|
addi r1, r1, 0x1 |
|
SC r1, addr |
|
beqz r1, Try |
- Compare & swap: Compare with r1, swap r2 and memory location (here we keep on trying until comparison passes)
Try: |
LL r3, addr |
|
sub r4, r3, r1 |
|
bnez r4, Try |
|
add r4, r2, r0 |
|
SC r4, addr |
|
beqz r4, Try |
|
add r2, r3, r0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|