ATOMIC(3) BSD Library Functions Manual ATOMIC(3)
NAME
OSAtomicAdd32, OSAtomicAdd32Barrier, OSAtomicIncrement32,
OSAtomicIncrement32Barrier, OSAtomicDecrement32,
OSAtomicDecrement32Barrier, OSAtomicOr32, OSAtomicOr32Barrier,
OSAtomicAnd32, OSAtomicAnd32Barrier, OSAtomicXor32, OSAtomicXor32Barrier,
OSAtomicAdd64, OSAtomicAdd64Barrier, OSAtomicIncrement64,
OSAtomicIncrement64Barrier, OSAtomicDecrement64,
OSAtomicDecrement64Barrier, OSAtomicCompareAndSwap32,
OSAtomicCompareAndSwap32Barrier, OSAtomicCompareAndSwap64,
OSAtomicCompareAndSwap64Barrier, OSAtomicTestAndSet,
OSAtomicTestAndSetBarrier, OSAtomicTestAndClear,
OSAtomicTestAndClearBarrier -- atomic add, increment, decrement, or, and,
xor, compare and swap, test and set, and test and clear
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
##include <>
int32t
OSAtomicAdd32(int32t theAmount, int32t *theValue);
int32t
OSAtomicAdd32Barrier(int32t theAmount, int32t *theValue);
int32t
OSAtomicIncrement32(int32t *theValue);
int32t
OSAtomicIncrement32Barrier(int32t *theValue);
int32t
OSAtomicDecrement32(int32t *theValue);
int32t
OSAtomicDecrement32Barrier(int32t *theValue);
int32t
OSAtomicOr32(uint32t theMask, uint32t *theValue);
int32t
OSAtomicOr32Barrier(uint32t theMask, uint32t *theValue);
int32t
OSAtomicAnd32(uint32t theMask, uint32t *theValue);
int32t
OSAtomicAnd32Barrier(uint32t theMask, uint32t *theValue);
int32t
OSAtomicXor32(uint32t theMask, uint32t *theValue);
int32t
OSAtomicXor32Barrier(uint32t theMask, uint32t *theValue);
int64t
OSAtomicAdd64(int64t theAmount, int64t *theValue);
int64t
OSAtomicAdd64Barrier(int64t theAmount, int64t *theValue);
int64t
OSAtomicIncrement64(int64t *theValue);
int64t
OSAtomicIncrement64Barrier(int64t *theValue);
int64t
OSAtomicDecrement64(int64t *theValue);
int64t
OSAtomicDecrement64Barrier(int64t *theValue);
bool
OSAtomicCompareAndSwap32(int32t oldValue, int32t newValue,
int32t *theValue);
bool
OSAtomicCompareAndSwap32Barrier(int32t oldValue, int32t newValue,
int32t *theValue);
bool
OSAtomicCompareAndSwap64(int64t oldValue, int64t newValue,
int64t *theValue);
bool
OSAtomicCompareAndSwap64Barrier(int64t oldValue, int64t newValue,
int64t *theValue);
bool
OSAtomicTestAndSet(uint32t n, void *theAddress);
bool
OSAtomicTestAndSetBarrier(uint32t n, void *theAddress);
bool
OSAtomicTestAndClear(uint32t n, void *theAddress);
bool
OSAtomicTestAndClearBarrier(uint32t n, void *theAddress);
DESCRIPTION
These functions are thread and multiprocessor safe. For each function,
there is a version that does and anoother that does not incorporate a
memory barrier. Barriers strictly order memory access on a weakly-
ordered architecture such as PC. All loads and stores executed in
sequential program order before the barrier will complete before any load
or store executed after the barrier. On a uniprocessor, the barrier
operation is typically a nop. On a multiprocessor, the barrier can be
quite expensive.
Most code will want to use the barrier functions to insure that memory
shared between threads is properly synchronized. For example, if you
want to initialize a shared data structure and then atomically increment
a variable to indicate that the initialization is complete, then you MUST
use OSAtomicIncrement32Barrier() to ensure that the stores to your data
structure complete before the atomic add. Likewise, the consumer of that
data structure MUST use OSAtomicDecrement32Barrier(), in order to ensure
that their loads of the structure are not executed before the atomic
decrement. On the other hand, if you are simply incrementing a global
counter, then it is safe and potentially much faster to use OSAtomicIn-
crement32(). If you are unsure which version to use, prefer the barrier
variants as they are safer.
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)
Darwin May 26, 2004 Darwin
|