Serializing on the mainframe with the compare-and-swap instruction

The compare-and-swap instruction is one of the mainframe's low-cost serialization options. In this tip, mainframe expert Robert Crawford explains compare-and-swap features and limitations.

The mainframe offers many serialization services, the most commonly used being the enqueue (ENQ) and dequeue (DEQ)...

macros. However, here is a much lower-cost alternative in the compare-and-swap (CS) instruction.

The compare-and swap-instruction has the following format:

CS     R1,R2,D1

R1 and R2 are registers and D1 is a memory reference. When executed, the contents of R1 are compared with the storage at D1. If they are equal, R2 gets stored into D1 and the condition code is set to 0. If they are not equal, the condition code becomes 1 and the contents of D1 are loaded into R1.

What makes this instruction special is that the processor serializes access to D1 across all the CPUs, thus ensuring that only one program will be able to update D1 at a time.

As an example, think of a counter containing the next invoice number. Access to the counter must be serialized so the application issues sequential, non-duplicate numbers. This can be accomplished easily if every process that needs to generate an invoice number goes through this logic.

L R2,INVNUM Get current invoice number
TRYAGAIN LA R3,1(,R2) Add one
CS R2,R3,INVNUM Update invoice number
BNZ TRYAGAIN Try again if changed

The above code fragment loads and increments the inventory number, then attempts to update the memory counter with a compare-and-swap instruction. If the number hasn't changed, the CS stores the new number. If another thread updated the INVNUM between the time of the load and the CS, the non-zero return code forces the logic to go around for another try. Note, this code may loop several times depending on how busy INVNUM is.

Other than this relatively simple example, CS can be used to update control blocks and linked lists very quickly and cheaply.

Compare and swap's main limitation is scope. If the target address is in private storage, the serialization only works within an address space. Otherwise, for an address in the common service area, the serialization applies across the entire LPAR. However, a programmer will have to resort to using ENQ and DEQ macros or the Coupling Facility to lock a resource across a Sysplex.

Also note that CS only works when all participants use the same rules. Updates through another instruction, such as store, will not be serialized and would update the storage without outside serialization control.

ABOUT THE AUTHOR: For 24 years, Robert Crawford has worked off and on as a CICS systems programmer. He is experienced in debugging and tuning applications and has written in COBOL, Assembler and C++ using VSAM, DLI and DB2.

What did you think of this feature? Write to SearchDataCenter.com's Matt Stansberry about your data center concerns at [email protected].

Dig Deeper on IBM system z and mainframe systems