location is not modified; instead, the
current value of the location is loaded
into a general register, inpreparation for the program to loop back and try
again. During the execution ofCOMPARE AND SWAP, no other CPU can perform a
store access or interlocked-update
access at the specified location.
Settingg Single Bit
The following instruction sequence shows
how theCOMPARE AND SWAP instruction can
be used to set a single bit in storage
to one. Assume that the first byte of a
word in storage called"WORD" contains
eight flag bits.
LA6,X'80' SLL 6,24
LRETRY LR OR CS BC 7,WORD 8,7
8,6
7,8,WORD4,RETRY Put bit to be ORed into GR6
Shift left 24 places
to align the byte
tobe ORed with
the location of
the flag bits
withinWORD Fetch current flag
values
load flags into GR8Set bit to one Store new flags if
current flags un
changed, or re
fetch current
flag values if
changed
Ifnew flags are not
stored, try again
The format of theCOMPARE AND SWAP instruction is: Machine Format Op Code BA 7
Assembler FormatCS 7,8,WORD
TheCOMPARE AND SWAP instruction
compares the first operand (general
register 7 containing the current flag
values) to the second operandin storage (WORD) while no CPU other than the one
executing theCOMPARE AND SWAP instruc
tion is permitted to perform a store
access or interlocked-update access at
the specified storage location.
If the comparison is successful, indi
cating that the flag bits have not been
changed since they were fetched, the
modified copy in general register 8 is
stored intoWORD. If the flags have
been changed, the compare will not be
successful, and their new values are
loaded into general register 7.
The conditional branch(BC) instruction
tests the condition code and reexecutes
the flag-modifying instructions if theCOMPARE AND SWAP instruction indicated
an unsuccessful comparison (condition
code 1). When theCOMPARE AND SWAP instruction is successful (condition
code0), the flags contain valid data,
and the program exits from the loop.
The branch toRETRY will be taken only if some other program modifies the
contents ofWORD. This type of a loop
differs from the typical "bit-spin"
loop. In a bit-spin loop, the program
continues to loop until the bit changes.
In this example, the program continues
to loop only if the valuedoes change
during each iteration. If a number ofCPUs simultaneously attempt to modify a
single location by using the sample
instruction sequence, oneCPU will fall
through on the first try, anotherwill loop once, and so on until all CPUs have
succeeded.
UpdatingCounters In this example, a 32-bit counter is
updated by a program using theCOMPARE AND SWAP instruction to ensure that the
counterwill be correctly updated. The
original value of the counter is
obtained by loading the word containing
the counter into general register 7.
This value is moved into general regis
ter 8 to provide a modifiable copy, and
general register 6 (containing an incre
ment to the counter) is added to the
modifiable copy to provide the updated
counter value. TheCOMPARE AND SWAP instruction is used to ensure valid
storing of the counter.
The program updating the counter checks
the result by examining the condition
code. The condition code0 indicates a
successful update, and the program can
proceed. If the counter hadbeen changed between the time that the
program loaded its original value and
the time thatit executed the COMPARE AND SWAP instruction, the execution
would have loaded the new counter value
into general register 7 and set the
condition code to1, indicating an
unsuccessful update. The program must
then repeat the updatesequence until
the execution of theCOMPARE AND SWAP instruction results in a successful
update.
The following instruction
performs the above procedure:
sequence
Appendix A. Number Representation and Instruction-Use Examples A-41
current value of the location is loaded
into a general register, in
again. During the execution of
store access or interlocked-update
access at the specified location.
Setting
The following instruction sequence shows
how the
be used to set a single bit in storage
to one. Assume that the first byte of a
word in storage called
eight flag bits.
LA
L
8,6
7,8,WORD
Shift left 24 places
to align the byte
to
the location of
the flag bits
within
values
load flags into GR8
current flags un
changed, or re
fetch current
flag values if
changed
If
stored, try again
The format of the
Assembler Format
The
compares the first operand (general
register 7 containing the current flag
values) to the second operand
executing the
tion is permitted to perform a store
access or interlocked-update access at
the specified storage location.
If the comparison is successful, indi
cating that the flag bits have not been
changed since they were fetched, the
modified copy in general register 8 is
stored into
been changed, the compare will not be
successful, and their new values are
loaded into general register 7.
The conditional branch
tests the condition code and reexecutes
the flag-modifying instructions if the
an unsuccessful comparison (condition
code 1). When the
code
and the program exits from the loop.
The branch to
contents of
differs from the typical "bit-spin"
loop. In a bit-spin loop, the program
continues to loop until the bit changes.
In this example, the program continues
to loop only if the value
during each iteration. If a number of
single location by using the sample
instruction sequence, one
through on the first try, another
succeeded.
Updating
updated by a program using the
counter
original value of the counter is
obtained by loading the word containing
the counter into general register 7.
This value is moved into general regis
ter 8 to provide a modifiable copy, and
general register 6 (containing an incre
ment to the counter) is added to the
modifiable copy to provide the updated
counter value. The
storing of the counter.
The program updating the counter checks
the result by examining the condition
code. The condition code
successful update, and the program can
proceed. If the counter had
program loaded its original value and
the time that
would have loaded the new counter value
into general register 7 and set the
condition code to
unsuccessful update. The program must
then repeat the update
the execution of the
update.
The following instruction
performs the above procedure:
sequence
Appendix A. Number Representation and Instruction-Use Examples A-41