2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
35 #ifndef _OS_OSATOMIC_H
36 #define _OS_OSATOMIC_H
38 #include <libkern/OSBase.h>
40 #if defined(__cplusplus)
44 /*! @function OSCompareAndSwap
45 @abstract Compare and swap operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
46 @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.
48 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.
49 @param oldValue The value to compare at address.
50 @param newValue The value to write to address if oldValue compares true.
51 @param address The 4-byte aligned address of the data to update atomically.
52 @result true if newValue was written to the address. */
54 extern Boolean
OSCompareAndSwap( UInt32 oldValue
, UInt32 newValue
, UInt32
* address
);
56 /*! @function OSAddAtomic
57 @abstract 32-bit add operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
58 @discussion The OSAddAtomic function adds the specified amount to the value at the specified address and returns the original value.
60 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.
61 @param amount The amount to add.
62 @param address The 4-byte aligned address of the value to update atomically.
63 @result The value before the addition */
65 extern SInt32
OSAddAtomic(SInt32 amount
, SInt32
* address
);
67 /*! @function OSAddAtomic16
68 @abstract 16-bit add operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
69 @discussion The OSAddAtomic16 function adds the specified amount to the value at the specified address and returns the original value.
71 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.
72 @param amount The amount to add.
73 @param address The 2-byte aligned address of the value to update atomically.
74 @result The value before the addition */
76 extern SInt16
OSAddAtomic16(SInt32 amount
, SInt16
* address
);
78 /*! @function OSAddAtomic8
79 @abstract 8-bit add operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
80 @discussion The OSAddAtomic8 function adds the specified amount to the value at the specified address and returns the original value.
82 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.
83 @param amount The amount to add.
84 @param address The address of the value to update atomically.
85 @result The value before the addition */
87 extern SInt8
OSAddAtomic8(SInt32 amount
, SInt8
* address
);
89 /*! @function OSIncrementAtomic
90 @abstract 32-bit increment operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
91 @discussion The OSIncrementAtomic function increments the value at the specified address by one and returns the original value.
93 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.
94 @param address The 4-byte aligned address of the value to update atomically.
95 @result The value before the increment. */
97 extern SInt32
OSIncrementAtomic(SInt32
* address
);
99 /*! @function OSIncrementAtomic16
100 @abstract 16-bit increment operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
101 @discussion The OSIncrementAtomic16 function increments the value at the specified address by one and returns the original value.
103 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.
104 @param address The 2-byte aligned address of the value to update atomically.
105 @result The value before the increment. */
107 extern SInt16
OSIncrementAtomic16(SInt16
* address
);
109 /*! @function OSIncrementAtomic8
110 @abstract 8-bit increment operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
111 @discussion The OSIncrementAtomic8 function increments the value at the specified address by one and returns the original value.
113 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.
114 @param address The address of the value to update atomically.
115 @result The value before the increment. */
117 extern SInt8
OSIncrementAtomic8(SInt8
* address
);
119 /*! @function OSDecrementAtomic
120 @abstract 32-bit decrement operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
121 @discussion The OSDecrementAtomic function decrements the value at the specified address by one and returns the original value.
123 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.
124 @param address The 4-byte aligned address of the value to update atomically.
125 @result The value before the decrement. */
127 extern SInt32
OSDecrementAtomic(SInt32
* address
);
129 /*! @function OSDecrementAtomic16
130 @abstract 16-bit decrement operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
131 @discussion The OSDecrementAtomic16 function decrements the value at the specified address by one and returns the original value.
133 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.
134 @param address The 2-byte aligned address of the value to update atomically.
135 @result The value before the decrement. */
137 extern SInt16
OSDecrementAtomic16(SInt16
* address
);
139 /*! @function OSDecrementAtomic8
140 @abstract 8-bit decrement operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
141 @discussion The OSDecrementAtomic8 function decrements the value at the specified address by one and returns the original value.
143 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.
144 @param address The address of the value to update atomically.
145 @result The value before the decrement. */
147 extern SInt8
OSDecrementAtomic8(SInt8
* address
);
149 /*! @function OSBitAndAtomic
150 @abstract 32-bit logical and operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
151 @discussion The OSBitAndAtomic function logically ands the bits of the specified mask into the value at the specified address and returns the original value.
153 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.
154 @param mask The mask to logically and with the value.
155 @param address The 4-byte aligned address of the value to update atomically.
156 @result The value before the bitwise operation */
158 extern UInt32
OSBitAndAtomic(UInt32 mask
, UInt32
* address
);
160 /*! @function OSBitAndAtomic16
161 @abstract 16-bit logical and operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
162 @discussion The OSBitAndAtomic16 function logically ands the bits of the specified mask into the value at the specified address and returns the original value.
164 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.
165 @param mask The mask to logically and with the value.
166 @param address The 2-byte aligned address of the value to update atomically.
167 @result The value before the bitwise operation. */
169 extern UInt16
OSBitAndAtomic16(UInt32 mask
, UInt16
* address
);
171 /*! @function OSBitAndAtomic8
172 @abstract 8-bit logical and operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
173 @discussion The OSBitAndAtomic8 function logically ands the bits of the specified mask into the value at the specified address and returns the original value.
175 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.
176 @param mask The mask to logically and with the value.
177 @param address The address of the value to update atomically.
178 @result The value before the bitwise operation. */
180 extern UInt8
OSBitAndAtomic8(UInt32 mask
, UInt8
* address
);
182 /*! @function OSBitOrAtomic
183 @abstract 32-bit logical or operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
184 @discussion The OSBitOrAtomic function logically ors the bits of the specified mask into the value at the specified address and returns the original value.
186 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.
187 @param mask The mask to logically or with the value.
188 @param address The 4-byte aligned address of the value to update atomically.
189 @result The value before the bitwise operation. */
191 extern UInt32
OSBitOrAtomic(UInt32 mask
, UInt32
* address
);
193 /*! @function OSBitOrAtomic16
194 @abstract 16-bit logical or operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
195 @discussion The OSBitOrAtomic16 function logically ors the bits of the specified mask into the value at the specified address and returns the original value.
197 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.
198 @param mask The mask to logically or with the value.
199 @param address The 2-byte aligned address of the value to update atomically.
200 @result The value before the bitwise operation. */
202 extern UInt16
OSBitOrAtomic16(UInt32 mask
, UInt16
* address
);
204 /*! @function OSBitOrAtomic8
205 @abstract 8-bit logical or operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
207 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.
208 @discussion The OSBitOrAtomic8 function logically ors the bits of the specified mask into the value at the specified address and returns the original value.
209 @param mask The mask to logically or with the value.
210 @param address The address of the value to update atomically.
211 @result The value before the bitwise operation. */
213 extern UInt8
OSBitOrAtomic8(UInt32 mask
, UInt8
* address
);
215 /*! @function OSBitXorAtomic
216 @abstract 32-bit logical xor operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
218 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.
219 @discussion The OSBitXorAtomic function logically xors the bits of the specified mask into the value at the specified address and returns the original value.
220 @param mask The mask to logically or with the value.
221 @param address The 4-byte aligned address of the value to update atomically.
222 @result The value before the bitwise operation. */
224 extern UInt32
OSBitXorAtomic(UInt32 mask
, UInt32
* address
);
226 /*! @function OSBitXorAtomic16
227 @abstract 16-bit logical xor operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
228 @discussion The OSBitXorAtomic16 function logically xors the bits of the specified mask into the value at the specified address and returns the original value.
230 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.
231 @param mask The mask to logically or with the value.
232 @param address The 2-byte aligned address of the value to update atomically.
233 @result The value before the bitwise operation. */
235 extern UInt16
OSBitXorAtomic16(UInt32 mask
, UInt16
* address
);
237 /*! @function OSBitXorAtomic8
238 @abstract 8-bit logical xor operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
240 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.
241 @discussion The OSBitXorAtomic8 function logically xors the bits of the specified mask into the value at the specified address and returns the original value.
242 @param mask The mask to logically or with the value.
243 @param address The address of the value to update atomically.
244 @result The value before the bitwise operation. */
246 extern UInt8
OSBitXorAtomic8(UInt32 mask
, UInt8
* address
);
248 /*! @function OSTestAndSet
249 @abstract Bit test and set operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
251 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.
252 @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.
253 @param bit The bit number in the range 0 through 7.
254 @param address The address of the byte to update atomically.
255 @result true if the bit was already set, false otherwise. */
257 extern Boolean
OSTestAndSet(UInt32 bit
, UInt8
* startAddress
);
259 /*! @function OSTestAndClear
260 @abstract Bit test and clear operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
261 @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.
263 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.
264 @param bit The bit number in the range 0 through 7.
265 @param address The address of the byte to update atomically.
266 @result true if the bit was already clear, false otherwise. */
268 extern Boolean
OSTestAndClear(UInt32 bit
, UInt8
* startAddress
);
271 /*! @function OSEnqueueAtomic
272 @abstract Singly linked list head insertion, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
273 @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.
275 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.
276 @param listHead The address of a head pointer for the list .
277 @param element The list element to insert at the head of the list.
278 @param elementNextFieldOffset The byte offset into the element where a pointer to the next element in the list is stored. */
280 extern void OSEnqueueAtomic(void ** listHead
, void * element
,
281 SInt32 elementNextFieldOffset
);
283 /*! @function OSDequeueAtomic
284 @abstract Singly linked list element head removal, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
285 @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.
287 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.
288 @param listHead The address of a head pointer for the list .
289 @param elementNextFieldOffset The byte offset into the element where a pointer to the next element in the list is stored.
290 @result A removed element, or zero if the list is empty. */
292 extern void * OSDequeueAtomic(void ** listHead
,
293 SInt32 elementNextFieldOffset
);
296 /*! @function OSSynchronizeIO
297 @abstract The OSSynchronizeIO routine ensures orderly load and store operations to noncached memory mapped I/O devices.
298 @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. */
300 static __inline__
void OSSynchronizeIO(void)
307 #if defined(__cplusplus)
311 #endif /* ! _OS_OSATOMIC_H */