xref: /xnu-10002.81.5/libkern/libkern/OSAtomic.h (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2007-2012 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4*5e3eaea3SApple OSS Distributions  *
5*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6*5e3eaea3SApple OSS Distributions  *
7*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
8*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
9*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
10*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
11*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
12*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
13*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
14*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
15*5e3eaea3SApple OSS Distributions  *
16*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
17*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
18*5e3eaea3SApple OSS Distributions  *
19*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
20*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
21*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
22*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
23*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
24*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
25*5e3eaea3SApple OSS Distributions  * limitations under the License.
26*5e3eaea3SApple OSS Distributions  *
27*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28*5e3eaea3SApple OSS Distributions  */
29*5e3eaea3SApple OSS Distributions /*
30*5e3eaea3SApple OSS Distributions  * Copyright (c) 1999 Apple Computer, Inc.  All rights reserved.
31*5e3eaea3SApple OSS Distributions  *
32*5e3eaea3SApple OSS Distributions  * HISTORY
33*5e3eaea3SApple OSS Distributions  *
34*5e3eaea3SApple OSS Distributions  */
35*5e3eaea3SApple OSS Distributions 
36*5e3eaea3SApple OSS Distributions #ifndef _OS_OSATOMIC_H
37*5e3eaea3SApple OSS Distributions #define _OS_OSATOMIC_H
38*5e3eaea3SApple OSS Distributions 
39*5e3eaea3SApple OSS Distributions #include <libkern/OSBase.h>
40*5e3eaea3SApple OSS Distributions #include <string.h>
41*5e3eaea3SApple OSS Distributions 
42*5e3eaea3SApple OSS Distributions #if defined(__cplusplus)
43*5e3eaea3SApple OSS Distributions extern "C" {
44*5e3eaea3SApple OSS Distributions #endif
45*5e3eaea3SApple OSS Distributions 
46*5e3eaea3SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
47*5e3eaea3SApple OSS Distributions /*
48*5e3eaea3SApple OSS Distributions  * The macro SAFE_CAST_PTR() casts one type of pointer to another type,
49*5e3eaea3SApple OSS Distributions  * making sure the data the pointer is referencing is the same size.
50*5e3eaea3SApple OSS Distributions  * If it is not, it will cause a "size mismatch" assertion.
51*5e3eaea3SApple OSS Distributions  *
52*5e3eaea3SApple OSS Distributions  * This is to work around "SInt32" being defined as "long" on ILP32 and as "int"
53*5e3eaea3SApple OSS Distributions  * on LP64, which would require an explicit cast to "SInt32*" when for instance
54*5e3eaea3SApple OSS Distributions  * passing an "int*" to OSAddAtomic() - which masks size mismatches.
55*5e3eaea3SApple OSS Distributions  * -- var is used, but sizeof does not evaluate the
56*5e3eaea3SApple OSS Distributions  *    argument, i.e. we're safe against "++" etc. in var --
57*5e3eaea3SApple OSS Distributions  */
58*5e3eaea3SApple OSS Distributions #define __SAFE_CAST_PTR(type, var) \
59*5e3eaea3SApple OSS Distributions 	({ _Static_assert(sizeof(*(var)) == sizeof(*(type)NULL), "size mismatch"); ((type)(var)); })
60*5e3eaea3SApple OSS Distributions #else
61*5e3eaea3SApple OSS Distributions #define __SAFE_CAST_PTR(type, var) ((type)(var))
62*5e3eaea3SApple OSS Distributions #endif
63*5e3eaea3SApple OSS Distributions 
64*5e3eaea3SApple OSS Distributions /*!
65*5e3eaea3SApple OSS Distributions  * @header
66*5e3eaea3SApple OSS Distributions  *
67*5e3eaea3SApple OSS Distributions  * @abstract
68*5e3eaea3SApple OSS Distributions  * This header declares the OSAtomic group of functions for atomic
69*5e3eaea3SApple OSS Distributions  * reading and updating of values.
70*5e3eaea3SApple OSS Distributions  */
71*5e3eaea3SApple OSS Distributions 
72*5e3eaea3SApple OSS Distributions /*!
73*5e3eaea3SApple OSS Distributions  * @function OSCompareAndSwap64
74*5e3eaea3SApple OSS Distributions  *
75*5e3eaea3SApple OSS Distributions  * @abstract
76*5e3eaea3SApple OSS Distributions  * 64-bit compare and swap operation.
77*5e3eaea3SApple OSS Distributions  *
78*5e3eaea3SApple OSS Distributions  * @discussion
79*5e3eaea3SApple OSS Distributions  * See OSCompareAndSwap.
80*5e3eaea3SApple OSS Distributions  */
81*5e3eaea3SApple OSS Distributions extern Boolean OSCompareAndSwap64(
82*5e3eaea3SApple OSS Distributions 	UInt64            oldValue,
83*5e3eaea3SApple OSS Distributions 	UInt64            newValue,
84*5e3eaea3SApple OSS Distributions 	volatile UInt64 * address);
85*5e3eaea3SApple OSS Distributions #define OSCompareAndSwap64(a, b, c) \
86*5e3eaea3SApple OSS Distributions 	(OSCompareAndSwap64(a, b, __SAFE_CAST_PTR(volatile UInt64*,c)))
87*5e3eaea3SApple OSS Distributions 
88*5e3eaea3SApple OSS Distributions /*!
89*5e3eaea3SApple OSS Distributions  * @function OSAddAtomic64
90*5e3eaea3SApple OSS Distributions  *
91*5e3eaea3SApple OSS Distributions  * @abstract
92*5e3eaea3SApple OSS Distributions  * 64-bit atomic add operation.
93*5e3eaea3SApple OSS Distributions  *
94*5e3eaea3SApple OSS Distributions  * @discussion
95*5e3eaea3SApple OSS Distributions  * See OSAddAtomic.
96*5e3eaea3SApple OSS Distributions  */
97*5e3eaea3SApple OSS Distributions extern SInt64 OSAddAtomic64(
98*5e3eaea3SApple OSS Distributions 	SInt64            theAmount,
99*5e3eaea3SApple OSS Distributions 	volatile SInt64 * address);
100*5e3eaea3SApple OSS Distributions #define OSAddAtomic64(a, b) \
101*5e3eaea3SApple OSS Distributions 	(OSAddAtomic64(a, __SAFE_CAST_PTR(volatile SInt64*,b)))
102*5e3eaea3SApple OSS Distributions 
103*5e3eaea3SApple OSS Distributions /*!
104*5e3eaea3SApple OSS Distributions  * @function OSIncrementAtomic64
105*5e3eaea3SApple OSS Distributions  *
106*5e3eaea3SApple OSS Distributions  * @abstract
107*5e3eaea3SApple OSS Distributions  * 64-bit increment.
108*5e3eaea3SApple OSS Distributions  *
109*5e3eaea3SApple OSS Distributions  * @discussion
110*5e3eaea3SApple OSS Distributions  * See OSIncrementAtomic.
111*5e3eaea3SApple OSS Distributions  */
112*5e3eaea3SApple OSS Distributions inline static SInt64
OSIncrementAtomic64(volatile SInt64 * address)113*5e3eaea3SApple OSS Distributions OSIncrementAtomic64(volatile SInt64 * address)
114*5e3eaea3SApple OSS Distributions {
115*5e3eaea3SApple OSS Distributions 	return OSAddAtomic64(1LL, address);
116*5e3eaea3SApple OSS Distributions }
117*5e3eaea3SApple OSS Distributions 
118*5e3eaea3SApple OSS Distributions /*!
119*5e3eaea3SApple OSS Distributions  * @function OSDecrementAtomic64
120*5e3eaea3SApple OSS Distributions  *
121*5e3eaea3SApple OSS Distributions  * @abstract
122*5e3eaea3SApple OSS Distributions  * 64-bit decrement.
123*5e3eaea3SApple OSS Distributions  *
124*5e3eaea3SApple OSS Distributions  * @discussion
125*5e3eaea3SApple OSS Distributions  * See OSDecrementAtomic.
126*5e3eaea3SApple OSS Distributions  */
127*5e3eaea3SApple OSS Distributions inline static SInt64
OSDecrementAtomic64(volatile SInt64 * address)128*5e3eaea3SApple OSS Distributions OSDecrementAtomic64(volatile SInt64 * address)
129*5e3eaea3SApple OSS Distributions {
130*5e3eaea3SApple OSS Distributions 	return OSAddAtomic64(-1LL, address);
131*5e3eaea3SApple OSS Distributions }
132*5e3eaea3SApple OSS Distributions 
133*5e3eaea3SApple OSS Distributions #if XNU_KERNEL_PRIVATE
134*5e3eaea3SApple OSS Distributions /* Not to be included in headerdoc.
135*5e3eaea3SApple OSS Distributions  *
136*5e3eaea3SApple OSS Distributions  * @function OSAddAtomicLong
137*5e3eaea3SApple OSS Distributions  *
138*5e3eaea3SApple OSS Distributions  * @abstract
139*5e3eaea3SApple OSS Distributions  * 32/64-bit atomic add operation, depending on sizeof(long).
140*5e3eaea3SApple OSS Distributions  *
141*5e3eaea3SApple OSS Distributions  * @discussion
142*5e3eaea3SApple OSS Distributions  * See OSAddAtomic.
143*5e3eaea3SApple OSS Distributions  */
144*5e3eaea3SApple OSS Distributions extern long OSAddAtomicLong(
145*5e3eaea3SApple OSS Distributions 	long            theAmount,
146*5e3eaea3SApple OSS Distributions 	volatile long * address);
147*5e3eaea3SApple OSS Distributions #define OSAddAtomicLong(a, b) \
148*5e3eaea3SApple OSS Distributions 	(OSAddAtomicLong(a, __SAFE_CAST_PTR(volatile long*,b)))
149*5e3eaea3SApple OSS Distributions 
150*5e3eaea3SApple OSS Distributions /* Not to be included in headerdoc.
151*5e3eaea3SApple OSS Distributions  *
152*5e3eaea3SApple OSS Distributions  * @function OSIncrementAtomicLong
153*5e3eaea3SApple OSS Distributions  *
154*5e3eaea3SApple OSS Distributions  * @abstract
155*5e3eaea3SApple OSS Distributions  * 32/64-bit increment, depending on sizeof(long)
156*5e3eaea3SApple OSS Distributions  *
157*5e3eaea3SApple OSS Distributions  * @discussion
158*5e3eaea3SApple OSS Distributions  * See OSIncrementAtomic.
159*5e3eaea3SApple OSS Distributions  */
160*5e3eaea3SApple OSS Distributions inline static long
OSIncrementAtomicLong(volatile long * address)161*5e3eaea3SApple OSS Distributions OSIncrementAtomicLong(volatile long * address)
162*5e3eaea3SApple OSS Distributions {
163*5e3eaea3SApple OSS Distributions 	return OSAddAtomicLong(1L, address);
164*5e3eaea3SApple OSS Distributions }
165*5e3eaea3SApple OSS Distributions 
166*5e3eaea3SApple OSS Distributions /* Not to be included in headerdoc.
167*5e3eaea3SApple OSS Distributions  *
168*5e3eaea3SApple OSS Distributions  * @function OSDecrementAtomicLong
169*5e3eaea3SApple OSS Distributions  *
170*5e3eaea3SApple OSS Distributions  * @abstract
171*5e3eaea3SApple OSS Distributions  * 32/64-bit decrement, depending on sizeof(long)
172*5e3eaea3SApple OSS Distributions  *@discussion See OSDecrementAtomic.
173*5e3eaea3SApple OSS Distributions  */
174*5e3eaea3SApple OSS Distributions inline static long
OSDecrementAtomicLong(volatile long * address)175*5e3eaea3SApple OSS Distributions OSDecrementAtomicLong(volatile long * address)
176*5e3eaea3SApple OSS Distributions {
177*5e3eaea3SApple OSS Distributions 	return OSAddAtomicLong(-1L, address);
178*5e3eaea3SApple OSS Distributions }
179*5e3eaea3SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
180*5e3eaea3SApple OSS Distributions 
181*5e3eaea3SApple OSS Distributions #if XNU_KERNEL_PRIVATE
182*5e3eaea3SApple OSS Distributions /*!
183*5e3eaea3SApple OSS Distributions  * @function OSCompareAndSwap8
184*5e3eaea3SApple OSS Distributions  *
185*5e3eaea3SApple OSS Distributions  * @abstract
186*5e3eaea3SApple OSS Distributions  * Compare and swap operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
187*5e3eaea3SApple OSS Distributions  *
188*5e3eaea3SApple OSS Distributions  * @discussion
189*5e3eaea3SApple OSS Distributions  * The OSCompareAndSwap8 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.
190*5e3eaea3SApple OSS Distributions  *
191*5e3eaea3SApple OSS Distributions  * 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.
192*5e3eaea3SApple OSS Distributions  *
193*5e3eaea3SApple OSS Distributions  * @param oldValue The value to compare at address.
194*5e3eaea3SApple OSS Distributions  * @param newValue The value to write to address if oldValue compares true.
195*5e3eaea3SApple OSS Distributions  * @param address The byte aligned address of the data to update atomically.
196*5e3eaea3SApple OSS Distributions  * @result true if newValue was written to the address.
197*5e3eaea3SApple OSS Distributions  */
198*5e3eaea3SApple OSS Distributions extern Boolean OSCompareAndSwap8(
199*5e3eaea3SApple OSS Distributions 	UInt8            oldValue,
200*5e3eaea3SApple OSS Distributions 	UInt8            newValue,
201*5e3eaea3SApple OSS Distributions 	volatile UInt8 * address);
202*5e3eaea3SApple OSS Distributions #define OSCompareAndSwap8(a, b, c) \
203*5e3eaea3SApple OSS Distributions 	(OSCompareAndSwap8(a, b, __SAFE_CAST_PTR(volatile UInt8*,c)))
204*5e3eaea3SApple OSS Distributions 
205*5e3eaea3SApple OSS Distributions /*!
206*5e3eaea3SApple OSS Distributions  * @function OSCompareAndSwap16
207*5e3eaea3SApple OSS Distributions  *
208*5e3eaea3SApple OSS Distributions  * @abstract
209*5e3eaea3SApple OSS Distributions  * Compare and swap operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
210*5e3eaea3SApple OSS Distributions  *
211*5e3eaea3SApple OSS Distributions  * @discussion
212*5e3eaea3SApple OSS Distributions  * The OSCompareAndSwap16 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.
213*5e3eaea3SApple OSS Distributions  *
214*5e3eaea3SApple OSS Distributions  * 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.
215*5e3eaea3SApple OSS Distributions  *
216*5e3eaea3SApple OSS Distributions  * @param oldValue The value to compare at address.
217*5e3eaea3SApple OSS Distributions  * @param newValue The value to write to address if oldValue compares true.
218*5e3eaea3SApple OSS Distributions  * @param address The 2-byte aligned address of the data to update atomically.
219*5e3eaea3SApple OSS Distributions  * @result true if newValue was written to the address.
220*5e3eaea3SApple OSS Distributions  */
221*5e3eaea3SApple OSS Distributions extern Boolean OSCompareAndSwap16(
222*5e3eaea3SApple OSS Distributions 	UInt16            oldValue,
223*5e3eaea3SApple OSS Distributions 	UInt16            newValue,
224*5e3eaea3SApple OSS Distributions 	volatile UInt16 * address);
225*5e3eaea3SApple OSS Distributions #define OSCompareAndSwap16(a, b, c) \
226*5e3eaea3SApple OSS Distributions 	(OSCompareAndSwap16(a, b, __SAFE_CAST_PTR(volatile UInt16*,c)))
227*5e3eaea3SApple OSS Distributions 
228*5e3eaea3SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
229*5e3eaea3SApple OSS Distributions 
230*5e3eaea3SApple OSS Distributions /*!
231*5e3eaea3SApple OSS Distributions  * @function OSCompareAndSwap
232*5e3eaea3SApple OSS Distributions  *
233*5e3eaea3SApple OSS Distributions  * @abstract
234*5e3eaea3SApple OSS Distributions  * Compare and swap operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
235*5e3eaea3SApple OSS Distributions  *
236*5e3eaea3SApple OSS Distributions  * @discussion
237*5e3eaea3SApple OSS Distributions  * 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.
238*5e3eaea3SApple OSS Distributions  *
239*5e3eaea3SApple OSS Distributions  * 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.
240*5e3eaea3SApple OSS Distributions  *
241*5e3eaea3SApple OSS Distributions  * @param oldValue The value to compare at address.
242*5e3eaea3SApple OSS Distributions  * @param newValue The value to write to address if oldValue compares true.
243*5e3eaea3SApple OSS Distributions  * @param address The 4-byte aligned address of the data to update atomically.
244*5e3eaea3SApple OSS Distributions  * @result true if newValue was written to the address.
245*5e3eaea3SApple OSS Distributions  */
246*5e3eaea3SApple OSS Distributions extern Boolean OSCompareAndSwap(
247*5e3eaea3SApple OSS Distributions 	UInt32            oldValue,
248*5e3eaea3SApple OSS Distributions 	UInt32            newValue,
249*5e3eaea3SApple OSS Distributions 	volatile UInt32 * address);
250*5e3eaea3SApple OSS Distributions #define OSCompareAndSwap(a, b, c) \
251*5e3eaea3SApple OSS Distributions 	(OSCompareAndSwap(a, b, __SAFE_CAST_PTR(volatile UInt32*,c)))
252*5e3eaea3SApple OSS Distributions 
253*5e3eaea3SApple OSS Distributions /*!
254*5e3eaea3SApple OSS Distributions  * @function OSCompareAndSwapPtr
255*5e3eaea3SApple OSS Distributions  *
256*5e3eaea3SApple OSS Distributions  * @abstract
257*5e3eaea3SApple OSS Distributions  * Compare and swap operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
258*5e3eaea3SApple OSS Distributions  *
259*5e3eaea3SApple OSS Distributions  * @discussion
260*5e3eaea3SApple OSS Distributions  * 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.
261*5e3eaea3SApple OSS Distributions  *
262*5e3eaea3SApple OSS Distributions  * 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.
263*5e3eaea3SApple OSS Distributions  * @param oldValue The pointer value to compare at address.
264*5e3eaea3SApple OSS Distributions  * @param newValue The pointer value to write to address if oldValue compares true.
265*5e3eaea3SApple OSS Distributions  * @param address The pointer-size aligned address of the data to update atomically.
266*5e3eaea3SApple OSS Distributions  * @result true if newValue was written to the address.
267*5e3eaea3SApple OSS Distributions  */
268*5e3eaea3SApple OSS Distributions extern Boolean OSCompareAndSwapPtr(
269*5e3eaea3SApple OSS Distributions 	void            * oldValue,
270*5e3eaea3SApple OSS Distributions 	void            * newValue,
271*5e3eaea3SApple OSS Distributions 	void * volatile * address);
272*5e3eaea3SApple OSS Distributions #define OSCompareAndSwapPtr(a, b, c) \
273*5e3eaea3SApple OSS Distributions 	(OSCompareAndSwapPtr(a, b, __SAFE_CAST_PTR(void * volatile *,c)))
274*5e3eaea3SApple OSS Distributions 
275*5e3eaea3SApple OSS Distributions /*!
276*5e3eaea3SApple OSS Distributions  * @function OSAddAtomic
277*5e3eaea3SApple OSS Distributions  *
278*5e3eaea3SApple OSS Distributions  * @abstract
279*5e3eaea3SApple OSS Distributions  * 32-bit add operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
280*5e3eaea3SApple OSS Distributions  *
281*5e3eaea3SApple OSS Distributions  * @discussion
282*5e3eaea3SApple OSS Distributions  * The OSAddAtomic function adds the specified amount to the value at the specified address and returns the original value.
283*5e3eaea3SApple OSS Distributions  *
284*5e3eaea3SApple OSS Distributions  * 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. Previous incarnations of this function incorporated a memory barrier on systems with weakly-ordered memory architectures, but current versions contain no barriers.
285*5e3eaea3SApple OSS Distributions  * @param amount The amount to add.
286*5e3eaea3SApple OSS Distributions  * @param address The 4-byte aligned address of the value to update atomically.
287*5e3eaea3SApple OSS Distributions  * @result The value before the addition
288*5e3eaea3SApple OSS Distributions  */
289*5e3eaea3SApple OSS Distributions extern SInt32 OSAddAtomic(
290*5e3eaea3SApple OSS Distributions 	SInt32            amount,
291*5e3eaea3SApple OSS Distributions 	volatile SInt32 * address);
292*5e3eaea3SApple OSS Distributions #define OSAddAtomic(a, b) \
293*5e3eaea3SApple OSS Distributions 	(OSAddAtomic(a, __SAFE_CAST_PTR(volatile SInt32*,b)))
294*5e3eaea3SApple OSS Distributions 
295*5e3eaea3SApple OSS Distributions /*!
296*5e3eaea3SApple OSS Distributions  * @function OSAddAtomic16
297*5e3eaea3SApple OSS Distributions  *
298*5e3eaea3SApple OSS Distributions  * @abstract
299*5e3eaea3SApple OSS Distributions  * 16-bit add operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
300*5e3eaea3SApple OSS Distributions  *
301*5e3eaea3SApple OSS Distributions  * @discussion
302*5e3eaea3SApple OSS Distributions  * The OSAddAtomic16 function adds the specified amount to the value at the specified address and returns the original value.
303*5e3eaea3SApple OSS Distributions  *
304*5e3eaea3SApple OSS Distributions  * 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. Previous incarnations of this function incorporated a memory barrier on systems with weakly-ordered memory architectures, but current versions contain no barriers.
305*5e3eaea3SApple OSS Distributions  * @param address The 2-byte aligned address of the value to update atomically.
306*5e3eaea3SApple OSS Distributions  * @result The value before the addition
307*5e3eaea3SApple OSS Distributions  */
308*5e3eaea3SApple OSS Distributions extern SInt16 OSAddAtomic16(
309*5e3eaea3SApple OSS Distributions 	SInt32            amount,
310*5e3eaea3SApple OSS Distributions 	volatile SInt16 * address);
311*5e3eaea3SApple OSS Distributions 
312*5e3eaea3SApple OSS Distributions /*!
313*5e3eaea3SApple OSS Distributions  * @function OSAddAtomic8
314*5e3eaea3SApple OSS Distributions  *
315*5e3eaea3SApple OSS Distributions  * @abstract
316*5e3eaea3SApple OSS Distributions  * 8-bit add operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
317*5e3eaea3SApple OSS Distributions  *
318*5e3eaea3SApple OSS Distributions  * @discussion
319*5e3eaea3SApple OSS Distributions  * The OSAddAtomic8 function adds the specified amount to the value at the specified address and returns the original value.
320*5e3eaea3SApple OSS Distributions  *
321*5e3eaea3SApple OSS Distributions  * 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. Previous incarnations of this function incorporated a memory barrier on systems with weakly-ordered memory architectures, but current versions contain no barriers.
322*5e3eaea3SApple OSS Distributions  * @param amount The amount to add.
323*5e3eaea3SApple OSS Distributions  * @param address The address of the value to update atomically.
324*5e3eaea3SApple OSS Distributions  * @result The value before the addition.
325*5e3eaea3SApple OSS Distributions  */
326*5e3eaea3SApple OSS Distributions extern SInt8 OSAddAtomic8(
327*5e3eaea3SApple OSS Distributions 	SInt32           amount,
328*5e3eaea3SApple OSS Distributions 	volatile SInt8 * address);
329*5e3eaea3SApple OSS Distributions 
330*5e3eaea3SApple OSS Distributions /*!
331*5e3eaea3SApple OSS Distributions  * @function OSIncrementAtomic
332*5e3eaea3SApple OSS Distributions  *
333*5e3eaea3SApple OSS Distributions  * @abstract
334*5e3eaea3SApple OSS Distributions  * 32-bit increment operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
335*5e3eaea3SApple OSS Distributions  *
336*5e3eaea3SApple OSS Distributions  * @discussion
337*5e3eaea3SApple OSS Distributions  * The OSIncrementAtomic function increments the value at the specified address by one and returns the original value.
338*5e3eaea3SApple OSS Distributions  *
339*5e3eaea3SApple OSS Distributions  * 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.
340*5e3eaea3SApple OSS Distributions  * @param address The 4-byte aligned address of the value to update atomically.
341*5e3eaea3SApple OSS Distributions  * @result The value before the increment.
342*5e3eaea3SApple OSS Distributions  */
343*5e3eaea3SApple OSS Distributions extern SInt32 OSIncrementAtomic(volatile SInt32 * address);
344*5e3eaea3SApple OSS Distributions #define OSIncrementAtomic(a) \
345*5e3eaea3SApple OSS Distributions 	(OSIncrementAtomic(__SAFE_CAST_PTR(volatile SInt32*,a)))
346*5e3eaea3SApple OSS Distributions 
347*5e3eaea3SApple OSS Distributions /*!
348*5e3eaea3SApple OSS Distributions  * @function OSIncrementAtomic16
349*5e3eaea3SApple OSS Distributions  *
350*5e3eaea3SApple OSS Distributions  * @abstract
351*5e3eaea3SApple OSS Distributions  * 16-bit increment operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
352*5e3eaea3SApple OSS Distributions  *
353*5e3eaea3SApple OSS Distributions  * @discussion
354*5e3eaea3SApple OSS Distributions  * The OSIncrementAtomic16 function increments the value at the specified address by one and returns the original value.
355*5e3eaea3SApple OSS Distributions  *
356*5e3eaea3SApple OSS Distributions  * 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. Previous incarnations of this function incorporated a memory barrier on systems with weakly-ordered memory architectures, but current versions contain no barriers.
357*5e3eaea3SApple OSS Distributions  * @param address The 2-byte aligned address of the value to update atomically.
358*5e3eaea3SApple OSS Distributions  * @result The value before the increment.
359*5e3eaea3SApple OSS Distributions  */
360*5e3eaea3SApple OSS Distributions extern SInt16 OSIncrementAtomic16(volatile SInt16 * address);
361*5e3eaea3SApple OSS Distributions 
362*5e3eaea3SApple OSS Distributions /*!
363*5e3eaea3SApple OSS Distributions  * @function OSIncrementAtomic8
364*5e3eaea3SApple OSS Distributions  *
365*5e3eaea3SApple OSS Distributions  * @abstract
366*5e3eaea3SApple OSS Distributions  * 8-bit increment operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
367*5e3eaea3SApple OSS Distributions  *
368*5e3eaea3SApple OSS Distributions  * @discussion
369*5e3eaea3SApple OSS Distributions  * The OSIncrementAtomic8 function increments the value at the specified address by one and returns the original value.
370*5e3eaea3SApple OSS Distributions  *
371*5e3eaea3SApple OSS Distributions  * 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. Previous incarnations of this function incorporated a memory barrier on systems with weakly-ordered memory architectures, but current versions contain no barriers.
372*5e3eaea3SApple OSS Distributions  * @param address The address of the value to update atomically.
373*5e3eaea3SApple OSS Distributions  * @result The value before the increment.
374*5e3eaea3SApple OSS Distributions  */
375*5e3eaea3SApple OSS Distributions extern SInt8 OSIncrementAtomic8(volatile SInt8 * address);
376*5e3eaea3SApple OSS Distributions 
377*5e3eaea3SApple OSS Distributions /*!
378*5e3eaea3SApple OSS Distributions  * @function OSDecrementAtomic
379*5e3eaea3SApple OSS Distributions  *
380*5e3eaea3SApple OSS Distributions  * @abstract
381*5e3eaea3SApple OSS Distributions  * 32-bit decrement operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
382*5e3eaea3SApple OSS Distributions  *
383*5e3eaea3SApple OSS Distributions  * @discussion
384*5e3eaea3SApple OSS Distributions  * The OSDecrementAtomic function decrements the value at the specified address by one and returns the original value.
385*5e3eaea3SApple OSS Distributions  *
386*5e3eaea3SApple OSS Distributions  * 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. Previous incarnations of this function incorporated a memory barrier on systems with weakly-ordered memory architectures, but current versions contain no barriers.
387*5e3eaea3SApple OSS Distributions  * @param address The 4-byte aligned address of the value to update atomically.
388*5e3eaea3SApple OSS Distributions  * @result The value before the decrement.
389*5e3eaea3SApple OSS Distributions  */
390*5e3eaea3SApple OSS Distributions extern SInt32 OSDecrementAtomic(volatile SInt32 * address);
391*5e3eaea3SApple OSS Distributions #define OSDecrementAtomic(a) \
392*5e3eaea3SApple OSS Distributions 	(OSDecrementAtomic(__SAFE_CAST_PTR(volatile SInt32*,a)))
393*5e3eaea3SApple OSS Distributions 
394*5e3eaea3SApple OSS Distributions /*!
395*5e3eaea3SApple OSS Distributions  * @function OSDecrementAtomic16
396*5e3eaea3SApple OSS Distributions  *
397*5e3eaea3SApple OSS Distributions  * @abstract
398*5e3eaea3SApple OSS Distributions  * 16-bit decrement operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
399*5e3eaea3SApple OSS Distributions  *
400*5e3eaea3SApple OSS Distributions  * @discussion
401*5e3eaea3SApple OSS Distributions  * The OSDecrementAtomic16 function decrements the value at the specified address by one and returns the original value.
402*5e3eaea3SApple OSS Distributions  *
403*5e3eaea3SApple OSS Distributions  * 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. Previous incarnations of this function incorporated a memory barrier on systems with weakly-ordered memory architectures, but current versions contain no barriers.
404*5e3eaea3SApple OSS Distributions  * @param address The 2-byte aligned address of the value to update atomically.
405*5e3eaea3SApple OSS Distributions  * @result The value before the decrement.
406*5e3eaea3SApple OSS Distributions  */
407*5e3eaea3SApple OSS Distributions extern SInt16 OSDecrementAtomic16(volatile SInt16 * address);
408*5e3eaea3SApple OSS Distributions 
409*5e3eaea3SApple OSS Distributions /*!
410*5e3eaea3SApple OSS Distributions  * @function OSDecrementAtomic8
411*5e3eaea3SApple OSS Distributions  *
412*5e3eaea3SApple OSS Distributions  * @abstract
413*5e3eaea3SApple OSS Distributions  * 8-bit decrement operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
414*5e3eaea3SApple OSS Distributions  *
415*5e3eaea3SApple OSS Distributions  * @discussion
416*5e3eaea3SApple OSS Distributions  * The OSDecrementAtomic8 function decrements the value at the specified address by one and returns the original value.
417*5e3eaea3SApple OSS Distributions  *
418*5e3eaea3SApple OSS Distributions  * 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. Previous incarnations of this function incorporated a memory barrier on systems with weakly-ordered memory architectures, but current versions contain no barriers.
419*5e3eaea3SApple OSS Distributions  * @param address The address of the value to update atomically.
420*5e3eaea3SApple OSS Distributions  * @result The value before the decrement.
421*5e3eaea3SApple OSS Distributions  */
422*5e3eaea3SApple OSS Distributions extern SInt8 OSDecrementAtomic8(volatile SInt8 * address);
423*5e3eaea3SApple OSS Distributions 
424*5e3eaea3SApple OSS Distributions /*!
425*5e3eaea3SApple OSS Distributions  * @function OSBitAndAtomic
426*5e3eaea3SApple OSS Distributions  *
427*5e3eaea3SApple OSS Distributions  * @abstract
428*5e3eaea3SApple OSS Distributions  * 32-bit logical and operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
429*5e3eaea3SApple OSS Distributions  *
430*5e3eaea3SApple OSS Distributions  * @discussion
431*5e3eaea3SApple OSS Distributions  * The OSBitAndAtomic function logically ands the bits of the specified mask into the value at the specified address and returns the original value.
432*5e3eaea3SApple OSS Distributions  *
433*5e3eaea3SApple OSS Distributions  * 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. Previous incarnations of this function incorporated a memory barrier on systems with weakly-ordered memory architectures, but current versions contain no barriers..
434*5e3eaea3SApple OSS Distributions  * @param mask The mask to logically and with the value.
435*5e3eaea3SApple OSS Distributions  * @param address The 4-byte aligned address of the value to update atomically.
436*5e3eaea3SApple OSS Distributions  * @result The value before the bitwise operation
437*5e3eaea3SApple OSS Distributions  */
438*5e3eaea3SApple OSS Distributions extern UInt32 OSBitAndAtomic(
439*5e3eaea3SApple OSS Distributions 	UInt32            mask,
440*5e3eaea3SApple OSS Distributions 	volatile UInt32 * address);
441*5e3eaea3SApple OSS Distributions #define OSBitAndAtomic(a, b) \
442*5e3eaea3SApple OSS Distributions 	(OSBitAndAtomic(a, __SAFE_CAST_PTR(volatile UInt32*,b)))
443*5e3eaea3SApple OSS Distributions 
444*5e3eaea3SApple OSS Distributions /*!
445*5e3eaea3SApple OSS Distributions  * @function OSBitAndAtomic16
446*5e3eaea3SApple OSS Distributions  *
447*5e3eaea3SApple OSS Distributions  * @abstract
448*5e3eaea3SApple OSS Distributions  * 16-bit logical and operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
449*5e3eaea3SApple OSS Distributions  *
450*5e3eaea3SApple OSS Distributions  * @discussion
451*5e3eaea3SApple OSS Distributions  * The OSBitAndAtomic16 function logically ands the bits of the specified mask into the value at the specified address and returns the original value.
452*5e3eaea3SApple OSS Distributions  *
453*5e3eaea3SApple OSS Distributions  * 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.
454*5e3eaea3SApple OSS Distributions  * @param mask The mask to logically and with the value.
455*5e3eaea3SApple OSS Distributions  * @param address The 2-byte aligned address of the value to update atomically.
456*5e3eaea3SApple OSS Distributions  * @result The value before the bitwise operation.
457*5e3eaea3SApple OSS Distributions  */
458*5e3eaea3SApple OSS Distributions extern UInt16 OSBitAndAtomic16(
459*5e3eaea3SApple OSS Distributions 	UInt32            mask,
460*5e3eaea3SApple OSS Distributions 	volatile UInt16 * address);
461*5e3eaea3SApple OSS Distributions 
462*5e3eaea3SApple OSS Distributions /*!
463*5e3eaea3SApple OSS Distributions  * @function OSBitAndAtomic8
464*5e3eaea3SApple OSS Distributions  *
465*5e3eaea3SApple OSS Distributions  * @abstract
466*5e3eaea3SApple OSS Distributions  * 8-bit logical and operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
467*5e3eaea3SApple OSS Distributions  *
468*5e3eaea3SApple OSS Distributions  * @discussion
469*5e3eaea3SApple OSS Distributions  * The OSBitAndAtomic8 function logically ands the bits of the specified mask into the value at the specified address and returns the original value.
470*5e3eaea3SApple OSS Distributions  *
471*5e3eaea3SApple OSS Distributions  * 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.
472*5e3eaea3SApple OSS Distributions  * @param mask The mask to logically and with the value.
473*5e3eaea3SApple OSS Distributions  * @param address The address of the value to update atomically.
474*5e3eaea3SApple OSS Distributions  * @result The value before the bitwise operation.
475*5e3eaea3SApple OSS Distributions  */
476*5e3eaea3SApple OSS Distributions extern UInt8 OSBitAndAtomic8(
477*5e3eaea3SApple OSS Distributions 	UInt32           mask,
478*5e3eaea3SApple OSS Distributions 	volatile UInt8 * address);
479*5e3eaea3SApple OSS Distributions 
480*5e3eaea3SApple OSS Distributions /*!
481*5e3eaea3SApple OSS Distributions  * @function OSBitOrAtomic
482*5e3eaea3SApple OSS Distributions  *
483*5e3eaea3SApple OSS Distributions  * @abstract
484*5e3eaea3SApple OSS Distributions  * 32-bit logical or operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
485*5e3eaea3SApple OSS Distributions  *
486*5e3eaea3SApple OSS Distributions  * @discussion
487*5e3eaea3SApple OSS Distributions  * The OSBitOrAtomic function logically ors the bits of the specified mask into the value at the specified address and returns the original value.
488*5e3eaea3SApple OSS Distributions  *
489*5e3eaea3SApple OSS Distributions  * 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.
490*5e3eaea3SApple OSS Distributions  * @param mask The mask to logically or with the value.
491*5e3eaea3SApple OSS Distributions  * @param address The 4-byte aligned address of the value to update atomically.
492*5e3eaea3SApple OSS Distributions  * @result The value before the bitwise operation.
493*5e3eaea3SApple OSS Distributions  */
494*5e3eaea3SApple OSS Distributions extern UInt32 OSBitOrAtomic(
495*5e3eaea3SApple OSS Distributions 	UInt32            mask,
496*5e3eaea3SApple OSS Distributions 	volatile UInt32 * address);
497*5e3eaea3SApple OSS Distributions #define OSBitOrAtomic(a, b) \
498*5e3eaea3SApple OSS Distributions 	(OSBitOrAtomic(a, __SAFE_CAST_PTR(volatile UInt32*,b)))
499*5e3eaea3SApple OSS Distributions 
500*5e3eaea3SApple OSS Distributions /*!
501*5e3eaea3SApple OSS Distributions  * @function OSBitOrAtomic16
502*5e3eaea3SApple OSS Distributions  *
503*5e3eaea3SApple OSS Distributions  * @abstract
504*5e3eaea3SApple OSS Distributions  * 16-bit logical or operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
505*5e3eaea3SApple OSS Distributions  *
506*5e3eaea3SApple OSS Distributions  * @discussion
507*5e3eaea3SApple OSS Distributions  * The OSBitOrAtomic16 function logically ors the bits of the specified mask into the value at the specified address and returns the original value.
508*5e3eaea3SApple OSS Distributions  *
509*5e3eaea3SApple OSS Distributions  * 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.
510*5e3eaea3SApple OSS Distributions  * @param mask The mask to logically or with the value.
511*5e3eaea3SApple OSS Distributions  * @param address The 2-byte aligned address of the value to update atomically.
512*5e3eaea3SApple OSS Distributions  * @result The value before the bitwise operation.
513*5e3eaea3SApple OSS Distributions  */
514*5e3eaea3SApple OSS Distributions extern UInt16 OSBitOrAtomic16(
515*5e3eaea3SApple OSS Distributions 	UInt32            mask,
516*5e3eaea3SApple OSS Distributions 	volatile UInt16 * address);
517*5e3eaea3SApple OSS Distributions 
518*5e3eaea3SApple OSS Distributions /*!
519*5e3eaea3SApple OSS Distributions  * @function OSBitOrAtomic8
520*5e3eaea3SApple OSS Distributions  *
521*5e3eaea3SApple OSS Distributions  * @abstract
522*5e3eaea3SApple OSS Distributions  * 8-bit logical or operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
523*5e3eaea3SApple OSS Distributions  *
524*5e3eaea3SApple OSS Distributions  * 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.
525*5e3eaea3SApple OSS Distributions  *
526*5e3eaea3SApple OSS Distributions  * @discussion
527*5e3eaea3SApple OSS Distributions  * The OSBitOrAtomic8 function logically ors the bits of the specified mask into the value at the specified address and returns the original value.
528*5e3eaea3SApple OSS Distributions  * @param mask The mask to logically or with the value.
529*5e3eaea3SApple OSS Distributions  * @param address The address of the value to update atomically.
530*5e3eaea3SApple OSS Distributions  * @result The value before the bitwise operation.
531*5e3eaea3SApple OSS Distributions  */
532*5e3eaea3SApple OSS Distributions extern UInt8 OSBitOrAtomic8(
533*5e3eaea3SApple OSS Distributions 	UInt32           mask,
534*5e3eaea3SApple OSS Distributions 	volatile UInt8 * address);
535*5e3eaea3SApple OSS Distributions 
536*5e3eaea3SApple OSS Distributions /*!
537*5e3eaea3SApple OSS Distributions  * @function OSBitXorAtomic
538*5e3eaea3SApple OSS Distributions  *
539*5e3eaea3SApple OSS Distributions  * @abstract
540*5e3eaea3SApple OSS Distributions  * 32-bit logical xor operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
541*5e3eaea3SApple OSS Distributions  *
542*5e3eaea3SApple OSS Distributions  * 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.
543*5e3eaea3SApple OSS Distributions  *
544*5e3eaea3SApple OSS Distributions  * @discussion
545*5e3eaea3SApple OSS Distributions  * The OSBitXorAtomic function logically xors the bits of the specified mask into the value at the specified address and returns the original value.
546*5e3eaea3SApple OSS Distributions  * @param mask The mask to logically or with the value.
547*5e3eaea3SApple OSS Distributions  * @param address The 4-byte aligned address of the value to update atomically.
548*5e3eaea3SApple OSS Distributions  * @result The value before the bitwise operation.
549*5e3eaea3SApple OSS Distributions  */
550*5e3eaea3SApple OSS Distributions extern UInt32 OSBitXorAtomic(
551*5e3eaea3SApple OSS Distributions 	UInt32            mask,
552*5e3eaea3SApple OSS Distributions 	volatile UInt32 * address);
553*5e3eaea3SApple OSS Distributions #define OSBitXorAtomic(a, b) \
554*5e3eaea3SApple OSS Distributions 	(OSBitXorAtomic(a, __SAFE_CAST_PTR(volatile UInt32*,b)))
555*5e3eaea3SApple OSS Distributions 
556*5e3eaea3SApple OSS Distributions /*!
557*5e3eaea3SApple OSS Distributions  * @function OSBitXorAtomic16
558*5e3eaea3SApple OSS Distributions  *
559*5e3eaea3SApple OSS Distributions  * @abstract
560*5e3eaea3SApple OSS Distributions  * 16-bit logical xor operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
561*5e3eaea3SApple OSS Distributions  *
562*5e3eaea3SApple OSS Distributions  * @discussion
563*5e3eaea3SApple OSS Distributions  * The OSBitXorAtomic16 function logically xors the bits of the specified mask into the value at the specified address and returns the original value.
564*5e3eaea3SApple OSS Distributions  *
565*5e3eaea3SApple OSS Distributions  * 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.
566*5e3eaea3SApple OSS Distributions  * @param mask The mask to logically or with the value.
567*5e3eaea3SApple OSS Distributions  * @param address The 2-byte aligned address of the value to update atomically.
568*5e3eaea3SApple OSS Distributions  * @result The value before the bitwise operation.
569*5e3eaea3SApple OSS Distributions  */
570*5e3eaea3SApple OSS Distributions extern UInt16 OSBitXorAtomic16(
571*5e3eaea3SApple OSS Distributions 	UInt32            mask,
572*5e3eaea3SApple OSS Distributions 	volatile UInt16 * address);
573*5e3eaea3SApple OSS Distributions 
574*5e3eaea3SApple OSS Distributions /*!
575*5e3eaea3SApple OSS Distributions  * @function OSBitXorAtomic8
576*5e3eaea3SApple OSS Distributions  *
577*5e3eaea3SApple OSS Distributions  * @abstract
578*5e3eaea3SApple OSS Distributions  * 8-bit logical xor operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
579*5e3eaea3SApple OSS Distributions  *
580*5e3eaea3SApple OSS Distributions  * 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.
581*5e3eaea3SApple OSS Distributions  *
582*5e3eaea3SApple OSS Distributions  * @discussion
583*5e3eaea3SApple OSS Distributions  * The OSBitXorAtomic8 function logically xors the bits of the specified mask into the value at the specified address and returns the original value.
584*5e3eaea3SApple OSS Distributions  * @param mask The mask to logically or with the value.
585*5e3eaea3SApple OSS Distributions  * @param address The address of the value to update atomically.
586*5e3eaea3SApple OSS Distributions  * @result The value before the bitwise operation.
587*5e3eaea3SApple OSS Distributions  */
588*5e3eaea3SApple OSS Distributions extern UInt8 OSBitXorAtomic8(
589*5e3eaea3SApple OSS Distributions 	UInt32           mask,
590*5e3eaea3SApple OSS Distributions 	volatile UInt8 * address);
591*5e3eaea3SApple OSS Distributions 
592*5e3eaea3SApple OSS Distributions /*!
593*5e3eaea3SApple OSS Distributions  * @function OSTestAndSet
594*5e3eaea3SApple OSS Distributions  *
595*5e3eaea3SApple OSS Distributions  * @abstract
596*5e3eaea3SApple OSS Distributions  * Bit test and set operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
597*5e3eaea3SApple OSS Distributions  *
598*5e3eaea3SApple OSS Distributions  * 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.
599*5e3eaea3SApple OSS Distributions  *
600*5e3eaea3SApple OSS Distributions  * @discussion
601*5e3eaea3SApple OSS Distributions  * 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.
602*5e3eaea3SApple OSS Distributions  * @param bit The bit number in the range 0 through 7. Bit 0 is the most significant.
603*5e3eaea3SApple OSS Distributions  * @param startAddress The address of the byte to update atomically.
604*5e3eaea3SApple OSS Distributions  * @result true if the bit was already set, false otherwise.
605*5e3eaea3SApple OSS Distributions  */
606*5e3eaea3SApple OSS Distributions extern Boolean OSTestAndSet(
607*5e3eaea3SApple OSS Distributions 	UInt32           bit,
608*5e3eaea3SApple OSS Distributions 	volatile UInt8 * startAddress);
609*5e3eaea3SApple OSS Distributions 
610*5e3eaea3SApple OSS Distributions /*!
611*5e3eaea3SApple OSS Distributions  * @function OSTestAndClear
612*5e3eaea3SApple OSS Distributions  *
613*5e3eaea3SApple OSS Distributions  * @abstract
614*5e3eaea3SApple OSS Distributions  * Bit test and clear operation, performed atomically with respect to all devices that participate in the coherency architecture of the platform.
615*5e3eaea3SApple OSS Distributions  *
616*5e3eaea3SApple OSS Distributions  * @discussion
617*5e3eaea3SApple OSS Distributions  * 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.
618*5e3eaea3SApple OSS Distributions  *
619*5e3eaea3SApple OSS Distributions  * 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.
620*5e3eaea3SApple OSS Distributions  * @param bit The bit number in the range 0 through 7. Bit 0 is the most significant.
621*5e3eaea3SApple OSS Distributions  * @param startAddress The address of the byte to update atomically.
622*5e3eaea3SApple OSS Distributions  * @result true if the bit was already clear, false otherwise.
623*5e3eaea3SApple OSS Distributions  */
624*5e3eaea3SApple OSS Distributions extern Boolean OSTestAndClear(
625*5e3eaea3SApple OSS Distributions 	UInt32           bit,
626*5e3eaea3SApple OSS Distributions 	volatile UInt8 * startAddress);
627*5e3eaea3SApple OSS Distributions 
628*5e3eaea3SApple OSS Distributions /*!
629*5e3eaea3SApple OSS Distributions  * @defined OS_SPINLOCK_INIT
630*5e3eaea3SApple OSS Distributions  *
631*5e3eaea3SApple OSS Distributions  * @abstract
632*5e3eaea3SApple OSS Distributions  * The default value for an OSSpinLock.
633*5e3eaea3SApple OSS Distributions  *
634*5e3eaea3SApple OSS Distributions  * @discussion
635*5e3eaea3SApple OSS Distributions  * The convention is that unlocked is zero, locked is nonzero.
636*5e3eaea3SApple OSS Distributions  */
637*5e3eaea3SApple OSS Distributions #define OS_SPINLOCK_INIT 0
638*5e3eaea3SApple OSS Distributions 
639*5e3eaea3SApple OSS Distributions /*!
640*5e3eaea3SApple OSS Distributions  * @typedef OSSpinLock
641*5e3eaea3SApple OSS Distributions  *
642*5e3eaea3SApple OSS Distributions  * @abstract
643*5e3eaea3SApple OSS Distributions  * Data type for a spinlock.
644*5e3eaea3SApple OSS Distributions  *
645*5e3eaea3SApple OSS Distributions  * @discussion
646*5e3eaea3SApple OSS Distributions  * You should always initialize a spinlock to OS_SPINLOCK_INIT before using it.
647*5e3eaea3SApple OSS Distributions  */
648*5e3eaea3SApple OSS Distributions typedef SInt32 OSSpinLock;
649*5e3eaea3SApple OSS Distributions 
650*5e3eaea3SApple OSS Distributions #ifdef PRIVATE
651*5e3eaea3SApple OSS Distributions /*!
652*5e3eaea3SApple OSS Distributions  * @function OSSpinLockTry
653*5e3eaea3SApple OSS Distributions  *
654*5e3eaea3SApple OSS Distributions  * @abstract
655*5e3eaea3SApple OSS Distributions  * Locks a spinlock if it would not block.
656*5e3eaea3SApple OSS Distributions  *
657*5e3eaea3SApple OSS Distributions  * @discussion
658*5e3eaea3SApple OSS Distributions  * Multiprocessor locks used within the shared memory area between the kernel and event system.  These must work in both user and kernel mode.
659*5e3eaea3SApple OSS Distributions  *
660*5e3eaea3SApple OSS Distributions  * @result
661*5e3eaea3SApple OSS Distributions  * Returns false if the lock was already held by another thread, true if it took the lock successfully.
662*5e3eaea3SApple OSS Distributions  */
663*5e3eaea3SApple OSS Distributions extern Boolean OSSpinLockTry(volatile OSSpinLock * lock);
664*5e3eaea3SApple OSS Distributions 
665*5e3eaea3SApple OSS Distributions /*!
666*5e3eaea3SApple OSS Distributions  * @function OSSpinLockUnlock
667*5e3eaea3SApple OSS Distributions  *
668*5e3eaea3SApple OSS Distributions  * @abstract
669*5e3eaea3SApple OSS Distributions  * Unlocks a spinlock.
670*5e3eaea3SApple OSS Distributions  *
671*5e3eaea3SApple OSS Distributions  * @discussion
672*5e3eaea3SApple OSS Distributions  * Unlocks a spinlock.
673*5e3eaea3SApple OSS Distributions  */
674*5e3eaea3SApple OSS Distributions extern void OSSpinLockUnlock(volatile OSSpinLock * lock);
675*5e3eaea3SApple OSS Distributions #endif /* PRIVATE */
676*5e3eaea3SApple OSS Distributions 
677*5e3eaea3SApple OSS Distributions /*!
678*5e3eaea3SApple OSS Distributions  * @function OSSynchronizeIO
679*5e3eaea3SApple OSS Distributions  *
680*5e3eaea3SApple OSS Distributions  * @abstract
681*5e3eaea3SApple OSS Distributions  * The OSSynchronizeIO routine ensures orderly load and store operations to noncached memory mapped I/O devices.
682*5e3eaea3SApple OSS Distributions  *
683*5e3eaea3SApple OSS Distributions  * @discussion
684*5e3eaea3SApple OSS Distributions  * The OSSynchronizeIO routine ensures orderly load and store operations to noncached memory mapped I/O devices. It executes the eieio instruction on PowerPC processors.
685*5e3eaea3SApple OSS Distributions  */
686*5e3eaea3SApple OSS Distributions #if defined(__arm__) || defined(__arm64__)
687*5e3eaea3SApple OSS Distributions extern void OSSynchronizeIO(void);
688*5e3eaea3SApple OSS Distributions #else
689*5e3eaea3SApple OSS Distributions static __inline__ void
OSSynchronizeIO(void)690*5e3eaea3SApple OSS Distributions OSSynchronizeIO(void)
691*5e3eaea3SApple OSS Distributions {
692*5e3eaea3SApple OSS Distributions }
693*5e3eaea3SApple OSS Distributions #endif
694*5e3eaea3SApple OSS Distributions 
695*5e3eaea3SApple OSS Distributions #if     defined(KERNEL_PRIVATE)
696*5e3eaea3SApple OSS Distributions 
697*5e3eaea3SApple OSS Distributions #if     defined(__arm__) || defined(__arm64__)
698*5e3eaea3SApple OSS Distributions static inline void
OSMemoryBarrier(void)699*5e3eaea3SApple OSS Distributions OSMemoryBarrier(void)
700*5e3eaea3SApple OSS Distributions {
701*5e3eaea3SApple OSS Distributions 	__asm__ volatile ("dmb ish" ::: "memory");
702*5e3eaea3SApple OSS Distributions }
703*5e3eaea3SApple OSS Distributions #elif defined(__i386__) || defined(__x86_64__)
704*5e3eaea3SApple OSS Distributions #if     defined(XNU_KERNEL_PRIVATE)
705*5e3eaea3SApple OSS Distributions static inline void
OSMemoryBarrier(void)706*5e3eaea3SApple OSS Distributions OSMemoryBarrier(void)
707*5e3eaea3SApple OSS Distributions {
708*5e3eaea3SApple OSS Distributions 	__asm__ volatile ("mfence" ::: "memory");
709*5e3eaea3SApple OSS Distributions }
710*5e3eaea3SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
711*5e3eaea3SApple OSS Distributions #endif
712*5e3eaea3SApple OSS Distributions 
713*5e3eaea3SApple OSS Distributions #endif /* KERNEL_PRIVATE */
714*5e3eaea3SApple OSS Distributions 
715*5e3eaea3SApple OSS Distributions #if defined(__cplusplus)
716*5e3eaea3SApple OSS Distributions }
717*5e3eaea3SApple OSS Distributions #endif
718*5e3eaea3SApple OSS Distributions 
719*5e3eaea3SApple OSS Distributions #endif /* ! _OS_OSATOMIC_H */
720