]> git.saurik.com Git - apple/xnu.git/blame - libkern/gen/OSAtomicOperations.c
xnu-2782.40.9.tar.gz
[apple/xnu.git] / libkern / gen / OSAtomicOperations.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28
29#include <libkern/OSAtomic.h>
30
31enum {
32 false = 0,
33 true = 1
34};
2d21ac55
A
35
36#ifndef NULL
37#define NULL ((void *)0)
38#endif
1c79356b 39
1c79356b
A
40/*
41 * atomic operations
42 * these are _the_ atomic operations, currently cast atop CompareAndSwap,
2d21ac55 43 * which is implemented in assembler. if we are worried about the cost of
1c79356b
A
44 * this layering (we shouldn't be), then all this stuff could be
45 * implemented in assembler, as it is in MacOS8/9
46 * (derived from SuperMario/NativeLibs/IO/DriverServices/Synchronization.s,
47 * which I wrote for NuKernel in a previous life with a different last name...)
48 *
49 * native Boolean CompareAndSwap(UInt32 oldValue, UInt32 newValue, UInt32 * oldValuePtr);
91447636
A
50 *
51 * We've since implemented a few more of these -- OSAddAtomic, OSDequeueAtomic,
52 * OSEnqueueAtomic etc -- in assembler, either for speed or correctness. See also the
53 * commpage atomic operations, and the platform specific versions.
54 * Like standards, there are a lot of atomic ops to choose from!
1c79356b
A
55 */
56
316670eb 57#if defined(__i386__) || defined(__x86_64__)
6d2010ae 58/* Implemented in assembly for i386 and x86_64 */
b0d623f7 59#else
316670eb 60#error Unsupported arch
b0d623f7
A
61#endif
62
b0d623f7
A
63#undef OSIncrementAtomic
64SInt32 OSIncrementAtomic(volatile SInt32 * value)
91447636 65{
b0d623f7 66 return OSAddAtomic(1, value);
91447636
A
67}
68
b0d623f7
A
69#undef OSDecrementAtomic
70SInt32 OSDecrementAtomic(volatile SInt32 * value)
91447636 71{
b0d623f7 72 return OSAddAtomic(-1, value);
91447636 73}
1c79356b 74
2d21ac55 75static UInt32 OSBitwiseAtomic(UInt32 and_mask, UInt32 or_mask, UInt32 xor_mask, volatile UInt32 * value)
1c79356b
A
76{
77 UInt32 oldValue;
78 UInt32 newValue;
79
80 do {
81 oldValue = *value;
82 newValue = ((oldValue & and_mask) | or_mask) ^ xor_mask;
83 } while (! OSCompareAndSwap(oldValue, newValue, value));
84
85 return oldValue;
86}
87
b0d623f7 88#undef OSBitAndAtomic
2d21ac55 89UInt32 OSBitAndAtomic(UInt32 mask, volatile UInt32 * value)
1c79356b
A
90{
91 return OSBitwiseAtomic(mask, 0, 0, value);
92}
93
b0d623f7 94#undef OSBitOrAtomic
2d21ac55 95UInt32 OSBitOrAtomic(UInt32 mask, volatile UInt32 * value)
1c79356b
A
96{
97 return OSBitwiseAtomic((UInt32) -1, mask, 0, value);
98}
99
b0d623f7 100#undef OSBitXorAtomic
2d21ac55 101UInt32 OSBitXorAtomic(UInt32 mask, volatile UInt32 * value)
1c79356b
A
102{
103 return OSBitwiseAtomic((UInt32) -1, 0, mask, value);
104}
105
39236c6e 106#if defined(__i386__) || defined(__x86_64__)
2d21ac55 107static Boolean OSCompareAndSwap8(UInt8 oldValue8, UInt8 newValue8, volatile UInt8 * value8)
1c79356b 108{
2d21ac55 109 UInt32 mask = 0x000000ff;
b0d623f7 110 UInt32 alignment = (UInt32)((unsigned long) value8) & (sizeof(UInt32) - 1);
2d21ac55
A
111 UInt32 shiftValues = (24 << 24) | (16 << 16) | (8 << 8);
112 int shift = (UInt32) *(((UInt8 *) &shiftValues) + alignment);
b0d623f7 113 volatile UInt32 * value32 = (volatile UInt32 *) ((uintptr_t)value8 - alignment);
2d21ac55
A
114 UInt32 oldValue;
115 UInt32 newValue;
1c79356b 116
2d21ac55 117 mask <<= shift;
0b4e3aa0 118
2d21ac55
A
119 oldValue = *value32;
120 oldValue = (oldValue & ~mask) | (oldValue8 << shift);
121 newValue = (oldValue & ~mask) | (newValue8 << shift);
0b4e3aa0
A
122
123 return OSCompareAndSwap(oldValue, newValue, value32);
1c79356b 124}
39236c6e 125#endif
1c79356b 126
2d21ac55 127static Boolean OSTestAndSetClear(UInt32 bit, Boolean wantSet, volatile UInt8 * startAddress)
1c79356b
A
128{
129 UInt8 mask = 1;
130 UInt8 oldValue;
131 UInt8 wantValue;
132
133 startAddress += (bit / 8);
134 mask <<= (7 - (bit % 8));
135 wantValue = wantSet ? mask : 0;
136
137 do {
138 oldValue = *startAddress;
139 if ((oldValue & mask) == wantValue) {
140 break;
141 }
142 } while (! OSCompareAndSwap8(oldValue, (oldValue & ~mask) | wantValue, startAddress));
143
144 return (oldValue & mask) == wantValue;
145}
146
2d21ac55 147Boolean OSTestAndSet(UInt32 bit, volatile UInt8 * startAddress)
1c79356b
A
148{
149 return OSTestAndSetClear(bit, true, startAddress);
150}
151
2d21ac55 152Boolean OSTestAndClear(UInt32 bit, volatile UInt8 * startAddress)
1c79356b
A
153{
154 return OSTestAndSetClear(bit, false, startAddress);
155}
156
1c79356b
A
157/*
158 * silly unaligned versions
159 */
160
2d21ac55 161SInt8 OSIncrementAtomic8(volatile SInt8 * value)
1c79356b
A
162{
163 return OSAddAtomic8(1, value);
164}
165
2d21ac55 166SInt8 OSDecrementAtomic8(volatile SInt8 * value)
1c79356b
A
167{
168 return OSAddAtomic8(-1, value);
169}
170
39236c6e 171#if defined(__i386__) || defined(__x86_64__)
2d21ac55 172SInt8 OSAddAtomic8(SInt32 amount, volatile SInt8 * value)
1c79356b
A
173{
174 SInt8 oldValue;
175 SInt8 newValue;
176
177 do {
178 oldValue = *value;
179 newValue = oldValue + amount;
2d21ac55 180 } while (! OSCompareAndSwap8((UInt8) oldValue, (UInt8) newValue, (volatile UInt8 *) value));
1c79356b
A
181
182 return oldValue;
183}
39236c6e 184#endif
1c79356b 185
2d21ac55 186static UInt8 OSBitwiseAtomic8(UInt32 and_mask, UInt32 or_mask, UInt32 xor_mask, volatile UInt8 * value)
1c79356b
A
187{
188 UInt8 oldValue;
189 UInt8 newValue;
190
191 do {
192 oldValue = *value;
193 newValue = ((oldValue & and_mask) | or_mask) ^ xor_mask;
194 } while (! OSCompareAndSwap8(oldValue, newValue, value));
195
196 return oldValue;
197}
198
2d21ac55 199UInt8 OSBitAndAtomic8(UInt32 mask, volatile UInt8 * value)
1c79356b
A
200{
201 return OSBitwiseAtomic8(mask, 0, 0, value);
202}
203
2d21ac55 204UInt8 OSBitOrAtomic8(UInt32 mask, volatile UInt8 * value)
1c79356b
A
205{
206 return OSBitwiseAtomic8((UInt32) -1, mask, 0, value);
207}
208
2d21ac55 209UInt8 OSBitXorAtomic8(UInt32 mask, volatile UInt8 * value)
1c79356b
A
210{
211 return OSBitwiseAtomic8((UInt32) -1, 0, mask, value);
212}
213
39236c6e 214#if defined(__i386__) || defined(__x86_64__)
2d21ac55 215static Boolean OSCompareAndSwap16(UInt16 oldValue16, UInt16 newValue16, volatile UInt16 * value16)
1c79356b 216{
2d21ac55 217 UInt32 mask = 0x0000ffff;
b0d623f7 218 UInt32 alignment = (UInt32)((unsigned long) value16) & (sizeof(UInt32) - 1);
2d21ac55
A
219 UInt32 shiftValues = (16 << 24) | (16 << 16);
220 UInt32 shift = (UInt32) *(((UInt8 *) &shiftValues) + alignment);
b0d623f7 221 volatile UInt32 * value32 = (volatile UInt32 *) (((unsigned long) value16) - alignment);
2d21ac55
A
222 UInt32 oldValue;
223 UInt32 newValue;
0b4e3aa0 224
2d21ac55 225 mask <<= shift;
0b4e3aa0 226
2d21ac55
A
227 oldValue = *value32;
228 oldValue = (oldValue & ~mask) | (oldValue16 << shift);
229 newValue = (oldValue & ~mask) | (newValue16 << shift);
0b4e3aa0
A
230
231 return OSCompareAndSwap(oldValue, newValue, value32);
1c79356b 232}
39236c6e 233#endif
1c79356b 234
2d21ac55 235SInt16 OSIncrementAtomic16(volatile SInt16 * value)
1c79356b
A
236{
237 return OSAddAtomic16(1, value);
238}
239
2d21ac55 240SInt16 OSDecrementAtomic16(volatile SInt16 * value)
1c79356b
A
241{
242 return OSAddAtomic16(-1, value);
243}
244
39236c6e 245#if defined(__i386__) || defined(__x86_64__)
2d21ac55 246SInt16 OSAddAtomic16(SInt32 amount, volatile SInt16 * value)
1c79356b
A
247{
248 SInt16 oldValue;
249 SInt16 newValue;
250
251 do {
252 oldValue = *value;
253 newValue = oldValue + amount;
2d21ac55 254 } while (! OSCompareAndSwap16((UInt16) oldValue, (UInt16) newValue, (volatile UInt16 *) value));
1c79356b
A
255
256 return oldValue;
257}
39236c6e 258#endif
1c79356b 259
2d21ac55 260static UInt16 OSBitwiseAtomic16(UInt32 and_mask, UInt32 or_mask, UInt32 xor_mask, volatile UInt16 * value)
1c79356b
A
261{
262 UInt16 oldValue;
263 UInt16 newValue;
264
265 do {
266 oldValue = *value;
267 newValue = ((oldValue & and_mask) | or_mask) ^ xor_mask;
268 } while (! OSCompareAndSwap16(oldValue, newValue, value));
269
270 return oldValue;
271}
272
2d21ac55 273UInt16 OSBitAndAtomic16(UInt32 mask, volatile UInt16 * value)
1c79356b
A
274{
275 return OSBitwiseAtomic16(mask, 0, 0, value);
276}
277
2d21ac55 278UInt16 OSBitOrAtomic16(UInt32 mask, volatile UInt16 * value)
1c79356b
A
279{
280 return OSBitwiseAtomic16((UInt32) -1, mask, 0, value);
281}
282
2d21ac55 283UInt16 OSBitXorAtomic16(UInt32 mask, volatile UInt16 * value)
1c79356b
A
284{
285 return OSBitwiseAtomic16((UInt32) -1, 0, mask, value);
286}
287