This
5.5.3 Atomic Update of Data Structures
Before accessing shared writable data structures (those that are not a single aligned longword or quadword), the programmer can acquire control of the data structure by using an atomic update to set a software lock variable. Such a software lock can be cleared with an ordinary store instruction.
A
stq_c_loop: |
|
| |
spin_loop: |
|
| |
LDQ | R1,lock_variable | ; This optional | |
BLBS R1,already_set | ; should be used unless the | ||
|
| ; lock is known to be | |
LDQ_L R1,lock_variable | ; \ | ||
BLBS R1,already_set | ; | \ | |
OR | R1,#1,R2 | ; | > Set lock bit |
STQ_C R2,lock_variable | ; | / | |
BEQ | R2,stq_c_fail | ; / |
MB
<critical section: updates various data structures>
MB |
| ; Second MB |
STQ | R31,lock_variable | ; Clear lock bit |
: |
|
|
: |
|
|
already_set: |
| |
<code to block or reschedule or test for too many iterations> | ||
BR | spin_loop |
|
stq_c_fail: |
| |
<code to test for too many iterations> | ||
BR | stq_c_loop |
|
This code has a number of subtleties:
•If the lock_variable is already set, the spin loop is done without doing any stores. This avoidance of stores improves memory subsystem performance and avoids the deadlock described below. The loop uses an ordinary load. This code sequence is preferred unless the lock is known to be
•If the lock_variable is actually being changed from 0 to 1, and the STQ_C fails (due to an interrupt, or because another processor simultaneously changed lock_variable), the entire process starts over by reading the lock_variable again.
•Only the
•Only
System Architecture and Programming Implications