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