interlocked-update reference as observed
by otherCPUs. A serialization function is performed
before the operandis fetched and again after the operation is completed.
The second operand ofCOMPARE AND SWAP must be designated on a word boundary.
The Rt and R3 fields forCOMPARE DOUBLE AND SWAP must each designate an even
register, and the second operand for theCDS instruction must be designated on a
doubleword boundary. Otherwise, a spec
ificationexception is recognized.
ResultingCondition Code: o
1
2
3
Firstand second equal, second operand
by third operandFirst and second
unequal,first operand by second operand Program Exceptions:
operandsreplaced operands replaced Access (fetch and store, operand 2)
Operation (ifthe conditional- swapping facility is not
installed)Specification Programming Notes 1. Several examples of the use of the COMPARE AND SWAP and COMPARE DOUBLE AND SWAP instructions are given in
Appendix A.
2.COMPARE AND SWAP can be used by CPU programs sharing common storage areas in either a multiprogramming or multiprocessing environment.
Twoexamples are: a. By performing the following
procedure,a CPU program can modify the contents of a stor age location even though the
possibilityexists that the CPU program may be interrupted by
anotherCPU program that will update the location or that another CPU program may simul
taneously update the location.First, the entire word contain ing the byte or bytes to be
updated is loaded intoa gener
alregister. Next, the updated
valueis computed and placed in
anothergeneral Then COMPARE AND SWAP is executed
with the Rj field designating
the register that contains theoriginal value and the R3 field
designating the register thatcontains the updated value. If
theupdate has been successful, condition code 0 is set. If
the storage location no longer
contains the original value,
the update has not been
successful, the general regis
ter designated by theR, field of the COMPARE AND SWAP instruction contains the new
current value of the storage
location, and condition code 1is set. When condition code 1
is set, theCPU program can repeat the procedure using the
new current value.
b.COMPARE AND SWAP can be used
for controlled sharing ofa common storage area, including
the capability of leaving amessage (in a chained list of
messages) when the commonarea is in use. To accomplish this,
a word in storagecan be used as a control word, with a zero value in the word indicating that the common area is not in
use and that no messages exist,
a negativevalue indicating that the area is in use and
that no messages exist,and a nonzero positive value indicat ing that the common area is in
use and thatthe value is the address of the most recent message added to the list.
Thus,any number of CPU programs desiring to seize the area can use COMPARE AND SWAP to update the control word to
indicate thatthe area is 1n
use or to add messages to the
list.The single CPU program
which hasseized the area can also safely use COMPARE AND SWAP to remove messages from
the list.
3.COMPARE DOUBLE AND SWAP can be in a manner similar to that
described forCOMPARE AND SWAP. In
addition,it has another use. Consider a chained list, with a control word used to address the first message 1n the list, as described in programming note 2b
above. If multipleCPU programs are to b2 permitted to delete
messages byusing COMPARE AND SWAP (and not just the single CPU pro
gram which hasseized the common area), there is a possibility the list will be incorrectly updated.
This would occur if, forexample, after one CPU program has fetched
the address of the most recent mes
sagein order to remove the
message, anotherCPU program
removes the first two messages and
then adds thefirst message back
into thechain. The first CPU program, on continuing, cannot
easily detectthat the list is
changed. By increasing the size of
the control word to a doublewordChapter 7. General Instructions 7-13
by other
before the operand
The second operand of
The Rt and R3 fields for
register, and the second operand for the
doubleword boundary. Otherwise, a spec
ification
Resulting
1
2
3
First
by third operand
unequal,
operands
Operation (if
installed)
Appendix A.
2.
Two
procedure,
possibility
another
taneously update the location.
updated is loaded into
al
value
another
with the Rj field designating
the register that contains the
designating the register that
the
the storage location no longer
contains the original value,
the update has not been
successful, the general regis
ter designated by the
current value of the storage
location, and condition code 1
is set, the
new current value.
b.
for controlled sharing of
the capability of leaving a
messages) when the common
a word in storage
use and that no messages exist,
a negative
that no messages exist,
use and that
Thus,
indicate that
use or to add messages to the
list.
which has
the list.
3.
described for
addition,
above. If multiple
messages by
gram which has
This would occur if, for
the address of the most recent mes
sage
message, another
removes the first two messages and
then adds the
into the
easily detect
changed. By increasing the size of
the control word to a doubleword