This load-locked/store-conditional paradigm may be used whenever an atomic update of a shared aligned quadword is desired, including getting the effect of atomic byte writes.

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 software-critical section, therefore, may look like the sequence:

stq_c_loop:

 

 

spin_loop:

 

 

LDQ

R1,lock_variable

; This optional spin-loop code

BLBS R1,already_set

; should be used unless the

 

 

; lock is known to be low-contention.

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 low-contention, because the sequence increases the probability that the LDQ_L hits in the cache and the LDQ_L/STQ_C sequence complete quickly and successfully.

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 fall-through path of the BLBS instructions does a STx_C; some implementa- tions may not allow a successful STx_C after a branch-taken.

Only register-to-register operate instructions are used to do the modify.

System Architecture and Programming Implications 5–7

Page 219
Image 219
Compaq ECQD2KCTE manual Atomic Update of Data Structures, Stq