ATOMIC(3)

NAME
OSAtomicAdd32, OSAtomicIncrement32, OSAtomicDecrement32, OSAtomicOr32, OSAtomicAnd32, OSAtomicXor32, OSAtomicAdd64, OSAtomicIncrement64, OSAtomicDecrement64, OSAtomicCompareAndSwap32, OSAtomicCompareAndSwap64, OSAtomicTestAndSet, OSAtomicTestAndClear - atomic add, increment, decrement, or, and, xor, compare and swap, test and set, and test and clear

LIBRARY
libc

SYNOPSIS
#include <libkern/OSAtomic.h>
int32_t
OSAtomicAdd32(int32_t theAmount, int32_t *theValue);
int32_t
OSAtomicIncrement32(int32_t *theValue);
int32_t
OSAtomicDecrement32(int32_t *theValue);
int32_t
OSAtomicOr32(uint32_t theMask, uint32_t *theValue);
int32_t
OSAtomicAnd32(uint32_t theMask, uint32_t *theValue);
int32_t
OSAtomicXor32(uint32_t theMask, uint32_t *theValue);
int64_t
OSAtomicAdd64(int64_t theAmount, int64_t *theValue);
int64_t
OSAtomicIncrement64(int64_t *theValue);
int64_t
OSAtomicDecrement64(int64_t *theValue);
bool
OSAtomicCompareAndSwap32(int32_t oldValue, int32_t newValue, int32_t *theValue);
bool
OSAtomicCompareAndSwap64(int64_t oldValue, int64_t newValue, int64_t *theValue);
bool
OSAtomicTestAndSet(uint32_t n, void *theAddress);
bool
OSAtomicTestAndClear(uint32_t n, void *theAddress);

DESCRIPTION
These functions are thread and multiprocessor safe, but do not incorporate memory barriers and thus cannot be used by themselves to synchronize shared memory other than the memory being operated on (ie, theValue and theAddress ). The logical (and, or, xor) and bit test operations are layered on top of the OSAtomicCompareAndSwap() primitives. The memory address theValue must be naturally aligned, ie 32-bit aligned for 32-bit operations and 64-bit aligned for 64-bit operations. The 64-bit operations are only implemented for 64-bit processes.

OSAtomicCompareAndSwap32() and OSAtomicCompareAndSwap64() compare oldValue to *theValue, and set *theValue to newValue if the comparison is equal. The comparison and assignment occur as one atomic operation.

OSAtomicTestAndSet() and OSAtomicTestAndClear() operate on bit (0x80 >> ( n & 7)) of byte ((char*) theAddress + ( n >> 3)). They set the named bit to either 1 or 0, respectively. theAddress need not be aligned.

RETURN VALUES
The arithmetic and logical operations return the new value, after the operation has been performed. The compare-and-swap operations return true if the comparison was equal, ie if the swap occured. The bit test and set/clear operations return the original value of the bit.

SEE ALSO
atomicqueue(3), spinlock(3), barrier(3)