2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
30 #ifndef _OS_OSATOMIC_H
31 #define _OS_OSATOMIC_H
33 #include <libkern/OSBase.h>
35 #if defined(__cplusplus)
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.
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. */
49 extern Boolean
OSCompareAndSwap( UInt32 oldValue
, UInt32 newValue
, UInt32
* address
);
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.
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 */
60 extern SInt32
OSAddAtomic(SInt32 amount
, SInt32
* address
);
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.
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 */
71 extern SInt16
OSAddAtomic16(SInt32 amount
, SInt16
* address
);
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.
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 */
82 extern SInt8
OSAddAtomic8(SInt32 amount
, SInt8
* address
);
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.
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. */
92 extern SInt32
OSIncrementAtomic(SInt32
* address
);
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.
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. */
102 extern SInt16
OSIncrementAtomic16(SInt16
* address
);
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.
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. */
112 extern SInt8
OSIncrementAtomic8(SInt8
* address
);
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.
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. */
122 extern SInt32
OSDecrementAtomic(SInt32
* address
);
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.
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. */
132 extern SInt16
OSDecrementAtomic16(SInt16
* address
);
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.
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. */
142 extern SInt8
OSDecrementAtomic8(SInt8
* address
);
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.
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 */
153 extern UInt32
OSBitAndAtomic(UInt32 mask
, UInt32
* address
);
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.
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. */
164 extern UInt16
OSBitAndAtomic16(UInt32 mask
, UInt16
* address
);
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.
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. */
175 extern UInt8
OSBitAndAtomic8(UInt32 mask
, UInt8
* address
);
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.
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. */
186 extern UInt32
OSBitOrAtomic(UInt32 mask
, UInt32
* address
);
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.
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. */
197 extern UInt16
OSBitOrAtomic16(UInt32 mask
, UInt16
* address
);
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.
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. */
208 extern UInt8
OSBitOrAtomic8(UInt32 mask
, UInt8
* address
);
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.
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. */
219 extern UInt32
OSBitXorAtomic(UInt32 mask
, UInt32
* address
);
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.
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. */
230 extern UInt16
OSBitXorAtomic16(UInt32 mask
, UInt16
* address
);
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.
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. */
241 extern UInt8
OSBitXorAtomic8(UInt32 mask
, UInt8
* address
);
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.
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. */
252 extern Boolean
OSTestAndSet(UInt32 bit
, UInt8
* startAddress
);
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.
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. */
263 extern Boolean
OSTestAndClear(UInt32 bit
, UInt8
* startAddress
);
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.
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. */
274 extern void OSEnqueueAtomic(void ** listHead
, void * element
,
275 SInt32 elementNextFieldOffset
);
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.
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. */
286 extern void * OSDequeueAtomic(void ** listHead
,
287 SInt32 elementNextFieldOffset
);
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. */
293 static __inline__
void OSSynchronizeIO(void)
300 #if defined(__cplusplus)
304 #endif /* ! _OS_OSATOMIC_H */