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)
48 * This header declares the OSAtomic group of functions for atomic
49 * reading and updating of values.
52 #if defined(__i386__) || defined(__x86_64__)
55 * @function OSCompareAndSwap64
58 * 64-bit compare and swap operation.
61 * See OSCompareAndSwap.
63 extern Boolean
OSCompareAndSwap64(
66 volatile UInt64
* address
);
68 #endif /* defined(__i386__) || defined(__x86_64__) */
70 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__)
73 * @function OSAddAtomic64
76 * 64-bit atomic add operation.
81 extern SInt64
OSAddAtomic64(
83 volatile SInt64
* address
);
86 * @function OSIncrementAtomic64
92 * See OSIncrementAtomic.
94 inline static SInt64
OSIncrementAtomic64(volatile SInt64
* address
)
96 return OSAddAtomic64(1LL, address
);
100 * @function OSDecrementAtomic64
106 * See OSDecrementAtomic.
108 inline static SInt64
OSDecrementAtomic64(volatile SInt64
* address
)
110 return OSAddAtomic64(-1LL, address
);
113 #endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm__) */
115 #if XNU_KERNEL_PRIVATE
116 /* Not to be included in headerdoc.
118 * @function OSAddAtomicLong
121 * 32/64-bit atomic add operation, depending on sizeof(long).
126 extern long OSAddAtomicLong(
128 volatile long * address
);
130 /* Not to be included in headerdoc.
132 * @function OSIncrementAtomicLong
135 * 32/64-bit increment, depending on sizeof(long)
138 * See OSIncrementAtomic.
140 inline static long OSIncrementAtomicLong(volatile long * address
)
142 return OSAddAtomicLong(1L, address
);
145 /* Not to be included in headerdoc.
147 * @function OSDecrementAtomicLong
150 * 32/64-bit decrement, depending on sizeof(long)
151 *@discussion See OSDecrementAtomic.
153 inline static long OSDecrementAtomicLong(volatile long * address
)
155 return OSAddAtomicLong(-1L, address
);
157 #endif /* XNU_KERNEL_PRIVATE */
160 * The macro SAFE_CAST_PTR() casts one type of pointer to another type, making sure
161 * the data the pointer is referencing is the same size. If it is not, it will cause
162 * a division by zero compiler warning. This is to work around "SInt32" being defined
163 * as "long" on ILP32 and as "int" on LP64, which would require an explicit cast to
164 * "SInt32*" when for instance passing an "int*" to OSAddAtomic() - which masks size
166 * -- var is used twice, but sizeof does not evaluate the
167 * argument, i.e. we're safe against "++" etc. in var --
169 #ifdef XNU_KERNEL_PRIVATE
170 #define SAFE_CAST_PTR(type, var) (((type)(var))+(0/(sizeof(*var) == sizeof(*(type)0) ? 1 : 0)))
172 #define SAFE_CAST_PTR(type, var) ((type)(var))
176 * @function OSCompareAndSwap
179 * Compare and swap operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
182 * 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.
184 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
186 * @param oldValue The value to compare at address.
187 * @param newValue The value to write to address if oldValue compares true.
188 * @param address The 4-byte aligned address of the data to update atomically.
189 * @result true if newValue was written to the address.
191 extern Boolean
OSCompareAndSwap(
194 volatile UInt32
* address
);
195 #define OSCompareAndSwap(a, b, c) \
196 (OSCompareAndSwap(a, b, SAFE_CAST_PTR(volatile UInt32*,c)))
199 * @function OSCompareAndSwapPtr
202 * Compare and swap operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
205 * The OSCompareAndSwapPtr function compares the pointer-sized 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. OSCompareAndSwapPtr returns true if newValue is written to the address; otherwise, it returns false.
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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
208 * @param oldValue The pointer value to compare at address.
209 * @param newValue The pointer value to write to address if oldValue compares true.
210 * @param address The pointer-size aligned address of the data to update atomically.
211 * @result true if newValue was written to the address.
213 extern Boolean
OSCompareAndSwapPtr(
216 void * volatile * address
);
217 #define OSCompareAndSwapPtr(a, b, c) \
218 (OSCompareAndSwapPtr(a, b, SAFE_CAST_PTR(void * volatile *,c)))
221 * @function OSAddAtomic
224 * 32-bit add operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
227 * The OSAddAtomic function adds the specified amount to the value at the specified address and returns the original value.
229 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
230 * @param amount The amount to add.
231 * @param address The 4-byte aligned address of the value to update atomically.
232 * @result The value before the addition
234 extern SInt32
OSAddAtomic(
236 volatile SInt32
* address
);
237 #define OSAddAtomic(a, b) \
238 (OSAddAtomic(a, SAFE_CAST_PTR(volatile SInt32*,b)))
241 * @function OSAddAtomic16
244 * 16-bit add operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
247 * The OSAddAtomic16 function adds the specified amount to the value at the specified address and returns the original value.
249 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
250 * @param amount The amount to add.
251 * @param address The 2-byte aligned address of the value to update atomically.
252 * @result The value before the addition
254 extern SInt16
OSAddAtomic16(
256 volatile SInt16
* address
);
259 * @function OSAddAtomic8
262 * 8-bit add operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
265 * The OSAddAtomic8 function adds the specified amount to the value at the specified address and returns the original value.
267 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
268 * @param amount The amount to add.
269 * @param address The address of the value to update atomically.
270 * @result The value before the addition.
272 extern SInt8
OSAddAtomic8(
274 volatile SInt8
* address
);
277 * @function OSIncrementAtomic
280 * 32-bit increment operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
283 * The OSIncrementAtomic function increments the value at the specified address by one and returns the original value.
285 * 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.
286 * @param address The 4-byte aligned address of the value to update atomically.
287 * @result The value before the increment.
289 extern SInt32
OSIncrementAtomic(volatile SInt32
* address
);
290 #define OSIncrementAtomic(a) \
291 (OSIncrementAtomic(SAFE_CAST_PTR(volatile SInt32*,a)))
294 * @function OSIncrementAtomic16
297 * 16-bit increment operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
300 * The OSIncrementAtomic16 function increments the value at the specified address by one and returns the original value.
302 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
303 * @param address The 2-byte aligned address of the value to update atomically.
304 * @result The value before the increment.
306 extern SInt16
OSIncrementAtomic16(volatile SInt16
* address
);
309 * @function OSIncrementAtomic8
312 * 8-bit increment operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
315 * The OSIncrementAtomic8 function increments the value at the specified address by one and returns the original value.
317 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
318 * @param address The address of the value to update atomically.
319 * @result The value before the increment.
321 extern SInt8
OSIncrementAtomic8(volatile SInt8
* address
);
324 * @function OSDecrementAtomic
327 * 32-bit decrement operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
330 * The OSDecrementAtomic function decrements the value at the specified address by one and returns the original value.
332 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
333 * @param address The 4-byte aligned address of the value to update atomically.
334 * @result The value before the decrement.
336 extern SInt32
OSDecrementAtomic(volatile SInt32
* address
);
337 #define OSDecrementAtomic(a) \
338 (OSDecrementAtomic(SAFE_CAST_PTR(volatile SInt32*,a)))
341 * @function OSDecrementAtomic16
344 * 16-bit decrement operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
347 * The OSDecrementAtomic16 function decrements the value at the specified address by one and returns the original value.
349 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
350 * @param address The 2-byte aligned address of the value to update atomically.
351 * @result The value before the decrement.
353 extern SInt16
OSDecrementAtomic16(volatile SInt16
* address
);
356 * @function OSDecrementAtomic8
359 * 8-bit decrement operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
362 * The OSDecrementAtomic8 function decrements the value at the specified address by one and returns the original value.
364 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
365 * @param address The address of the value to update atomically.
366 * @result The value before the decrement.
368 extern SInt8
OSDecrementAtomic8(volatile SInt8
* address
);
371 * @function OSBitAndAtomic
374 * 32-bit logical and operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
377 * The OSBitAndAtomic function logically ands the bits of the specified mask into the value at the specified address and returns the original value.
379 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
380 * @param mask The mask to logically and with the value.
381 * @param address The 4-byte aligned address of the value to update atomically.
382 * @result The value before the bitwise operation
384 extern UInt32
OSBitAndAtomic(
386 volatile UInt32
* address
);
387 #define OSBitAndAtomic(a, b) \
388 (OSBitAndAtomic(a, SAFE_CAST_PTR(volatile UInt32*,b)))
391 * @function OSBitAndAtomic16
394 * 16-bit logical and operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
397 * The OSBitAndAtomic16 function logically ands the bits of the specified mask into the value at the specified address and returns the original value.
399 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
400 * @param mask The mask to logically and with the value.
401 * @param address The 2-byte aligned address of the value to update atomically.
402 * @result The value before the bitwise operation.
404 extern UInt16
OSBitAndAtomic16(
406 volatile UInt16
* address
);
409 * @function OSBitAndAtomic8
412 * 8-bit logical and operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
415 * The OSBitAndAtomic8 function logically ands the bits of the specified mask into the value at the specified address and returns the original value.
417 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
418 * @param mask The mask to logically and with the value.
419 * @param address The address of the value to update atomically.
420 * @result The value before the bitwise operation.
422 extern UInt8
OSBitAndAtomic8(
424 volatile UInt8
* address
);
427 * @function OSBitOrAtomic
430 * 32-bit logical or operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
433 * The OSBitOrAtomic function logically ors the bits of the specified mask into the value at the specified address and returns the original value.
435 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
436 * @param mask The mask to logically or with the value.
437 * @param address The 4-byte aligned address of the value to update atomically.
438 * @result The value before the bitwise operation.
440 extern UInt32
OSBitOrAtomic(
442 volatile UInt32
* address
);
443 #define OSBitOrAtomic(a, b) \
444 (OSBitOrAtomic(a, SAFE_CAST_PTR(volatile UInt32*,b)))
447 * @function OSBitOrAtomic16
450 * 16-bit logical or operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
453 * The OSBitOrAtomic16 function logically ors the bits of the specified mask into the value at the specified address and returns the original value.
455 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
456 * @param mask The mask to logically or with the value.
457 * @param address The 2-byte aligned address of the value to update atomically.
458 * @result The value before the bitwise operation.
460 extern UInt16
OSBitOrAtomic16(
462 volatile UInt16
* address
);
465 * @function OSBitOrAtomic8
468 * 8-bit logical or operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
470 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
473 * The OSBitOrAtomic8 function logically ors the bits of the specified mask into the value at the specified address and returns the original value.
474 * @param mask The mask to logically or with the value.
475 * @param address The address of the value to update atomically.
476 * @result The value before the bitwise operation.
478 extern UInt8
OSBitOrAtomic8(
480 volatile UInt8
* address
);
483 * @function OSBitXorAtomic
486 * 32-bit logical xor operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
488 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
491 * The OSBitXorAtomic function logically xors the bits of the specified mask into the value at the specified address and returns the original value.
492 * @param mask The mask to logically or with the value.
493 * @param address The 4-byte aligned address of the value to update atomically.
494 * @result The value before the bitwise operation.
496 extern UInt32
OSBitXorAtomic(
498 volatile UInt32
* address
);
499 #define OSBitXorAtomic(a, b) \
500 (OSBitXorAtomic(a, SAFE_CAST_PTR(volatile UInt32*,b)))
503 * @function OSBitXorAtomic16
506 * 16-bit logical xor operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
509 * The OSBitXorAtomic16 function logically xors the bits of the specified mask into the value at the specified address and returns the original value.
511 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
512 * @param mask The mask to logically or with the value.
513 * @param address The 2-byte aligned address of the value to update atomically.
514 * @result The value before the bitwise operation.
516 extern UInt16
OSBitXorAtomic16(
518 volatile UInt16
* address
);
521 * @function OSBitXorAtomic8
524 * 8-bit logical xor operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
526 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
529 * The OSBitXorAtomic8 function logically xors the bits of the specified mask into the value at the specified address and returns the original value.
530 * @param mask The mask to logically or with the value.
531 * @param address The address of the value to update atomically.
532 * @result The value before the bitwise operation.
534 extern UInt8
OSBitXorAtomic8(
536 volatile UInt8
* address
);
539 * @function OSTestAndSet
542 * Bit test and set operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
544 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
547 * 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.
548 * @param bit The bit number in the range 0 through 7.
549 * @param startAddress The address of the byte to update atomically.
550 * @result true if the bit was already set, false otherwise.
552 extern Boolean
OSTestAndSet(
554 volatile UInt8
* startAddress
);
557 * @function OSTestAndClear
560 * Bit test and clear operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
563 * 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.
565 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
566 * @param bit The bit number in the range 0 through 7.
567 * @param startAddress The address of the byte to update atomically.
568 * @result true if the bit was already clear, false otherwise.
570 extern Boolean
OSTestAndClear(
572 volatile UInt8
* startAddress
);
576 * @function OSEnqueueAtomic
579 * Singly linked list head insertion, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
582 * 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.
584 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
585 * @param listHead The address of a head pointer for the list .
586 * @param element The list element to insert at the head of the list.
587 * @param elementNextFieldOffset The byte offset into the element where a pointer to the next element in the list is stored.
589 extern void OSEnqueueAtomic(
590 void * volatile * listHead
,
592 SInt32 elementNextFieldOffset
);
595 * @function OSDequeueAtomic
598 * Singly linked list element head removal, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
601 * 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.
603 * 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. Additionally, this function incorporates a memory barrier on systems with weakly-ordered memory architectures.
604 * @param listHead The address of a head pointer for the list .
605 * @param elementNextFieldOffset The byte offset into the element where a pointer to the next element in the list is stored.
606 * @result A removed element, or zero if the list is empty.
608 extern void * OSDequeueAtomic(
609 void * volatile * listHead
,
610 SInt32 elementNextFieldOffset
);
614 * @function OSSynchronizeIO
617 * The OSSynchronizeIO routine ensures orderly load and store operations to noncached memory mapped I/O devices.
620 * The OSSynchronizeIO routine ensures orderly load and store operations to noncached memory mapped I/O devices. It executes the eieio instruction on PowerPC processors.
622 static __inline__
void OSSynchronizeIO(void)
629 #if defined(__cplusplus)
633 #endif /* ! _OS_OSATOMIC_H */