]>
git.saurik.com Git - apple/xnu.git/blob - libkern/gen/OSAtomicOperations.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <libkern/OSAtomic.h>
39 * these are _the_ atomic operations, currently cast atop CompareAndSwap,
40 * which is implemented in assembler. if we are worried about the cost of
41 * this layering (we shouldn't be), then all this stuff could be
42 * implemented in assembler, as it is in MacOS8/9
43 * (derived from SuperMario/NativeLibs/IO/DriverServices/Synchronization.s,
44 * which I wrote for NuKernel in a previous life with a different last name...)
46 * native Boolean CompareAndSwap(UInt32 oldValue, UInt32 newValue, UInt32 * oldValuePtr);
48 * We've since implemented a few more of these -- OSAddAtomic, OSDequeueAtomic,
49 * OSEnqueueAtomic etc -- in assembler, either for speed or correctness. See also the
50 * commpage atomic operations, and the platform specific versions.
51 * Like standards, there are a lot of atomic ops to choose from!
56 SInt32
OSAddAtomic(SInt32 amount
, SInt32
* value
)
63 newValue
= oldValue
+ amount
;
64 } while (! OSCompareAndSwap((UInt32
) oldValue
, (UInt32
) newValue
, (UInt32
*) value
));
69 SInt32
OSIncrementAtomic(SInt32
* value
)
71 return OSAddAtomic(1, value
);
74 SInt32
OSDecrementAtomic(SInt32
* value
)
76 return OSAddAtomic(-1, value
);
80 void * OSDequeueAtomic(void ** inList
, SInt32 inOffset
)
86 oldListHead
= *inList
;
87 if (oldListHead
== NULL
) {
91 newListHead
= *(void **) (((char *) oldListHead
) + inOffset
);
92 } while (! OSCompareAndSwap((UInt32
)oldListHead
,
93 (UInt32
)newListHead
, (UInt32
*)inList
));
97 void OSEnqueueAtomic(void ** inList
, void * inNewLink
, SInt32 inOffset
)
100 void * newListHead
= inNewLink
;
101 void ** newLinkNextPtr
= (void **) (((char *) inNewLink
) + inOffset
);
104 oldListHead
= *inList
;
105 *newLinkNextPtr
= oldListHead
;
106 } while (! OSCompareAndSwap((UInt32
)oldListHead
, (UInt32
)newListHead
,
109 #endif /* CMPXCHG8B */
110 #endif /* !__ppc__ */
112 static UInt32
OSBitwiseAtomic(UInt32 and_mask
, UInt32 or_mask
, UInt32 xor_mask
, UInt32
* value
)
119 newValue
= ((oldValue
& and_mask
) | or_mask
) ^ xor_mask
;
120 } while (! OSCompareAndSwap(oldValue
, newValue
, value
));
125 UInt32
OSBitAndAtomic(UInt32 mask
, UInt32
* value
)
127 return OSBitwiseAtomic(mask
, 0, 0, value
);
130 UInt32
OSBitOrAtomic(UInt32 mask
, UInt32
* value
)
132 return OSBitwiseAtomic((UInt32
) -1, mask
, 0, value
);
135 UInt32
OSBitXorAtomic(UInt32 mask
, UInt32
* value
)
137 return OSBitwiseAtomic((UInt32
) -1, 0, mask
, value
);
140 static Boolean
OSCompareAndSwap8(UInt8 oldValue8
, UInt8 newValue8
, UInt8
* value8
)
142 UInt32 mask
= 0x000000ff;
143 UInt32 alignment
= ((UInt32
) value8
) & (sizeof(UInt32
) - 1);
144 UInt32 shiftValues
= (24 << 24) | (16 << 16) | (8 << 8);
145 int shift
= (UInt32
) *(((UInt8
*) &shiftValues
) + alignment
);
146 UInt32
* value32
= (UInt32
*) (value8
- alignment
);
153 oldValue
= (oldValue
& ~mask
) | (oldValue8
<< shift
);
154 newValue
= (oldValue
& ~mask
) | (newValue8
<< shift
);
156 return OSCompareAndSwap(oldValue
, newValue
, value32
);
159 static Boolean
OSTestAndSetClear(UInt32 bit
, Boolean wantSet
, UInt8
* startAddress
)
165 startAddress
+= (bit
/ 8);
166 mask
<<= (7 - (bit
% 8));
167 wantValue
= wantSet
? mask
: 0;
170 oldValue
= *startAddress
;
171 if ((oldValue
& mask
) == wantValue
) {
174 } while (! OSCompareAndSwap8(oldValue
, (oldValue
& ~mask
) | wantValue
, startAddress
));
176 return (oldValue
& mask
) == wantValue
;
179 Boolean
OSTestAndSet(UInt32 bit
, UInt8
* startAddress
)
181 return OSTestAndSetClear(bit
, true, startAddress
);
184 Boolean
OSTestAndClear(UInt32 bit
, UInt8
* startAddress
)
186 return OSTestAndSetClear(bit
, false, startAddress
);
190 * silly unaligned versions
193 SInt8
OSIncrementAtomic8(SInt8
* value
)
195 return OSAddAtomic8(1, value
);
198 SInt8
OSDecrementAtomic8(SInt8
* value
)
200 return OSAddAtomic8(-1, value
);
203 SInt8
OSAddAtomic8(SInt32 amount
, SInt8
* value
)
210 newValue
= oldValue
+ amount
;
211 } while (! OSCompareAndSwap8((UInt8
) oldValue
, (UInt8
) newValue
, (UInt8
*) value
));
216 static UInt8
OSBitwiseAtomic8(UInt32 and_mask
, UInt32 or_mask
, UInt32 xor_mask
, UInt8
* value
)
223 newValue
= ((oldValue
& and_mask
) | or_mask
) ^ xor_mask
;
224 } while (! OSCompareAndSwap8(oldValue
, newValue
, value
));
229 UInt8
OSBitAndAtomic8(UInt32 mask
, UInt8
* value
)
231 return OSBitwiseAtomic8(mask
, 0, 0, value
);
234 UInt8
OSBitOrAtomic8(UInt32 mask
, UInt8
* value
)
236 return OSBitwiseAtomic8((UInt32
) -1, mask
, 0, value
);
239 UInt8
OSBitXorAtomic8(UInt32 mask
, UInt8
* value
)
241 return OSBitwiseAtomic8((UInt32
) -1, 0, mask
, value
);
244 static Boolean
OSCompareAndSwap16(UInt16 oldValue16
, UInt16 newValue16
, UInt16
* value16
)
246 UInt32 mask
= 0x0000ffff;
247 UInt32 alignment
= ((UInt32
) value16
) & (sizeof(UInt32
) - 1);
248 UInt32 shiftValues
= (16 << 24) | (16 << 16);
249 UInt32 shift
= (UInt32
) *(((UInt8
*) &shiftValues
) + alignment
);
250 UInt32
* value32
= (UInt32
*) (((UInt32
) value16
) - alignment
);
257 oldValue
= (oldValue
& ~mask
) | (oldValue16
<< shift
);
258 newValue
= (oldValue
& ~mask
) | (newValue16
<< shift
);
260 return OSCompareAndSwap(oldValue
, newValue
, value32
);
263 SInt16
OSIncrementAtomic16(SInt16
* value
)
265 return OSAddAtomic16(1, value
);
268 SInt16
OSDecrementAtomic16(SInt16
* value
)
270 return OSAddAtomic16(-1, value
);
273 SInt16
OSAddAtomic16(SInt32 amount
, SInt16
* value
)
280 newValue
= oldValue
+ amount
;
281 } while (! OSCompareAndSwap16((UInt16
) oldValue
, (UInt16
) newValue
, (UInt16
*) value
));
286 static UInt16
OSBitwiseAtomic16(UInt32 and_mask
, UInt32 or_mask
, UInt32 xor_mask
, UInt16
* value
)
293 newValue
= ((oldValue
& and_mask
) | or_mask
) ^ xor_mask
;
294 } while (! OSCompareAndSwap16(oldValue
, newValue
, value
));
299 UInt16
OSBitAndAtomic16(UInt32 mask
, UInt16
* value
)
301 return OSBitwiseAtomic16(mask
, 0, 0, value
);
304 UInt16
OSBitOrAtomic16(UInt32 mask
, UInt16
* value
)
306 return OSBitwiseAtomic16((UInt32
) -1, mask
, 0, value
);
309 UInt16
OSBitXorAtomic16(UInt32 mask
, UInt16
* value
)
311 return OSBitwiseAtomic16((UInt32
) -1, 0, mask
, value
);