6 .Nm OSAtomicAdd32Barrier ,
7 .Nm OSAtomicIncrement32 ,
8 .Nm OSAtomicIncrement32Barrier ,
9 .Nm OSAtomicDecrement32 ,
10 .Nm OSAtomicDecrement32Barrier ,
12 .Nm OSAtomicOr32Barrier ,
13 .Nm OSAtomicOr32Orig ,
14 .Nm OSAtomicOr32OrigBarrier ,
16 .Nm OSAtomicAnd32Barrier ,
17 .Nm OSAtomicAnd32Orig ,
18 .Nm OSAtomicAnd32OrigBarrier ,
20 .Nm OSAtomicXor32Barrier ,
21 .Nm OSAtomicXor32Orig ,
22 .Nm OSAtomicXor32OrigBarrier ,
24 .Nm OSAtomicAdd64Barrier ,
25 .Nm OSAtomicIncrement64 ,
26 .Nm OSAtomicIncrement64Barrier ,
27 .Nm OSAtomicDecrement64 ,
28 .Nm OSAtomicDecrement64Barrier ,
29 .Nm OSAtomicCompareAndSwapInt ,
30 .Nm OSAtomicCompareAndSwapIntBarrier ,
31 .Nm OSAtomicCompareAndSwapLong ,
32 .Nm OSAtomicCompareAndSwapLongBarrier ,
33 .Nm OSAtomicCompareAndSwapPtr ,
34 .Nm OSAtomicCompareAndSwapPtrBarrier ,
35 .Nm OSAtomicCompareAndSwap32 ,
36 .Nm OSAtomicCompareAndSwap32Barrier ,
37 .Nm OSAtomicCompareAndSwap64 ,
38 .Nm OSAtomicCompareAndSwap64Barrier ,
39 .Nm OSAtomicTestAndSet ,
40 .Nm OSAtomicTestAndSetBarrier ,
41 .Nm OSAtomicTestAndClear ,
42 .Nm OSAtomicTestAndClearBarrier ,
45 .Nm OSSpinLockUnlock ,
48 .Nd atomic add, increment, decrement, or, and, xor, compare and swap, test and set, test and clear, spinlocks, and lockless queues
52 .In libkern/OSAtomic.h
54 .Fn OSAtomicAdd32 "int32_t theAmount" "volatile int32_t *theValue"
56 .Fn OSAtomicAdd32Barrier "int32_t theAmount" "volatile int32_t *theValue"
58 .Fn OSAtomicIncrement32 "volatile int32_t *theValue"
60 .Fn OSAtomicIncrement32Barrier "volatile int32_t *theValue"
62 .Fn OSAtomicDecrement32 "volatile int32_t *theValue"
64 .Fn OSAtomicDecrement32Barrier "volatile int32_t *theValue"
66 .Fn OSAtomicOr32 "uint32_t theMask" "volatile uint32_t *theValue"
68 .Fn OSAtomicOr32Barrier "uint32_t theMask" "volatile uint32_t *theValue"
70 .Fn OSAtomicAnd32 "uint32_t theMask" "volatile uint32_t *theValue"
72 .Fn OSAtomicAnd32Barrier "uint32_t theMask" "volatile uint32_t *theValue"
74 .Fn OSAtomicXor32 "uint32_t theMask" "volatile uint32_t *theValue"
76 .Fn OSAtomicXor32Barrier "uint32_t theMask" "volatile uint32_t *theValue"
78 .Fn OSAtomicOr32Orig "uint32_t theMask" "volatile uint32_t *theValue"
80 .Fn OSAtomicOr32OrigBarrier "uint32_t theMask" "volatile uint32_t *theValue"
82 .Fn OSAtomicAnd32Orig "uint32_t theMask" "volatile uint32_t *theValue"
84 .Fn OSAtomicAnd32OrigBarrier "uint32_t theMask" "volatile uint32_t *theValue"
86 .Fn OSAtomicXor32Orig "uint32_t theMask" "volatile uint32_t *theValue"
88 .Fn OSAtomicXor32OrigBarrier "uint32_t theMask" "volatile uint32_t *theValue"
90 .Fn OSAtomicAdd64 "int64_t theAmount" "volatile int64_t *theValue"
92 .Fn OSAtomicAdd64Barrier "int64_t theAmount" "volatile int64_t *theValue"
94 .Fn OSAtomicIncrement64 "volatile int64_t *theValue"
96 .Fn OSAtomicIncrement64Barrier "volatile int64_t *theValue"
98 .Fn OSAtomicDecrement64 "volatile int64_t *theValue"
100 .Fn OSAtomicDecrement64Barrier "volatile int64_t *theValue"
102 .Fn OSAtomicCompareAndSwapInt "int oldValue" "int newValue" "volatile int *theValue"
104 .Fn OSAtomicCompareAndSwapIntBarrier "int oldValue" "int newValue" "volatile int *theValue"
106 .Fn OSAtomicCompareAndSwapLong "long oldValue" "long newValue" "volatile long *theValue"
108 .Fn OSAtomicCompareAndSwapLongBarrier "long oldValue" "long newValue" "volatile long *theValue"
110 .Fn OSAtomicCompareAndSwapPtr "void* oldValue" "void* newValue" "void* volatile *theValue"
112 .Fn OSAtomicCompareAndSwapPtrBarrier "void* oldValue" "void* newValue" "void* volatile *theValue"
114 .Fn OSAtomicCompareAndSwap32 "int32_t oldValue" "int32_t newValue" "volatile int32_t *theValue"
116 .Fn OSAtomicCompareAndSwap32Barrier "int32_t oldValue" "int32_t newValue" "volatile int32_t *theValue"
118 .Fn OSAtomicCompareAndSwap64 "int64_t oldValue" "int64_t newValue" "volatile int64_t *theValue"
120 .Fn OSAtomicCompareAndSwap64Barrier "int64_t oldValue" "int64_t newValue" "volatile int64_t *theValue"
122 .Fn OSAtomicTestAndSet "uint32_t n" "volatile void *theAddress"
124 .Fn OSAtomicTestAndSetBarrier "uint32_t n" "volatile void *theAddress"
126 .Fn OSAtomicTestAndClear "uint32_t n" "volatile void *theAddress"
128 .Fn OSAtomicTestAndClearBarrier "uint32_t n" "volatile void *theAddress"
130 .Fn OSSpinLockTry "OSSpinLock *lock"
132 .Fn OSSpinLockLock "OSSpinLock *lock"
134 .Fn OSSpinLockUnlock "OSSpinLock *lock"
136 .Fn OSAtomicEnqueue "OSQueueHead *list" "void *new" "size_t offset"
138 .Fn OSAtomicDequeue "OSQueueHead *list" "size_t offset"
140 These functions are thread and multiprocessor safe. For each function, there
141 is a version that does and another that does not incorporate a memory barrier.
142 Barriers strictly order memory access on a weakly-ordered
143 architecture such as PPC. All loads and stores executed in sequential program
144 order before the barrier will complete before any load or store executed after
145 the barrier. On a uniprocessor, the barrier operation is typically a nop.
146 On a multiprocessor, the barrier can be quite expensive.
148 Most code will want to use the barrier functions to insure that memory shared
149 between threads is properly synchronized. For example, if you want to initialize
150 a shared data structure and then atomically increment a variable to indicate
151 that the initialization is complete, then you must use OSAtomicIncrement32Barrier()
152 to ensure that the stores to your data structure complete before the atomic add.
153 Likewise, the consumer of that data structure must use OSAtomicDecrement32Barrier(),
154 in order to ensure that their loads of the structure are not executed before
155 the atomic decrement. On the other hand,
156 if you are simply incrementing a global counter, then it is safe and potentially much
157 faster to use OSAtomicIncrement32(). If you are unsure which version to use, prefer
158 the barrier variants as they are safer.
160 The logical (and, or, xor) and bit test operations are layered on top of the
161 .Fn OSAtomicCompareAndSwap
162 primitives. There are four versions of each logical operation, depending on whether
163 or not there is a barrier, and whether the return value is the result of the
166 ) or the original value before the operation (eg,
172 must be naturally aligned, ie 32-bit aligned for 32-bit operations and 64-bit
173 aligned for 64-bit operations.
175 The 64-bit operations are not implemented for 32-bit processes on PPC platforms.
178 .Fn OSAtomicCompareAndSwap
187 if the comparison is equal. The comparison and assignment
188 occur as one atomic operation.
190 .Fn OSAtomicTestAndSet
192 .Fn OSAtomicTestAndClear
193 operate on bit (0x80 >> (
195 & 7)) of byte ((char*)
199 >> 3)). They set the named bit to either 1 or 0, respectively.
207 operate on singly linked LIFO queues. Ie, a dequeue operation will return the
208 most recently enqueued element, or NULL if the list is empty. The operations
209 are lockless, and barriers are used as necessary to permit thread-safe access to
212 is the offset in bytes to the link field in the queue element. For example:
213 .Bd -literal -offset indent
214 typedef struct elem {
220 elem_t fred, mary, *p;
222 OSQueueHead q = OS_ATOMIC_QUEUE_INIT;
224 OSAtomicEnqueue( &q, &fred, offsetof(elem_t,link) );
225 OSAtomicEnqueue( &q, &mary, offsetof(elem_t,link) );
227 p = OSAtomicDequeue( &q, offsetof(elem_t,link) );
230 In this example, the call of
232 will return a ptr to mary.
234 The arithmetic operations return the new value, after the operation has been performed.
235 The boolean operations come in two styles, one of which returns the new value, and one
236 of which (the "Orig" versions) returns the old.
237 The compare-and-swap operations return true if the comparison was equal, ie if the swap occured.
238 The bit test and set/clear operations return the original value of the bit.
239 The dequeue operation returns the most recently enqueued element, or NULL if the list in empty.
244 Most of these functions first appeared in Mac OS 10.4 (Tiger). The "Orig" forms of the
245 boolean operations, the "int", "long" and "ptr" forms of compare-and-swap, and lockless
246 enqueue/dequeue first appeared in Mac OS 10.5 (Leopard).