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