]>
git.saurik.com Git - apple/xnu.git/blob - libkern/gen/OSAtomicOperations.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 #include <libkern/OSAtomic.h>
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...)
41 * native Boolean CompareAndSwap(UInt32 oldValue, UInt32 newValue, UInt32 * oldValuePtr);
43 * We've since implemented a few more of these -- OSAddAtomic, OSDequeueAtomic,
44 * OSEnqueueAtomic etc -- in assembler, either for speed or correctness. See also the
45 * commpage atomic operations, and the platform specific versions.
46 * Like standards, there are a lot of atomic ops to choose from!
51 SInt32
OSAddAtomic(SInt32 amount
, SInt32
* value
)
58 newValue
= oldValue
+ amount
;
59 } while (! OSCompareAndSwap((UInt32
) oldValue
, (UInt32
) newValue
, (UInt32
*) value
));
64 SInt32
OSIncrementAtomic(SInt32
* value
)
66 return OSAddAtomic(1, value
);
69 SInt32
OSDecrementAtomic(SInt32
* value
)
71 return OSAddAtomic(-1, value
);
74 void * OSDequeueAtomic(void ** inList
, SInt32 inOffset
)
80 oldListHead
= *inList
;
81 if (oldListHead
== NULL
) {
85 newListHead
= *(void **) (((char *) oldListHead
) + inOffset
);
86 } while (! OSCompareAndSwap((UInt32
)oldListHead
,
87 (UInt32
)newListHead
, (UInt32
*)inList
));
92 void OSEnqueueAtomic(void ** inList
, void * inNewLink
, SInt32 inOffset
)
95 void * newListHead
= inNewLink
;
96 void ** newLinkNextPtr
= (void **) (((char *) inNewLink
) + inOffset
);
99 oldListHead
= *inList
;
100 *newLinkNextPtr
= oldListHead
;
101 } while (! OSCompareAndSwap((UInt32
)oldListHead
, (UInt32
)newListHead
,
105 #endif /* !__ppc__ */
107 static UInt32
OSBitwiseAtomic(UInt32 and_mask
, UInt32 or_mask
, UInt32 xor_mask
, UInt32
* value
)
114 newValue
= ((oldValue
& and_mask
) | or_mask
) ^ xor_mask
;
115 } while (! OSCompareAndSwap(oldValue
, newValue
, value
));
120 UInt32
OSBitAndAtomic(UInt32 mask
, UInt32
* value
)
122 return OSBitwiseAtomic(mask
, 0, 0, value
);
125 UInt32
OSBitOrAtomic(UInt32 mask
, UInt32
* value
)
127 return OSBitwiseAtomic((UInt32
) -1, mask
, 0, value
);
130 UInt32
OSBitXorAtomic(UInt32 mask
, UInt32
* value
)
132 return OSBitwiseAtomic((UInt32
) -1, 0, mask
, value
);
135 static Boolean
OSCompareAndSwap8(UInt8 oldValue8
, UInt8 newValue8
, UInt8
* value8
)
137 UInt32 mask
= 0x000000ff;
138 UInt32 alignment
= ((UInt32
) value8
) & (sizeof(UInt32
) - 1);
139 UInt32 shiftValues
= (24 << 24) | (16 << 16) | (8 << 8);
140 int shift
= (UInt32
) *(((UInt8
*) &shiftValues
) + alignment
);
141 UInt32
* value32
= (UInt32
*) (value8
- alignment
);
148 oldValue
= (oldValue
& ~mask
) | (oldValue8
<< shift
);
149 newValue
= (oldValue
& ~mask
) | (newValue8
<< shift
);
151 return OSCompareAndSwap(oldValue
, newValue
, value32
);
154 static Boolean
OSTestAndSetClear(UInt32 bit
, Boolean wantSet
, UInt8
* startAddress
)
160 startAddress
+= (bit
/ 8);
161 mask
<<= (7 - (bit
% 8));
162 wantValue
= wantSet
? mask
: 0;
165 oldValue
= *startAddress
;
166 if ((oldValue
& mask
) == wantValue
) {
169 } while (! OSCompareAndSwap8(oldValue
, (oldValue
& ~mask
) | wantValue
, startAddress
));
171 return (oldValue
& mask
) == wantValue
;
174 Boolean
OSTestAndSet(UInt32 bit
, UInt8
* startAddress
)
176 return OSTestAndSetClear(bit
, true, startAddress
);
179 Boolean
OSTestAndClear(UInt32 bit
, UInt8
* startAddress
)
181 return OSTestAndSetClear(bit
, false, startAddress
);
185 * silly unaligned versions
188 SInt8
OSIncrementAtomic8(SInt8
* value
)
190 return OSAddAtomic8(1, value
);
193 SInt8
OSDecrementAtomic8(SInt8
* value
)
195 return OSAddAtomic8(-1, value
);
198 SInt8
OSAddAtomic8(SInt32 amount
, SInt8
* value
)
205 newValue
= oldValue
+ amount
;
206 } while (! OSCompareAndSwap8((UInt8
) oldValue
, (UInt8
) newValue
, (UInt8
*) value
));
211 static UInt8
OSBitwiseAtomic8(UInt32 and_mask
, UInt32 or_mask
, UInt32 xor_mask
, UInt8
* value
)
218 newValue
= ((oldValue
& and_mask
) | or_mask
) ^ xor_mask
;
219 } while (! OSCompareAndSwap8(oldValue
, newValue
, value
));
224 UInt8
OSBitAndAtomic8(UInt32 mask
, UInt8
* value
)
226 return OSBitwiseAtomic8(mask
, 0, 0, value
);
229 UInt8
OSBitOrAtomic8(UInt32 mask
, UInt8
* value
)
231 return OSBitwiseAtomic8((UInt32
) -1, mask
, 0, value
);
234 UInt8
OSBitXorAtomic8(UInt32 mask
, UInt8
* value
)
236 return OSBitwiseAtomic8((UInt32
) -1, 0, mask
, value
);
239 static Boolean
OSCompareAndSwap16(UInt16 oldValue16
, UInt16 newValue16
, UInt16
* value16
)
241 UInt32 mask
= 0x0000ffff;
242 UInt32 alignment
= ((UInt32
) value16
) & (sizeof(UInt32
) - 1);
243 UInt32 shiftValues
= (16 << 24) | (16 << 16);
244 UInt32 shift
= (UInt32
) *(((UInt8
*) &shiftValues
) + alignment
);
245 UInt32
* value32
= (UInt32
*) (((UInt32
) value16
) - alignment
);
252 oldValue
= (oldValue
& ~mask
) | (oldValue16
<< shift
);
253 newValue
= (oldValue
& ~mask
) | (newValue16
<< shift
);
255 return OSCompareAndSwap(oldValue
, newValue
, value32
);
258 SInt16
OSIncrementAtomic16(SInt16
* value
)
260 return OSAddAtomic16(1, value
);
263 SInt16
OSDecrementAtomic16(SInt16
* value
)
265 return OSAddAtomic16(-1, value
);
268 SInt16
OSAddAtomic16(SInt32 amount
, SInt16
* value
)
275 newValue
= oldValue
+ amount
;
276 } while (! OSCompareAndSwap16((UInt16
) oldValue
, (UInt16
) newValue
, (UInt16
*) value
));
281 static UInt16
OSBitwiseAtomic16(UInt32 and_mask
, UInt32 or_mask
, UInt32 xor_mask
, UInt16
* value
)
288 newValue
= ((oldValue
& and_mask
) | or_mask
) ^ xor_mask
;
289 } while (! OSCompareAndSwap16(oldValue
, newValue
, value
));
294 UInt16
OSBitAndAtomic16(UInt32 mask
, UInt16
* value
)
296 return OSBitwiseAtomic16(mask
, 0, 0, value
);
299 UInt16
OSBitOrAtomic16(UInt32 mask
, UInt16
* value
)
301 return OSBitwiseAtomic16((UInt32
) -1, mask
, 0, value
);
304 UInt16
OSBitXorAtomic16(UInt32 mask
, UInt16
* value
)
306 return OSBitwiseAtomic16((UInt32
) -1, 0, mask
, value
);