6 .Nm OSAtomicAdd32Barrier ,
7 .Nm OSAtomicIncrement32 ,
8 .Nm OSAtomicIncrement32Barrier ,
9 .Nm OSAtomicDecrement32 ,
10 .Nm OSAtomicDecrement32Barrier ,
12 .Nm OSAtomicOr32Barrier ,
14 .Nm OSAtomicAnd32Barrier ,
16 .Nm OSAtomicXor32Barrier ,
18 .Nm OSAtomicAdd64Barrier ,
19 .Nm OSAtomicIncrement64 ,
20 .Nm OSAtomicIncrement64Barrier ,
21 .Nm OSAtomicDecrement64 ,
22 .Nm OSAtomicDecrement64Barrier ,
23 .Nm OSAtomicCompareAndSwap32 ,
24 .Nm OSAtomicCompareAndSwap32Barrier ,
25 .Nm OSAtomicCompareAndSwap64 ,
26 .Nm OSAtomicCompareAndSwap64Barrier ,
27 .Nm OSAtomicTestAndSet ,
28 .Nm OSAtomicTestAndSetBarrier ,
29 .Nm OSAtomicTestAndClear ,
30 .Nm OSAtomicTestAndClearBarrier
31 .Nd atomic add, increment, decrement, or, and, xor, compare and swap, test and set, and test and clear
35 .In libkern/OSAtomic.h
37 .Fn OSAtomicAdd32 "int32_t theAmount, int32_t *theValue"
39 .Fn OSAtomicAdd32Barrier "int32_t theAmount, int32_t *theValue"
41 .Fn OSAtomicIncrement32 "int32_t *theValue"
43 .Fn OSAtomicIncrement32Barrier "int32_t *theValue"
45 .Fn OSAtomicDecrement32 "int32_t *theValue"
47 .Fn OSAtomicDecrement32Barrier "int32_t *theValue"
49 .Fn OSAtomicOr32 "uint32_t theMask, uint32_t *theValue"
51 .Fn OSAtomicOr32Barrier "uint32_t theMask, uint32_t *theValue"
53 .Fn OSAtomicAnd32 "uint32_t theMask, uint32_t *theValue"
55 .Fn OSAtomicAnd32Barrier "uint32_t theMask, uint32_t *theValue"
57 .Fn OSAtomicXor32 "uint32_t theMask, uint32_t *theValue"
59 .Fn OSAtomicXor32Barrier "uint32_t theMask, uint32_t *theValue"
61 .Fn OSAtomicAdd64 "int64_t theAmount, int64_t *theValue"
63 .Fn OSAtomicAdd64Barrier "int64_t theAmount, int64_t *theValue"
65 .Fn OSAtomicIncrement64 "int64_t *theValue"
67 .Fn OSAtomicIncrement64Barrier "int64_t *theValue"
69 .Fn OSAtomicDecrement64 "int64_t *theValue"
71 .Fn OSAtomicDecrement64Barrier "int64_t *theValue"
73 .Fn OSAtomicCompareAndSwap32 "int32_t oldValue" "int32_t newValue" "int32_t *theValue"
75 .Fn OSAtomicCompareAndSwap32Barrier "int32_t oldValue" "int32_t newValue" "int32_t *theValue"
77 .Fn OSAtomicCompareAndSwap64 "int64_t oldValue" "int64_t newValue" "int64_t *theValue"
79 .Fn OSAtomicCompareAndSwap64Barrier "int64_t oldValue" "int64_t newValue" "int64_t *theValue"
81 .Fn OSAtomicTestAndSet "uint32_t n, void *theAddress"
83 .Fn OSAtomicTestAndSetBarrier "uint32_t n, void *theAddress"
85 .Fn OSAtomicTestAndClear "uint32_t n, void *theAddress"
87 .Fn OSAtomicTestAndClearBarrier "uint32_t n, void *theAddress"
89 These functions are thread and multiprocessor safe. For each function, there
90 is a version that does and anoother that does not incorporate a memory barrier.
91 Barriers strictly order memory access on a weakly-ordered
92 architecture such as PPC. All loads and stores executed in sequential program
93 order before the barrier will complete before any load or store executed after
94 the barrier. On a uniprocessor, the barrier operation is typically a nop.
95 On a multiprocessor, the barrier can be quite expensive.
97 Most code will want to use the barrier functions to insure that memory shared
98 between threads is properly synchronized. For example, if you want to initialize
99 a shared data structure and then atomically increment a variable to indicate
100 that the initialization is complete, then you MUST use OSAtomicIncrement32Barrier()
101 to ensure that the stores to your data structure complete before the atomic add.
102 Likewise, the consumer of that data structure MUST use OSAtomicDecrement32Barrier(),
103 in order to ensure that their loads of the structure are not executed before
104 the atomic decrement. On the other hand,
105 if you are simply incrementing a global counter, then it is safe and potentially much
106 faster to use OSAtomicIncrement32(). If you are unsure which version to use, prefer
107 the barrier variants as they are safer.
109 The logical (and, or, xor) and bit test operations are layered on top of the
110 .Fn OSAtomicCompareAndSwap
115 must be naturally aligned, ie 32-bit aligned for 32-bit operations and 64-bit
116 aligned for 64-bit operations.
118 The 64-bit operations are only implemented for
121 .Fn OSAtomicCompareAndSwap32
123 .Fn OSAtomicCompareAndSwap64
132 if the comparison is equal. The comparison and assignment
133 occur as one atomic operation.
135 .Fn OSAtomicTestAndSet
137 .Fn OSAtomicTestAndClear
138 operate on bit (0x80 >> (
140 & 7)) of byte ((char*)
144 >> 3)). They set the named bit to either 1 or 0, respectively.
148 The arithmetic and logical operations return the new value, after the operation has been performed.
149 The compare-and-swap operations return true if the comparison was equal, ie if the swap occured.
150 The bit test and set/clear operations return the original value of the bit.