]>
git.saurik.com Git - apple/xnu.git/blob - libkern/gen/OSAtomicOperations.c
222db2a333e870bb4a259b867978684131c7ace9
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 #include <libkern/OSAtomic.h>
37 * these are _the_ atomic operations, currently cast atop CompareAndSwap,
38 * which is implemented in assembler. if we are worried about the cost of
39 * this layering (we shouldn't be), then all this stuff could be
40 * implemented in assembler, as it is in MacOS8/9
41 * (derived from SuperMario/NativeLibs/IO/DriverServices/Synchronization.s,
42 * which I wrote for NuKernel in a previous life with a different last name...)
44 * native Boolean CompareAndSwap(UInt32 oldValue, UInt32 newValue, UInt32 * oldValuePtr);
49 SInt32
OSAddAtomic(SInt32 amount
, SInt32
* value
)
56 newValue
= oldValue
+ amount
;
57 } while (! OSCompareAndSwap((UInt32
) oldValue
, (UInt32
) newValue
, (UInt32
*) value
));
62 SInt32
OSIncrementAtomic(SInt32
* value
)
64 return OSAddAtomic(1, value
);
67 SInt32
OSDecrementAtomic(SInt32
* value
)
69 return OSAddAtomic(-1, value
);
74 static UInt32
OSBitwiseAtomic(UInt32 and_mask
, UInt32 or_mask
, UInt32 xor_mask
, UInt32
* value
)
81 newValue
= ((oldValue
& and_mask
) | or_mask
) ^ xor_mask
;
82 } while (! OSCompareAndSwap(oldValue
, newValue
, value
));
87 UInt32
OSBitAndAtomic(UInt32 mask
, UInt32
* value
)
89 return OSBitwiseAtomic(mask
, 0, 0, value
);
92 UInt32
OSBitOrAtomic(UInt32 mask
, UInt32
* value
)
94 return OSBitwiseAtomic((UInt32
) -1, mask
, 0, value
);
97 UInt32
OSBitXorAtomic(UInt32 mask
, UInt32
* value
)
99 return OSBitwiseAtomic((UInt32
) -1, 0, mask
, value
);
102 static Boolean
OSCompareAndSwap8(UInt8 oldValue8
, UInt8 newValue8
, UInt8
* value8
)
104 UInt32 mask
= 0x000000ff;
105 UInt32 alignment
= ((UInt32
) value8
) & (sizeof(UInt32
) - 1);
106 UInt32 shiftValues
= (24 << 24) | (16 << 16) | (8 << 8);
107 int shift
= (UInt32
) *(((UInt8
*) &shiftValues
) + alignment
);
108 UInt32
* value32
= (UInt32
*) (value8
- alignment
);
115 oldValue
= (oldValue
& ~mask
) | (oldValue8
<< shift
);
116 newValue
= (oldValue
& ~mask
) | (newValue8
<< shift
);
118 return OSCompareAndSwap(oldValue
, newValue
, value32
);
121 static Boolean
OSTestAndSetClear(UInt32 bit
, Boolean wantSet
, UInt8
* startAddress
)
127 startAddress
+= (bit
/ 8);
128 mask
<<= (7 - (bit
% 8));
129 wantValue
= wantSet
? mask
: 0;
132 oldValue
= *startAddress
;
133 if ((oldValue
& mask
) == wantValue
) {
136 } while (! OSCompareAndSwap8(oldValue
, (oldValue
& ~mask
) | wantValue
, startAddress
));
138 return (oldValue
& mask
) == wantValue
;
141 Boolean
OSTestAndSet(UInt32 bit
, UInt8
* startAddress
)
143 return OSTestAndSetClear(bit
, true, startAddress
);
146 Boolean
OSTestAndClear(UInt32 bit
, UInt8
* startAddress
)
148 return OSTestAndSetClear(bit
, false, startAddress
);
151 void * OSDequeueAtomic(void ** inList
, SInt32 inOffset
)
157 oldListHead
= *inList
;
158 if (oldListHead
== NULL
) {
162 newListHead
= *(void **) (((char *) oldListHead
) + inOffset
);
163 } while (! OSCompareAndSwap((UInt32
)oldListHead
,
164 (UInt32
)newListHead
, (UInt32
*)inList
));
169 void OSEnqueueAtomic(void ** inList
, void * inNewLink
, SInt32 inOffset
)
172 void * newListHead
= inNewLink
;
173 void ** newLinkNextPtr
= (void **) (((char *) inNewLink
) + inOffset
);
176 oldListHead
= *inList
;
177 *newLinkNextPtr
= oldListHead
;
178 } while (! OSCompareAndSwap((UInt32
)oldListHead
, (UInt32
)newListHead
,
183 * silly unaligned versions
186 SInt8
OSIncrementAtomic8(SInt8
* value
)
188 return OSAddAtomic8(1, value
);
191 SInt8
OSDecrementAtomic8(SInt8
* value
)
193 return OSAddAtomic8(-1, value
);
196 SInt8
OSAddAtomic8(SInt32 amount
, SInt8
* value
)
203 newValue
= oldValue
+ amount
;
204 } while (! OSCompareAndSwap8((UInt8
) oldValue
, (UInt8
) newValue
, (UInt8
*) value
));
209 static UInt8
OSBitwiseAtomic8(UInt32 and_mask
, UInt32 or_mask
, UInt32 xor_mask
, UInt8
* value
)
216 newValue
= ((oldValue
& and_mask
) | or_mask
) ^ xor_mask
;
217 } while (! OSCompareAndSwap8(oldValue
, newValue
, value
));
222 UInt8
OSBitAndAtomic8(UInt32 mask
, UInt8
* value
)
224 return OSBitwiseAtomic8(mask
, 0, 0, value
);
227 UInt8
OSBitOrAtomic8(UInt32 mask
, UInt8
* value
)
229 return OSBitwiseAtomic8((UInt32
) -1, mask
, 0, value
);
232 UInt8
OSBitXorAtomic8(UInt32 mask
, UInt8
* value
)
234 return OSBitwiseAtomic8((UInt32
) -1, 0, mask
, value
);
237 static Boolean
OSCompareAndSwap16(UInt16 oldValue16
, UInt16 newValue16
, UInt16
* value16
)
239 UInt32 mask
= 0x0000ffff;
240 UInt32 alignment
= ((UInt32
) value16
) & (sizeof(UInt32
) - 1);
241 UInt32 shiftValues
= (16 << 24) | (16 << 16);
242 UInt32 shift
= (UInt32
) *(((UInt8
*) &shiftValues
) + alignment
);
243 UInt32
* value32
= (UInt32
*) (((UInt32
) value16
) - alignment
);
250 oldValue
= (oldValue
& ~mask
) | (oldValue16
<< shift
);
251 newValue
= (oldValue
& ~mask
) | (newValue16
<< shift
);
253 return OSCompareAndSwap(oldValue
, newValue
, value32
);
256 SInt16
OSIncrementAtomic16(SInt16
* value
)
258 return OSAddAtomic16(1, value
);
261 SInt16
OSDecrementAtomic16(SInt16
* value
)
263 return OSAddAtomic16(-1, value
);
266 SInt16
OSAddAtomic16(SInt32 amount
, SInt16
* value
)
273 newValue
= oldValue
+ amount
;
274 } while (! OSCompareAndSwap16((UInt16
) oldValue
, (UInt16
) newValue
, (UInt16
*) value
));
279 static UInt16
OSBitwiseAtomic16(UInt32 and_mask
, UInt32 or_mask
, UInt32 xor_mask
, UInt16
* value
)
286 newValue
= ((oldValue
& and_mask
) | or_mask
) ^ xor_mask
;
287 } while (! OSCompareAndSwap16(oldValue
, newValue
, value
));
292 UInt16
OSBitAndAtomic16(UInt32 mask
, UInt16
* value
)
294 return OSBitwiseAtomic16(mask
, 0, 0, value
);
297 UInt16
OSBitOrAtomic16(UInt32 mask
, UInt16
* value
)
299 return OSBitwiseAtomic16((UInt32
) -1, mask
, 0, value
);
302 UInt16
OSBitXorAtomic16(UInt32 mask
, UInt16
* value
)
304 return OSBitwiseAtomic16((UInt32
) -1, 0, mask
, value
);