cs() — Compare and swap

Standards

Standards / Extensions C or C++ Dependencies
Language Environment both  

Format

#include <stdlib.h>

int cs(cs_t *oldptr, cs_t *curptr, cs_t newword);

General description

The cs() built-in function compares the 4-byte value pointed to by oldptr to the 4-byte value pointed to by curptr. If they are equal, the 4-byte value, newword, is copied into the location pointed to by curptr. If they are unequal, the value pointed to by curptr is copied into the location pointed to by oldptr.

If this function is used in a multi-threading environment, then it is the users responsibility to protect the oldptr variable. The user can create a local variable per thread to contain this value or provide locking code to protect the global variable used. The oldptr variable may not reflect the curptr variable if the curptr variable changes via another thread before the user has a chance to examine oldptr.

To avoid infringing on the user's name space, this nonstandard function is exposed only when you use the compiler option LANGLVL(EXTENDED). When you use LANGLVL(EXTENDED) any relevant information in the header is also exposed.

The function uses the COMPARE SWAP (CS) instructions, which can be used in multiprogramming or multiprocessing environments to serialize access to counters, flags, control words, and other common storage areas. For a detailed description, refer to topics "XL C/C++ Macros" and "Function support table" in the z/Architecture Principles of Operation on number representation and instruction.

Returned value

cs() returns 0 if the 4-byte value pointed to by oldptr is equal to the 4-byte value pointed to by curptr.

If the value is not equal, cs() returns 1.

Related information