]>
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_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <libkern/OSAtomic.h>
42 * these are _the_ atomic operations, currently cast atop CompareAndSwap,
43 * which is implemented in assembler. if we are worried about the cost of
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...)
49 * native Boolean CompareAndSwap(UInt32 oldValue, UInt32 newValue, UInt32 * oldValuePtr);
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!
59 SInt32
OSAddAtomic(SInt32 amount
, SInt32
* value
)
66 newValue
= oldValue
+ amount
;
67 } while (! OSCompareAndSwap((UInt32
) oldValue
, (UInt32
) newValue
, (UInt32
*) value
));
72 SInt32
OSIncrementAtomic(SInt32
* value
)
74 return OSAddAtomic(1, value
);
77 SInt32
OSDecrementAtomic(SInt32
* value
)
79 return OSAddAtomic(-1, value
);
82 void * OSDequeueAtomic(void ** inList
, SInt32 inOffset
)
88 oldListHead
= *inList
;
89 if (oldListHead
== NULL
) {
93 newListHead
= *(void **) (((char *) oldListHead
) + inOffset
);
94 } while (! OSCompareAndSwap((UInt32
)oldListHead
,
95 (UInt32
)newListHead
, (UInt32
*)inList
));
100 void OSEnqueueAtomic(void ** inList
, void * inNewLink
, SInt32 inOffset
)
103 void * newListHead
= inNewLink
;
104 void ** newLinkNextPtr
= (void **) (((char *) inNewLink
) + inOffset
);
107 oldListHead
= *inList
;
108 *newLinkNextPtr
= oldListHead
;
109 } while (! OSCompareAndSwap((UInt32
)oldListHead
, (UInt32
)newListHead
,
113 #endif /* !__ppc__ */
115 static UInt32
OSBitwiseAtomic(UInt32 and_mask
, UInt32 or_mask
, UInt32 xor_mask
, UInt32
* value
)
122 newValue
= ((oldValue
& and_mask
) | or_mask
) ^ xor_mask
;
123 } while (! OSCompareAndSwap(oldValue
, newValue
, value
));
128 UInt32
OSBitAndAtomic(UInt32 mask
, UInt32
* value
)
130 return OSBitwiseAtomic(mask
, 0, 0, value
);
133 UInt32
OSBitOrAtomic(UInt32 mask
, UInt32
* value
)
135 return OSBitwiseAtomic((UInt32
) -1, mask
, 0, value
);
138 UInt32
OSBitXorAtomic(UInt32 mask
, UInt32
* value
)
140 return OSBitwiseAtomic((UInt32
) -1, 0, mask
, value
);
143 static Boolean
OSCompareAndSwap8(UInt8 oldValue8
, UInt8 newValue8
, UInt8
* value8
)
145 UInt32 mask
= 0x000000ff;
146 UInt32 alignment
= ((UInt32
) value8
) & (sizeof(UInt32
) - 1);
147 UInt32 shiftValues
= (24 << 24) | (16 << 16) | (8 << 8);
148 int shift
= (UInt32
) *(((UInt8
*) &shiftValues
) + alignment
);
149 UInt32
* value32
= (UInt32
*) (value8
- alignment
);
156 oldValue
= (oldValue
& ~mask
) | (oldValue8
<< shift
);
157 newValue
= (oldValue
& ~mask
) | (newValue8
<< shift
);
159 return OSCompareAndSwap(oldValue
, newValue
, value32
);
162 static Boolean
OSTestAndSetClear(UInt32 bit
, Boolean wantSet
, UInt8
* startAddress
)
168 startAddress
+= (bit
/ 8);
169 mask
<<= (7 - (bit
% 8));
170 wantValue
= wantSet
? mask
: 0;
173 oldValue
= *startAddress
;
174 if ((oldValue
& mask
) == wantValue
) {
177 } while (! OSCompareAndSwap8(oldValue
, (oldValue
& ~mask
) | wantValue
, startAddress
));
179 return (oldValue
& mask
) == wantValue
;
182 Boolean
OSTestAndSet(UInt32 bit
, UInt8
* startAddress
)
184 return OSTestAndSetClear(bit
, true, startAddress
);
187 Boolean
OSTestAndClear(UInt32 bit
, UInt8
* startAddress
)
189 return OSTestAndSetClear(bit
, false, startAddress
);
193 * silly unaligned versions
196 SInt8
OSIncrementAtomic8(SInt8
* value
)
198 return OSAddAtomic8(1, value
);
201 SInt8
OSDecrementAtomic8(SInt8
* value
)
203 return OSAddAtomic8(-1, value
);
206 SInt8
OSAddAtomic8(SInt32 amount
, SInt8
* value
)
213 newValue
= oldValue
+ amount
;
214 } while (! OSCompareAndSwap8((UInt8
) oldValue
, (UInt8
) newValue
, (UInt8
*) value
));
219 static UInt8
OSBitwiseAtomic8(UInt32 and_mask
, UInt32 or_mask
, UInt32 xor_mask
, UInt8
* value
)
226 newValue
= ((oldValue
& and_mask
) | or_mask
) ^ xor_mask
;
227 } while (! OSCompareAndSwap8(oldValue
, newValue
, value
));
232 UInt8
OSBitAndAtomic8(UInt32 mask
, UInt8
* value
)
234 return OSBitwiseAtomic8(mask
, 0, 0, value
);
237 UInt8
OSBitOrAtomic8(UInt32 mask
, UInt8
* value
)
239 return OSBitwiseAtomic8((UInt32
) -1, mask
, 0, value
);
242 UInt8
OSBitXorAtomic8(UInt32 mask
, UInt8
* value
)
244 return OSBitwiseAtomic8((UInt32
) -1, 0, mask
, value
);
247 static Boolean
OSCompareAndSwap16(UInt16 oldValue16
, UInt16 newValue16
, UInt16
* value16
)
249 UInt32 mask
= 0x0000ffff;
250 UInt32 alignment
= ((UInt32
) value16
) & (sizeof(UInt32
) - 1);
251 UInt32 shiftValues
= (16 << 24) | (16 << 16);
252 UInt32 shift
= (UInt32
) *(((UInt8
*) &shiftValues
) + alignment
);
253 UInt32
* value32
= (UInt32
*) (((UInt32
) value16
) - alignment
);
260 oldValue
= (oldValue
& ~mask
) | (oldValue16
<< shift
);
261 newValue
= (oldValue
& ~mask
) | (newValue16
<< shift
);
263 return OSCompareAndSwap(oldValue
, newValue
, value32
);
266 SInt16
OSIncrementAtomic16(SInt16
* value
)
268 return OSAddAtomic16(1, value
);
271 SInt16
OSDecrementAtomic16(SInt16
* value
)
273 return OSAddAtomic16(-1, value
);
276 SInt16
OSAddAtomic16(SInt32 amount
, SInt16
* value
)
283 newValue
= oldValue
+ amount
;
284 } while (! OSCompareAndSwap16((UInt16
) oldValue
, (UInt16
) newValue
, (UInt16
*) value
));
289 static UInt16
OSBitwiseAtomic16(UInt32 and_mask
, UInt32 or_mask
, UInt32 xor_mask
, UInt16
* value
)
296 newValue
= ((oldValue
& and_mask
) | or_mask
) ^ xor_mask
;
297 } while (! OSCompareAndSwap16(oldValue
, newValue
, value
));
302 UInt16
OSBitAndAtomic16(UInt32 mask
, UInt16
* value
)
304 return OSBitwiseAtomic16(mask
, 0, 0, value
);
307 UInt16
OSBitOrAtomic16(UInt32 mask
, UInt16
* value
)
309 return OSBitwiseAtomic16((UInt32
) -1, mask
, 0, value
);
312 UInt16
OSBitXorAtomic16(UInt32 mask
, UInt16
* value
)
314 return OSBitwiseAtomic16((UInt32
) -1, 0, mask
, value
);