xref: /xnu-8796.141.3/libkern/gen/OSAtomicOperations.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2000-2015 Apple Computer, Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions  *
15*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions  *
18*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions  * limitations under the License.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions 
29*1b191cb5SApple OSS Distributions #include <libkern/OSAtomic.h>
30*1b191cb5SApple OSS Distributions #include <kern/debug.h>
31*1b191cb5SApple OSS Distributions #include <machine/atomic.h>
32*1b191cb5SApple OSS Distributions #include <stdbool.h>
33*1b191cb5SApple OSS Distributions 
34*1b191cb5SApple OSS Distributions #ifndef NULL
35*1b191cb5SApple OSS Distributions #define NULL ((void *)0)
36*1b191cb5SApple OSS Distributions #endif
37*1b191cb5SApple OSS Distributions 
38*1b191cb5SApple OSS Distributions #define ATOMIC_DEBUG DEBUG
39*1b191cb5SApple OSS Distributions 
40*1b191cb5SApple OSS Distributions #if ATOMIC_DEBUG
41*1b191cb5SApple OSS Distributions #define ALIGN_TEST(p, t) do{if((uintptr_t)p&(sizeof(t)-1)) panic("Unaligned atomic pointer %p",p);}while(0)
42*1b191cb5SApple OSS Distributions #else
43*1b191cb5SApple OSS Distributions #define ALIGN_TEST(p, t) do{}while(0)
44*1b191cb5SApple OSS Distributions #endif
45*1b191cb5SApple OSS Distributions 
46*1b191cb5SApple OSS Distributions /*
47*1b191cb5SApple OSS Distributions  * atomic operations
48*1b191cb5SApple OSS Distributions  *	These are _the_ atomic operations, now implemented via compiler built-ins.
49*1b191cb5SApple OSS Distributions  *	It is expected that this C implementation is a candidate for Link-Time-
50*1b191cb5SApple OSS Distributions  *	Optimization inlining, whereas the assembler implementations they replace
51*1b191cb5SApple OSS Distributions  *	were not.
52*1b191cb5SApple OSS Distributions  */
53*1b191cb5SApple OSS Distributions 
54*1b191cb5SApple OSS Distributions #undef OSCompareAndSwap8
55*1b191cb5SApple OSS Distributions Boolean
OSCompareAndSwap8(UInt8 oldValue,UInt8 newValue,volatile UInt8 * address)56*1b191cb5SApple OSS Distributions OSCompareAndSwap8(UInt8 oldValue, UInt8 newValue, volatile UInt8 *address)
57*1b191cb5SApple OSS Distributions {
58*1b191cb5SApple OSS Distributions 	return (Boolean)os_atomic_cmpxchg(address, oldValue, newValue, acq_rel);
59*1b191cb5SApple OSS Distributions }
60*1b191cb5SApple OSS Distributions 
61*1b191cb5SApple OSS Distributions #undef OSCompareAndSwap16
62*1b191cb5SApple OSS Distributions Boolean
OSCompareAndSwap16(UInt16 oldValue,UInt16 newValue,volatile UInt16 * address)63*1b191cb5SApple OSS Distributions OSCompareAndSwap16(UInt16 oldValue, UInt16 newValue, volatile UInt16 *address)
64*1b191cb5SApple OSS Distributions {
65*1b191cb5SApple OSS Distributions 	return (Boolean)os_atomic_cmpxchg(address, oldValue, newValue, acq_rel);
66*1b191cb5SApple OSS Distributions }
67*1b191cb5SApple OSS Distributions 
68*1b191cb5SApple OSS Distributions #undef OSCompareAndSwap
69*1b191cb5SApple OSS Distributions Boolean
OSCompareAndSwap(UInt32 oldValue,UInt32 newValue,volatile UInt32 * address)70*1b191cb5SApple OSS Distributions OSCompareAndSwap(UInt32 oldValue, UInt32 newValue, volatile UInt32 *address)
71*1b191cb5SApple OSS Distributions {
72*1b191cb5SApple OSS Distributions 	ALIGN_TEST(address, UInt32);
73*1b191cb5SApple OSS Distributions 	return (Boolean)os_atomic_cmpxchg(address, oldValue, newValue, acq_rel);
74*1b191cb5SApple OSS Distributions }
75*1b191cb5SApple OSS Distributions 
76*1b191cb5SApple OSS Distributions #undef OSCompareAndSwap64
77*1b191cb5SApple OSS Distributions Boolean
OSCompareAndSwap64(UInt64 oldValue,UInt64 newValue,volatile UInt64 * address)78*1b191cb5SApple OSS Distributions OSCompareAndSwap64(UInt64 oldValue, UInt64 newValue, volatile UInt64 *address)
79*1b191cb5SApple OSS Distributions {
80*1b191cb5SApple OSS Distributions 	/*
81*1b191cb5SApple OSS Distributions 	 * _Atomic uint64 requires 8-byte alignment on all architectures.
82*1b191cb5SApple OSS Distributions 	 * This silences the compiler cast warning.  ALIGN_TEST() verifies
83*1b191cb5SApple OSS Distributions 	 * that the cast was legal, if defined.
84*1b191cb5SApple OSS Distributions 	 */
85*1b191cb5SApple OSS Distributions 	_Atomic UInt64 *aligned_addr = (_Atomic UInt64 *)(uintptr_t)address;
86*1b191cb5SApple OSS Distributions 
87*1b191cb5SApple OSS Distributions 	ALIGN_TEST(address, UInt64);
88*1b191cb5SApple OSS Distributions 	return (Boolean)os_atomic_cmpxchg(aligned_addr, oldValue, newValue, acq_rel);
89*1b191cb5SApple OSS Distributions }
90*1b191cb5SApple OSS Distributions 
91*1b191cb5SApple OSS Distributions #undef OSCompareAndSwapPtr
92*1b191cb5SApple OSS Distributions Boolean
OSCompareAndSwapPtr(void * oldValue,void * newValue,void * volatile * address)93*1b191cb5SApple OSS Distributions OSCompareAndSwapPtr(void *oldValue, void *newValue, void * volatile *address)
94*1b191cb5SApple OSS Distributions {
95*1b191cb5SApple OSS Distributions 	return (Boolean)os_atomic_cmpxchg(address, oldValue, newValue, acq_rel);
96*1b191cb5SApple OSS Distributions }
97*1b191cb5SApple OSS Distributions 
98*1b191cb5SApple OSS Distributions SInt8
OSAddAtomic8(SInt32 amount,volatile SInt8 * address)99*1b191cb5SApple OSS Distributions OSAddAtomic8(SInt32 amount, volatile SInt8 *address)
100*1b191cb5SApple OSS Distributions {
101*1b191cb5SApple OSS Distributions 	return os_atomic_add_orig(address, (SInt8)amount, relaxed);
102*1b191cb5SApple OSS Distributions }
103*1b191cb5SApple OSS Distributions 
104*1b191cb5SApple OSS Distributions SInt16
OSAddAtomic16(SInt32 amount,volatile SInt16 * address)105*1b191cb5SApple OSS Distributions OSAddAtomic16(SInt32 amount, volatile SInt16 *address)
106*1b191cb5SApple OSS Distributions {
107*1b191cb5SApple OSS Distributions 	return os_atomic_add_orig(address, (SInt16)amount, relaxed);
108*1b191cb5SApple OSS Distributions }
109*1b191cb5SApple OSS Distributions 
110*1b191cb5SApple OSS Distributions #undef OSAddAtomic
111*1b191cb5SApple OSS Distributions SInt32
OSAddAtomic(SInt32 amount,volatile SInt32 * address)112*1b191cb5SApple OSS Distributions OSAddAtomic(SInt32 amount, volatile SInt32 *address)
113*1b191cb5SApple OSS Distributions {
114*1b191cb5SApple OSS Distributions 	ALIGN_TEST(address, UInt32);
115*1b191cb5SApple OSS Distributions 	return os_atomic_add_orig(address, amount, relaxed);
116*1b191cb5SApple OSS Distributions }
117*1b191cb5SApple OSS Distributions 
118*1b191cb5SApple OSS Distributions #undef OSAddAtomic64
119*1b191cb5SApple OSS Distributions SInt64
OSAddAtomic64(SInt64 amount,volatile SInt64 * address)120*1b191cb5SApple OSS Distributions OSAddAtomic64(SInt64 amount, volatile SInt64 *address)
121*1b191cb5SApple OSS Distributions {
122*1b191cb5SApple OSS Distributions 	_Atomic SInt64* aligned_address = (_Atomic SInt64*)(uintptr_t)address;
123*1b191cb5SApple OSS Distributions 
124*1b191cb5SApple OSS Distributions 	ALIGN_TEST(address, SInt64);
125*1b191cb5SApple OSS Distributions 	return os_atomic_add_orig(aligned_address, amount, relaxed);
126*1b191cb5SApple OSS Distributions }
127*1b191cb5SApple OSS Distributions 
128*1b191cb5SApple OSS Distributions #undef OSAddAtomicLong
129*1b191cb5SApple OSS Distributions long
OSAddAtomicLong(long theAmount,volatile long * address)130*1b191cb5SApple OSS Distributions OSAddAtomicLong(long theAmount, volatile long *address)
131*1b191cb5SApple OSS Distributions {
132*1b191cb5SApple OSS Distributions 	return os_atomic_add_orig(address, theAmount, relaxed);
133*1b191cb5SApple OSS Distributions }
134*1b191cb5SApple OSS Distributions 
135*1b191cb5SApple OSS Distributions #undef OSIncrementAtomic
136*1b191cb5SApple OSS Distributions SInt32
OSIncrementAtomic(volatile SInt32 * value)137*1b191cb5SApple OSS Distributions OSIncrementAtomic(volatile SInt32 * value)
138*1b191cb5SApple OSS Distributions {
139*1b191cb5SApple OSS Distributions 	return os_atomic_inc_orig(value, relaxed);
140*1b191cb5SApple OSS Distributions }
141*1b191cb5SApple OSS Distributions 
142*1b191cb5SApple OSS Distributions #undef OSDecrementAtomic
143*1b191cb5SApple OSS Distributions SInt32
OSDecrementAtomic(volatile SInt32 * value)144*1b191cb5SApple OSS Distributions OSDecrementAtomic(volatile SInt32 * value)
145*1b191cb5SApple OSS Distributions {
146*1b191cb5SApple OSS Distributions 	return os_atomic_dec_orig(value, relaxed);
147*1b191cb5SApple OSS Distributions }
148*1b191cb5SApple OSS Distributions 
149*1b191cb5SApple OSS Distributions #undef OSBitAndAtomic
150*1b191cb5SApple OSS Distributions UInt32
OSBitAndAtomic(UInt32 mask,volatile UInt32 * value)151*1b191cb5SApple OSS Distributions OSBitAndAtomic(UInt32 mask, volatile UInt32 * value)
152*1b191cb5SApple OSS Distributions {
153*1b191cb5SApple OSS Distributions 	return os_atomic_and_orig(value, mask, relaxed);
154*1b191cb5SApple OSS Distributions }
155*1b191cb5SApple OSS Distributions 
156*1b191cb5SApple OSS Distributions #undef OSBitOrAtomic
157*1b191cb5SApple OSS Distributions UInt32
OSBitOrAtomic(UInt32 mask,volatile UInt32 * value)158*1b191cb5SApple OSS Distributions OSBitOrAtomic(UInt32 mask, volatile UInt32 * value)
159*1b191cb5SApple OSS Distributions {
160*1b191cb5SApple OSS Distributions 	return os_atomic_or_orig(value, mask, relaxed);
161*1b191cb5SApple OSS Distributions }
162*1b191cb5SApple OSS Distributions 
163*1b191cb5SApple OSS Distributions #undef OSBitXorAtomic
164*1b191cb5SApple OSS Distributions UInt32
OSBitXorAtomic(UInt32 mask,volatile UInt32 * value)165*1b191cb5SApple OSS Distributions OSBitXorAtomic(UInt32 mask, volatile UInt32 * value)
166*1b191cb5SApple OSS Distributions {
167*1b191cb5SApple OSS Distributions 	return os_atomic_xor_orig(value, mask, relaxed);
168*1b191cb5SApple OSS Distributions }
169*1b191cb5SApple OSS Distributions 
170*1b191cb5SApple OSS Distributions static Boolean
OSTestAndSetClear(UInt32 bit,Boolean wantSet,volatile UInt8 * startAddress)171*1b191cb5SApple OSS Distributions OSTestAndSetClear(UInt32 bit, Boolean wantSet, volatile UInt8 * startAddress)
172*1b191cb5SApple OSS Distributions {
173*1b191cb5SApple OSS Distributions 	UInt8           mask = 1;
174*1b191cb5SApple OSS Distributions 	UInt8           oldValue, newValue;
175*1b191cb5SApple OSS Distributions 	UInt8           wantValue;
176*1b191cb5SApple OSS Distributions 	UInt8           *address;
177*1b191cb5SApple OSS Distributions 
178*1b191cb5SApple OSS Distributions 	address = (UInt8 *)(uintptr_t)(startAddress + (bit / 8));
179*1b191cb5SApple OSS Distributions 	mask <<= (7 - (bit % 8));
180*1b191cb5SApple OSS Distributions 	wantValue = wantSet ? mask : 0;
181*1b191cb5SApple OSS Distributions 
182*1b191cb5SApple OSS Distributions 	return !os_atomic_rmw_loop(address, oldValue, newValue, relaxed, {
183*1b191cb5SApple OSS Distributions 		if ((oldValue & mask) == wantValue) {
184*1b191cb5SApple OSS Distributions 		        os_atomic_rmw_loop_give_up(break);
185*1b191cb5SApple OSS Distributions 		}
186*1b191cb5SApple OSS Distributions 		newValue = (oldValue & ~mask) | wantValue;
187*1b191cb5SApple OSS Distributions 	});
188*1b191cb5SApple OSS Distributions }
189*1b191cb5SApple OSS Distributions 
190*1b191cb5SApple OSS Distributions Boolean
OSTestAndSet(UInt32 bit,volatile UInt8 * startAddress)191*1b191cb5SApple OSS Distributions OSTestAndSet(UInt32 bit, volatile UInt8 * startAddress)
192*1b191cb5SApple OSS Distributions {
193*1b191cb5SApple OSS Distributions 	return OSTestAndSetClear(bit, true, startAddress);
194*1b191cb5SApple OSS Distributions }
195*1b191cb5SApple OSS Distributions 
196*1b191cb5SApple OSS Distributions Boolean
OSTestAndClear(UInt32 bit,volatile UInt8 * startAddress)197*1b191cb5SApple OSS Distributions OSTestAndClear(UInt32 bit, volatile UInt8 * startAddress)
198*1b191cb5SApple OSS Distributions {
199*1b191cb5SApple OSS Distributions 	return OSTestAndSetClear(bit, false, startAddress);
200*1b191cb5SApple OSS Distributions }
201*1b191cb5SApple OSS Distributions 
202*1b191cb5SApple OSS Distributions /*
203*1b191cb5SApple OSS Distributions  * silly unaligned versions
204*1b191cb5SApple OSS Distributions  */
205*1b191cb5SApple OSS Distributions 
206*1b191cb5SApple OSS Distributions SInt8
OSIncrementAtomic8(volatile SInt8 * value)207*1b191cb5SApple OSS Distributions OSIncrementAtomic8(volatile SInt8 * value)
208*1b191cb5SApple OSS Distributions {
209*1b191cb5SApple OSS Distributions 	return os_atomic_inc_orig(value, relaxed);
210*1b191cb5SApple OSS Distributions }
211*1b191cb5SApple OSS Distributions 
212*1b191cb5SApple OSS Distributions SInt8
OSDecrementAtomic8(volatile SInt8 * value)213*1b191cb5SApple OSS Distributions OSDecrementAtomic8(volatile SInt8 * value)
214*1b191cb5SApple OSS Distributions {
215*1b191cb5SApple OSS Distributions 	return os_atomic_dec_orig(value, relaxed);
216*1b191cb5SApple OSS Distributions }
217*1b191cb5SApple OSS Distributions 
218*1b191cb5SApple OSS Distributions UInt8
OSBitAndAtomic8(UInt32 mask,volatile UInt8 * value)219*1b191cb5SApple OSS Distributions OSBitAndAtomic8(UInt32 mask, volatile UInt8 * value)
220*1b191cb5SApple OSS Distributions {
221*1b191cb5SApple OSS Distributions 	return os_atomic_and_orig(value, (UInt8)mask, relaxed);
222*1b191cb5SApple OSS Distributions }
223*1b191cb5SApple OSS Distributions 
224*1b191cb5SApple OSS Distributions UInt8
OSBitOrAtomic8(UInt32 mask,volatile UInt8 * value)225*1b191cb5SApple OSS Distributions OSBitOrAtomic8(UInt32 mask, volatile UInt8 * value)
226*1b191cb5SApple OSS Distributions {
227*1b191cb5SApple OSS Distributions 	return os_atomic_or_orig(value, (UInt8)mask, relaxed);
228*1b191cb5SApple OSS Distributions }
229*1b191cb5SApple OSS Distributions 
230*1b191cb5SApple OSS Distributions UInt8
OSBitXorAtomic8(UInt32 mask,volatile UInt8 * value)231*1b191cb5SApple OSS Distributions OSBitXorAtomic8(UInt32 mask, volatile UInt8 * value)
232*1b191cb5SApple OSS Distributions {
233*1b191cb5SApple OSS Distributions 	return os_atomic_xor_orig(value, (UInt8)mask, relaxed);
234*1b191cb5SApple OSS Distributions }
235*1b191cb5SApple OSS Distributions 
236*1b191cb5SApple OSS Distributions SInt16
OSIncrementAtomic16(volatile SInt16 * value)237*1b191cb5SApple OSS Distributions OSIncrementAtomic16(volatile SInt16 * value)
238*1b191cb5SApple OSS Distributions {
239*1b191cb5SApple OSS Distributions 	return OSAddAtomic16(1, value);
240*1b191cb5SApple OSS Distributions }
241*1b191cb5SApple OSS Distributions 
242*1b191cb5SApple OSS Distributions SInt16
OSDecrementAtomic16(volatile SInt16 * value)243*1b191cb5SApple OSS Distributions OSDecrementAtomic16(volatile SInt16 * value)
244*1b191cb5SApple OSS Distributions {
245*1b191cb5SApple OSS Distributions 	return OSAddAtomic16(-1, value);
246*1b191cb5SApple OSS Distributions }
247*1b191cb5SApple OSS Distributions 
248*1b191cb5SApple OSS Distributions UInt16
OSBitAndAtomic16(UInt32 mask,volatile UInt16 * value)249*1b191cb5SApple OSS Distributions OSBitAndAtomic16(UInt32 mask, volatile UInt16 * value)
250*1b191cb5SApple OSS Distributions {
251*1b191cb5SApple OSS Distributions 	return os_atomic_and_orig(value, (UInt16)mask, relaxed);
252*1b191cb5SApple OSS Distributions }
253*1b191cb5SApple OSS Distributions 
254*1b191cb5SApple OSS Distributions UInt16
OSBitOrAtomic16(UInt32 mask,volatile UInt16 * value)255*1b191cb5SApple OSS Distributions OSBitOrAtomic16(UInt32 mask, volatile UInt16 * value)
256*1b191cb5SApple OSS Distributions {
257*1b191cb5SApple OSS Distributions 	return os_atomic_or_orig(value, (UInt16)mask, relaxed);
258*1b191cb5SApple OSS Distributions }
259*1b191cb5SApple OSS Distributions 
260*1b191cb5SApple OSS Distributions UInt16
OSBitXorAtomic16(UInt32 mask,volatile UInt16 * value)261*1b191cb5SApple OSS Distributions OSBitXorAtomic16(UInt32 mask, volatile UInt16 * value)
262*1b191cb5SApple OSS Distributions {
263*1b191cb5SApple OSS Distributions 	return os_atomic_xor_orig(value, (UInt16)mask, relaxed);
264*1b191cb5SApple OSS Distributions }
265