Conditionally updates or returns a variable atomically.
Standard C library (libc.a)
#include <sys/atomic_op.h>
boolean_t compare_and_swap ( addr, old_val_addr, new_val)
atomic_p addr;
int *old_val_addr;
int new_val;
boolean_t compare_and_swaplp ( addr, old_val_addr, new_val)
atomic_l addr;
long *old_val_addr;
long new_val;
The compare_and_swap and compare_and_swaplp subroutines perform an atomic operation that compares the contents of a variable with a stored old value. If the values are equal, a new value is stored in the variable and TRUE is returned. If the values are not equal, the old value is set to the current value of the variable and FALSE is returned.
For 32-bit applications, the compare_and_swap and compare_and_swaplp subroutines are identical and operate on a word aligned single word (32-bit variable aligned on a 4-byte boundary).
For 64-bit applications, the compare_and_swap subroutine operates on a word aligned single word (32-bit variable aligned on a 4-byte boundary) and the compare_and_swaplp subroutine operates on a double word aligned double word (64-bit variable aligned on an 8-byte boundary).
The compare_and_swap and compare_and_swaplp subroutines are useful when a word value must be updated only if it has not been changed since it was last read.
Item | Description |
---|---|
addr | Specifies the address of the variable. |
old_val_addr | Specifies the address of the old value to be checked against (and conditionally updated with) the value of the variable. |
new_val | Specifies the new value to be conditionally assigned to the variable. |
Item | Description |
---|---|
TRUE | Indicates that the variable was equal to the old value, and has been set to the new value. |
FALSE | Indicates that the variable was not equal to the old value, and that its current value has been returned to the location where the old value was previously stored. |