Simple Lock with LL-SC
lock: ll reg1, location /* LL location to reg1 */
sc location, reg2 /* SC reg2 into location*/
beqz reg2, lock /* if failed, start again */
ret
unlock: st location, #0 /* write 0 to location */
ret
Can do more fancy atomic ops by changing what’s between LL & SC
- But keep it small so SC likely to succeed
- Don’t include instructions that would need to be undone (e.g. stores)
SC can fail (without putting transaction on bus) if:
- Detects intervening write even before trying to get bus
- Tries to get bus but another processor’s SC gets bus first
LL, SC are not lock, unlock respectively
- Only guarantee no conflicting write to lock variable between them
- But can use directly to implement simple operations on shared variables