Conditionally updates or returns a single word variable atomically.
#include <sys/atomic_op.h>
boolean_t compare_and_swap (word_addr, old_val_addr, new_val) atomic_p word_addr; int *old_val_addr; int new_val;
The compare_and_swap subroutine performs an atomic operation which compares the contents of a single word variable with a stored old value. If the values are equal, a new value is stored in the single word variable and TRUE is returned; otherwise, the old value is set to the current value of the single word variable and FALSE is returned.
The compare_and_swap subroutine is useful when a word value must be updated only if it has not been changed since it was last read.
Note: The word containing the single word variable must be aligned on a full word boundary.
Note: If compare_and_swap is used as a locking primitive, insert an isync at the start of any critical sections.
The compare_and_swap subroutine is part of the Base Operating System (BOS) Runtime.
The fetch_and_add subroutine, fetch_and_and subroutine, fetch_and_or subroutine.