]> git.saurik.com Git - apple/xnu.git/blob - libkern/gen/OSAtomicOperations.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / libkern / gen / OSAtomicOperations.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <libkern/OSAtomic.h>
30
31 enum {
32 false = 0,
33 true = 1
34 };
35 #define NULL 0L
36
37
38 /*
39 * atomic operations
40 * these are _the_ atomic operations, currently cast atop CompareAndSwap,
41 * which is implemented in assembler. if we are worried about the cost of
42 * this layering (we shouldn't be), then all this stuff could be
43 * implemented in assembler, as it is in MacOS8/9
44 * (derived from SuperMario/NativeLibs/IO/DriverServices/Synchronization.s,
45 * which I wrote for NuKernel in a previous life with a different last name...)
46 *
47 * native Boolean CompareAndSwap(UInt32 oldValue, UInt32 newValue, UInt32 * oldValuePtr);
48 *
49 * We've since implemented a few more of these -- OSAddAtomic, OSDequeueAtomic,
50 * OSEnqueueAtomic etc -- in assembler, either for speed or correctness. See also the
51 * commpage atomic operations, and the platform specific versions.
52 * Like standards, there are a lot of atomic ops to choose from!
53 */
54
55 #ifndef __ppc__
56
57 SInt32 OSAddAtomic(SInt32 amount, SInt32 * value)
58 {
59 SInt32 oldValue;
60 SInt32 newValue;
61
62 do {
63 oldValue = *value;
64 newValue = oldValue + amount;
65 } while (! OSCompareAndSwap((UInt32) oldValue, (UInt32) newValue, (UInt32 *) value));
66
67 return oldValue;
68 }
69
70 SInt32 OSIncrementAtomic(SInt32 * value)
71 {
72 return OSAddAtomic(1, value);
73 }
74
75 SInt32 OSDecrementAtomic(SInt32 * value)
76 {
77 return OSAddAtomic(-1, value);
78 }
79
80 void * OSDequeueAtomic(void ** inList, SInt32 inOffset)
81 {
82 void * oldListHead;
83 void * newListHead;
84
85 do {
86 oldListHead = *inList;
87 if (oldListHead == NULL) {
88 break;
89 }
90
91 newListHead = *(void **) (((char *) oldListHead) + inOffset);
92 } while (! OSCompareAndSwap((UInt32)oldListHead,
93 (UInt32)newListHead, (UInt32 *)inList));
94
95 return oldListHead;
96 }
97
98 void OSEnqueueAtomic(void ** inList, void * inNewLink, SInt32 inOffset)
99 {
100 void * oldListHead;
101 void * newListHead = inNewLink;
102 void ** newLinkNextPtr = (void **) (((char *) inNewLink) + inOffset);
103
104 do {
105 oldListHead = *inList;
106 *newLinkNextPtr = oldListHead;
107 } while (! OSCompareAndSwap((UInt32)oldListHead, (UInt32)newListHead,
108 (UInt32 *)inList));
109 }
110
111 #endif /* !__ppc__ */
112
113 static UInt32 OSBitwiseAtomic(UInt32 and_mask, UInt32 or_mask, UInt32 xor_mask, UInt32 * value)
114 {
115 UInt32 oldValue;
116 UInt32 newValue;
117
118 do {
119 oldValue = *value;
120 newValue = ((oldValue & and_mask) | or_mask) ^ xor_mask;
121 } while (! OSCompareAndSwap(oldValue, newValue, value));
122
123 return oldValue;
124 }
125
126 UInt32 OSBitAndAtomic(UInt32 mask, UInt32 * value)
127 {
128 return OSBitwiseAtomic(mask, 0, 0, value);
129 }
130
131 UInt32 OSBitOrAtomic(UInt32 mask, UInt32 * value)
132 {
133 return OSBitwiseAtomic((UInt32) -1, mask, 0, value);
134 }
135
136 UInt32 OSBitXorAtomic(UInt32 mask, UInt32 * value)
137 {
138 return OSBitwiseAtomic((UInt32) -1, 0, mask, value);
139 }
140
141 static Boolean OSCompareAndSwap8(UInt8 oldValue8, UInt8 newValue8, UInt8 * value8)
142 {
143 UInt32 mask = 0x000000ff;
144 UInt32 alignment = ((UInt32) value8) & (sizeof(UInt32) - 1);
145 UInt32 shiftValues = (24 << 24) | (16 << 16) | (8 << 8);
146 int shift = (UInt32) *(((UInt8 *) &shiftValues) + alignment);
147 UInt32 * value32 = (UInt32 *) (value8 - alignment);
148 UInt32 oldValue;
149 UInt32 newValue;
150
151 mask <<= shift;
152
153 oldValue = *value32;
154 oldValue = (oldValue & ~mask) | (oldValue8 << shift);
155 newValue = (oldValue & ~mask) | (newValue8 << shift);
156
157 return OSCompareAndSwap(oldValue, newValue, value32);
158 }
159
160 static Boolean OSTestAndSetClear(UInt32 bit, Boolean wantSet, UInt8 * startAddress)
161 {
162 UInt8 mask = 1;
163 UInt8 oldValue;
164 UInt8 wantValue;
165
166 startAddress += (bit / 8);
167 mask <<= (7 - (bit % 8));
168 wantValue = wantSet ? mask : 0;
169
170 do {
171 oldValue = *startAddress;
172 if ((oldValue & mask) == wantValue) {
173 break;
174 }
175 } while (! OSCompareAndSwap8(oldValue, (oldValue & ~mask) | wantValue, startAddress));
176
177 return (oldValue & mask) == wantValue;
178 }
179
180 Boolean OSTestAndSet(UInt32 bit, UInt8 * startAddress)
181 {
182 return OSTestAndSetClear(bit, true, startAddress);
183 }
184
185 Boolean OSTestAndClear(UInt32 bit, UInt8 * startAddress)
186 {
187 return OSTestAndSetClear(bit, false, startAddress);
188 }
189
190 /*
191 * silly unaligned versions
192 */
193
194 SInt8 OSIncrementAtomic8(SInt8 * value)
195 {
196 return OSAddAtomic8(1, value);
197 }
198
199 SInt8 OSDecrementAtomic8(SInt8 * value)
200 {
201 return OSAddAtomic8(-1, value);
202 }
203
204 SInt8 OSAddAtomic8(SInt32 amount, SInt8 * value)
205 {
206 SInt8 oldValue;
207 SInt8 newValue;
208
209 do {
210 oldValue = *value;
211 newValue = oldValue + amount;
212 } while (! OSCompareAndSwap8((UInt8) oldValue, (UInt8) newValue, (UInt8 *) value));
213
214 return oldValue;
215 }
216
217 static UInt8 OSBitwiseAtomic8(UInt32 and_mask, UInt32 or_mask, UInt32 xor_mask, UInt8 * value)
218 {
219 UInt8 oldValue;
220 UInt8 newValue;
221
222 do {
223 oldValue = *value;
224 newValue = ((oldValue & and_mask) | or_mask) ^ xor_mask;
225 } while (! OSCompareAndSwap8(oldValue, newValue, value));
226
227 return oldValue;
228 }
229
230 UInt8 OSBitAndAtomic8(UInt32 mask, UInt8 * value)
231 {
232 return OSBitwiseAtomic8(mask, 0, 0, value);
233 }
234
235 UInt8 OSBitOrAtomic8(UInt32 mask, UInt8 * value)
236 {
237 return OSBitwiseAtomic8((UInt32) -1, mask, 0, value);
238 }
239
240 UInt8 OSBitXorAtomic8(UInt32 mask, UInt8 * value)
241 {
242 return OSBitwiseAtomic8((UInt32) -1, 0, mask, value);
243 }
244
245 static Boolean OSCompareAndSwap16(UInt16 oldValue16, UInt16 newValue16, UInt16 * value16)
246 {
247 UInt32 mask = 0x0000ffff;
248 UInt32 alignment = ((UInt32) value16) & (sizeof(UInt32) - 1);
249 UInt32 shiftValues = (16 << 24) | (16 << 16);
250 UInt32 shift = (UInt32) *(((UInt8 *) &shiftValues) + alignment);
251 UInt32 * value32 = (UInt32 *) (((UInt32) value16) - alignment);
252 UInt32 oldValue;
253 UInt32 newValue;
254
255 mask <<= shift;
256
257 oldValue = *value32;
258 oldValue = (oldValue & ~mask) | (oldValue16 << shift);
259 newValue = (oldValue & ~mask) | (newValue16 << shift);
260
261 return OSCompareAndSwap(oldValue, newValue, value32);
262 }
263
264 SInt16 OSIncrementAtomic16(SInt16 * value)
265 {
266 return OSAddAtomic16(1, value);
267 }
268
269 SInt16 OSDecrementAtomic16(SInt16 * value)
270 {
271 return OSAddAtomic16(-1, value);
272 }
273
274 SInt16 OSAddAtomic16(SInt32 amount, SInt16 * value)
275 {
276 SInt16 oldValue;
277 SInt16 newValue;
278
279 do {
280 oldValue = *value;
281 newValue = oldValue + amount;
282 } while (! OSCompareAndSwap16((UInt16) oldValue, (UInt16) newValue, (UInt16 *) value));
283
284 return oldValue;
285 }
286
287 static UInt16 OSBitwiseAtomic16(UInt32 and_mask, UInt32 or_mask, UInt32 xor_mask, UInt16 * value)
288 {
289 UInt16 oldValue;
290 UInt16 newValue;
291
292 do {
293 oldValue = *value;
294 newValue = ((oldValue & and_mask) | or_mask) ^ xor_mask;
295 } while (! OSCompareAndSwap16(oldValue, newValue, value));
296
297 return oldValue;
298 }
299
300 UInt16 OSBitAndAtomic16(UInt32 mask, UInt16 * value)
301 {
302 return OSBitwiseAtomic16(mask, 0, 0, value);
303 }
304
305 UInt16 OSBitOrAtomic16(UInt32 mask, UInt16 * value)
306 {
307 return OSBitwiseAtomic16((UInt32) -1, mask, 0, value);
308 }
309
310 UInt16 OSBitXorAtomic16(UInt32 mask, UInt16 * value)
311 {
312 return OSBitwiseAtomic16((UInt32) -1, 0, mask, value);
313 }
314