]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/OSAtomic.h
dfe1194cae6fa8ea4f581abaf10ce6ef42c31a47
[apple/xnu.git] / libkern / libkern / OSAtomic.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
25 *
26 * HISTORY
27 *
28 */
29
30 #ifndef _OS_OSATOMIC_H
31 #define _OS_OSATOMIC_H
32
33 #include <libkern/OSBase.h>
34
35 #if defined(__cplusplus)
36 extern "C" {
37 #endif
38
39 /*! @function OSCompareAndSwap
40 @abstract Compare and swap operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
41 @discussion The OSCompareAndSwap function compares the value at the specified address with oldVal. The value of newValue is written to the address only if oldValue and the value at the address are equal. OSCompareAndSwap returns true if newValue is written to the address; otherwise, it returns false.
42
43 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
44 @param oldValue The value to compare at address.
45 @param newValue The value to write to address if oldValue compares true.
46 @param address The 4-byte aligned address of the data to update atomically.
47 @result true if newValue was written to the address. */
48
49 extern Boolean OSCompareAndSwap( UInt32 oldValue, UInt32 newValue, UInt32 * address );
50
51 /*! @function OSAddAtomic
52 @abstract 32-bit add operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
53 @discussion The OSAddAtomic function adds the specified amount to the value at the specified address and returns the original value.
54
55 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
56 @param amount The amount to add.
57 @param address The 4-byte aligned address of the value to update atomically.
58 @result The value before the addition */
59
60 extern SInt32 OSAddAtomic(SInt32 amount, SInt32 * address);
61
62 /*! @function OSAddAtomic16
63 @abstract 16-bit add operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
64 @discussion The OSAddAtomic16 function adds the specified amount to the value at the specified address and returns the original value.
65
66 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
67 @param amount The amount to add.
68 @param address The 2-byte aligned address of the value to update atomically.
69 @result The value before the addition */
70
71 extern SInt16 OSAddAtomic16(SInt32 amount, SInt16 * address);
72
73 /*! @function OSAddAtomic8
74 @abstract 8-bit add operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
75 @discussion The OSAddAtomic8 function adds the specified amount to the value at the specified address and returns the original value.
76
77 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
78 @param amount The amount to add.
79 @param address The address of the value to update atomically.
80 @result The value before the addition */
81
82 extern SInt8 OSAddAtomic8(SInt32 amount, SInt8 * address);
83
84 /*! @function OSIncrementAtomic
85 @abstract 32-bit increment operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
86 @discussion The OSIncrementAtomic function increments the value at the specified address by one and returns the original value.
87
88 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
89 @param address The 4-byte aligned address of the value to update atomically.
90 @result The value before the increment. */
91
92 extern SInt32 OSIncrementAtomic(SInt32 * address);
93
94 /*! @function OSIncrementAtomic16
95 @abstract 16-bit increment operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
96 @discussion The OSIncrementAtomic16 function increments the value at the specified address by one and returns the original value.
97
98 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
99 @param address The 2-byte aligned address of the value to update atomically.
100 @result The value before the increment. */
101
102 extern SInt16 OSIncrementAtomic16(SInt16 * address);
103
104 /*! @function OSIncrementAtomic8
105 @abstract 8-bit increment operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
106 @discussion The OSIncrementAtomic8 function increments the value at the specified address by one and returns the original value.
107
108 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
109 @param address The address of the value to update atomically.
110 @result The value before the increment. */
111
112 extern SInt8 OSIncrementAtomic8(SInt8 * address);
113
114 /*! @function OSDecrementAtomic
115 @abstract 32-bit decrement operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
116 @discussion The OSDecrementAtomic function decrements the value at the specified address by one and returns the original value.
117
118 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
119 @param address The 4-byte aligned address of the value to update atomically.
120 @result The value before the decrement. */
121
122 extern SInt32 OSDecrementAtomic(SInt32 * address);
123
124 /*! @function OSDecrementAtomic16
125 @abstract 16-bit decrement operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
126 @discussion The OSDecrementAtomic16 function decrements the value at the specified address by one and returns the original value.
127
128 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
129 @param address The 2-byte aligned address of the value to update atomically.
130 @result The value before the decrement. */
131
132 extern SInt16 OSDecrementAtomic16(SInt16 * address);
133
134 /*! @function OSDecrementAtomic8
135 @abstract 8-bit decrement operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
136 @discussion The OSDecrementAtomic8 function decrements the value at the specified address by one and returns the original value.
137
138 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
139 @param address The address of the value to update atomically.
140 @result The value before the decrement. */
141
142 extern SInt8 OSDecrementAtomic8(SInt8 * address);
143
144 /*! @function OSBitAndAtomic
145 @abstract 32-bit logical and operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
146 @discussion The OSBitAndAtomic function logically ands the bits of the specified mask into the value at the specified address and returns the original value.
147
148 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
149 @param mask The mask to logically and with the value.
150 @param address The 4-byte aligned address of the value to update atomically.
151 @result The value before the bitwise operation */
152
153 extern UInt32 OSBitAndAtomic(UInt32 mask, UInt32 * address);
154
155 /*! @function OSBitAndAtomic16
156 @abstract 16-bit logical and operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
157 @discussion The OSBitAndAtomic16 function logically ands the bits of the specified mask into the value at the specified address and returns the original value.
158
159 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
160 @param mask The mask to logically and with the value.
161 @param address The 2-byte aligned address of the value to update atomically.
162 @result The value before the bitwise operation. */
163
164 extern UInt16 OSBitAndAtomic16(UInt32 mask, UInt16 * address);
165
166 /*! @function OSBitAndAtomic8
167 @abstract 8-bit logical and operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
168 @discussion The OSBitAndAtomic8 function logically ands the bits of the specified mask into the value at the specified address and returns the original value.
169
170 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
171 @param mask The mask to logically and with the value.
172 @param address The address of the value to update atomically.
173 @result The value before the bitwise operation. */
174
175 extern UInt8 OSBitAndAtomic8(UInt32 mask, UInt8 * address);
176
177 /*! @function OSBitOrAtomic
178 @abstract 32-bit logical or operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
179 @discussion The OSBitOrAtomic function logically ors the bits of the specified mask into the value at the specified address and returns the original value.
180
181 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
182 @param mask The mask to logically or with the value.
183 @param address The 4-byte aligned address of the value to update atomically.
184 @result The value before the bitwise operation. */
185
186 extern UInt32 OSBitOrAtomic(UInt32 mask, UInt32 * address);
187
188 /*! @function OSBitOrAtomic16
189 @abstract 16-bit logical or operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
190 @discussion The OSBitOrAtomic16 function logically ors the bits of the specified mask into the value at the specified address and returns the original value.
191
192 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
193 @param mask The mask to logically or with the value.
194 @param address The 2-byte aligned address of the value to update atomically.
195 @result The value before the bitwise operation. */
196
197 extern UInt16 OSBitOrAtomic16(UInt32 mask, UInt16 * address);
198
199 /*! @function OSBitOrAtomic8
200 @abstract 8-bit logical or operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
201
202 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
203 @discussion The OSBitOrAtomic8 function logically ors the bits of the specified mask into the value at the specified address and returns the original value.
204 @param mask The mask to logically or with the value.
205 @param address The address of the value to update atomically.
206 @result The value before the bitwise operation. */
207
208 extern UInt8 OSBitOrAtomic8(UInt32 mask, UInt8 * address);
209
210 /*! @function OSBitXorAtomic
211 @abstract 32-bit logical xor operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
212
213 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
214 @discussion The OSBitXorAtomic function logically xors the bits of the specified mask into the value at the specified address and returns the original value.
215 @param mask The mask to logically or with the value.
216 @param address The 4-byte aligned address of the value to update atomically.
217 @result The value before the bitwise operation. */
218
219 extern UInt32 OSBitXorAtomic(UInt32 mask, UInt32 * address);
220
221 /*! @function OSBitXorAtomic16
222 @abstract 16-bit logical xor operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
223 @discussion The OSBitXorAtomic16 function logically xors the bits of the specified mask into the value at the specified address and returns the original value.
224
225 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
226 @param mask The mask to logically or with the value.
227 @param address The 2-byte aligned address of the value to update atomically.
228 @result The value before the bitwise operation. */
229
230 extern UInt16 OSBitXorAtomic16(UInt32 mask, UInt16 * address);
231
232 /*! @function OSBitXorAtomic8
233 @abstract 8-bit logical xor operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
234
235 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
236 @discussion The OSBitXorAtomic8 function logically xors the bits of the specified mask into the value at the specified address and returns the original value.
237 @param mask The mask to logically or with the value.
238 @param address The address of the value to update atomically.
239 @result The value before the bitwise operation. */
240
241 extern UInt8 OSBitXorAtomic8(UInt32 mask, UInt8 * address);
242
243 /*! @function OSTestAndSet
244 @abstract Bit test and set operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
245
246 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
247 @discussion The OSTestAndSet function sets a single bit in a byte at a specified address. It returns true if the bit was already set, false otherwise.
248 @param bit The bit number in the range 0 through 7.
249 @param address The address of the byte to update atomically.
250 @result true if the bit was already set, false otherwise. */
251
252 extern Boolean OSTestAndSet(UInt32 bit, UInt8 * startAddress);
253
254 /*! @function OSTestAndClear
255 @abstract Bit test and clear operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
256 @discussion The OSTestAndClear function clears a single bit in a byte at a specified address. It returns true if the bit was already clear, false otherwise.
257
258 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
259 @param bit The bit number in the range 0 through 7.
260 @param address The address of the byte to update atomically.
261 @result true if the bit was already clear, false otherwise. */
262
263 extern Boolean OSTestAndClear(UInt32 bit, UInt8 * startAddress);
264
265 /*! @function OSEnqueueAtomic
266 @abstract Singly linked list head insertion, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
267 @discussion The OSEnqueueAtomic function places an element at the head of a single linked list, which is specified with the address of a head pointer, listHead. The element structure has a next field whose offset is specified.
268
269 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
270 @param listHead The address of a head pointer for the list .
271 @param element The list element to insert at the head of the list.
272 @param elementNextFieldOffset The byte offset into the element where a pointer to the next element in the list is stored. */
273
274 extern void OSEnqueueAtomic(void ** listHead, void * element,
275 SInt32 elementNextFieldOffset);
276
277 /*! @function OSDequeueAtomic
278 @abstract Singly linked list element head removal, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
279 @discussion The OSDequeueAtomic function removes an element from the head of a single linked list, which is specified with the address of a head pointer, listHead. The element structure has a next field whose offset is specified.
280
281 This function guarantees atomicity only with main system memory. It is specifically unsuitable for use on noncacheable memory such as that in devices; this function cannot guarantee atomicity, for example, on memory mapped from a PCI device.
282 @param listHead The address of a head pointer for the list .
283 @param elementNextFieldOffset The byte offset into the element where a pointer to the next element in the list is stored.
284 @result A removed element, or zero if the list is empty. */
285
286 extern void * OSDequeueAtomic(void ** listHead,
287 SInt32 elementNextFieldOffset);
288
289 /*! @function OSSynchronizeIO
290 @abstract The OSSynchronizeIO routine ensures orderly load and store operations to noncached memory mapped I/O devices.
291 @discussion The OSSynchronizeIO routine ensures orderly load and store operations to noncached memory mapped I/O devices. It executes the eieio instruction on PowerPC processors. */
292
293 static __inline__ void OSSynchronizeIO(void)
294 {
295 #if defined(__ppc__)
296 __asm__ ("eieio");
297 #endif
298 }
299
300 #if defined(__cplusplus)
301 }
302 #endif
303
304 #endif /* ! _OS_OSATOMIC_H */