Support for the atomic operations for ARM, PPC, MIPS, SH, X86 platforms has been added.
authorMike Gorchak
Sat, 10 Oct 2009 08:06:18 +0000
changeset 336470bfe3337f8a
parent 3363 90aec03bf9fd
child 3365 9cd5b35c0184
Support for the atomic operations for ARM, PPC, MIPS, SH, X86 platforms has been added.
src/atomic/qnx/SDL_atomic.c
     1.1 --- a/src/atomic/qnx/SDL_atomic.c	Sat Oct 10 07:48:57 2009 +0000
     1.2 +++ b/src/atomic/qnx/SDL_atomic.c	Sat Oct 10 08:06:18 2009 +0000
     1.3 @@ -18,865 +18,488 @@
     1.4  
     1.5      Sam Lantinga
     1.6      slouken@libsdl.org
     1.7 +
     1.8 +    QNX native atomic operations
     1.9 +    Copyright (C) 2009 Mike Gorchak
    1.10 +    (mike@malva.ua, lestat@i.com.ua)
    1.11  */
    1.12  
    1.13  #include "SDL_stdinc.h"
    1.14  #include "SDL_atomic.h"
    1.15 +#include "SDL_error.h"
    1.16  
    1.17  #include <atomic.h>
    1.18  
    1.19 +/* SMP Exchange for PPC platform */
    1.20 +#ifdef __PPC__
    1.21 +#include <ppc/smpxchg.h>
    1.22 +#endif /* __PPC__ */
    1.23 +
    1.24 +/* SMP Exchange for ARM platform */
    1.25 +#ifdef __ARM__
    1.26 +#include <arm/smpxchg.h>
    1.27 +#endif /* __ARM__ */
    1.28 +
    1.29 +/* SMP Exchange for MIPS platform */
    1.30 +#if defined (__MIPSEB__) || defined(__MIPSEL__)
    1.31 +#include <mips/smpxchg.h>
    1.32 +#endif /* __MIPSEB__ || __MIPSEL__ */
    1.33 +
    1.34 +/* SMP Exchange for SH platform */
    1.35 +#ifdef __SH__
    1.36 +#include <sh/smpxchg.h>
    1.37 +#endif /* __SH__ */
    1.38 +
    1.39 +/* SMP Exchange for x86 platform */
    1.40 +#ifdef __X86__
    1.41 +#include <x86/smpxchg.h>
    1.42 +#endif /* __X86__ */
    1.43 +
    1.44  /*
    1.45 -  This file provides 8, 16, 32, and 64 bit atomic operations. If the
    1.46 +  This file provides 32, and 64 bit atomic operations. If the
    1.47    operations are provided by the native hardware and operating system
    1.48    they are used. If they are not then the operations are emulated
    1.49 -  using the SDL mutex operations. 
    1.50 - */
    1.51 -
    1.52 -/* 
    1.53 -  First, detect whether the operations are supported and create
    1.54 -  #defines that indicate that they do exist. The goal is to have all
    1.55 -  the system dependent code in the top part of the file so that the
    1.56 -  bottom can be use unchanged across all platforms.
    1.57 -
    1.58 -  Second, #define all the operations in each size class that are
    1.59 -  supported. Doing this allows supported operations to be used along
    1.60 -  side of emulated operations.
    1.61 +  using the SDL spin lock operations. If spin lock can not be
    1.62 +  implemented then these functions must fail.
    1.63  */
    1.64  
    1.65 -/* 
    1.66 -   Emmulated version.
    1.67 -
    1.68 -   Assume there is no support for atomic operations. All such
    1.69 -   operations are implemented using SDL mutex operations.
    1.70 - */
    1.71 -
    1.72 -#ifdef EMULATED_ATOMIC_OPERATIONS
    1.73 -#undef EMULATED_ATOMIC_OPERATIONS
    1.74 -#endif
    1.75 -
    1.76 -#ifdef EMULATED_ATOMIC_OPERATIONS
    1.77 -#define HAVE_ALL_8_BIT_OPS
    1.78 -
    1.79 -#define nativeExchange8(ptr, value)			()
    1.80 -#define nativeCompareThenSet8(ptr, oldvalue, newvalue) 	()
    1.81 -#define nativeTestThenSet8(ptr)    	     		()
    1.82 -#define nativeClear8(ptr)				()
    1.83 -#define nativeFetchThenIncrement8(ptr)   		()
    1.84 -#define nativeFetchThenDecrement8(ptr) 			()
    1.85 -#define nativeFetchThenAdd8(ptr, value) 		()
    1.86 -#define nativeFetchThenSubtract8(ptr, value) 		()
    1.87 -#define nativeIncrementThenFetch8(ptr) 			()
    1.88 -#define nativeDecrementThenFetch8(ptr) 			()
    1.89 -#define nativeAddThenFetch8(ptr, value) 		()
    1.90 -#define nativeSubtractThenFetch8(ptr, value) 		()
    1.91 -#endif
    1.92 -
    1.93 -#ifdef EMULATED_ATOMIC_OPERATIONS
    1.94 -#define HAVE_ALL_16_BIT_OPS
    1.95 -
    1.96 -#define nativeExchange16(ptr, value)			()
    1.97 -#define nativeCompareThenSet16(ptr, oldvalue, newvalue) ()
    1.98 -#define nativeTestThenSet16(ptr)    	     		()
    1.99 -#define nativeClear16(ptr)				()
   1.100 -#define nativeFetchThenIncrement16(ptr)   		()
   1.101 -#define nativeFetchThenDecrement16(ptr) 		()
   1.102 -#define nativeFetchThenAdd16(ptr, value) 		()
   1.103 -#define nativeFetchThenSubtract16(ptr, value) 		()
   1.104 -#define nativeIncrementThenFetch16(ptr) 		()
   1.105 -#define nativeDecrementThenFetch16(ptr) 		()
   1.106 -#define nativeAddThenFetch16(ptr, value) 		()
   1.107 -#define nativeSubtractThenFetch16(ptr, value) 		()
   1.108 -#endif
   1.109 +void 
   1.110 +SDL_AtomicLock(SDL_SpinLock *lock)
   1.111 +{
   1.112 +   unsigned volatile* l = (unsigned volatile*)lock;
   1.113 +   Uint32 oldval = 0;
   1.114 +   Uint32 newval = 1;
   1.115  
   1.116 -#ifdef EMULATED_ATOMIC_OPERATIONS
   1.117 -#define HAVE_ALL_64_BIT_OPS
   1.118 -
   1.119 -#define nativeExchange64(ptr, value)			()
   1.120 -#define nativeCompareThenSet64(ptr, oldvalue, newvalue) ()
   1.121 -#define nativeTestThenSet64(ptr)    	     		()
   1.122 -#define nativeClear64(ptr)				()
   1.123 -#define nativeFetchThenIncrement64(ptr)   		()
   1.124 -#define nativeFetchThenDecrement64(ptr) 		()
   1.125 -#define nativeFetchThenAdd64(ptr, value) 		()
   1.126 -#define nativeFetchThenSubtract64(ptr, value) 		()
   1.127 -#define nativeIncrementThenFetch64(ptr) 		()
   1.128 -#define nativeDecrementThenFetch64(ptr) 		()
   1.129 -#define nativeAddThenFetch64(ptr, value) 		()
   1.130 -#define nativeSubtractThenFetch64(ptr, value) 		()
   1.131 -#endif
   1.132 -
   1.133 -/*
   1.134 -If any of the operations are not provided then we must emulate some of
   1.135 -them.
   1.136 - */
   1.137 -
   1.138 -#if !defined(HAVE_ALL_8_BIT_OPS) || !defined(HAVE_ALL_16_BIT_OPS) || !defined(HAVE_ALL_64_BIT_OPS)
   1.139 -
   1.140 -#include "SDL_mutex.h"
   1.141 -#include "SDL_error.h"
   1.142 -
   1.143 -static SDL_mutex * lock = NULL;
   1.144 -
   1.145 -static __inline__ void
   1.146 -privateWaitLock()
   1.147 -{
   1.148 -   if(NULL == lock)
   1.149 +   oldval = _smp_xchg(l, newval);
   1.150 +   while(1 == oldval)
   1.151     {
   1.152 -      lock = SDL_CreateMutex();
   1.153 -      if (NULL == lock)
   1.154 -      {
   1.155 -	 SDL_SetError("SDL_atomic.c: can't create a mutex");
   1.156 -	 return;
   1.157 -      }
   1.158 -   }
   1.159 -
   1.160 -   if (-1 == SDL_LockMutex(lock))
   1.161 -   {
   1.162 -      SDL_SetError("SDL_atomic.c: can't lock mutex");
   1.163 +      oldval = _smp_xchg(l, newval);
   1.164     }
   1.165  }
   1.166  
   1.167 -static __inline__ void
   1.168 -privateUnlock()
   1.169 +void 
   1.170 +SDL_AtomicUnlock(SDL_SpinLock *lock)
   1.171  {
   1.172 -   if (-1 == SDL_UnlockMutex(lock))
   1.173 -   {
   1.174 -      SDL_SetError("SDL_atomic.c: can't unlock mutex");
   1.175 -   }
   1.176 +   unsigned volatile* l = (unsigned volatile*)lock;
   1.177 +   Uint32 newval = 0;
   1.178 +
   1.179 +   _smp_xchg(l, newval);
   1.180  }
   1.181  
   1.182 -#endif
   1.183 +/*
   1.184 +   QNX 6.4.1 supports only 32 bit atomic access
   1.185 +*/
   1.186 +
   1.187 +#undef   nativeTestThenSet32
   1.188 +#define  nativeClear32
   1.189 +#define  nativeFetchThenIncrement32
   1.190 +#define  nativeFetchThenDecrement32
   1.191 +#define  nativeFetchThenAdd32
   1.192 +#define  nativeFetchThenSubtract32
   1.193 +#define  nativeIncrementThenFetch32
   1.194 +#define  nativeDecrementThenFetch32
   1.195 +#define  nativeAddThenFetch32
   1.196 +#define  nativeSubtractThenFetch32
   1.197 +
   1.198 +#undef  nativeTestThenSet64
   1.199 +#undef  nativeClear64
   1.200 +#undef  nativeFetchThenIncrement64
   1.201 +#undef  nativeFetchThenDecrement64
   1.202 +#undef  nativeFetchThenAdd64
   1.203 +#undef  nativeFetchThenSubtract64
   1.204 +#undef  nativeIncrementThenFetch64
   1.205 +#undef  nativeDecrementThenFetch64
   1.206 +#undef  nativeAddThenFetch64
   1.207 +#undef  nativeSubtractThenFetch64
   1.208  
   1.209 -/* 8 bit atomic operations */
   1.210 +/*
   1.211 +  If any of the operations are not provided then we must emulate some
   1.212 +  of them. That means we need a nice implementation of spin locks
   1.213 +  that avoids the "one big lock" problem. We use a vector of spin
   1.214 +  locks and pick which one to use based on the address of the operand
   1.215 +  of the function.
   1.216 +
   1.217 +  To generate the index of the lock we first shift by 3 bits to get
   1.218 +  rid on the zero bits that result from 32 and 64 bit allignment of
   1.219 +  data. We then mask off all but 5 bits and use those 5 bits as an
   1.220 +  index into the table.
   1.221  
   1.222 -Uint8
   1.223 -SDL_AtomicExchange8(volatile Uint8 * ptr, Uint8 value)
   1.224 +  Picking the lock this way insures that accesses to the same data at
   1.225 +  the same time will go to the same lock. OTOH, accesses to different
   1.226 +  data have only a 1/32 chance of hitting the same lock. That should
   1.227 +  pretty much eliminate the chances of several atomic operations on
   1.228 +  different data from waiting on the same "big lock". If it isn't
   1.229 +  then the table of locks can be expanded to a new size so long as
   1.230 +  the new size is a power of two.
   1.231 +*/
   1.232 +
   1.233 +static SDL_SpinLock locks[32] = {
   1.234 +   0, 0, 0, 0, 0, 0, 0, 0,
   1.235 +   0, 0, 0, 0, 0, 0, 0, 0,
   1.236 +   0, 0, 0, 0, 0, 0, 0, 0,
   1.237 +   0, 0, 0, 0, 0, 0, 0, 0,
   1.238 +};
   1.239 +
   1.240 +static __inline__ void
   1.241 +privateWaitLock(volatile void *ptr)
   1.242  {
   1.243 -#ifdef nativeExchange8
   1.244 -   return nativeExchange8(ptr, value);
   1.245 -#else
   1.246 -   Uint8 tmp = 0;
   1.247 +#if SIZEOF_VOIDP == 4
   1.248 +   Uint32 index = ((((Uint32)ptr) >> 3) & 0x1f);
   1.249 +#elif SIZEOF_VOIDP == 8
   1.250 +   Uint64 index = ((((Uint64)ptr) >> 3) & 0x1f);
   1.251 +#endif /* SIZEOF_VOIDP */
   1.252  
   1.253 -   privateWaitLock();
   1.254 -   tmp = *ptr;
   1.255 -   *ptr = value;
   1.256 -   privateUnlock();
   1.257 -
   1.258 -   return tmp;
   1.259 -#endif
   1.260 +   SDL_AtomicLock(&locks[index]);
   1.261  }
   1.262  
   1.263 -SDL_bool
   1.264 -SDL_AtomicCompareThenSet8(volatile Uint8 * ptr, Uint8 oldvalue, Uint8 newvalue)
   1.265 +static __inline__ void
   1.266 +privateUnlock(volatile void *ptr)
   1.267  {
   1.268 -#ifdef nativeCompareThenSet8
   1.269 -   return (SDL_bool)nativeCompareThenSet8(ptr, oldvalue, newvalue);
   1.270 +#if SIZEOF_VOIDP == 4
   1.271 +   Uint32 index = ((((Uint32)ptr) >> 3) & 0x1f);
   1.272 +#elif SIZEOF_VOIDP == 8
   1.273 +   Uint64 index = ((((Uint64)ptr) >> 3) & 0x1f);
   1.274 +#endif /* SIZEOF_VOIDP */
   1.275 +
   1.276 +   SDL_AtomicUnlock(&locks[index]);
   1.277 +}
   1.278 +
   1.279 +/* 32 bit atomic operations */
   1.280 +
   1.281 +SDL_bool
   1.282 +SDL_AtomicTestThenSet32(volatile Uint32 * ptr)
   1.283 +{
   1.284 +#ifdef nativeTestThenSet32
   1.285  #else
   1.286     SDL_bool result = SDL_FALSE;
   1.287  
   1.288 -   privateWaitLock();
   1.289 -   result = (*ptr == oldvalue);
   1.290 -   if (result)
   1.291 -   {
   1.292 -      *ptr = newvalue;
   1.293 -   }
   1.294 -   privateUnlock();
   1.295 -
   1.296 -   return result;
   1.297 -#endif
   1.298 -}
   1.299 -
   1.300 -SDL_bool
   1.301 -SDL_AtomicTestThenSet8(volatile Uint8 * ptr)
   1.302 -{
   1.303 -#ifdef nativeTestThenSet8
   1.304 -   return (SDL_bool)nativeTestThenSet8(ptr);
   1.305 -#else
   1.306 -   SDL_bool result = SDL_FALSE;
   1.307 -
   1.308 -   privateWaitLock();
   1.309 +   privateWaitLock(ptr);
   1.310     result = (*ptr == 0);
   1.311     if (result)
   1.312     {
   1.313        *ptr = 1;
   1.314     }
   1.315 -   privateUnlock();
   1.316 +   privateUnlock(ptr);
   1.317  
   1.318     return result;
   1.319 -#endif
   1.320 +#endif /* nativeTestThenSet32 */
   1.321  }
   1.322  
   1.323  void
   1.324 -SDL_AtomicClear8(volatile Uint8 * ptr)
   1.325 +SDL_AtomicClear32(volatile Uint32 * ptr)
   1.326  {
   1.327 -#ifdef nativeClear8
   1.328 -   nativeClear8(ptr);
   1.329 +#ifdef nativeClear32
   1.330 +   atomic_clr(ptr, 0xFFFFFFFF);
   1.331  #else
   1.332 -   privateWaitLock();
   1.333 +   privateWaitLock(ptr);
   1.334     *ptr = 0;
   1.335 -   privateUnlock();
   1.336 +   privateUnlock(ptr);
   1.337  
   1.338     return;
   1.339 -#endif
   1.340 +#endif /* nativeClear32 */
   1.341  }
   1.342  
   1.343 -Uint8
   1.344 -SDL_AtomicFetchThenIncrement8(volatile Uint8 * ptr)
   1.345 +Uint32
   1.346 +SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr)
   1.347  {
   1.348 -#ifdef nativeFetchThenIncrement8
   1.349 -   return nativeFetchThenIncrement8(ptr);
   1.350 +#ifdef nativeFetchThenIncrement32
   1.351 +   return atomic_add_value(ptr, 0x00000001);
   1.352  #else
   1.353 -   Uint8 tmp = 0;
   1.354 +   Uint32 tmp = 0;
   1.355  
   1.356 -   privateWaitLock();
   1.357 +   privateWaitLock(ptr);
   1.358     tmp = *ptr;
   1.359     (*ptr)+= 1;
   1.360 -   privateUnlock();
   1.361 +   privateUnlock(ptr);
   1.362  
   1.363     return tmp;
   1.364 -#endif
   1.365 +#endif /* nativeFetchThenIncrement32 */
   1.366  }
   1.367  
   1.368 -Uint8
   1.369 -SDL_AtomicFetchThenDecrement8(volatile Uint8 * ptr)
   1.370 +Uint32
   1.371 +SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr)
   1.372  {
   1.373 -#ifdef nativeFetchThenDecrement8
   1.374 -   return nativeFetchThenDecrement8(ptr);
   1.375 +#ifdef nativeFetchThenDecrement32
   1.376 +   return atomic_sub_value(ptr, 0x00000001);
   1.377  #else
   1.378 -   Uint8 tmp = 0;
   1.379 +   Uint32 tmp = 0;
   1.380  
   1.381 -   privateWaitLock();
   1.382 +   privateWaitLock(ptr);
   1.383     tmp = *ptr;
   1.384     (*ptr) -= 1;
   1.385 -   privateUnlock();
   1.386 +   privateUnlock(ptr);
   1.387  
   1.388     return tmp;
   1.389 -#endif
   1.390 +#endif /* nativeFetchThenDecrement32 */
   1.391  }
   1.392  
   1.393 -Uint8
   1.394 -SDL_AtomicFetchThenAdd8(volatile Uint8 * ptr, Uint8 value)
   1.395 +Uint32
   1.396 +SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value)
   1.397  {
   1.398 -#ifdef nativeFetchThenAdd8
   1.399 -   return nativeFetchThenAdd8(ptr, value);
   1.400 +#ifdef nativeFetchThenAdd32
   1.401 +   return atomic_add_value(ptr, value);
   1.402  #else
   1.403 -   Uint8 tmp = 0;
   1.404 +   Uint32 tmp = 0;
   1.405  
   1.406 -   privateWaitLock();
   1.407 +   privateWaitLock(ptr);
   1.408     tmp = *ptr;
   1.409     (*ptr)+= value;
   1.410 -   privateUnlock();
   1.411 +   privateUnlock(ptr);
   1.412  
   1.413     return tmp;
   1.414 -#endif
   1.415 -}
   1.416 -
   1.417 -Uint8
   1.418 -SDL_AtomicFetchThenSubtract8(volatile Uint8 * ptr, Uint8 value)
   1.419 -{
   1.420 -#ifdef nativeFetchThenSubtract8
   1.421 -   return nativeFetchThenSubtract8(ptr, value);
   1.422 -#else
   1.423 -   Uint8 tmp = 0;
   1.424 -
   1.425 -   privateWaitLock();
   1.426 -   tmp = *ptr;
   1.427 -   (*ptr)-= value;
   1.428 -   privateUnlock();
   1.429 -
   1.430 -   return tmp;
   1.431 -#endif
   1.432 +#endif /* nativeFetchThenAdd32 */
   1.433  }
   1.434  
   1.435 -Uint8
   1.436 -SDL_AtomicIncrementThenFetch8(volatile Uint8 * ptr)
   1.437 +Uint32
   1.438 +SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value)
   1.439  {
   1.440 -#ifdef nativeIncrementThenFetch8
   1.441 -   return nativeIncrementThenFetch8(ptr);
   1.442 +#ifdef nativeFetchThenSubtract32
   1.443 +   return atomic_sub_value(ptr, value);
   1.444  #else
   1.445 -   Uint8 tmp = 0;
   1.446 +   Uint32 tmp = 0;
   1.447  
   1.448 -   privateWaitLock();
   1.449 -   (*ptr)+= 1;
   1.450 +   privateWaitLock(ptr);
   1.451     tmp = *ptr;
   1.452 -   privateUnlock();
   1.453 +   (*ptr)-= value;
   1.454 +   privateUnlock(ptr);
   1.455  
   1.456     return tmp;
   1.457 -#endif
   1.458 +#endif /* nativeFetchThenSubtract32 */
   1.459  }
   1.460  
   1.461 -Uint8
   1.462 -SDL_AtomicDecrementThenFetch8(volatile Uint8 * ptr)
   1.463 +Uint32
   1.464 +SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr)
   1.465  {
   1.466 -#ifdef nativeDecrementThenFetch8
   1.467 -   return nativeDecrementThenFetch8(ptr);
   1.468 +#ifdef nativeIncrementThenFetch32
   1.469 +   atomic_add(ptr, 0x00000001);
   1.470 +   return atomic_add_value(ptr, 0x00000000);
   1.471  #else
   1.472 -   Uint8 tmp = 0;
   1.473 +   Uint32 tmp = 0;
   1.474 +
   1.475 +   privateWaitLock(ptr);
   1.476 +   (*ptr)+= 1;
   1.477 +   tmp = *ptr;
   1.478 +   privateUnlock(ptr);
   1.479 +
   1.480 +   return tmp;
   1.481 +#endif /* nativeIncrementThenFetch32 */
   1.482 +}
   1.483  
   1.484 -   privateWaitLock();
   1.485 +Uint32
   1.486 +SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr)
   1.487 +{
   1.488 +#ifdef nativeDecrementThenFetch32
   1.489 +   atomic_sub(ptr, 0x00000001);
   1.490 +   return atomic_sub_value(ptr, 0x00000000);
   1.491 +#else
   1.492 +   Uint32 tmp = 0;
   1.493 +
   1.494 +   privateWaitLock(ptr);
   1.495     (*ptr)-= 1;
   1.496     tmp = *ptr;
   1.497 -   privateUnlock();
   1.498 +   privateUnlock(ptr);
   1.499  
   1.500     return tmp;
   1.501 -#endif
   1.502 +#endif /* nativeDecrementThenFetch32 */
   1.503  }
   1.504  
   1.505 -Uint8
   1.506 -SDL_AtomicAddThenFetch8(volatile Uint8 * ptr, Uint8 value)
   1.507 +Uint32
   1.508 +SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value)
   1.509  {
   1.510 -#ifdef nativeAddThenFetch8
   1.511 -   return nativeAddThenFetch8(ptr, value);
   1.512 +#ifdef nativeAddThenFetch32
   1.513 +   atomic_add(ptr, value);
   1.514 +   return atomic_add_value(ptr, 0x00000000);
   1.515  #else
   1.516 -   Uint8 tmp = 0;
   1.517 +   Uint32 tmp = 0;
   1.518  
   1.519 -   privateWaitLock();
   1.520 +   privateWaitLock(ptr);
   1.521     (*ptr)+= value;
   1.522     tmp = *ptr;
   1.523 -   privateUnlock();
   1.524 +   privateUnlock(ptr);
   1.525  
   1.526     return tmp;
   1.527 -#endif
   1.528 -}
   1.529 -
   1.530 -Uint8
   1.531 -SDL_AtomicSubtractThenFetch8(volatile Uint8 * ptr, Uint8 value)
   1.532 -{
   1.533 -#ifdef nativeSubtractThenFetch8
   1.534 -   return nativeSubtractThenFetch8(ptr, value);
   1.535 -#else
   1.536 -   Uint8 tmp = 0;
   1.537 -
   1.538 -   privateWaitLock();
   1.539 -   (*ptr)-= value;
   1.540 -   tmp = *ptr;
   1.541 -   privateUnlock();
   1.542 -
   1.543 -   return tmp;
   1.544 -#endif
   1.545 +#endif /* nativeAddThenFetch32 */
   1.546  }
   1.547  
   1.548 -/* 16 bit atomic operations */
   1.549 -
   1.550 -Uint16
   1.551 -SDL_AtomicExchange16(volatile Uint16 * ptr, Uint16 value)
   1.552 +Uint32
   1.553 +SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value)
   1.554  {
   1.555 -#ifdef nativeExchange16
   1.556 -   return nativeExchange16(ptr, value);
   1.557 +#ifdef nativeSubtractThenFetch32
   1.558 +   atomic_sub(ptr, value);
   1.559 +   return atomic_sub_value(ptr, 0x00000000);
   1.560  #else
   1.561 -   Uint16 tmp = 0;
   1.562 +   Uint32 tmp = 0;
   1.563  
   1.564 -   privateWaitLock();
   1.565 +   privateWaitLock(ptr);
   1.566 +   (*ptr)-= value;
   1.567     tmp = *ptr;
   1.568 -   *ptr = value;
   1.569 -   privateUnlock();
   1.570 +   privateUnlock(ptr);
   1.571  
   1.572     return tmp;
   1.573 -#endif
   1.574 +#endif /* nativeSubtractThenFetch32 */
   1.575  }
   1.576  
   1.577 +/* 64 bit atomic operations */
   1.578 +#ifdef SDL_HAS_64BIT_TYPE
   1.579 +
   1.580  SDL_bool
   1.581 -SDL_AtomicCompareThenSet16(volatile Uint16 * ptr, Uint16 oldvalue, Uint16 newvalue)
   1.582 +SDL_AtomicTestThenSet64(volatile Uint64 * ptr)
   1.583  {
   1.584 -#ifdef nativeCompareThenSet16
   1.585 -   return (SDL_bool)nativeCompareThenSet16(ptr, oldvalue, newvalue);
   1.586 +#ifdef nativeTestThenSet64
   1.587  #else
   1.588     SDL_bool result = SDL_FALSE;
   1.589  
   1.590 -   privateWaitLock();
   1.591 -   result = (*ptr == oldvalue);
   1.592 -   if (result)
   1.593 -   {
   1.594 -      *ptr = newvalue;
   1.595 -   }
   1.596 -   privateUnlock();
   1.597 -
   1.598 -   return result;
   1.599 -#endif
   1.600 -}
   1.601 -
   1.602 -SDL_bool
   1.603 -SDL_AtomicTestThenSet16(volatile Uint16 * ptr)
   1.604 -{
   1.605 -#ifdef nativeTestThenSet16
   1.606 -   return (SDL_bool)nativeTestThenSet16(ptr);
   1.607 -#else
   1.608 -   SDL_bool result = SDL_FALSE;
   1.609 -
   1.610 -   privateWaitLock();
   1.611 +   privateWaitLock(ptr);
   1.612     result = (*ptr == 0);
   1.613     if (result)
   1.614     {
   1.615        *ptr = 1;
   1.616     }
   1.617 -   privateUnlock();
   1.618 +   privateUnlock(ptr);
   1.619  
   1.620     return result;
   1.621 -#endif
   1.622 -}
   1.623 -
   1.624 -void
   1.625 -SDL_AtomicClear16(volatile Uint16 * ptr)
   1.626 -{
   1.627 -#ifdef nativeClear16
   1.628 -   nativeClear16(ptr);
   1.629 -#else
   1.630 -   privateWaitLock();
   1.631 -   *ptr = 0;
   1.632 -   privateUnlock();
   1.633 -
   1.634 -   return;
   1.635 -#endif
   1.636 -}
   1.637 -
   1.638 -Uint16
   1.639 -SDL_AtomicFetchThenIncrement16(volatile Uint16 * ptr)
   1.640 -{
   1.641 -#ifdef nativeFetchThenIncrement16
   1.642 -   return nativeFetchThenIncrement16(ptr);
   1.643 -#else
   1.644 -   Uint16 tmp = 0;
   1.645 -
   1.646 -   privateWaitLock();
   1.647 -   tmp = *ptr;
   1.648 -   (*ptr)+= 1;
   1.649 -   privateUnlock();
   1.650 -
   1.651 -   return tmp;
   1.652 -#endif
   1.653 -}
   1.654 -
   1.655 -Uint16
   1.656 -SDL_AtomicFetchThenDecrement16(volatile Uint16 * ptr)
   1.657 -{
   1.658 -#ifdef nativeFetchThenDecrement16
   1.659 -   return nativeFetchThenDecrement16(ptr);
   1.660 -#else
   1.661 -   Uint16 tmp = 0;
   1.662 -
   1.663 -   privateWaitLock();
   1.664 -   tmp = *ptr;
   1.665 -   (*ptr) -= 1;
   1.666 -   privateUnlock();
   1.667 -
   1.668 -   return tmp;
   1.669 -#endif
   1.670 -}
   1.671 -
   1.672 -Uint16
   1.673 -SDL_AtomicFetchThenAdd16(volatile Uint16 * ptr, Uint16 value)
   1.674 -{
   1.675 -#ifdef nativeFetchThenAdd16
   1.676 -   return nativeFetchThenAdd16(ptr, value);
   1.677 -#else
   1.678 -   Uint16 tmp = 0;
   1.679 -
   1.680 -   privateWaitLock();
   1.681 -   tmp = *ptr;
   1.682 -   (*ptr)+= value;
   1.683 -   privateUnlock();
   1.684 -
   1.685 -   return tmp;
   1.686 -#endif
   1.687 -}
   1.688 -
   1.689 -Uint16
   1.690 -SDL_AtomicFetchThenSubtract16(volatile Uint16 * ptr, Uint16 value)
   1.691 -{
   1.692 -#ifdef nativeFetchThenSubtract16
   1.693 -   return nativeFetchThenSubtract16(ptr, value);
   1.694 -#else
   1.695 -   Uint16 tmp = 0;
   1.696 -
   1.697 -   privateWaitLock();
   1.698 -   tmp = *ptr;
   1.699 -   (*ptr)-= value;
   1.700 -   privateUnlock();
   1.701 -
   1.702 -   return tmp;
   1.703 -#endif
   1.704 -}
   1.705 -
   1.706 -Uint16
   1.707 -SDL_AtomicIncrementThenFetch16(volatile Uint16 * ptr)
   1.708 -{
   1.709 -#ifdef nativeIncrementThenFetch16
   1.710 -   return nativeIncrementThenFetch16(ptr);
   1.711 -#else
   1.712 -   Uint16 tmp = 0;
   1.713 -
   1.714 -   privateWaitLock();
   1.715 -   (*ptr)+= 1;
   1.716 -   tmp = *ptr;
   1.717 -   privateUnlock();
   1.718 -
   1.719 -   return tmp;
   1.720 -#endif
   1.721 -}
   1.722 -
   1.723 -Uint16
   1.724 -SDL_AtomicDecrementThenFetch16(volatile Uint16 * ptr)
   1.725 -{
   1.726 -#ifdef nativeDecrementThenFetch16
   1.727 -   return nativeDecrementThenFetch16(ptr);
   1.728 -#else
   1.729 -   Uint16 tmp = 0;
   1.730 -
   1.731 -   privateWaitLock();
   1.732 -   (*ptr)-= 1;
   1.733 -   tmp = *ptr;
   1.734 -   privateUnlock();
   1.735 -
   1.736 -   return tmp;
   1.737 -#endif
   1.738 -}
   1.739 -
   1.740 -Uint16
   1.741 -SDL_AtomicAddThenFetch16(volatile Uint16 * ptr, Uint16 value)
   1.742 -{
   1.743 -#ifdef nativeAddThenFetch16
   1.744 -   return nativeAddThenFetch16(ptr, value);
   1.745 -#else
   1.746 -   Uint16 tmp = 0;
   1.747 -
   1.748 -   privateWaitLock();
   1.749 -   (*ptr)+= value;
   1.750 -   tmp = *ptr;
   1.751 -   privateUnlock();
   1.752 -
   1.753 -   return tmp;
   1.754 -#endif
   1.755 -}
   1.756 -
   1.757 -Uint16
   1.758 -SDL_AtomicSubtractThenFetch16(volatile Uint16 * ptr, Uint16 value)
   1.759 -{
   1.760 -#ifdef nativeSubtractThenFetch16
   1.761 -   return nativeSubtractThenFetch16(ptr, value);
   1.762 -#else
   1.763 -   Uint16 tmp = 0;
   1.764 -
   1.765 -   privateWaitLock();
   1.766 -   (*ptr)-= value;
   1.767 -   tmp = *ptr;
   1.768 -   privateUnlock();
   1.769 -
   1.770 -   return tmp;
   1.771 -#endif
   1.772 -}
   1.773 -
   1.774 -/* 64 bit atomic operations */
   1.775 -#ifdef SDL_HAS_64BIT_TYPE
   1.776 -
   1.777 -Uint64
   1.778 -SDL_AtomicExchange64(volatile Uint64 * ptr, Uint64 value)
   1.779 -{
   1.780 -#ifdef nativeExchange64
   1.781 -   return nativeExchange64(ptr, value);
   1.782 -#else
   1.783 -   Uint64 tmp = 0;
   1.784 -
   1.785 -   privateWaitLock();
   1.786 -   tmp = *ptr;
   1.787 -   *ptr = value;
   1.788 -   privateUnlock();
   1.789 -
   1.790 -   return tmp;
   1.791 -#endif
   1.792 -}
   1.793 -
   1.794 -SDL_bool
   1.795 -SDL_AtomicCompareThenSet64(volatile Uint64 * ptr, Uint64 oldvalue, Uint64 newvalue)
   1.796 -{
   1.797 -#ifdef nativeCompareThenSet64
   1.798 -   return (SDL_bool)nativeCompareThenSet64(ptr, oldvalue, newvalue);
   1.799 -#else
   1.800 -   SDL_bool result = SDL_FALSE;
   1.801 -
   1.802 -   privateWaitLock();
   1.803 -   result = (*ptr == oldvalue);
   1.804 -   if (result)
   1.805 -   {
   1.806 -      *ptr = newvalue;
   1.807 -   }
   1.808 -   privateUnlock();
   1.809 -
   1.810 -   return result;
   1.811 -#endif
   1.812 -}
   1.813 -
   1.814 -SDL_bool
   1.815 -SDL_AtomicTestThenSet64(volatile Uint64 * ptr)
   1.816 -{
   1.817 -#ifdef nativeTestThenSet64
   1.818 -   return (SDL_bool)nativeTestThenSet64(ptr);
   1.819 -#else
   1.820 -   SDL_bool result = SDL_FALSE;
   1.821 -
   1.822 -   privateWaitLock();
   1.823 -   result = (*ptr == 0);
   1.824 -   if (result)
   1.825 -   {
   1.826 -      *ptr = 1;
   1.827 -   }
   1.828 -   privateUnlock();
   1.829 -
   1.830 -   return result;
   1.831 -#endif
   1.832 +#endif /* nativeTestThenSet64 */
   1.833  }
   1.834  
   1.835  void
   1.836  SDL_AtomicClear64(volatile Uint64 * ptr)
   1.837  {
   1.838  #ifdef nativeClear64
   1.839 -   nativeClear64(ptr);
   1.840  #else
   1.841 -   privateWaitLock();
   1.842 +   privateWaitLock(ptr);
   1.843     *ptr = 0;
   1.844 -   privateUnlock();
   1.845 +   privateUnlock(ptr);
   1.846  
   1.847     return;
   1.848 -#endif
   1.849 +#endif /* nativeClear64 */
   1.850  }
   1.851  
   1.852  Uint64
   1.853  SDL_AtomicFetchThenIncrement64(volatile Uint64 * ptr)
   1.854  {
   1.855  #ifdef nativeFetchThenIncrement64
   1.856 -   return nativeFetchThenIncrement64(ptr);
   1.857  #else
   1.858     Uint64 tmp = 0;
   1.859  
   1.860 -   privateWaitLock();
   1.861 +   privateWaitLock(ptr);
   1.862     tmp = *ptr;
   1.863     (*ptr)+= 1;
   1.864 -   privateUnlock();
   1.865 +   privateUnlock(ptr);
   1.866  
   1.867     return tmp;
   1.868 -#endif
   1.869 +#endif /* nativeFetchThenIncrement64 */
   1.870  }
   1.871  
   1.872  Uint64
   1.873  SDL_AtomicFetchThenDecrement64(volatile Uint64 * ptr)
   1.874  {
   1.875  #ifdef nativeFetchThenDecrement64
   1.876 -   return nativeFetchThenDecrement64(ptr);
   1.877  #else
   1.878     Uint64 tmp = 0;
   1.879  
   1.880 -   privateWaitLock();
   1.881 +   privateWaitLock(ptr);
   1.882     tmp = *ptr;
   1.883     (*ptr) -= 1;
   1.884 -   privateUnlock();
   1.885 +   privateUnlock(ptr);
   1.886  
   1.887     return tmp;
   1.888 -#endif
   1.889 +#endif /* nativeFetchThenDecrement64 */
   1.890  }
   1.891  
   1.892  Uint64
   1.893  SDL_AtomicFetchThenAdd64(volatile Uint64 * ptr, Uint64 value)
   1.894  {
   1.895  #ifdef nativeFetchThenAdd64
   1.896 -   return nativeFetchThenAdd64(ptr, value);
   1.897  #else
   1.898     Uint64 tmp = 0;
   1.899  
   1.900 -   privateWaitLock();
   1.901 +   privateWaitLock(ptr);
   1.902     tmp = *ptr;
   1.903     (*ptr)+= value;
   1.904 -   privateUnlock();
   1.905 +   privateUnlock(ptr);
   1.906  
   1.907     return tmp;
   1.908 -#endif
   1.909 +#endif /* nativeFetchThenAdd64 */
   1.910  }
   1.911  
   1.912  Uint64
   1.913  SDL_AtomicFetchThenSubtract64(volatile Uint64 * ptr, Uint64 value)
   1.914  {
   1.915  #ifdef nativeFetchThenSubtract64
   1.916 -   return nativeFetchThenSubtract64(ptr, value);
   1.917  #else
   1.918     Uint64 tmp = 0;
   1.919  
   1.920 -   privateWaitLock();
   1.921 +   privateWaitLock(ptr);
   1.922     tmp = *ptr;
   1.923     (*ptr)-= value;
   1.924 -   privateUnlock();
   1.925 +   privateUnlock(ptr);
   1.926  
   1.927     return tmp;
   1.928 -#endif
   1.929 +#endif /* nativeFetchThenSubtract64 */
   1.930  }
   1.931  
   1.932  Uint64
   1.933  SDL_AtomicIncrementThenFetch64(volatile Uint64 * ptr)
   1.934  {
   1.935  #ifdef nativeIncrementThenFetch64
   1.936 -   return nativeIncrementThenFetch64(ptr);
   1.937  #else
   1.938     Uint64 tmp = 0;
   1.939  
   1.940 -   privateWaitLock();
   1.941 +   privateWaitLock(ptr);
   1.942     (*ptr)+= 1;
   1.943     tmp = *ptr;
   1.944 -   privateUnlock();
   1.945 +   privateUnlock(ptr);
   1.946  
   1.947     return tmp;
   1.948 -#endif
   1.949 +#endif /* nativeIncrementThenFetch64 */
   1.950  }
   1.951  
   1.952  Uint64
   1.953  SDL_AtomicDecrementThenFetch64(volatile Uint64 * ptr)
   1.954  {
   1.955  #ifdef nativeDecrementThenFetch64
   1.956 -   return nativeDecrementThenFetch64(ptr);
   1.957  #else
   1.958     Uint64 tmp = 0;
   1.959  
   1.960 -   privateWaitLock();
   1.961 +   privateWaitLock(ptr);
   1.962     (*ptr)-= 1;
   1.963     tmp = *ptr;
   1.964 -   privateUnlock();
   1.965 +   privateUnlock(ptr);
   1.966  
   1.967     return tmp;
   1.968 -#endif
   1.969 +#endif /* nativeDecrementThenFetch64 */
   1.970  }
   1.971  
   1.972  Uint64
   1.973  SDL_AtomicAddThenFetch64(volatile Uint64 * ptr, Uint64 value)
   1.974  {
   1.975  #ifdef nativeAddThenFetch64
   1.976 -   return nativeAddThenFetch64(ptr, value);
   1.977  #else
   1.978     Uint64 tmp = 0;
   1.979  
   1.980 -   privateWaitLock();
   1.981 +   privateWaitLock(ptr);
   1.982     (*ptr)+= value;
   1.983     tmp = *ptr;
   1.984 -   privateUnlock();
   1.985 +   privateUnlock(ptr);
   1.986  
   1.987     return tmp;
   1.988 -#endif
   1.989 +#endif /* nativeAddThenFetch64 */
   1.990  }
   1.991  
   1.992  Uint64
   1.993  SDL_AtomicSubtractThenFetch64(volatile Uint64 * ptr, Uint64 value)
   1.994  {
   1.995  #ifdef nativeSubtractThenFetch64
   1.996 -   return nativeSubtractThenFetch64(ptr, value);
   1.997  #else
   1.998     Uint64 tmp = 0;
   1.999  
  1.1000 -   privateWaitLock();
  1.1001 +   privateWaitLock(ptr);
  1.1002     (*ptr)-= value;
  1.1003     tmp = *ptr;
  1.1004 -   privateUnlock();
  1.1005 -
  1.1006 -   return tmp;
  1.1007 -#endif
  1.1008 -}
  1.1009 -#endif
  1.1010 -
  1.1011 -/* QNX native 32 bit atomic operations */
  1.1012 -
  1.1013 -Uint32
  1.1014 -SDL_AtomicExchange32(volatile Uint32 * ptr, Uint32 value)
  1.1015 -{
  1.1016 -   Uint32 tmp = 0;
  1.1017 -
  1.1018 -   privateWaitLock();
  1.1019 -   tmp = *ptr;
  1.1020 -   *ptr = value;
  1.1021 -   privateUnlock();
  1.1022 +   privateUnlock(ptr);
  1.1023  
  1.1024     return tmp;
  1.1025 -}
  1.1026 -
  1.1027 -SDL_bool
  1.1028 -SDL_AtomicCompareThenSet32(volatile Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
  1.1029 -{
  1.1030 -   SDL_bool result = SDL_FALSE;
  1.1031 -
  1.1032 -   privateWaitLock();
  1.1033 -   result = (*ptr == oldvalue);
  1.1034 -   if (result)
  1.1035 -   {
  1.1036 -      *ptr = newvalue;
  1.1037 -   }
  1.1038 -   privateUnlock();
  1.1039 -
  1.1040 -   return result;
  1.1041 -}
  1.1042 -
  1.1043 -SDL_bool
  1.1044 -SDL_AtomicTestThenSet32(volatile Uint32 * ptr)
  1.1045 -{
  1.1046 -   SDL_bool result = SDL_FALSE;
  1.1047 -
  1.1048 -   privateWaitLock();
  1.1049 -   result = (*ptr == 0);
  1.1050 -   if (result)
  1.1051 -   {
  1.1052 -      *ptr = 1;
  1.1053 -   }
  1.1054 -   privateUnlock();
  1.1055 -
  1.1056 -   return result;
  1.1057 +#endif /* nativeSubtractThenFetch64 */
  1.1058  }
  1.1059  
  1.1060 -void
  1.1061 -SDL_AtomicClear32(volatile Uint32 * ptr)
  1.1062 -{
  1.1063 -   atomic_clr(ptr, 0xFFFFFFFF);
  1.1064 -}
  1.1065 -
  1.1066 -Uint32
  1.1067 -SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr)
  1.1068 -{
  1.1069 -   return atomic_add_value(ptr, 0x00000001);
  1.1070 -}
  1.1071 -
  1.1072 -Uint32
  1.1073 -SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr)
  1.1074 -{
  1.1075 -   return atomic_sub_value(ptr, 0x00000001);
  1.1076 -}
  1.1077 -
  1.1078 -Uint32
  1.1079 -SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value)
  1.1080 -{
  1.1081 -   return atomic_add_value(ptr, value);
  1.1082 -}
  1.1083 -
  1.1084 -Uint32
  1.1085 -SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value)
  1.1086 -{
  1.1087 -   return atomic_sub_value(ptr, value);
  1.1088 -}
  1.1089 -
  1.1090 -Uint32
  1.1091 -SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr)
  1.1092 -{
  1.1093 -   atomic_add(ptr, 0x00000001);
  1.1094 -   return atomic_add_value(ptr, 0x00000000);
  1.1095 -}
  1.1096 -
  1.1097 -Uint32
  1.1098 -SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr)
  1.1099 -{
  1.1100 -   atomic_sub(ptr, 0x00000001);
  1.1101 -   return atomic_sub_value(ptr, 0x00000000);
  1.1102 -}
  1.1103 -
  1.1104 -Uint32
  1.1105 -SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value)
  1.1106 -{
  1.1107 -   atomic_add(ptr, value);
  1.1108 -   return atomic_add_value(ptr, 0x00000000);
  1.1109 -}
  1.1110 -
  1.1111 -Uint32
  1.1112 -SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value)
  1.1113 -{
  1.1114 -   atomic_sub(ptr, value);
  1.1115 -   return atomic_sub_value(ptr, 0x00000000);
  1.1116 -}
  1.1117 +#endif /* SDL_HAS_64BIT_TYPE */