Waiting for too long may lead to long latency and lost opportunity
Constant and variable backoff
Special kind of variable backoff : exponential backoff (after the i th attempt the delay is k* c i where k and c are constants)
Test & set with exponential backoff works pretty well
delay = k Lock: ts register, lock_addr
bez register, Enter_CS
pause (delay) /* Can be simulated as a timed loop */
delay = delay*c
j Lock
Test & Test & Set
Reduce traffic further
Before trying test & set make sure that the lock is free
Lock: ts register, lock_addr
bez register, Enter_CS Test: lw register, lock_addr
bnez register, Test
j Lock
How good is it?
In a cacheable lock environment the Test loop will execute from cache until it receives an invalidation (due to store in unlock); at this point the load may return a zero value after fetching the cache line
If the location is zero then only everyone will try test & set