Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | /* * Copyright (c) 2004, 2008 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <libkern/OSAtomic.h> #include <arm/arch.h> #if !defined(_ARM_ARCH_6) /* * The only atomic operation ARMv4T provides (atomic swap) is not * sufficient for the general 32-bit arithmetic and compare-and-swap * operations OSAtomic is supposed to provide. So we use a global * spin lock around all operations. * * Since we have only a single, in-order, CPU, we do not need * memory barriers for data. */ static OSSpinLock _atomic_lock = OS_SPINLOCK_INIT; int32_t OSAtomicAdd32( int32_t theAmount, volatile int32_t *theValue ) { int32_t result; OSSpinLockLock(&_atomic_lock); result = (*theValue += theAmount); OSSpinLockUnlock(&_atomic_lock); return result; } int32_t OSAtomicAdd32Barrier( int32_t theAmount, volatile int32_t *theValue ) { return OSAtomicAdd32(theAmount, theValue); } int64_t OSAtomicAdd64( int64_t theAmount, volatile int64_t *theValue ) { int64_t result; OSSpinLockLock(&_atomic_lock); result = (*theValue += theAmount); OSSpinLockUnlock(&_atomic_lock); return result; } int64_t OSAtomicAdd64Barrier( int64_t theAmount, volatile int64_t *theValue ) { return OSAtomicAdd64(theAmount, theValue); } int32_t OSAtomicOr32( uint32_t theMask, volatile uint32_t *theValue ) { int32_t result; OSSpinLockLock(&_atomic_lock); result = (*theValue |= theMask); OSSpinLockUnlock(&_atomic_lock); return result; } int32_t OSAtomicOr32Barrier( uint32_t theMask, volatile uint32_t *theValue ) { return OSAtomicOr32(theMask, theValue); } int32_t OSAtomicOr32Orig( uint32_t theMask, volatile uint32_t *theValue ) { int32_t result; OSSpinLockLock(&_atomic_lock); result = *theValue; *theValue |= theMask; OSSpinLockUnlock(&_atomic_lock); return result; } int32_t OSAtomicOr32OrigBarrier( uint32_t theMask, volatile uint32_t *theValue ) { return OSAtomicOr32Orig(theMask, theValue); } int32_t OSAtomicAnd32( uint32_t theMask, volatile uint32_t *theValue ) { int32_t result; OSSpinLockLock(&_atomic_lock); result = (*theValue &= theMask); OSSpinLockUnlock(&_atomic_lock); return result; } int32_t OSAtomicAnd32Barrier( uint32_t theMask, volatile uint32_t *theValue ) { return OSAtomicAnd32(theMask, theValue); } int32_t OSAtomicAnd32Orig( uint32_t theMask, volatile uint32_t *theValue ) { int32_t result; OSSpinLockLock(&_atomic_lock); result = *theValue; *theValue &= theMask; OSSpinLockUnlock(&_atomic_lock); return result; } int32_t OSAtomicAnd32OrigBarrier( uint32_t theMask, volatile uint32_t *theValue ) { return OSAtomicAnd32Orig(theMask, theValue); } int32_t OSAtomicXor32( uint32_t theMask, volatile uint32_t *theValue ) { int32_t result; OSSpinLockLock(&_atomic_lock); result = (*theValue ^= theMask); OSSpinLockUnlock(&_atomic_lock); return result; } int32_t OSAtomicXor32Barrier( uint32_t theMask, volatile uint32_t *theValue ) { return OSAtomicXor32(theMask, theValue); } int32_t OSAtomicXor32Orig( uint32_t theMask, volatile uint32_t *theValue ) { int32_t result; OSSpinLockLock(&_atomic_lock); result = *theValue; *theValue ^= theMask; OSSpinLockUnlock(&_atomic_lock); return result; } int32_t OSAtomicXor32OrigBarrier( uint32_t theMask, volatile uint32_t *theValue ) { return OSAtomicXor32Orig(theMask, theValue); } bool OSAtomicCompareAndSwap32( int32_t oldValue, int32_t newValue, volatile int32_t *theValue ) { bool result; OSSpinLockLock(&_atomic_lock); result = (*theValue == oldValue); if (result) *theValue = newValue; OSSpinLockUnlock(&_atomic_lock); return result; } bool OSAtomicCompareAndSwap32Barrier( int32_t oldValue, int32_t newValue, volatile int32_t *theValue ) { return OSAtomicCompareAndSwap32(oldValue, newValue, theValue); } bool OSAtomicCompareAndSwapInt(int oldValue, int newValue, volatile int *theValue) { return OSAtomicCompareAndSwap32(oldValue, newValue, theValue); } bool OSAtomicCompareAndSwapIntBarrier(int oldValue, int newValue, volatile int *theValue) { return OSAtomicCompareAndSwap32(oldValue, newValue, theValue); } bool OSAtomicCompareAndSwapLong(long oldValue, long newValue, volatile long *theValue) { return OSAtomicCompareAndSwap32(oldValue, newValue, (volatile int32_t *)theValue); } bool OSAtomicCompareAndSwapLongBarrier(long oldValue, long newValue, volatile long *theValue) { return OSAtomicCompareAndSwap32(oldValue, newValue, (volatile int32_t *)theValue); } bool OSAtomicCompareAndSwap64( int64_t oldValue, int64_t newValue, volatile int64_t *theValue ) { bool result; OSSpinLockLock(&_atomic_lock); result = (*theValue == oldValue); if (result) *theValue = newValue; OSSpinLockUnlock(&_atomic_lock); return result; } bool OSAtomicCompareAndSwap64Barrier( int64_t oldValue, int64_t newValue, volatile int64_t *theValue ) { return OSAtomicCompareAndSwap64(oldValue, newValue, theValue); } bool OSAtomicTestAndSet( uint32_t n, volatile void *theAddress ) { char *byteAddress = ((char*)theAddress + (n>>3)); uint32_t byteBit = (0x80>>(n&7)); bool result; OSSpinLockLock(&_atomic_lock); result = *byteAddress & byteBit; *byteAddress |= byteBit; OSSpinLockUnlock(&_atomic_lock); return result; } bool OSAtomicTestAndSetBarrier( uint32_t n, volatile void *theAddress ) { return OSAtomicTestAndSet(n, theAddress); } bool OSAtomicTestAndClear( uint32_t n, volatile void *theAddress ) { char *byteAddress = ((char*)theAddress + (n>>3)); uint32_t byteBit = (0x80>>(n&7)); bool result; OSSpinLockLock(&_atomic_lock); result = *byteAddress & byteBit; *byteAddress &= (~byteBit); OSSpinLockUnlock(&_atomic_lock); return result; } bool OSAtomicTestAndClearBarrier( uint32_t n, volatile void *theAddress ) { return OSAtomicTestAndClear(n, theAddress); } void OSMemoryBarrier( void ) { return; } bool OSAtomicCompareAndSwapPtrBarrier( void *__oldValue, void *__newValue, void * volatile *__theValue ) { return OSAtomicCompareAndSwapPtr(__oldValue, __newValue, __theValue); } bool OSAtomicCompareAndSwapPtr( void *__oldValue, void *__newValue, void * volatile *__theValue ) { bool result; OSSpinLockLock(&_atomic_lock); result = (*__theValue == __oldValue); if (result) *__theValue = __newValue; OSSpinLockUnlock(&_atomic_lock); return result; } void OSAtomicEnqueue( OSQueueHead *__list, void *__new, size_t __offset ) { OSSpinLockLock(&_atomic_lock); *((void **)((char *)__new + __offset)) = __list->opaque1; __list->opaque1 = __new; OSSpinLockUnlock(&_atomic_lock); } void* OSAtomicDequeue( OSQueueHead *__list, size_t __offset ) { void *head; OSSpinLockLock(&_atomic_lock); head = __list->opaque1; if (head != NULL) { void **next = (void **)((char *)head + __offset); __list->opaque1 = *next; } OSSpinLockUnlock(&_atomic_lock); return head; } #endif /* !defined(_ARM_ARCH_6) */ |