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