The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
authorBob Pendleton <bob@pendleton.com>
Thu, 17 Sep 2009 20:35:12 +0000
changeset 326172b542f34739
parent 3260 85bf3f297b5c
child 3262 e3d33bd599eb
The new, cleaner, version of the atomic operations. The dummy code is what you should start working with to port atomic ops.
The linux code appears to be complete and *should* be the base of all Unix and GCC based versions. The macosx and win32 versions
are currently just copies of the dummy code. I will begin working on the windows version as soon as this check in is done. I
need someone to work on the Mac OS X version.

I'm afraid that this check in will break QNX (Sorry!)
include/SDL_atomic.h
src/atomic/dummy/SDL_atomic.c
src/atomic/linux/SDL_atomic.c
src/atomic/macosx/SDL_atomic.c
src/atomic/win32/SDL_atomic.c
test/testatomic.c
     1.1 --- a/include/SDL_atomic.h	Mon Sep 07 16:04:44 2009 +0000
     1.2 +++ b/include/SDL_atomic.h	Thu Sep 17 20:35:12 2009 +0000
     1.3 @@ -18,6 +18,8 @@
     1.4  
     1.5      Sam Lantinga
     1.6      slouken@libsdl.org
     1.7 +
     1.8 +    Contributed by Bob Pendleton, bob@pendleton.com
     1.9   */
    1.10  
    1.11  /**
    1.12 @@ -46,48 +48,50 @@
    1.13   * processor specific atomic operations. When possible they are
    1.14   * implemented as true processor specific atomic operations. When that
    1.15   * is not possible the are implemented using locks that *do* use the
    1.16 - * available atomic operations. In rare cases they may be implemented
    1.17 - * using SDL's mutex fuctions.
    1.18 + * available atomic operations.
    1.19 + *
    1.20 + * At the very minimum spin locks must be implemented. Without spin
    1.21 + * locks it is not possible (AFAICT) to emulate the rest of the atomic
    1.22 + * operations.
    1.23   */
    1.24  
    1.25  /* Function prototypes */
    1.26  
    1.27 +/**
    1.28 + * SDL AtomicLock.
    1.29 + * 
    1.30 + * The spin lock functions and type are required and can not be
    1.31 + * emulated because they are used in the emulation code.
    1.32 + */
    1.33 +
    1.34 +typedef volatile Uint32 SDL_SpinLock;
    1.35 +
    1.36 +/**
    1.37 + * \fn  void SDL_AtomicLock(SDL_SpinLock *lock);
    1.38 + *
    1.39 + * \brief Lock a spin lock by setting it to a none zero value.
    1.40 + *
    1.41 + * \param lock points to the lock.
    1.42 + *
    1.43 + */
    1.44 +extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
    1.45 +
    1.46 +/**
    1.47 + * \fn  void SDL_AtomicUnlock(SDL_SpinLock *lock);
    1.48 + *
    1.49 + * \brief Unlock a spin lock by setting it to 0. Always returns immediately
    1.50 + *
    1.51 + * \param lock points to the lock.
    1.52 + *
    1.53 + */
    1.54 +extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
    1.55 +
    1.56  /* 32 bit atomic operations */
    1.57  
    1.58  /**
    1.59 - * \fn int SDL_AtomicExchange32(volatile Uint32 * ptr, Uint32 value)
    1.60 - *
    1.61 - * \brief Atomically exchange two 32 bit values.
    1.62 - *
    1.63 - * \return the value point to by ptr.
    1.64 - *
    1.65 - * \param ptr points to the value to be fetched from *ptr.  
    1.66 - * \param value is value to be stored at *ptr.
    1.67 - *
    1.68 - * The current value stored at *ptr is returned and it is replaced
    1.69 - * with value. This function can be used to implement SDL_TestThenSet.
    1.70 - *
    1.71 - */
    1.72 -extern DECLSPEC Uint32 SDLCALL SDL_AtomicExchange32(volatile Uint32 * ptr, Uint32 value);
    1.73 -
    1.74 -/**
    1.75 - * \fn int SDL_AtomicCompareThenSet32(volatile Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
    1.76 - *
    1.77 - * \brief If *ptr == oldvalue then replace the contents of *ptr by new value. 
    1.78 - *
    1.79 - * \return true if the newvalue was stored.
    1.80 - *
    1.81 - * \param *ptr is the value to be compared and replaced.
    1.82 - * \param oldvalue is value to be compared to *ptr.
    1.83 - * \param newvalue is value to be stored at *ptr.
    1.84 - *
    1.85 - */
    1.86 -extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet32(volatile Uint32 * ptr,
    1.87 -                                                            Uint32 oldvalue, Uint32 newvalue);
    1.88 -/**
    1.89   * \fn  SDL_bool SDL_AtomicTestThenSet32(volatile Uint32 * ptr);
    1.90   *
    1.91 - * \brief Check to see if *ptr == 0 and set it to non-zero.
    1.92 + * \brief Check to see if *ptr == 0 and set it to 1.
    1.93   *
    1.94   * \return SDL_True if the value pointed to by ptr was zero and
    1.95   * SDL_False if it was not zero
    1.96 @@ -211,9 +215,6 @@
    1.97  /* 64 bit atomic operations */
    1.98  #ifdef SDL_HAS_64BIT_TYPE
    1.99  
   1.100 -extern DECLSPEC Uint64 SDLCALL SDL_AtomicExchange64(volatile Uint64 * ptr, Uint64 value);
   1.101 -extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet64(volatile Uint64 * ptr,
   1.102 -                                                            Uint64 oldvalue, Uint64 newvalue);
   1.103  extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet64(volatile Uint64 * ptr);
   1.104  extern DECLSPEC void SDLCALL SDL_AtomicClear64(volatile Uint64 * ptr);
   1.105  extern DECLSPEC Uint64 SDLCALL SDL_AtomicFetchThenIncrement64(volatile Uint64 * ptr);
     2.1 --- a/src/atomic/dummy/SDL_atomic.c	Mon Sep 07 16:04:44 2009 +0000
     2.2 +++ b/src/atomic/dummy/SDL_atomic.c	Thu Sep 17 20:35:12 2009 +0000
     2.3 @@ -1,639 +1,162 @@
     2.4  /*
     2.5 -    SDL - Simple DirectMedia Layer
     2.6 -    Copyright (C) 1997-2009 Sam Lantinga
     2.7 +  SDL - Simple DirectMedia Layer
     2.8 +  Copyright (C) 1997-2009 Sam Lantinga
     2.9  
    2.10 -    This library is free software; you can redistribute it and/or
    2.11 -    modify it under the terms of the GNU Lesser General Public
    2.12 -    License as published by the Free Software Foundation; either
    2.13 -    version 2.1 of the License, or (at your option) any later version.
    2.14 +  This library is free software; you can redistribute it and/or
    2.15 +  modify it under the terms of the GNU Lesser General Public
    2.16 +  License as published by the Free Software Foundation; either
    2.17 +  version 2.1 of the License, or (at your option) any later version.
    2.18  
    2.19 -    This library is distributed in the hope that it will be useful,
    2.20 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.21 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.22 -    Lesser General Public License for more details.
    2.23 +  This library is distributed in the hope that it will be useful,
    2.24 +  but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.25 +  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.26 +  Lesser General Public License for more details.
    2.27  
    2.28 -    You should have received a copy of the GNU Lesser General Public
    2.29 -    License along with this library; if not, write to the Free Software
    2.30 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2.31 +  You should have received a copy of the GNU Lesser General Public
    2.32 +  License along with this library; if not, write to the Free Software
    2.33 +  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2.34  
    2.35 -    Sam Lantinga
    2.36 -    slouken@libsdl.org
    2.37 +  Sam Lantinga
    2.38 +  slouken@libsdl.org
    2.39 +
    2.40 +  Contributed by Bob Pendleton, bob@pendleton.com
    2.41  */
    2.42  
    2.43  #include "SDL_stdinc.h"
    2.44  #include "SDL_atomic.h"
    2.45  
    2.46 +#include "SDL_error.h"
    2.47 +
    2.48  /*
    2.49 -  This file provides 8, 16, 32, and 64 bit atomic operations. If the
    2.50 +  This file provides 32, and 64 bit atomic operations. If the
    2.51    operations are provided by the native hardware and operating system
    2.52    they are used. If they are not then the operations are emulated
    2.53 -  using the SDL mutex operations. 
    2.54 - */
    2.55 -
    2.56 -/* 
    2.57 -  First, detect whether the operations are supported and create
    2.58 -  #defines that indicate that they do exist. The goal is to have all
    2.59 -  the system dependent code in the top part of the file so that the
    2.60 -  bottom can be use unchanged across all platforms.
    2.61 -
    2.62 -  Second, #define all the operations in each size class that are
    2.63 -  supported. Doing this allows supported operations to be used along
    2.64 -  side of emulated operations.
    2.65 +  using the SDL spin lock operations. If spin lock can not be
    2.66 +  implemented then these functions must fail.
    2.67  */
    2.68  
    2.69  /* 
    2.70 -   Emmulated version.
    2.71 +  DUMMY VERSION.
    2.72  
    2.73 -   Assume there is no support for atomic operations. All such
    2.74 -   operations are implemented using SDL mutex operations.
    2.75 - */
    2.76 +  This version of the code assumes there is no support for atomic
    2.77 +  operations. Therefore, every function sets the SDL error
    2.78 +  message. Oddly enough, if you only have one thread then this
    2.79 +  version actuallys works.
    2.80 +*/
    2.81  
    2.82 -#ifdef EMULATED_ATOMIC_OPERATIONS
    2.83 -#undef EMULATED_ATOMIC_OPERATIONS
    2.84 +/*
    2.85 +  Native spinlock routines. Because this is the dummy implementation
    2.86 +  these will always call SDL_SetError() and do nothing.
    2.87 +*/
    2.88 +
    2.89 +void 
    2.90 +SDL_AtomicLock(SDL_SpinLock *lock)
    2.91 +{
    2.92 +   SDL_SetError("SDL_atomic.c: is not implemented on this platform");
    2.93 +}
    2.94 +
    2.95 +void 
    2.96 +SDL_AtomicUnlock(SDL_SpinLock *lock)
    2.97 +{
    2.98 +   SDL_SetError("SDL_atomic.c: is not implemented on this platform");
    2.99 +}
   2.100 +
   2.101 +/*
   2.102 +  Note that platform specific versions can be built from this version
   2.103 +  by changing the #undefs to #defines and adding platform specific
   2.104 +  code.
   2.105 +*/
   2.106 +
   2.107 +#undef  nativeTestThenSet32
   2.108 +#undef  nativeClear32
   2.109 +#undef  nativeFetchThenIncrement32
   2.110 +#undef  nativeFetchThenDecrement32
   2.111 +#undef  nativeFetchThenAdd32
   2.112 +#undef  nativeFetchThenSubtract32
   2.113 +#undef  nativeIncrementThenFetch32
   2.114 +#undef  nativeDecrementThenFetch32
   2.115 +#undef  nativeAddThenFetch32
   2.116 +#undef  nativeSubtractThenFetch32
   2.117 +
   2.118 +#undef  nativeTestThenSet64
   2.119 +#undef  nativeClear64
   2.120 +#undef  nativeFetchThenIncrement64
   2.121 +#undef  nativeFetchThenDecrement64
   2.122 +#undef  nativeFetchThenAdd64
   2.123 +#undef  nativeFetchThenSubtract64
   2.124 +#undef  nativeIncrementThenFetch64
   2.125 +#undef  nativeDecrementThenFetch64
   2.126 +#undef  nativeAddThenFetch64
   2.127 +#undef  nativeSubtractThenFetch64
   2.128 +
   2.129 +/* 
   2.130 +  If any of the operations are not provided then we must emulate some
   2.131 +  of them. That means we need a nice implementation of spin locks
   2.132 +  that avoids the "one big lock" problem. We use a vector of spin
   2.133 +  locks and pick which one to use based on the address of the operand
   2.134 +  of the function.
   2.135 +
   2.136 +  To generate the index of the lock we first shift by 3 bits to get
   2.137 +  rid on the zero bits that result from 32 and 64 bit allignment of
   2.138 +  data. We then mask off all but 5 bits and use those 5 bits as an
   2.139 +  index into the table. 
   2.140 +
   2.141 +  Picking the lock this way insures that accesses to the same data at
   2.142 +  the same time will go to the same lock. OTOH, accesses to different
   2.143 +  data have only a 1/32 chance of hitting the same lock. That should
   2.144 +  pretty much eliminate the chances of several atomic operations on
   2.145 +  different data from waiting on the same "big lock". If it isn't
   2.146 +  then the table of locks can be expanded to a new size so long as
   2.147 +  the new size if a power of two.
   2.148 +*/
   2.149 +
   2.150 +static SDL_SpinLock locks[32] = {
   2.151 +   0, 0, 0, 0, 0, 0, 0, 0,
   2.152 +   0, 0, 0, 0, 0, 0, 0, 0,
   2.153 +   0, 0, 0, 0, 0, 0, 0, 0,
   2.154 +   0, 0, 0, 0, 0, 0, 0, 0,
   2.155 +};
   2.156 +
   2.157 +static __inline__ void
   2.158 +privateWaitLock(volatile void *ptr)
   2.159 +{
   2.160 +#if SIZEOF_VOIDP == 4
   2.161 +   Uint32 index = ((((Uint32)ptr) >> 3) & 0x1f);
   2.162 +#elif SIZEOF_VOIDP == 8
   2.163 +   Uint64 index = ((((Uint64)ptr) >> 3) & 0x1f);
   2.164  #endif
   2.165  
   2.166 -#ifdef EMULATED_ATOMIC_OPERATIONS
   2.167 -#define HAVE_ALL_8_BIT_OPS
   2.168 -
   2.169 -#define nativeExchange8(ptr, value)			()
   2.170 -#define nativeCompareThenSet8(ptr, oldvalue, newvalue) 	()
   2.171 -#define nativeTestThenSet8(ptr)    	     		()
   2.172 -#define nativeClear8(ptr)				()
   2.173 -#define nativeFetchThenIncrement8(ptr)   		()
   2.174 -#define nativeFetchThenDecrement8(ptr) 			()
   2.175 -#define nativeFetchThenAdd8(ptr, value) 		()
   2.176 -#define nativeFetchThenSubtract8(ptr, value) 		()
   2.177 -#define nativeIncrementThenFetch8(ptr) 			()
   2.178 -#define nativeDecrementThenFetch8(ptr) 			()
   2.179 -#define nativeAddThenFetch8(ptr, value) 		()
   2.180 -#define nativeSubtractThenFetch8(ptr, value) 		()
   2.181 -#endif
   2.182 -
   2.183 -#ifdef EMULATED_ATOMIC_OPERATIONS
   2.184 -#define HAVE_ALL_16_BIT_OPS
   2.185 -
   2.186 -#define nativeExchange16(ptr, value)			()
   2.187 -#define nativeCompareThenSet16(ptr, oldvalue, newvalue) ()
   2.188 -#define nativeTestThenSet16(ptr)    	     		()
   2.189 -#define nativeClear16(ptr)				()
   2.190 -#define nativeFetchThenIncrement16(ptr)   		()
   2.191 -#define nativeFetchThenDecrement16(ptr) 		()
   2.192 -#define nativeFetchThenAdd16(ptr, value) 		()
   2.193 -#define nativeFetchThenSubtract16(ptr, value) 		()
   2.194 -#define nativeIncrementThenFetch16(ptr) 		()
   2.195 -#define nativeDecrementThenFetch16(ptr) 		()
   2.196 -#define nativeAddThenFetch16(ptr, value) 		()
   2.197 -#define nativeSubtractThenFetch16(ptr, value) 		()
   2.198 -#endif
   2.199 -
   2.200 -#ifdef EMULATED_ATOMIC_OPERATIONS
   2.201 -#define HAVE_ALL_32_BIT_OPS
   2.202 -
   2.203 -#define nativeExchange32(ptr, value)			()
   2.204 -#define nativeCompareThenSet32(ptr, oldvalue, newvalue) ()
   2.205 -#define nativeTestThenSet32(ptr)    	     		()
   2.206 -#define nativeClear32(ptr)				()
   2.207 -#define nativeFetchThenIncrement32(ptr)   		()
   2.208 -#define nativeFetchThenDecrement32(ptr) 		()
   2.209 -#define nativeFetchThenAdd32(ptr, value) 		()
   2.210 -#define nativeFetchThenSubtract32(ptr, value) 		()
   2.211 -#define nativeIncrementThenFetch32(ptr) 		()
   2.212 -#define nativeDecrementThenFetch32(ptr) 		()
   2.213 -#define nativeAddThenFetch32(ptr, value) 		()
   2.214 -#define nativeSubtractThenFetch32(ptr, value) 		()
   2.215 -#endif
   2.216 -
   2.217 -#ifdef EMULATED_ATOMIC_OPERATIONS
   2.218 -#define HAVE_ALL_64_BIT_OPS
   2.219 -
   2.220 -#define nativeExchange64(ptr, value)			()
   2.221 -#define nativeCompareThenSet64(ptr, oldvalue, newvalue) ()
   2.222 -#define nativeTestThenSet64(ptr)    	     		()
   2.223 -#define nativeClear64(ptr)				()
   2.224 -#define nativeFetchThenIncrement64(ptr)   		()
   2.225 -#define nativeFetchThenDecrement64(ptr) 		()
   2.226 -#define nativeFetchThenAdd64(ptr, value) 		()
   2.227 -#define nativeFetchThenSubtract64(ptr, value) 		()
   2.228 -#define nativeIncrementThenFetch64(ptr) 		()
   2.229 -#define nativeDecrementThenFetch64(ptr) 		()
   2.230 -#define nativeAddThenFetch64(ptr, value) 		()
   2.231 -#define nativeSubtractThenFetch64(ptr, value) 		()
   2.232 -#endif
   2.233 -
   2.234 -/* 
   2.235 -If any of the operations are not provided then we must emulate some of
   2.236 -them.
   2.237 - */
   2.238 -
   2.239 -#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)
   2.240 -
   2.241 -#include "SDL_mutex.h"
   2.242 -#include "SDL_error.h"
   2.243 -
   2.244 -static SDL_mutex * lock = NULL;
   2.245 -
   2.246 -static __inline__ void
   2.247 -privateWaitLock()
   2.248 -{
   2.249 -   if(NULL == lock)
   2.250 -   {
   2.251 -      lock = SDL_CreateMutex();
   2.252 -      if (NULL == lock)
   2.253 -      {
   2.254 -	 SDL_SetError("SDL_atomic.c: can't create a mutex");
   2.255 -	 return;
   2.256 -      }
   2.257 -   }
   2.258 -
   2.259 -   if (-1 == SDL_LockMutex(lock))
   2.260 -   {
   2.261 -      SDL_SetError("SDL_atomic.c: can't lock mutex");
   2.262 -   }
   2.263 +   SDL_AtomicLock(&locks[index]);
   2.264  }
   2.265  
   2.266  static __inline__ void
   2.267 -privateUnlock()
   2.268 +privateUnlock(volatile void *ptr)
   2.269  {
   2.270 -   if (-1 == SDL_UnlockMutex(lock))
   2.271 -   {
   2.272 -      SDL_SetError("SDL_atomic.c: can't unlock mutex");
   2.273 -   }
   2.274 +#if SIZEOF_VOIDP == 4
   2.275 +   Uint32 index = ((((Uint32)ptr) >> 3) & 0x1f);
   2.276 +#elif SIZEOF_VOIDP == 8
   2.277 +   Uint64 index = ((((Uint64)ptr) >> 3) & 0x1f);
   2.278 +#endif
   2.279 +
   2.280 +   SDL_AtomicUnlock(&locks[index]);
   2.281  }
   2.282  
   2.283 -#endif
   2.284 -
   2.285 -/* 8 bit atomic operations */
   2.286 -
   2.287 -Uint8
   2.288 -SDL_AtomicExchange8(volatile Uint8 * ptr, Uint8 value)
   2.289 -{
   2.290 -#ifdef nativeExchange8
   2.291 -   return nativeExchange8(ptr, value);
   2.292 -#else
   2.293 -   Uint8 tmp = 0;
   2.294 -
   2.295 -   privateWaitLock();
   2.296 -   tmp = *ptr;
   2.297 -   *ptr = value;
   2.298 -   privateUnlock();
   2.299 -
   2.300 -   return tmp;
   2.301 -#endif
   2.302 -}
   2.303 +/* 32 bit atomic operations */
   2.304  
   2.305  SDL_bool
   2.306 -SDL_AtomicCompareThenSet8(volatile Uint8 * ptr, Uint8 oldvalue, Uint8 newvalue)
   2.307 +SDL_AtomicTestThenSet32(volatile Uint32 * ptr)
   2.308  {
   2.309 -#ifdef nativeCompareThenSet8
   2.310 -   return (SDL_bool)nativeCompareThenSet8(ptr, oldvalue, newvalue);
   2.311 +#ifdef nativeTestThenSet32
   2.312  #else
   2.313     SDL_bool result = SDL_FALSE;
   2.314  
   2.315 -   privateWaitLock();
   2.316 -   result = (*ptr == oldvalue);
   2.317 -   if (result)
   2.318 -   {
   2.319 -      *ptr = newvalue;
   2.320 -   }
   2.321 -   privateUnlock();
   2.322 -
   2.323 -   return result;
   2.324 -#endif
   2.325 -}
   2.326 -
   2.327 -SDL_bool
   2.328 -SDL_AtomicTestThenSet8(volatile Uint8 * ptr)
   2.329 -{
   2.330 -#ifdef nativeTestThenSet8
   2.331 -   return (SDL_bool)nativeTestThenSet8(ptr);
   2.332 -#else
   2.333 -   SDL_bool result = SDL_FALSE;
   2.334 -
   2.335 -   privateWaitLock();
   2.336 +   privateWaitLock(ptr);
   2.337     result = (*ptr == 0);
   2.338     if (result)
   2.339     {
   2.340        *ptr = 1;
   2.341     }
   2.342 -   privateUnlock();
   2.343 -
   2.344 -   return result;
   2.345 -#endif
   2.346 -}
   2.347 -
   2.348 -void
   2.349 -SDL_AtomicClear8(volatile Uint8 * ptr)
   2.350 -{
   2.351 -#ifdef nativeClear8
   2.352 -   nativeClear8(ptr);
   2.353 -#else
   2.354 -   privateWaitLock();
   2.355 -   *ptr = 0;
   2.356 -   privateUnlock();
   2.357 -
   2.358 -   return;
   2.359 -#endif
   2.360 -}
   2.361 -
   2.362 -Uint8
   2.363 -SDL_AtomicFetchThenIncrement8(volatile Uint8 * ptr)
   2.364 -{
   2.365 -#ifdef nativeFetchThenIncrement8
   2.366 -   return nativeFetchThenIncrement8(ptr);
   2.367 -#else
   2.368 -   Uint8 tmp = 0;
   2.369 -
   2.370 -   privateWaitLock();
   2.371 -   tmp = *ptr;
   2.372 -   (*ptr)+= 1;
   2.373 -   privateUnlock();
   2.374 -
   2.375 -   return tmp;
   2.376 -#endif
   2.377 -}
   2.378 -
   2.379 -Uint8
   2.380 -SDL_AtomicFetchThenDecrement8(volatile Uint8 * ptr)
   2.381 -{
   2.382 -#ifdef nativeFetchThenDecrement8
   2.383 -   return nativeFetchThenDecrement8(ptr);
   2.384 -#else
   2.385 -   Uint8 tmp = 0;
   2.386 -
   2.387 -   privateWaitLock();
   2.388 -   tmp = *ptr;
   2.389 -   (*ptr) -= 1;
   2.390 -   privateUnlock();
   2.391 -
   2.392 -   return tmp;
   2.393 -#endif
   2.394 -}
   2.395 -
   2.396 -Uint8
   2.397 -SDL_AtomicFetchThenAdd8(volatile Uint8 * ptr, Uint8 value)
   2.398 -{
   2.399 -#ifdef nativeFetchThenAdd8
   2.400 -   return nativeFetchThenAdd8(ptr, value);
   2.401 -#else
   2.402 -   Uint8 tmp = 0;
   2.403 -
   2.404 -   privateWaitLock();
   2.405 -   tmp = *ptr;
   2.406 -   (*ptr)+= value;
   2.407 -   privateUnlock();
   2.408 -
   2.409 -   return tmp;
   2.410 -#endif
   2.411 -}
   2.412 -
   2.413 -Uint8
   2.414 -SDL_AtomicFetchThenSubtract8(volatile Uint8 * ptr, Uint8 value)
   2.415 -{
   2.416 -#ifdef nativeFetchThenSubtract8
   2.417 -   return nativeFetchThenSubtract8(ptr, value);
   2.418 -#else
   2.419 -   Uint8 tmp = 0;
   2.420 -
   2.421 -   privateWaitLock();
   2.422 -   tmp = *ptr;
   2.423 -   (*ptr)-= value;
   2.424 -   privateUnlock();
   2.425 -
   2.426 -   return tmp;
   2.427 -#endif
   2.428 -}
   2.429 -
   2.430 -Uint8
   2.431 -SDL_AtomicIncrementThenFetch8(volatile Uint8 * ptr)
   2.432 -{
   2.433 -#ifdef nativeIncrementThenFetch8
   2.434 -   return nativeIncrementThenFetch8(ptr);
   2.435 -#else
   2.436 -   Uint8 tmp = 0;
   2.437 -
   2.438 -   privateWaitLock();
   2.439 -   (*ptr)+= 1;
   2.440 -   tmp = *ptr;
   2.441 -   privateUnlock();
   2.442 -
   2.443 -   return tmp;
   2.444 -#endif
   2.445 -}
   2.446 -
   2.447 -Uint8
   2.448 -SDL_AtomicDecrementThenFetch8(volatile Uint8 * ptr)
   2.449 -{
   2.450 -#ifdef nativeDecrementThenFetch8
   2.451 -   return nativeDecrementThenFetch8(ptr);
   2.452 -#else
   2.453 -   Uint8 tmp = 0;
   2.454 -
   2.455 -   privateWaitLock();
   2.456 -   (*ptr)-= 1;
   2.457 -   tmp = *ptr;
   2.458 -   privateUnlock();
   2.459 -
   2.460 -   return tmp;
   2.461 -#endif
   2.462 -}
   2.463 -
   2.464 -Uint8
   2.465 -SDL_AtomicAddThenFetch8(volatile Uint8 * ptr, Uint8 value)
   2.466 -{
   2.467 -#ifdef nativeAddThenFetch8
   2.468 -   return nativeAddThenFetch8(ptr, value);
   2.469 -#else
   2.470 -   Uint8 tmp = 0;
   2.471 -
   2.472 -   privateWaitLock();
   2.473 -   (*ptr)+= value;
   2.474 -   tmp = *ptr;
   2.475 -   privateUnlock();
   2.476 -
   2.477 -   return tmp;
   2.478 -#endif
   2.479 -}
   2.480 -
   2.481 -Uint8
   2.482 -SDL_AtomicSubtractThenFetch8(volatile Uint8 * ptr, Uint8 value)
   2.483 -{
   2.484 -#ifdef nativeSubtractThenFetch8
   2.485 -   return nativeSubtractThenFetch8(ptr, value);
   2.486 -#else
   2.487 -   Uint8 tmp = 0;
   2.488 -
   2.489 -   privateWaitLock();
   2.490 -   (*ptr)-= value;
   2.491 -   tmp = *ptr;
   2.492 -   privateUnlock();
   2.493 -
   2.494 -   return tmp;
   2.495 -#endif
   2.496 -}
   2.497 -
   2.498 -/* 16 bit atomic operations */
   2.499 -
   2.500 -Uint16
   2.501 -SDL_AtomicExchange16(volatile Uint16 * ptr, Uint16 value)
   2.502 -{
   2.503 -#ifdef nativeExchange16
   2.504 -   return nativeExchange16(ptr, value);
   2.505 -#else
   2.506 -   Uint16 tmp = 0;
   2.507 -
   2.508 -   privateWaitLock();
   2.509 -   tmp = *ptr;
   2.510 -   *ptr = value;
   2.511 -   privateUnlock();
   2.512 -
   2.513 -   return tmp;
   2.514 -#endif
   2.515 -}
   2.516 -
   2.517 -SDL_bool
   2.518 -SDL_AtomicCompareThenSet16(volatile Uint16 * ptr, Uint16 oldvalue, Uint16 newvalue)
   2.519 -{
   2.520 -#ifdef nativeCompareThenSet16
   2.521 -   return (SDL_bool)nativeCompareThenSet16(ptr, oldvalue, newvalue);
   2.522 -#else
   2.523 -   SDL_bool result = SDL_FALSE;
   2.524 -
   2.525 -   privateWaitLock();
   2.526 -   result = (*ptr == oldvalue);
   2.527 -   if (result)
   2.528 -   {
   2.529 -      *ptr = newvalue;
   2.530 -   }
   2.531 -   privateUnlock();
   2.532 -
   2.533 -   return result;
   2.534 -#endif
   2.535 -}
   2.536 -
   2.537 -SDL_bool
   2.538 -SDL_AtomicTestThenSet16(volatile Uint16 * ptr)
   2.539 -{
   2.540 -#ifdef nativeTestThenSet16
   2.541 -   return (SDL_bool)nativeTestThenSet16(ptr);
   2.542 -#else
   2.543 -   SDL_bool result = SDL_FALSE;
   2.544 -
   2.545 -   privateWaitLock();
   2.546 -   result = (*ptr == 0);
   2.547 -   if (result)
   2.548 -   {
   2.549 -      *ptr = 1;
   2.550 -   }
   2.551 -   privateUnlock();
   2.552 -
   2.553 -   return result;
   2.554 -#endif
   2.555 -}
   2.556 -
   2.557 -void
   2.558 -SDL_AtomicClear16(volatile Uint16 * ptr)
   2.559 -{
   2.560 -#ifdef nativeClear16
   2.561 -   nativeClear16(ptr);
   2.562 -#else
   2.563 -   privateWaitLock();
   2.564 -   *ptr = 0;
   2.565 -   privateUnlock();
   2.566 -
   2.567 -   return;
   2.568 -#endif
   2.569 -}
   2.570 -
   2.571 -Uint16
   2.572 -SDL_AtomicFetchThenIncrement16(volatile Uint16 * ptr)
   2.573 -{
   2.574 -#ifdef nativeFetchThenIncrement16
   2.575 -   return nativeFetchThenIncrement16(ptr);
   2.576 -#else
   2.577 -   Uint16 tmp = 0;
   2.578 -
   2.579 -   privateWaitLock();
   2.580 -   tmp = *ptr;
   2.581 -   (*ptr)+= 1;
   2.582 -   privateUnlock();
   2.583 -
   2.584 -   return tmp;
   2.585 -#endif
   2.586 -}
   2.587 -
   2.588 -Uint16
   2.589 -SDL_AtomicFetchThenDecrement16(volatile Uint16 * ptr)
   2.590 -{
   2.591 -#ifdef nativeFetchThenDecrement16
   2.592 -   return nativeFetchThenDecrement16(ptr);
   2.593 -#else
   2.594 -   Uint16 tmp = 0;
   2.595 -
   2.596 -   privateWaitLock();
   2.597 -   tmp = *ptr;
   2.598 -   (*ptr) -= 1;
   2.599 -   privateUnlock();
   2.600 -
   2.601 -   return tmp;
   2.602 -#endif
   2.603 -}
   2.604 -
   2.605 -Uint16
   2.606 -SDL_AtomicFetchThenAdd16(volatile Uint16 * ptr, Uint16 value)
   2.607 -{
   2.608 -#ifdef nativeFetchThenAdd16
   2.609 -   return nativeFetchThenAdd16(ptr, value);
   2.610 -#else
   2.611 -   Uint16 tmp = 0;
   2.612 -
   2.613 -   privateWaitLock();
   2.614 -   tmp = *ptr;
   2.615 -   (*ptr)+= value;
   2.616 -   privateUnlock();
   2.617 -
   2.618 -   return tmp;
   2.619 -#endif
   2.620 -}
   2.621 -
   2.622 -Uint16
   2.623 -SDL_AtomicFetchThenSubtract16(volatile Uint16 * ptr, Uint16 value)
   2.624 -{
   2.625 -#ifdef nativeFetchThenSubtract16
   2.626 -   return nativeFetchThenSubtract16(ptr, value);
   2.627 -#else
   2.628 -   Uint16 tmp = 0;
   2.629 -
   2.630 -   privateWaitLock();
   2.631 -   tmp = *ptr;
   2.632 -   (*ptr)-= value;
   2.633 -   privateUnlock();
   2.634 -
   2.635 -   return tmp;
   2.636 -#endif
   2.637 -}
   2.638 -
   2.639 -Uint16
   2.640 -SDL_AtomicIncrementThenFetch16(volatile Uint16 * ptr)
   2.641 -{
   2.642 -#ifdef nativeIncrementThenFetch16
   2.643 -   return nativeIncrementThenFetch16(ptr);
   2.644 -#else
   2.645 -   Uint16 tmp = 0;
   2.646 -
   2.647 -   privateWaitLock();
   2.648 -   (*ptr)+= 1;
   2.649 -   tmp = *ptr;
   2.650 -   privateUnlock();
   2.651 -
   2.652 -   return tmp;
   2.653 -#endif
   2.654 -}
   2.655 -
   2.656 -Uint16
   2.657 -SDL_AtomicDecrementThenFetch16(volatile Uint16 * ptr)
   2.658 -{
   2.659 -#ifdef nativeDecrementThenFetch16
   2.660 -   return nativeDecrementThenFetch16(ptr);
   2.661 -#else
   2.662 -   Uint16 tmp = 0;
   2.663 -
   2.664 -   privateWaitLock();
   2.665 -   (*ptr)-= 1;
   2.666 -   tmp = *ptr;
   2.667 -   privateUnlock();
   2.668 -
   2.669 -   return tmp;
   2.670 -#endif
   2.671 -}
   2.672 -
   2.673 -Uint16
   2.674 -SDL_AtomicAddThenFetch16(volatile Uint16 * ptr, Uint16 value)
   2.675 -{
   2.676 -#ifdef nativeAddThenFetch16
   2.677 -   return nativeAddThenFetch16(ptr, value);
   2.678 -#else
   2.679 -   Uint16 tmp = 0;
   2.680 -
   2.681 -   privateWaitLock();
   2.682 -   (*ptr)+= value;
   2.683 -   tmp = *ptr;
   2.684 -   privateUnlock();
   2.685 -
   2.686 -   return tmp;
   2.687 -#endif
   2.688 -}
   2.689 -
   2.690 -Uint16
   2.691 -SDL_AtomicSubtractThenFetch16(volatile Uint16 * ptr, Uint16 value)
   2.692 -{
   2.693 -#ifdef nativeSubtractThenFetch16
   2.694 -   return nativeSubtractThenFetch16(ptr, value);
   2.695 -#else
   2.696 -   Uint16 tmp = 0;
   2.697 -
   2.698 -   privateWaitLock();
   2.699 -   (*ptr)-= value;
   2.700 -   tmp = *ptr;
   2.701 -   privateUnlock();
   2.702 -
   2.703 -   return tmp;
   2.704 -#endif
   2.705 -}
   2.706 -
   2.707 -/* 32 bit atomic operations */
   2.708 -
   2.709 -Uint32
   2.710 -SDL_AtomicExchange32(volatile Uint32 * ptr, Uint32 value)
   2.711 -{
   2.712 -#ifdef nativeExchange32
   2.713 -   return nativeExchange32(ptr, value);
   2.714 -#else
   2.715 -   Uint32 tmp = 0;
   2.716 -
   2.717 -   privateWaitLock();
   2.718 -   tmp = *ptr;
   2.719 -   *ptr = value;
   2.720 -   privateUnlock();
   2.721 -
   2.722 -   return tmp;
   2.723 -#endif
   2.724 -}
   2.725 -
   2.726 -SDL_bool
   2.727 -SDL_AtomicCompareThenSet32(volatile Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
   2.728 -{
   2.729 -#ifdef nativeCompareThenSet32
   2.730 -   return (SDL_bool)nativeCompareThenSet32(ptr, oldvalue, newvalue);
   2.731 -#else
   2.732 -   SDL_bool result = SDL_FALSE;
   2.733 -
   2.734 -   privateWaitLock();
   2.735 -   result = (*ptr == oldvalue);
   2.736 -   if (result)
   2.737 -   {
   2.738 -      *ptr = newvalue;
   2.739 -   }
   2.740 -   privateUnlock();
   2.741 -
   2.742 -   return result;
   2.743 -#endif
   2.744 -}
   2.745 -
   2.746 -SDL_bool
   2.747 -SDL_AtomicTestThenSet32(volatile Uint32 * ptr)
   2.748 -{
   2.749 -#ifdef nativeTestThenSet32
   2.750 -   return (SDL_bool)nativeTestThenSet32(ptr);
   2.751 -#else
   2.752 -   SDL_bool result = SDL_FALSE;
   2.753 -
   2.754 -   privateWaitLock();
   2.755 -   result = (*ptr == 0);
   2.756 -   if (result)
   2.757 -   {
   2.758 -      *ptr = 1;
   2.759 -   }
   2.760 -   privateUnlock();
   2.761 +   privateUnlock(ptr);
   2.762  
   2.763     return result;
   2.764  #endif
   2.765 @@ -643,11 +166,10 @@
   2.766  SDL_AtomicClear32(volatile Uint32 * ptr)
   2.767  {
   2.768  #ifdef nativeClear32
   2.769 -   nativeClear32(ptr);
   2.770  #else
   2.771 -   privateWaitLock();
   2.772 +   privateWaitLock(ptr);
   2.773     *ptr = 0;
   2.774 -   privateUnlock();
   2.775 +   privateUnlock(ptr);
   2.776  
   2.777     return;
   2.778  #endif
   2.779 @@ -657,14 +179,13 @@
   2.780  SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr)
   2.781  {
   2.782  #ifdef nativeFetchThenIncrement32
   2.783 -   return nativeFetchThenIncrement32(ptr);
   2.784  #else
   2.785     Uint32 tmp = 0;
   2.786  
   2.787 -   privateWaitLock();
   2.788 +   privateWaitLock(ptr);
   2.789     tmp = *ptr;
   2.790     (*ptr)+= 1;
   2.791 -   privateUnlock();
   2.792 +   privateUnlock(ptr);
   2.793  
   2.794     return tmp;
   2.795  #endif
   2.796 @@ -674,14 +195,13 @@
   2.797  SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr)
   2.798  {
   2.799  #ifdef nativeFetchThenDecrement32
   2.800 -   return nativeFetchThenDecrement32(ptr);
   2.801  #else
   2.802     Uint32 tmp = 0;
   2.803  
   2.804 -   privateWaitLock();
   2.805 +   privateWaitLock(ptr);
   2.806     tmp = *ptr;
   2.807     (*ptr) -= 1;
   2.808 -   privateUnlock();
   2.809 +   privateUnlock(ptr);
   2.810  
   2.811     return tmp;
   2.812  #endif
   2.813 @@ -691,14 +211,13 @@
   2.814  SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value)
   2.815  {
   2.816  #ifdef nativeFetchThenAdd32
   2.817 -   return nativeFetchThenAdd32(ptr, value);
   2.818  #else
   2.819     Uint32 tmp = 0;
   2.820  
   2.821 -   privateWaitLock();
   2.822 +   privateWaitLock(ptr);
   2.823     tmp = *ptr;
   2.824     (*ptr)+= value;
   2.825 -   privateUnlock();
   2.826 +   privateUnlock(ptr);
   2.827  
   2.828     return tmp;
   2.829  #endif
   2.830 @@ -708,14 +227,13 @@
   2.831  SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value)
   2.832  {
   2.833  #ifdef nativeFetchThenSubtract32
   2.834 -   return nativeFetchThenSubtract32(ptr, value);
   2.835  #else
   2.836     Uint32 tmp = 0;
   2.837  
   2.838 -   privateWaitLock();
   2.839 +   privateWaitLock(ptr);
   2.840     tmp = *ptr;
   2.841     (*ptr)-= value;
   2.842 -   privateUnlock();
   2.843 +   privateUnlock(ptr);
   2.844  
   2.845     return tmp;
   2.846  #endif
   2.847 @@ -725,14 +243,13 @@
   2.848  SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr)
   2.849  {
   2.850  #ifdef nativeIncrementThenFetch32
   2.851 -   return nativeIncrementThenFetch32(ptr);
   2.852  #else
   2.853     Uint32 tmp = 0;
   2.854  
   2.855 -   privateWaitLock();
   2.856 +   privateWaitLock(ptr);
   2.857     (*ptr)+= 1;
   2.858     tmp = *ptr;
   2.859 -   privateUnlock();
   2.860 +   privateUnlock(ptr);
   2.861  
   2.862     return tmp;
   2.863  #endif
   2.864 @@ -742,14 +259,13 @@
   2.865  SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr)
   2.866  {
   2.867  #ifdef nativeDecrementThenFetch32
   2.868 -   return nativeDecrementThenFetch32(ptr);
   2.869  #else
   2.870     Uint32 tmp = 0;
   2.871  
   2.872 -   privateWaitLock();
   2.873 +   privateWaitLock(ptr);
   2.874     (*ptr)-= 1;
   2.875     tmp = *ptr;
   2.876 -   privateUnlock();
   2.877 +   privateUnlock(ptr);
   2.878  
   2.879     return tmp;
   2.880  #endif
   2.881 @@ -759,14 +275,13 @@
   2.882  SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value)
   2.883  {
   2.884  #ifdef nativeAddThenFetch32
   2.885 -   return nativeAddThenFetch32(ptr, value);
   2.886  #else
   2.887     Uint32 tmp = 0;
   2.888  
   2.889 -   privateWaitLock();
   2.890 +   privateWaitLock(ptr);
   2.891     (*ptr)+= value;
   2.892     tmp = *ptr;
   2.893 -   privateUnlock();
   2.894 +   privateUnlock(ptr);
   2.895  
   2.896     return tmp;
   2.897  #endif
   2.898 @@ -776,14 +291,13 @@
   2.899  SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value)
   2.900  {
   2.901  #ifdef nativeSubtractThenFetch32
   2.902 -   return nativeSubtractThenFetch32(ptr, value);
   2.903  #else
   2.904     Uint32 tmp = 0;
   2.905  
   2.906 -   privateWaitLock();
   2.907 +   privateWaitLock(ptr);
   2.908     (*ptr)-= value;
   2.909     tmp = *ptr;
   2.910 -   privateUnlock();
   2.911 +   privateUnlock(ptr);
   2.912  
   2.913     return tmp;
   2.914  #endif
   2.915 @@ -792,58 +306,20 @@
   2.916  /* 64 bit atomic operations */
   2.917  #ifdef SDL_HAS_64BIT_TYPE
   2.918  
   2.919 -Uint64
   2.920 -SDL_AtomicExchange64(volatile Uint64 * ptr, Uint64 value)
   2.921 -{
   2.922 -#ifdef nativeExchange64
   2.923 -   return nativeExchange64(ptr, value);
   2.924 -#else
   2.925 -   Uint64 tmp = 0;
   2.926 -
   2.927 -   privateWaitLock();
   2.928 -   tmp = *ptr;
   2.929 -   *ptr = value;
   2.930 -   privateUnlock();
   2.931 -
   2.932 -   return tmp;
   2.933 -#endif
   2.934 -}
   2.935 -
   2.936 -SDL_bool
   2.937 -SDL_AtomicCompareThenSet64(volatile Uint64 * ptr, Uint64 oldvalue, Uint64 newvalue)
   2.938 -{
   2.939 -#ifdef nativeCompareThenSet64
   2.940 -   return (SDL_bool)nativeCompareThenSet64(ptr, oldvalue, newvalue);
   2.941 -#else
   2.942 -   SDL_bool result = SDL_FALSE;
   2.943 -
   2.944 -   privateWaitLock();
   2.945 -   result = (*ptr == oldvalue);
   2.946 -   if (result)
   2.947 -   {
   2.948 -      *ptr = newvalue;
   2.949 -   }
   2.950 -   privateUnlock();
   2.951 -
   2.952 -   return result;
   2.953 -#endif
   2.954 -}
   2.955 -
   2.956  SDL_bool
   2.957  SDL_AtomicTestThenSet64(volatile Uint64 * ptr)
   2.958  {
   2.959  #ifdef nativeTestThenSet64
   2.960 -   return (SDL_bool)nativeTestThenSet64(ptr);
   2.961  #else
   2.962     SDL_bool result = SDL_FALSE;
   2.963  
   2.964 -   privateWaitLock();
   2.965 +   privateWaitLock(ptr);
   2.966     result = (*ptr == 0);
   2.967     if (result)
   2.968     {
   2.969        *ptr = 1;
   2.970     }
   2.971 -   privateUnlock();
   2.972 +   privateUnlock(ptr);
   2.973  
   2.974     return result;
   2.975  #endif
   2.976 @@ -853,11 +329,10 @@
   2.977  SDL_AtomicClear64(volatile Uint64 * ptr)
   2.978  {
   2.979  #ifdef nativeClear64
   2.980 -   nativeClear64(ptr);
   2.981  #else
   2.982 -   privateWaitLock();
   2.983 +   privateWaitLock(ptr);
   2.984     *ptr = 0;
   2.985 -   privateUnlock();
   2.986 +   privateUnlock(ptr);
   2.987  
   2.988     return;
   2.989  #endif
   2.990 @@ -867,14 +342,13 @@
   2.991  SDL_AtomicFetchThenIncrement64(volatile Uint64 * ptr)
   2.992  {
   2.993  #ifdef nativeFetchThenIncrement64
   2.994 -   return nativeFetchThenIncrement64(ptr);
   2.995  #else
   2.996     Uint64 tmp = 0;
   2.997  
   2.998 -   privateWaitLock();
   2.999 +   privateWaitLock(ptr);
  2.1000     tmp = *ptr;
  2.1001     (*ptr)+= 1;
  2.1002 -   privateUnlock();
  2.1003 +   privateUnlock(ptr);
  2.1004  
  2.1005     return tmp;
  2.1006  #endif
  2.1007 @@ -884,14 +358,13 @@
  2.1008  SDL_AtomicFetchThenDecrement64(volatile Uint64 * ptr)
  2.1009  {
  2.1010  #ifdef nativeFetchThenDecrement64
  2.1011 -   return nativeFetchThenDecrement64(ptr);
  2.1012  #else
  2.1013     Uint64 tmp = 0;
  2.1014  
  2.1015 -   privateWaitLock();
  2.1016 +   privateWaitLock(ptr);
  2.1017     tmp = *ptr;
  2.1018     (*ptr) -= 1;
  2.1019 -   privateUnlock();
  2.1020 +   privateUnlock(ptr);
  2.1021  
  2.1022     return tmp;
  2.1023  #endif
  2.1024 @@ -901,14 +374,13 @@
  2.1025  SDL_AtomicFetchThenAdd64(volatile Uint64 * ptr, Uint64 value)
  2.1026  {
  2.1027  #ifdef nativeFetchThenAdd64
  2.1028 -   return nativeFetchThenAdd64(ptr, value);
  2.1029  #else
  2.1030     Uint64 tmp = 0;
  2.1031  
  2.1032 -   privateWaitLock();
  2.1033 +   privateWaitLock(ptr);
  2.1034     tmp = *ptr;
  2.1035     (*ptr)+= value;
  2.1036 -   privateUnlock();
  2.1037 +   privateUnlock(ptr);
  2.1038  
  2.1039     return tmp;
  2.1040  #endif
  2.1041 @@ -918,14 +390,13 @@
  2.1042  SDL_AtomicFetchThenSubtract64(volatile Uint64 * ptr, Uint64 value)
  2.1043  {
  2.1044  #ifdef nativeFetchThenSubtract64
  2.1045 -   return nativeFetchThenSubtract64(ptr, value);
  2.1046  #else
  2.1047     Uint64 tmp = 0;
  2.1048  
  2.1049 -   privateWaitLock();
  2.1050 +   privateWaitLock(ptr);
  2.1051     tmp = *ptr;
  2.1052     (*ptr)-= value;
  2.1053 -   privateUnlock();
  2.1054 +   privateUnlock(ptr);
  2.1055  
  2.1056     return tmp;
  2.1057  #endif
  2.1058 @@ -935,14 +406,13 @@
  2.1059  SDL_AtomicIncrementThenFetch64(volatile Uint64 * ptr)
  2.1060  {
  2.1061  #ifdef nativeIncrementThenFetch64
  2.1062 -   return nativeIncrementThenFetch64(ptr);
  2.1063  #else
  2.1064     Uint64 tmp = 0;
  2.1065  
  2.1066 -   privateWaitLock();
  2.1067 +   privateWaitLock(ptr);
  2.1068     (*ptr)+= 1;
  2.1069     tmp = *ptr;
  2.1070 -   privateUnlock();
  2.1071 +   privateUnlock(ptr);
  2.1072  
  2.1073     return tmp;
  2.1074  #endif
  2.1075 @@ -952,14 +422,13 @@
  2.1076  SDL_AtomicDecrementThenFetch64(volatile Uint64 * ptr)
  2.1077  {
  2.1078  #ifdef nativeDecrementThenFetch64
  2.1079 -   return nativeDecrementThenFetch64(ptr);
  2.1080  #else
  2.1081     Uint64 tmp = 0;
  2.1082  
  2.1083 -   privateWaitLock();
  2.1084 +   privateWaitLock(ptr);
  2.1085     (*ptr)-= 1;
  2.1086     tmp = *ptr;
  2.1087 -   privateUnlock();
  2.1088 +   privateUnlock(ptr);
  2.1089  
  2.1090     return tmp;
  2.1091  #endif
  2.1092 @@ -969,14 +438,13 @@
  2.1093  SDL_AtomicAddThenFetch64(volatile Uint64 * ptr, Uint64 value)
  2.1094  {
  2.1095  #ifdef nativeAddThenFetch64
  2.1096 -   return nativeAddThenFetch64(ptr, value);
  2.1097  #else
  2.1098     Uint64 tmp = 0;
  2.1099  
  2.1100 -   privateWaitLock();
  2.1101 +   privateWaitLock(ptr);
  2.1102     (*ptr)+= value;
  2.1103     tmp = *ptr;
  2.1104 -   privateUnlock();
  2.1105 +   privateUnlock(ptr);
  2.1106  
  2.1107     return tmp;
  2.1108  #endif
  2.1109 @@ -986,14 +454,13 @@
  2.1110  SDL_AtomicSubtractThenFetch64(volatile Uint64 * ptr, Uint64 value)
  2.1111  {
  2.1112  #ifdef nativeSubtractThenFetch64
  2.1113 -   return nativeSubtractThenFetch64(ptr, value);
  2.1114  #else
  2.1115     Uint64 tmp = 0;
  2.1116  
  2.1117 -   privateWaitLock();
  2.1118 +   privateWaitLock(ptr);
  2.1119     (*ptr)-= value;
  2.1120     tmp = *ptr;
  2.1121 -   privateUnlock();
  2.1122 +   privateUnlock(ptr);
  2.1123  
  2.1124     return tmp;
  2.1125  #endif
     3.1 --- a/src/atomic/linux/SDL_atomic.c	Mon Sep 07 16:04:44 2009 +0000
     3.2 +++ b/src/atomic/linux/SDL_atomic.c	Thu Sep 17 20:35:12 2009 +0000
     3.3 @@ -1,611 +1,167 @@
     3.4  /*
     3.5 -    SDL - Simple DirectMedia Layer
     3.6 -    Copyright (C) 1997-2009 Sam Lantinga
     3.7 +  SDL - Simple DirectMedia Layer
     3.8 +  Copyright (C) 1997-2009 Sam Lantinga
     3.9  
    3.10 -    This library is free software; you can redistribute it and/or
    3.11 -    modify it under the terms of the GNU Lesser General Public
    3.12 -    License as published by the Free Software Foundation; either
    3.13 -    version 2.1 of the License, or (at your option) any later version.
    3.14 +  This library is free software; you can redistribute it and/or
    3.15 +  modify it under the terms of the GNU Lesser General Public
    3.16 +  License as published by the Free Software Foundation; either
    3.17 +  version 2.1 of the License, or (at your option) any later version.
    3.18  
    3.19 -    This library is distributed in the hope that it will be useful,
    3.20 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.21 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.22 -    Lesser General Public License for more details.
    3.23 +  This library is distributed in the hope that it will be useful,
    3.24 +  but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.25 +  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    3.26 +  Lesser General Public License for more details.
    3.27  
    3.28 -    You should have received a copy of the GNU Lesser General Public
    3.29 -    License along with this library; if not, write to the Free Software
    3.30 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    3.31 +  You should have received a copy of the GNU Lesser General Public
    3.32 +  License along with this library; if not, write to the Free Software
    3.33 +  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    3.34  
    3.35 -    Sam Lantinga
    3.36 -    slouken@libsdl.org
    3.37 +  Sam Lantinga
    3.38 +  slouken@libsdl.org
    3.39 +
    3.40 +  Contributed by Bob Pendleton, bob@pendleton.com
    3.41  */
    3.42  
    3.43  #include "SDL_stdinc.h"
    3.44  #include "SDL_atomic.h"
    3.45  
    3.46 +#include "SDL_error.h"
    3.47 +
    3.48  /*
    3.49 -  This file provides 8, 16, 32, and 64 bit atomic operations. If the
    3.50 +  This file provides 32, and 64 bit atomic operations. If the
    3.51    operations are provided by the native hardware and operating system
    3.52    they are used. If they are not then the operations are emulated
    3.53 -  using the SDL mutex operations. 
    3.54 - */
    3.55 -
    3.56 -/* 
    3.57 -  First, detect whether the operations are supported and create
    3.58 -  #defines that indicate that they do exist. The goal is to have all
    3.59 -  the system dependent code in the top part of the file so that the
    3.60 -  bottom can be use unchanged across all platforms.
    3.61 -
    3.62 -  Second, #define all the operations in each size class that are
    3.63 -  supported. Doing this allows supported operations to be used along
    3.64 -  side of emulated operations.
    3.65 +  using the SDL spin lock operations. If spin lock can not be
    3.66 +  implemented then these functions must fail.
    3.67  */
    3.68  
    3.69  /* 
    3.70 -   Linux version.
    3.71 +  LINUX/GCC VERSION.
    3.72  
    3.73 -   Test for gnu C builtin support for atomic operations. The only way
    3.74 -   I know of is to check to see if the
    3.75 -   __GCC_HAVE_SYNC_COMPARE_AND_SWAP_* macros are defined.
    3.76 - */
    3.77 +  This version of the code assumes support of the atomic builtins as
    3.78 +  documented at gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html This
    3.79 +  code should work on any modern x86 or other processor supported by
    3.80 +  GCC. 
    3.81  
    3.82 -#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1
    3.83 -#define HAVE_ALL_8_BIT_OPS
    3.84 +  Some processors will only support some of these operations so
    3.85 +  #ifdefs will have to be added as incompatibilities are discovered
    3.86 +*/
    3.87  
    3.88 -#define nativeExchange8(ptr, value)			(__sync_lock_test_and_set(ptr, value))
    3.89 -#define nativeCompareThenSet8(ptr, oldvalue, newvalue) 	(oldvalue == __sync_val_compare_and_swap(ptr, oldvalue, newvalue))
    3.90 -#define nativeTestThenSet8(ptr)    	     		(0 == __sync_lock_test_and_set(ptr, 1))
    3.91 -#define nativeClear8(ptr)				(__sync_lock_release(ptr))
    3.92 -#define nativeFetchThenIncrement8(ptr)   		(__sync_fetch_and_add(ptr, 1))
    3.93 -#define nativeFetchThenDecrement8(ptr) 			(__sync_fetch_and_sub(ptr, 1))
    3.94 -#define nativeFetchThenAdd8(ptr, value) 		(__sync_fetch_and_add(ptr, value))
    3.95 -#define nativeFetchThenSubtract8(ptr, value) 		(__sync_fetch_and_sub(ptr, value))
    3.96 -#define nativeIncrementThenFetch8(ptr) 			(__sync_add_and_fetch(ptr, 1))
    3.97 -#define nativeDecrementThenFetch8(ptr) 			(__sync_sub_and_fetch(ptr, 1))
    3.98 -#define nativeAddThenFetch8(ptr, value) 		(__sync_add_and_fetch(ptr, value))
    3.99 -#define nativeSubtractThenFetch8(ptr, value) 		(__sync_sub_and_fetch(ptr, value))
   3.100 +/*
   3.101 +  Native spinlock routines.
   3.102 +*/
   3.103 +
   3.104 +void 
   3.105 +SDL_AtomicLock(SDL_SpinLock *lock)
   3.106 +{
   3.107 +   while (0 != __sync_lock_test_and_set(lock, 1))
   3.108 +   {
   3.109 +   }
   3.110 +}
   3.111 +
   3.112 +void 
   3.113 +SDL_AtomicUnlock(SDL_SpinLock *lock)
   3.114 +{
   3.115 +   __sync_lock_test_and_set(lock, 0);
   3.116 +}
   3.117 +
   3.118 +/*
   3.119 +  Note that platform specific versions can be built from this version
   3.120 +  by changing the #undefs to #defines and adding platform specific
   3.121 +  code.
   3.122 +*/
   3.123 +
   3.124 +#define nativeTestThenSet32
   3.125 +#define nativeClear32
   3.126 +#define nativeFetchThenIncrement32
   3.127 +#define nativeFetchThenDecrement32
   3.128 +#define nativeFetchThenAdd32
   3.129 +#define nativeFetchThenSubtract32
   3.130 +#define nativeIncrementThenFetch32
   3.131 +#define nativeDecrementThenFetch32
   3.132 +#define nativeAddThenFetch32
   3.133 +#define nativeSubtractThenFetch32
   3.134 +
   3.135 +#define nativeTestThenSet64
   3.136 +#define nativeClear64
   3.137 +#define nativeFetchThenIncrement64
   3.138 +#define nativeFetchThenDecrement64
   3.139 +#define nativeFetchThenAdd64
   3.140 +#define nativeFetchThenSubtract64
   3.141 +#define nativeIncrementThenFetch64
   3.142 +#define nativeDecrementThenFetch64
   3.143 +#define nativeAddThenFetch64
   3.144 +#define nativeSubtractThenFetch64
   3.145 +
   3.146 +/* 
   3.147 +  If any of the operations are not provided then we must emulate some
   3.148 +  of them. That means we need a nice implementation of spin locks
   3.149 +  that avoids the "one big lock" problem. We use a vector of spin
   3.150 +  locks and pick which one to use based on the address of the operand
   3.151 +  of the function.
   3.152 +
   3.153 +  To generate the index of the lock we first shift by 3 bits to get
   3.154 +  rid on the zero bits that result from 32 and 64 bit allignment of
   3.155 +  data. We then mask off all but 5 bits and use those 5 bits as an
   3.156 +  index into the table. 
   3.157 +
   3.158 +  Picking the lock this way insures that accesses to the same data at
   3.159 +  the same time will go to the same lock. OTOH, accesses to different
   3.160 +  data have only a 1/32 chance of hitting the same lock. That should
   3.161 +  pretty much eliminate the chances of several atomic operations on
   3.162 +  different data from waiting on the same "big lock". If it isn't
   3.163 +  then the table of locks can be expanded to a new size so long as
   3.164 +  the new size if a power of two.
   3.165 +*/
   3.166 +
   3.167 +static SDL_SpinLock locks[32] = {
   3.168 +   0, 0, 0, 0, 0, 0, 0, 0,
   3.169 +   0, 0, 0, 0, 0, 0, 0, 0,
   3.170 +   0, 0, 0, 0, 0, 0, 0, 0,
   3.171 +   0, 0, 0, 0, 0, 0, 0, 0,
   3.172 +};
   3.173 +
   3.174 +static __inline__ void
   3.175 +privateWaitLock(volatile void *ptr)
   3.176 +{
   3.177 +#if SIZEOF_VOIDP == 4
   3.178 +   Uint32 index = ((((Uint32)ptr) >> 3) & 0x1f);
   3.179 +#elif SIZEOF_VOIDP == 8
   3.180 +   Uint64 index = ((((Uint64)ptr) >> 3) & 0x1f);
   3.181  #endif
   3.182  
   3.183 -#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
   3.184 -#define HAVE_ALL_16_BIT_OPS
   3.185 +   SDL_AtomicLock(&locks[index]);
   3.186 +}
   3.187  
   3.188 -#define nativeExchange16(ptr, value)			(__sync_lock_test_and_set(ptr, value))
   3.189 -#define nativeCompareThenSet16(ptr, oldvalue, newvalue) (oldvalue == __sync_val_compare_and_swap(ptr, oldvalue, newvalue))
   3.190 -#define nativeTestThenSet16(ptr)    	     		(0 == __sync_lock_test_and_set(ptr, 1))
   3.191 -#define nativeClear16(ptr)				(__sync_lock_release(ptr))
   3.192 -#define nativeFetchThenIncrement16(ptr)   		(__sync_fetch_and_add(ptr, 1))
   3.193 -#define nativeFetchThenDecrement16(ptr) 		(__sync_fetch_and_sub(ptr, 1))
   3.194 -#define nativeFetchThenAdd16(ptr, value) 		(__sync_fetch_and_add(ptr, value))
   3.195 -#define nativeFetchThenSubtract16(ptr, value) 		(__sync_fetch_and_sub(ptr, value))
   3.196 -#define nativeIncrementThenFetch16(ptr) 		(__sync_add_and_fetch(ptr, 1))
   3.197 -#define nativeDecrementThenFetch16(ptr) 		(__sync_sub_and_fetch(ptr, 1))
   3.198 -#define nativeAddThenFetch16(ptr, value) 		(__sync_add_and_fetch(ptr, value))
   3.199 -#define nativeSubtractThenFetch16(ptr, value) 		(__sync_sub_and_fetch(ptr, value))
   3.200 +static __inline__ void
   3.201 +privateUnlock(volatile void *ptr)
   3.202 +{
   3.203 +#if SIZEOF_VOIDP == 4
   3.204 +   Uint32 index = ((((Uint32)ptr) >> 3) & 0x1f);
   3.205 +#elif SIZEOF_VOIDP == 8
   3.206 +   Uint64 index = ((((Uint64)ptr) >> 3) & 0x1f);
   3.207  #endif
   3.208  
   3.209 -#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
   3.210 -#define HAVE_ALL_32_BIT_OPS
   3.211 -
   3.212 -#define nativeExchange32(ptr, value)			(__sync_lock_test_and_set(ptr, value))
   3.213 -#define nativeCompareThenSet32(ptr, oldvalue, newvalue) (oldvalue == __sync_val_compare_and_swap(ptr, oldvalue, newvalue))
   3.214 -#define nativeTestThenSet32(ptr)    	     		(0 == __sync_lock_test_and_set(ptr, 1))
   3.215 -#define nativeClear32(ptr)				(__sync_lock_release(ptr))
   3.216 -#define nativeFetchThenIncrement32(ptr)   		(__sync_fetch_and_add(ptr, 1))
   3.217 -#define nativeFetchThenDecrement32(ptr) 		(__sync_fetch_and_sub(ptr, 1))
   3.218 -#define nativeFetchThenAdd32(ptr, value) 		(__sync_fetch_and_add(ptr, value))
   3.219 -#define nativeFetchThenSubtract32(ptr, value) 		(__sync_fetch_and_sub(ptr, value))
   3.220 -#define nativeIncrementThenFetch32(ptr) 		(__sync_add_and_fetch(ptr, 1))
   3.221 -#define nativeDecrementThenFetch32(ptr) 		(__sync_sub_and_fetch(ptr, 1))
   3.222 -#define nativeAddThenFetch32(ptr, value) 		(__sync_add_and_fetch(ptr, value))
   3.223 -#define nativeSubtractThenFetch32(ptr, value) 		(__sync_sub_and_fetch(ptr, value))
   3.224 -#endif
   3.225 -
   3.226 -#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
   3.227 -#define HAVE_ALL_64_BIT_OPS
   3.228 -
   3.229 -#define nativeExchange64(ptr, value)			(__sync_lock_test_and_set(ptr, value))
   3.230 -#define nativeCompareThenSet64(ptr, oldvalue, newvalue) (oldvalue == __sync_val_compare_and_swap(ptr, oldvalue, newvalue))
   3.231 -#define nativeTestThenSet64(ptr)    	     		(0 == __sync_lock_test_and_set(ptr, 1))
   3.232 -#define nativeClear64(ptr)				(__sync_lock_release(ptr))
   3.233 -#define nativeFetchThenIncrement64(ptr)   		(__sync_fetch_and_add(ptr, 1))
   3.234 -#define nativeFetchThenDecrement64(ptr) 		(__sync_fetch_and_sub(ptr, 1))
   3.235 -#define nativeFetchThenAdd64(ptr, value) 		(__sync_fetch_and_add(ptr, value))
   3.236 -#define nativeFetchThenSubtract64(ptr, value) 		(__sync_fetch_and_sub(ptr, value))
   3.237 -#define nativeIncrementThenFetch64(ptr) 		(__sync_add_and_fetch(ptr, 1))
   3.238 -#define nativeDecrementThenFetch64(ptr) 		(__sync_sub_and_fetch(ptr, 1))
   3.239 -#define nativeAddThenFetch64(ptr, value) 		(__sync_add_and_fetch(ptr, value))
   3.240 -#define nativeSubtractThenFetch64(ptr, value) 		(__sync_sub_and_fetch(ptr, value))
   3.241 -#endif
   3.242 -
   3.243 -/* 
   3.244 -If any of the operations are not provided then we must emulate some of
   3.245 -them.
   3.246 - */
   3.247 -
   3.248 -#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)
   3.249 -
   3.250 -static Uint32 lock = 0;
   3.251 -
   3.252 -#define privateWaitLock()	       \
   3.253 -   while (nativeTestThenSet32(&lock))  \
   3.254 -   {				       \
   3.255 -   };
   3.256 -
   3.257 -#define privateUnlock() (nativeClear32(&lock))
   3.258 -#endif
   3.259 -
   3.260 -/* 8 bit atomic operations */
   3.261 -
   3.262 -Uint8
   3.263 -SDL_AtomicExchange8(volatile Uint8 * ptr, Uint8 value)
   3.264 -{
   3.265 -#ifdef nativeExchange8
   3.266 -   return nativeExchange8(ptr, value);
   3.267 -#else
   3.268 -   Uint8 tmp = 0;
   3.269 -
   3.270 -   privateWaitLock();
   3.271 -   tmp = *ptr;
   3.272 -   *ptr = value;
   3.273 -   privateUnlock();
   3.274 -
   3.275 -   return tmp;
   3.276 -#endif
   3.277 +   SDL_AtomicUnlock(&locks[index]);
   3.278  }
   3.279  
   3.280 +/* 32 bit atomic operations */
   3.281 +
   3.282  SDL_bool
   3.283 -SDL_AtomicCompareThenSet8(volatile Uint8 * ptr, Uint8 oldvalue, Uint8 newvalue)
   3.284 +SDL_AtomicTestThenSet32(volatile Uint32 * ptr)
   3.285  {
   3.286 -#ifdef nativeCompareThenSet8
   3.287 -   return (SDL_bool)nativeCompareThenSet8(ptr, oldvalue, newvalue);
   3.288 +#ifdef nativeTestThenSet32
   3.289 +   return 0 == __sync_lock_test_and_set(ptr, 1);
   3.290  #else
   3.291     SDL_bool result = SDL_FALSE;
   3.292  
   3.293 -   privateWaitLock();
   3.294 -   result = (*ptr == oldvalue);
   3.295 -   if (result)
   3.296 -   {
   3.297 -      *ptr = newvalue;
   3.298 -   }
   3.299 -   privateUnlock();
   3.300 -
   3.301 -   return result;
   3.302 -#endif
   3.303 -}
   3.304 -
   3.305 -SDL_bool
   3.306 -SDL_AtomicTestThenSet8(volatile Uint8 * ptr)
   3.307 -{
   3.308 -#ifdef nativeTestThenSet8
   3.309 -   return (SDL_bool)nativeTestThenSet8(ptr);
   3.310 -#else
   3.311 -   SDL_bool result = SDL_FALSE;
   3.312 -
   3.313 -   privateWaitLock();
   3.314 +   privateWaitLock(ptr);
   3.315     result = (*ptr == 0);
   3.316     if (result)
   3.317     {
   3.318        *ptr = 1;
   3.319     }
   3.320 -   privateUnlock();
   3.321 -
   3.322 -   return result;
   3.323 -#endif
   3.324 -}
   3.325 -
   3.326 -void
   3.327 -SDL_AtomicClear8(volatile Uint8 * ptr)
   3.328 -{
   3.329 -#ifdef nativeClear8
   3.330 -   nativeClear8(ptr);
   3.331 -#else
   3.332 -   privateWaitLock();
   3.333 -   *ptr = 0;
   3.334 -   privateUnlock();
   3.335 -
   3.336 -   return;
   3.337 -#endif
   3.338 -}
   3.339 -
   3.340 -Uint8
   3.341 -SDL_AtomicFetchThenIncrement8(volatile Uint8 * ptr)
   3.342 -{
   3.343 -#ifdef nativeFetchThenIncrement8
   3.344 -   return nativeFetchThenIncrement8(ptr);
   3.345 -#else
   3.346 -   Uint8 tmp = 0;
   3.347 -
   3.348 -   privateWaitLock();
   3.349 -   tmp = *ptr;
   3.350 -   (*ptr)+= 1;
   3.351 -   privateUnlock();
   3.352 -
   3.353 -   return tmp;
   3.354 -#endif
   3.355 -}
   3.356 -
   3.357 -Uint8
   3.358 -SDL_AtomicFetchThenDecrement8(volatile Uint8 * ptr)
   3.359 -{
   3.360 -#ifdef nativeFetchThenDecrement8
   3.361 -   return nativeFetchThenDecrement8(ptr);
   3.362 -#else
   3.363 -   Uint8 tmp = 0;
   3.364 -
   3.365 -   privateWaitLock();
   3.366 -   tmp = *ptr;
   3.367 -   (*ptr) -= 1;
   3.368 -   privateUnlock();
   3.369 -
   3.370 -   return tmp;
   3.371 -#endif
   3.372 -}
   3.373 -
   3.374 -Uint8
   3.375 -SDL_AtomicFetchThenAdd8(volatile Uint8 * ptr, Uint8 value)
   3.376 -{
   3.377 -#ifdef nativeFetchThenAdd8
   3.378 -   return nativeFetchThenAdd8(ptr, value);
   3.379 -#else
   3.380 -   Uint8 tmp = 0;
   3.381 -
   3.382 -   privateWaitLock();
   3.383 -   tmp = *ptr;
   3.384 -   (*ptr)+= value;
   3.385 -   privateUnlock();
   3.386 -
   3.387 -   return tmp;
   3.388 -#endif
   3.389 -}
   3.390 -
   3.391 -Uint8
   3.392 -SDL_AtomicFetchThenSubtract8(volatile Uint8 * ptr, Uint8 value)
   3.393 -{
   3.394 -#ifdef nativeFetchThenSubtract8
   3.395 -   return nativeFetchThenSubtract8(ptr, value);
   3.396 -#else
   3.397 -   Uint8 tmp = 0;
   3.398 -
   3.399 -   privateWaitLock();
   3.400 -   tmp = *ptr;
   3.401 -   (*ptr)-= value;
   3.402 -   privateUnlock();
   3.403 -
   3.404 -   return tmp;
   3.405 -#endif
   3.406 -}
   3.407 -
   3.408 -Uint8
   3.409 -SDL_AtomicIncrementThenFetch8(volatile Uint8 * ptr)
   3.410 -{
   3.411 -#ifdef nativeIncrementThenFetch8
   3.412 -   return nativeIncrementThenFetch8(ptr);
   3.413 -#else
   3.414 -   Uint8 tmp = 0;
   3.415 -
   3.416 -   privateWaitLock();
   3.417 -   (*ptr)+= 1;
   3.418 -   tmp = *ptr;
   3.419 -   privateUnlock();
   3.420 -
   3.421 -   return tmp;
   3.422 -#endif
   3.423 -}
   3.424 -
   3.425 -Uint8
   3.426 -SDL_AtomicDecrementThenFetch8(volatile Uint8 * ptr)
   3.427 -{
   3.428 -#ifdef nativeDecrementThenFetch8
   3.429 -   return nativeDecrementThenFetch8(ptr);
   3.430 -#else
   3.431 -   Uint8 tmp = 0;
   3.432 -
   3.433 -   privateWaitLock();
   3.434 -   (*ptr)-= 1;
   3.435 -   tmp = *ptr;
   3.436 -   privateUnlock();
   3.437 -
   3.438 -   return tmp;
   3.439 -#endif
   3.440 -}
   3.441 -
   3.442 -Uint8
   3.443 -SDL_AtomicAddThenFetch8(volatile Uint8 * ptr, Uint8 value)
   3.444 -{
   3.445 -#ifdef nativeAddThenFetch8
   3.446 -   return nativeAddThenFetch8(ptr, value);
   3.447 -#else
   3.448 -   Uint8 tmp = 0;
   3.449 -
   3.450 -   privateWaitLock();
   3.451 -   (*ptr)+= value;
   3.452 -   tmp = *ptr;
   3.453 -   privateUnlock();
   3.454 -
   3.455 -   return tmp;
   3.456 -#endif
   3.457 -}
   3.458 -
   3.459 -Uint8
   3.460 -SDL_AtomicSubtractThenFetch8(volatile Uint8 * ptr, Uint8 value)
   3.461 -{
   3.462 -#ifdef nativeSubtractThenFetch8
   3.463 -   return nativeSubtractThenFetch8(ptr, value);
   3.464 -#else
   3.465 -   Uint8 tmp = 0;
   3.466 -
   3.467 -   privateWaitLock();
   3.468 -   (*ptr)-= value;
   3.469 -   tmp = *ptr;
   3.470 -   privateUnlock();
   3.471 -
   3.472 -   return tmp;
   3.473 -#endif
   3.474 -}
   3.475 -
   3.476 -/* 16 bit atomic operations */
   3.477 -
   3.478 -Uint16
   3.479 -SDL_AtomicExchange16(volatile Uint16 * ptr, Uint16 value)
   3.480 -{
   3.481 -#ifdef nativeExchange16
   3.482 -   return nativeExchange16(ptr, value);
   3.483 -#else
   3.484 -   Uint16 tmp = 0;
   3.485 -
   3.486 -   privateWaitLock();
   3.487 -   tmp = *ptr;
   3.488 -   *ptr = value;
   3.489 -   privateUnlock();
   3.490 -
   3.491 -   return tmp;
   3.492 -#endif
   3.493 -}
   3.494 -
   3.495 -SDL_bool
   3.496 -SDL_AtomicCompareThenSet16(volatile Uint16 * ptr, Uint16 oldvalue, Uint16 newvalue)
   3.497 -{
   3.498 -#ifdef nativeCompareThenSet16
   3.499 -   return (SDL_bool)nativeCompareThenSet16(ptr, oldvalue, newvalue);
   3.500 -#else
   3.501 -   SDL_bool result = SDL_FALSE;
   3.502 -
   3.503 -   privateWaitLock();
   3.504 -   result = (*ptr == oldvalue);
   3.505 -   if (result)
   3.506 -   {
   3.507 -      *ptr = newvalue;
   3.508 -   }
   3.509 -   privateUnlock();
   3.510 -
   3.511 -   return result;
   3.512 -#endif
   3.513 -}
   3.514 -
   3.515 -SDL_bool
   3.516 -SDL_AtomicTestThenSet16(volatile Uint16 * ptr)
   3.517 -{
   3.518 -#ifdef nativeTestThenSet16
   3.519 -   return (SDL_bool)nativeTestThenSet16(ptr);
   3.520 -#else
   3.521 -   SDL_bool result = SDL_FALSE;
   3.522 -
   3.523 -   privateWaitLock();
   3.524 -   result = (*ptr == 0);
   3.525 -   if (result)
   3.526 -   {
   3.527 -      *ptr = 1;
   3.528 -   }
   3.529 -   privateUnlock();
   3.530 -
   3.531 -   return result;
   3.532 -#endif
   3.533 -}
   3.534 -
   3.535 -void
   3.536 -SDL_AtomicClear16(volatile Uint16 * ptr)
   3.537 -{
   3.538 -#ifdef nativeClear16
   3.539 -   nativeClear16(ptr);
   3.540 -#else
   3.541 -   privateWaitLock();
   3.542 -   *ptr = 0;
   3.543 -   privateUnlock();
   3.544 -
   3.545 -   return;
   3.546 -#endif
   3.547 -}
   3.548 -
   3.549 -Uint16
   3.550 -SDL_AtomicFetchThenIncrement16(volatile Uint16 * ptr)
   3.551 -{
   3.552 -#ifdef nativeFetchThenIncrement16
   3.553 -   return nativeFetchThenIncrement16(ptr);
   3.554 -#else
   3.555 -   Uint16 tmp = 0;
   3.556 -
   3.557 -   privateWaitLock();
   3.558 -   tmp = *ptr;
   3.559 -   (*ptr)+= 1;
   3.560 -   privateUnlock();
   3.561 -
   3.562 -   return tmp;
   3.563 -#endif
   3.564 -}
   3.565 -
   3.566 -Uint16
   3.567 -SDL_AtomicFetchThenDecrement16(volatile Uint16 * ptr)
   3.568 -{
   3.569 -#ifdef nativeFetchThenDecrement16
   3.570 -   return nativeFetchThenDecrement16(ptr);
   3.571 -#else
   3.572 -   Uint16 tmp = 0;
   3.573 -
   3.574 -   privateWaitLock();
   3.575 -   tmp = *ptr;
   3.576 -   (*ptr) -= 1;
   3.577 -   privateUnlock();
   3.578 -
   3.579 -   return tmp;
   3.580 -#endif
   3.581 -}
   3.582 -
   3.583 -Uint16
   3.584 -SDL_AtomicFetchThenAdd16(volatile Uint16 * ptr, Uint16 value)
   3.585 -{
   3.586 -#ifdef nativeFetchThenAdd16
   3.587 -   return nativeFetchThenAdd16(ptr, value);
   3.588 -#else
   3.589 -   Uint16 tmp = 0;
   3.590 -
   3.591 -   privateWaitLock();
   3.592 -   tmp = *ptr;
   3.593 -   (*ptr)+= value;
   3.594 -   privateUnlock();
   3.595 -
   3.596 -   return tmp;
   3.597 -#endif
   3.598 -}
   3.599 -
   3.600 -Uint16
   3.601 -SDL_AtomicFetchThenSubtract16(volatile Uint16 * ptr, Uint16 value)
   3.602 -{
   3.603 -#ifdef nativeFetchThenSubtract16
   3.604 -   return nativeFetchThenSubtract16(ptr, value);
   3.605 -#else
   3.606 -   Uint16 tmp = 0;
   3.607 -
   3.608 -   privateWaitLock();
   3.609 -   tmp = *ptr;
   3.610 -   (*ptr)-= value;
   3.611 -   privateUnlock();
   3.612 -
   3.613 -   return tmp;
   3.614 -#endif
   3.615 -}
   3.616 -
   3.617 -Uint16
   3.618 -SDL_AtomicIncrementThenFetch16(volatile Uint16 * ptr)
   3.619 -{
   3.620 -#ifdef nativeIncrementThenFetch16
   3.621 -   return nativeIncrementThenFetch16(ptr);
   3.622 -#else
   3.623 -   Uint16 tmp = 0;
   3.624 -
   3.625 -   privateWaitLock();
   3.626 -   (*ptr)+= 1;
   3.627 -   tmp = *ptr;
   3.628 -   privateUnlock();
   3.629 -
   3.630 -   return tmp;
   3.631 -#endif
   3.632 -}
   3.633 -
   3.634 -Uint16
   3.635 -SDL_AtomicDecrementThenFetch16(volatile Uint16 * ptr)
   3.636 -{
   3.637 -#ifdef nativeDecrementThenFetch16
   3.638 -   return nativeDecrementThenFetch16(ptr);
   3.639 -#else
   3.640 -   Uint16 tmp = 0;
   3.641 -
   3.642 -   privateWaitLock();
   3.643 -   (*ptr)-= 1;
   3.644 -   tmp = *ptr;
   3.645 -   privateUnlock();
   3.646 -
   3.647 -   return tmp;
   3.648 -#endif
   3.649 -}
   3.650 -
   3.651 -Uint16
   3.652 -SDL_AtomicAddThenFetch16(volatile Uint16 * ptr, Uint16 value)
   3.653 -{
   3.654 -#ifdef nativeAddThenFetch16
   3.655 -   return nativeAddThenFetch16(ptr, value);
   3.656 -#else
   3.657 -   Uint16 tmp = 0;
   3.658 -
   3.659 -   privateWaitLock();
   3.660 -   (*ptr)+= value;
   3.661 -   tmp = *ptr;
   3.662 -   privateUnlock();
   3.663 -
   3.664 -   return tmp;
   3.665 -#endif
   3.666 -}
   3.667 -
   3.668 -Uint16
   3.669 -SDL_AtomicSubtractThenFetch16(volatile Uint16 * ptr, Uint16 value)
   3.670 -{
   3.671 -#ifdef nativeSubtractThenFetch16
   3.672 -   return nativeSubtractThenFetch16(ptr, value);
   3.673 -#else
   3.674 -   Uint16 tmp = 0;
   3.675 -
   3.676 -   privateWaitLock();
   3.677 -   (*ptr)-= value;
   3.678 -   tmp = *ptr;
   3.679 -   privateUnlock();
   3.680 -
   3.681 -   return tmp;
   3.682 -#endif
   3.683 -}
   3.684 -
   3.685 -/* 32 bit atomic operations */
   3.686 -
   3.687 -Uint32
   3.688 -SDL_AtomicExchange32(volatile Uint32 * ptr, Uint32 value)
   3.689 -{
   3.690 -#ifdef nativeExchange32
   3.691 -   return nativeExchange32(ptr, value);
   3.692 -#else
   3.693 -   Uint32 tmp = 0;
   3.694 -
   3.695 -   privateWaitLock();
   3.696 -   tmp = *ptr;
   3.697 -   *ptr = value;
   3.698 -   privateUnlock();
   3.699 -
   3.700 -   return tmp;
   3.701 -#endif
   3.702 -}
   3.703 -
   3.704 -SDL_bool
   3.705 -SDL_AtomicCompareThenSet32(volatile Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
   3.706 -{
   3.707 -#ifdef nativeCompareThenSet32
   3.708 -   return (SDL_bool)nativeCompareThenSet32(ptr, oldvalue, newvalue);
   3.709 -#else
   3.710 -   SDL_bool result = SDL_FALSE;
   3.711 -
   3.712 -   privateWaitLock();
   3.713 -   result = (*ptr == oldvalue);
   3.714 -   if (result)
   3.715 -   {
   3.716 -      *ptr = newvalue;
   3.717 -   }
   3.718 -   privateUnlock();
   3.719 -
   3.720 -   return result;
   3.721 -#endif
   3.722 -}
   3.723 -
   3.724 -SDL_bool
   3.725 -SDL_AtomicTestThenSet32(volatile Uint32 * ptr)
   3.726 -{
   3.727 -#ifdef nativeTestThenSet32
   3.728 -   return (SDL_bool)nativeTestThenSet32(ptr);
   3.729 -#else
   3.730 -   SDL_bool result = SDL_FALSE;
   3.731 -
   3.732 -   privateWaitLock();
   3.733 -   result = (*ptr == 0);
   3.734 -   if (result)
   3.735 -   {
   3.736 -      *ptr = 1;
   3.737 -   }
   3.738 -   privateUnlock();
   3.739 +   privateUnlock(ptr);
   3.740  
   3.741     return result;
   3.742  #endif
   3.743 @@ -615,11 +171,12 @@
   3.744  SDL_AtomicClear32(volatile Uint32 * ptr)
   3.745  {
   3.746  #ifdef nativeClear32
   3.747 -   nativeClear32(ptr);
   3.748 +   __sync_lock_test_and_set(ptr, 0);
   3.749 +   return;
   3.750  #else
   3.751 -   privateWaitLock();
   3.752 +   privateWaitLock(ptr);
   3.753     *ptr = 0;
   3.754 -   privateUnlock();
   3.755 +   privateUnlock(ptr);
   3.756  
   3.757     return;
   3.758  #endif
   3.759 @@ -629,14 +186,14 @@
   3.760  SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr)
   3.761  {
   3.762  #ifdef nativeFetchThenIncrement32
   3.763 -   return nativeFetchThenIncrement32(ptr);
   3.764 +   return __sync_fetch_and_add(ptr, 1);
   3.765  #else
   3.766     Uint32 tmp = 0;
   3.767  
   3.768 -   privateWaitLock();
   3.769 +   privateWaitLock(ptr);
   3.770     tmp = *ptr;
   3.771     (*ptr)+= 1;
   3.772 -   privateUnlock();
   3.773 +   privateUnlock(ptr);
   3.774  
   3.775     return tmp;
   3.776  #endif
   3.777 @@ -646,14 +203,14 @@
   3.778  SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr)
   3.779  {
   3.780  #ifdef nativeFetchThenDecrement32
   3.781 -   return nativeFetchThenDecrement32(ptr);
   3.782 +   return __sync_fetch_and_sub(ptr, 1);
   3.783  #else
   3.784     Uint32 tmp = 0;
   3.785  
   3.786 -   privateWaitLock();
   3.787 +   privateWaitLock(ptr);
   3.788     tmp = *ptr;
   3.789     (*ptr) -= 1;
   3.790 -   privateUnlock();
   3.791 +   privateUnlock(ptr);
   3.792  
   3.793     return tmp;
   3.794  #endif
   3.795 @@ -663,14 +220,14 @@
   3.796  SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value)
   3.797  {
   3.798  #ifdef nativeFetchThenAdd32
   3.799 -   return nativeFetchThenAdd32(ptr, value);
   3.800 +   return __sync_fetch_and_add(ptr, value);
   3.801  #else
   3.802     Uint32 tmp = 0;
   3.803  
   3.804 -   privateWaitLock();
   3.805 +   privateWaitLock(ptr);
   3.806     tmp = *ptr;
   3.807     (*ptr)+= value;
   3.808 -   privateUnlock();
   3.809 +   privateUnlock(ptr);
   3.810  
   3.811     return tmp;
   3.812  #endif
   3.813 @@ -680,14 +237,14 @@
   3.814  SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value)
   3.815  {
   3.816  #ifdef nativeFetchThenSubtract32
   3.817 -   return nativeFetchThenSubtract32(ptr, value);
   3.818 +   return __sync_fetch_and_sub(ptr, value);
   3.819  #else
   3.820     Uint32 tmp = 0;
   3.821  
   3.822 -   privateWaitLock();
   3.823 +   privateWaitLock(ptr);
   3.824     tmp = *ptr;
   3.825     (*ptr)-= value;
   3.826 -   privateUnlock();
   3.827 +   privateUnlock(ptr);
   3.828  
   3.829     return tmp;
   3.830  #endif
   3.831 @@ -697,14 +254,14 @@
   3.832  SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr)
   3.833  {
   3.834  #ifdef nativeIncrementThenFetch32
   3.835 -   return nativeIncrementThenFetch32(ptr);
   3.836 +   return __sync_add_and_fetch(ptr, 1);
   3.837  #else
   3.838     Uint32 tmp = 0;
   3.839  
   3.840 -   privateWaitLock();
   3.841 +   privateWaitLock(ptr);
   3.842     (*ptr)+= 1;
   3.843     tmp = *ptr;
   3.844 -   privateUnlock();
   3.845 +   privateUnlock(ptr);
   3.846  
   3.847     return tmp;
   3.848  #endif
   3.849 @@ -714,14 +271,14 @@
   3.850  SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr)
   3.851  {
   3.852  #ifdef nativeDecrementThenFetch32
   3.853 -   return nativeDecrementThenFetch32(ptr);
   3.854 +   return __sync_sub_and_fetch(ptr, 1);
   3.855  #else
   3.856     Uint32 tmp = 0;
   3.857  
   3.858 -   privateWaitLock();
   3.859 +   privateWaitLock(ptr);
   3.860     (*ptr)-= 1;
   3.861     tmp = *ptr;
   3.862 -   privateUnlock();
   3.863 +   privateUnlock(ptr);
   3.864  
   3.865     return tmp;
   3.866  #endif
   3.867 @@ -731,14 +288,14 @@
   3.868  SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value)
   3.869  {
   3.870  #ifdef nativeAddThenFetch32
   3.871 -   return nativeAddThenFetch32(ptr, value);
   3.872 +   return __sync_add_and_fetch(ptr, value);
   3.873  #else
   3.874     Uint32 tmp = 0;
   3.875  
   3.876 -   privateWaitLock();
   3.877 +   privateWaitLock(ptr);
   3.878     (*ptr)+= value;
   3.879     tmp = *ptr;
   3.880 -   privateUnlock();
   3.881 +   privateUnlock(ptr);
   3.882  
   3.883     return tmp;
   3.884  #endif
   3.885 @@ -748,14 +305,14 @@
   3.886  SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value)
   3.887  {
   3.888  #ifdef nativeSubtractThenFetch32
   3.889 -   return nativeSubtractThenFetch32(ptr, value);
   3.890 +   return __sync_sub_and_fetch(ptr, value);
   3.891  #else
   3.892     Uint32 tmp = 0;
   3.893  
   3.894 -   privateWaitLock();
   3.895 +   privateWaitLock(ptr);
   3.896     (*ptr)-= value;
   3.897     tmp = *ptr;
   3.898 -   privateUnlock();
   3.899 +   privateUnlock(ptr);
   3.900  
   3.901     return tmp;
   3.902  #endif
   3.903 @@ -764,58 +321,21 @@
   3.904  /* 64 bit atomic operations */
   3.905  #ifdef SDL_HAS_64BIT_TYPE
   3.906  
   3.907 -Uint64
   3.908 -SDL_AtomicExchange64(volatile Uint64 * ptr, Uint64 value)
   3.909 -{
   3.910 -#ifdef nativeExchange64
   3.911 -   return nativeExchange64(ptr, value);
   3.912 -#else
   3.913 -   Uint64 tmp = 0;
   3.914 -
   3.915 -   privateWaitLock();
   3.916 -   tmp = *ptr;
   3.917 -   *ptr = value;
   3.918 -   privateUnlock();
   3.919 -
   3.920 -   return tmp;
   3.921 -#endif
   3.922 -}
   3.923 -
   3.924 -SDL_bool
   3.925 -SDL_AtomicCompareThenSet64(volatile Uint64 * ptr, Uint64 oldvalue, Uint64 newvalue)
   3.926 -{
   3.927 -#ifdef nativeCompareThenSet64
   3.928 -   return (SDL_bool)nativeCompareThenSet64(ptr, oldvalue, newvalue);
   3.929 -#else
   3.930 -   SDL_bool result = SDL_FALSE;
   3.931 -
   3.932 -   privateWaitLock();
   3.933 -   result = (*ptr == oldvalue);
   3.934 -   if (result)
   3.935 -   {
   3.936 -      *ptr = newvalue;
   3.937 -   }
   3.938 -   privateUnlock();
   3.939 -
   3.940 -   return result;
   3.941 -#endif
   3.942 -}
   3.943 -
   3.944  SDL_bool
   3.945  SDL_AtomicTestThenSet64(volatile Uint64 * ptr)
   3.946  {
   3.947  #ifdef nativeTestThenSet64
   3.948 -   return (SDL_bool)nativeTestThenSet64(ptr);
   3.949 +   return 0 == __sync_lock_test_and_set(ptr, 1);
   3.950  #else
   3.951     SDL_bool result = SDL_FALSE;
   3.952  
   3.953 -   privateWaitLock();
   3.954 +   privateWaitLock(ptr);
   3.955     result = (*ptr == 0);
   3.956     if (result)
   3.957     {
   3.958        *ptr = 1;
   3.959     }
   3.960 -   privateUnlock();
   3.961 +   privateUnlock(ptr);
   3.962  
   3.963     return result;
   3.964  #endif
   3.965 @@ -825,11 +345,12 @@
   3.966  SDL_AtomicClear64(volatile Uint64 * ptr)
   3.967  {
   3.968  #ifdef nativeClear64
   3.969 -   nativeClear64(ptr);
   3.970 +   __sync_lock_test_and_set(ptr, 0);
   3.971 +   return;
   3.972  #else
   3.973 -   privateWaitLock();
   3.974 +   privateWaitLock(ptr);
   3.975     *ptr = 0;
   3.976 -   privateUnlock();
   3.977 +   privateUnlock(ptr);
   3.978  
   3.979     return;
   3.980  #endif
   3.981 @@ -839,14 +360,14 @@
   3.982  SDL_AtomicFetchThenIncrement64(volatile Uint64 * ptr)
   3.983  {
   3.984  #ifdef nativeFetchThenIncrement64
   3.985 -   return nativeFetchThenIncrement64(ptr);
   3.986 +   return __sync_fetch_and_add(ptr, 1);
   3.987  #else
   3.988     Uint64 tmp = 0;
   3.989  
   3.990 -   privateWaitLock();
   3.991 +   privateWaitLock(ptr);
   3.992     tmp = *ptr;
   3.993     (*ptr)+= 1;
   3.994 -   privateUnlock();
   3.995 +   privateUnlock(ptr);
   3.996  
   3.997     return tmp;
   3.998  #endif
   3.999 @@ -856,14 +377,14 @@
  3.1000  SDL_AtomicFetchThenDecrement64(volatile Uint64 * ptr)
  3.1001  {
  3.1002  #ifdef nativeFetchThenDecrement64
  3.1003 -   return nativeFetchThenDecrement64(ptr);
  3.1004 +   return __sync_fetch_and_sub(ptr, 1);
  3.1005  #else
  3.1006     Uint64 tmp = 0;
  3.1007  
  3.1008 -   privateWaitLock();
  3.1009 +   privateWaitLock(ptr);
  3.1010     tmp = *ptr;
  3.1011     (*ptr) -= 1;
  3.1012 -   privateUnlock();
  3.1013 +   privateUnlock(ptr);
  3.1014  
  3.1015     return tmp;
  3.1016  #endif
  3.1017 @@ -873,14 +394,14 @@
  3.1018  SDL_AtomicFetchThenAdd64(volatile Uint64 * ptr, Uint64 value)
  3.1019  {
  3.1020  #ifdef nativeFetchThenAdd64
  3.1021 -   return nativeFetchThenAdd64(ptr, value);
  3.1022 +   return __sync_fetch_and_add(ptr, value);
  3.1023  #else
  3.1024     Uint64 tmp = 0;
  3.1025  
  3.1026 -   privateWaitLock();
  3.1027 +   privateWaitLock(ptr);
  3.1028     tmp = *ptr;
  3.1029     (*ptr)+= value;
  3.1030 -   privateUnlock();
  3.1031 +   privateUnlock(ptr);
  3.1032  
  3.1033     return tmp;
  3.1034  #endif
  3.1035 @@ -890,14 +411,14 @@
  3.1036  SDL_AtomicFetchThenSubtract64(volatile Uint64 * ptr, Uint64 value)
  3.1037  {
  3.1038  #ifdef nativeFetchThenSubtract64
  3.1039 -   return nativeFetchThenSubtract64(ptr, value);
  3.1040 +   return __sync_fetch_and_sub(ptr, value);
  3.1041  #else
  3.1042     Uint64 tmp = 0;
  3.1043  
  3.1044 -   privateWaitLock();
  3.1045 +   privateWaitLock(ptr);
  3.1046     tmp = *ptr;
  3.1047     (*ptr)-= value;
  3.1048 -   privateUnlock();
  3.1049 +   privateUnlock(ptr);
  3.1050  
  3.1051     return tmp;
  3.1052  #endif
  3.1053 @@ -907,14 +428,14 @@
  3.1054  SDL_AtomicIncrementThenFetch64(volatile Uint64 * ptr)
  3.1055  {
  3.1056  #ifdef nativeIncrementThenFetch64
  3.1057 -   return nativeIncrementThenFetch64(ptr);
  3.1058 +   return __sync_add_and_fetch(ptr, 1);
  3.1059  #else
  3.1060     Uint64 tmp = 0;
  3.1061  
  3.1062 -   privateWaitLock();
  3.1063 +   privateWaitLock(ptr);
  3.1064     (*ptr)+= 1;
  3.1065     tmp = *ptr;
  3.1066 -   privateUnlock();
  3.1067 +   privateUnlock(ptr);
  3.1068  
  3.1069     return tmp;
  3.1070  #endif
  3.1071 @@ -924,14 +445,14 @@
  3.1072  SDL_AtomicDecrementThenFetch64(volatile Uint64 * ptr)
  3.1073  {
  3.1074  #ifdef nativeDecrementThenFetch64
  3.1075 -   return nativeDecrementThenFetch64(ptr);
  3.1076 +   return __sync_sub_and_fetch(ptr, 1);
  3.1077  #else
  3.1078     Uint64 tmp = 0;
  3.1079  
  3.1080 -   privateWaitLock();
  3.1081 +   privateWaitLock(ptr);
  3.1082     (*ptr)-= 1;
  3.1083     tmp = *ptr;
  3.1084 -   privateUnlock();
  3.1085 +   privateUnlock(ptr);
  3.1086  
  3.1087     return tmp;
  3.1088  #endif
  3.1089 @@ -941,14 +462,14 @@
  3.1090  SDL_AtomicAddThenFetch64(volatile Uint64 * ptr, Uint64 value)
  3.1091  {
  3.1092  #ifdef nativeAddThenFetch64
  3.1093 -   return nativeAddThenFetch64(ptr, value);
  3.1094 +   return __sync_add_and_fetch(ptr, value);
  3.1095  #else
  3.1096     Uint64 tmp = 0;
  3.1097  
  3.1098 -   privateWaitLock();
  3.1099 +   privateWaitLock(ptr);
  3.1100     (*ptr)+= value;
  3.1101     tmp = *ptr;
  3.1102 -   privateUnlock();
  3.1103 +   privateUnlock(ptr);
  3.1104  
  3.1105     return tmp;
  3.1106  #endif
  3.1107 @@ -958,17 +479,17 @@
  3.1108  SDL_AtomicSubtractThenFetch64(volatile Uint64 * ptr, Uint64 value)
  3.1109  {
  3.1110  #ifdef nativeSubtractThenFetch64
  3.1111 -   return nativeSubtractThenFetch64(ptr, value);
  3.1112 +   return __sync_sub_and_fetch(ptr, value);
  3.1113  #else
  3.1114     Uint64 tmp = 0;
  3.1115  
  3.1116 -   privateWaitLock();
  3.1117 +   privateWaitLock(ptr);
  3.1118     (*ptr)-= value;
  3.1119     tmp = *ptr;
  3.1120 -   privateUnlock();
  3.1121 +   privateUnlock(ptr);
  3.1122  
  3.1123     return tmp;
  3.1124  #endif
  3.1125  }
  3.1126 -#endif
  3.1127  
  3.1128 +#endif /* SDL_HAS_64BIT_TYPE */
     4.1 --- a/src/atomic/macosx/SDL_atomic.c	Mon Sep 07 16:04:44 2009 +0000
     4.2 +++ b/src/atomic/macosx/SDL_atomic.c	Thu Sep 17 20:35:12 2009 +0000
     4.3 @@ -1,639 +1,162 @@
     4.4  /*
     4.5 -    SDL - Simple DirectMedia Layer
     4.6 -    Copyright (C) 1997-2009 Sam Lantinga
     4.7 +  SDL - Simple DirectMedia Layer
     4.8 +  Copyright (C) 1997-2009 Sam Lantinga
     4.9  
    4.10 -    This library is free software; you can redistribute it and/or
    4.11 -    modify it under the terms of the GNU Lesser General Public
    4.12 -    License as published by the Free Software Foundation; either
    4.13 -    version 2.1 of the License, or (at your option) any later version.
    4.14 +  This library is free software; you can redistribute it and/or
    4.15 +  modify it under the terms of the GNU Lesser General Public
    4.16 +  License as published by the Free Software Foundation; either
    4.17 +  version 2.1 of the License, or (at your option) any later version.
    4.18  
    4.19 -    This library is distributed in the hope that it will be useful,
    4.20 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.21 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    4.22 -    Lesser General Public License for more details.
    4.23 +  This library is distributed in the hope that it will be useful,
    4.24 +  but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.25 +  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    4.26 +  Lesser General Public License for more details.
    4.27  
    4.28 -    You should have received a copy of the GNU Lesser General Public
    4.29 -    License along with this library; if not, write to the Free Software
    4.30 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    4.31 +  You should have received a copy of the GNU Lesser General Public
    4.32 +  License along with this library; if not, write to the Free Software
    4.33 +  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    4.34  
    4.35 -    Sam Lantinga
    4.36 -    slouken@libsdl.org
    4.37 +  Sam Lantinga
    4.38 +  slouken@libsdl.org
    4.39 +
    4.40 +  Contributed by Bob Pendleton, bob@pendleton.com
    4.41  */
    4.42  
    4.43  #include "SDL_stdinc.h"
    4.44  #include "SDL_atomic.h"
    4.45  
    4.46 +#include "SDL_error.h"
    4.47 +
    4.48  /*
    4.49 -  This file provides 8, 16, 32, and 64 bit atomic operations. If the
    4.50 +  This file provides 32, and 64 bit atomic operations. If the
    4.51    operations are provided by the native hardware and operating system
    4.52    they are used. If they are not then the operations are emulated
    4.53 -  using the SDL mutex operations. 
    4.54 - */
    4.55 -
    4.56 -/* 
    4.57 -  First, detect whether the operations are supported and create
    4.58 -  #defines that indicate that they do exist. The goal is to have all
    4.59 -  the system dependent code in the top part of the file so that the
    4.60 -  bottom can be use unchanged across all platforms.
    4.61 -
    4.62 -  Second, #define all the operations in each size class that are
    4.63 -  supported. Doing this allows supported operations to be used along
    4.64 -  side of emulated operations.
    4.65 +  using the SDL spin lock operations. If spin lock can not be
    4.66 +  implemented then these functions must fail.
    4.67  */
    4.68  
    4.69  /* 
    4.70 -   Emmulated version.
    4.71 +  DUMMY VERSION.
    4.72  
    4.73 -   Assume there is no support for atomic operations. All such
    4.74 -   operations are implemented using SDL mutex operations.
    4.75 - */
    4.76 +  This version of the code assumes there is no support for atomic
    4.77 +  operations. Therefore, every function sets the SDL error
    4.78 +  message. Oddly enough, if you only have one thread then this
    4.79 +  version actuallys works.
    4.80 +*/
    4.81  
    4.82 -#ifdef EMULATED_ATOMIC_OPERATIONS
    4.83 -#undef EMULATED_ATOMIC_OPERATIONS
    4.84 +/*
    4.85 +  Native spinlock routines. Because this is the dummy implementation
    4.86 +  these will always call SDL_SetError() and do nothing.
    4.87 +*/
    4.88 +
    4.89 +void 
    4.90 +SDL_AtomicLock(SDL_SpinLock *lock)
    4.91 +{
    4.92 +   SDL_SetError("SDL_atomic.c: is not implemented on this platform");
    4.93 +}
    4.94 +
    4.95 +void 
    4.96 +SDL_AtomicUnlock(SDL_SpinLock *lock)
    4.97 +{
    4.98 +   SDL_SetError("SDL_atomic.c: is not implemented on this platform");
    4.99 +}
   4.100 +
   4.101 +/*
   4.102 +  Note that platform specific versions can be built from this version
   4.103 +  by changing the #undefs to #defines and adding platform specific
   4.104 +  code.
   4.105 +*/
   4.106 +
   4.107 +#undef  nativeTestThenSet32
   4.108 +#undef  nativeClear32
   4.109 +#undef  nativeFetchThenIncrement32
   4.110 +#undef  nativeFetchThenDecrement32
   4.111 +#undef  nativeFetchThenAdd32
   4.112 +#undef  nativeFetchThenSubtract32
   4.113 +#undef  nativeIncrementThenFetch32
   4.114 +#undef  nativeDecrementThenFetch32
   4.115 +#undef  nativeAddThenFetch32
   4.116 +#undef  nativeSubtractThenFetch32
   4.117 +
   4.118 +#undef  nativeTestThenSet64
   4.119 +#undef  nativeClear64
   4.120 +#undef  nativeFetchThenIncrement64
   4.121 +#undef  nativeFetchThenDecrement64
   4.122 +#undef  nativeFetchThenAdd64
   4.123 +#undef  nativeFetchThenSubtract64
   4.124 +#undef  nativeIncrementThenFetch64
   4.125 +#undef  nativeDecrementThenFetch64
   4.126 +#undef  nativeAddThenFetch64
   4.127 +#undef  nativeSubtractThenFetch64
   4.128 +
   4.129 +/* 
   4.130 +  If any of the operations are not provided then we must emulate some
   4.131 +  of them. That means we need a nice implementation of spin locks
   4.132 +  that avoids the "one big lock" problem. We use a vector of spin
   4.133 +  locks and pick which one to use based on the address of the operand
   4.134 +  of the function.
   4.135 +
   4.136 +  To generate the index of the lock we first shift by 3 bits to get
   4.137 +  rid on the zero bits that result from 32 and 64 bit allignment of
   4.138 +  data. We then mask off all but 5 bits and use those 5 bits as an
   4.139 +  index into the table. 
   4.140 +
   4.141 +  Picking the lock this way insures that accesses to the same data at
   4.142 +  the same time will go to the same lock. OTOH, accesses to different
   4.143 +  data have only a 1/32 chance of hitting the same lock. That should
   4.144 +  pretty much eliminate the chances of several atomic operations on
   4.145 +  different data from waiting on the same "big lock". If it isn't
   4.146 +  then the table of locks can be expanded to a new size so long as
   4.147 +  the new size if a power of two.
   4.148 +*/
   4.149 +
   4.150 +static SDL_SpinLock locks[32] = {
   4.151 +   0, 0, 0, 0, 0, 0, 0, 0,
   4.152 +   0, 0, 0, 0, 0, 0, 0, 0,
   4.153 +   0, 0, 0, 0, 0, 0, 0, 0,
   4.154 +   0, 0, 0, 0, 0, 0, 0, 0,
   4.155 +};
   4.156 +
   4.157 +static __inline__ void
   4.158 +privateWaitLock(volatile void *ptr)
   4.159 +{
   4.160 +#if SIZEOF_VOIDP == 4
   4.161 +   Uint32 index = ((((Uint32)ptr) >> 3) & 0x1f);
   4.162 +#elif SIZEOF_VOIDP == 8
   4.163 +   Uint64 index = ((((Uint64)ptr) >> 3) & 0x1f);
   4.164  #endif
   4.165  
   4.166 -#ifdef EMULATED_ATOMIC_OPERATIONS
   4.167 -#define HAVE_ALL_8_BIT_OPS
   4.168 -
   4.169 -#define nativeExchange8(ptr, value)			()
   4.170 -#define nativeCompareThenSet8(ptr, oldvalue, newvalue) 	()
   4.171 -#define nativeTestThenSet8(ptr)    	     		()
   4.172 -#define nativeClear8(ptr)				()
   4.173 -#define nativeFetchThenIncrement8(ptr)   		()
   4.174 -#define nativeFetchThenDecrement8(ptr) 			()
   4.175 -#define nativeFetchThenAdd8(ptr, value) 		()
   4.176 -#define nativeFetchThenSubtract8(ptr, value) 		()
   4.177 -#define nativeIncrementThenFetch8(ptr) 			()
   4.178 -#define nativeDecrementThenFetch8(ptr) 			()
   4.179 -#define nativeAddThenFetch8(ptr, value) 		()
   4.180 -#define nativeSubtractThenFetch8(ptr, value) 		()
   4.181 -#endif
   4.182 -
   4.183 -#ifdef EMULATED_ATOMIC_OPERATIONS
   4.184 -#define HAVE_ALL_16_BIT_OPS
   4.185 -
   4.186 -#define nativeExchange16(ptr, value)			()
   4.187 -#define nativeCompareThenSet16(ptr, oldvalue, newvalue) ()
   4.188 -#define nativeTestThenSet16(ptr)    	     		()
   4.189 -#define nativeClear16(ptr)				()
   4.190 -#define nativeFetchThenIncrement16(ptr)   		()
   4.191 -#define nativeFetchThenDecrement16(ptr) 		()
   4.192 -#define nativeFetchThenAdd16(ptr, value) 		()
   4.193 -#define nativeFetchThenSubtract16(ptr, value) 		()
   4.194 -#define nativeIncrementThenFetch16(ptr) 		()
   4.195 -#define nativeDecrementThenFetch16(ptr) 		()
   4.196 -#define nativeAddThenFetch16(ptr, value) 		()
   4.197 -#define nativeSubtractThenFetch16(ptr, value) 		()
   4.198 -#endif
   4.199 -
   4.200 -#ifdef EMULATED_ATOMIC_OPERATIONS
   4.201 -#define HAVE_ALL_32_BIT_OPS
   4.202 -
   4.203 -#define nativeExchange32(ptr, value)			()
   4.204 -#define nativeCompareThenSet32(ptr, oldvalue, newvalue) ()
   4.205 -#define nativeTestThenSet32(ptr)    	     		()
   4.206 -#define nativeClear32(ptr)				()
   4.207 -#define nativeFetchThenIncrement32(ptr)   		()
   4.208 -#define nativeFetchThenDecrement32(ptr) 		()
   4.209 -#define nativeFetchThenAdd32(ptr, value) 		()
   4.210 -#define nativeFetchThenSubtract32(ptr, value) 		()
   4.211 -#define nativeIncrementThenFetch32(ptr) 		()
   4.212 -#define nativeDecrementThenFetch32(ptr) 		()
   4.213 -#define nativeAddThenFetch32(ptr, value) 		()
   4.214 -#define nativeSubtractThenFetch32(ptr, value) 		()
   4.215 -#endif
   4.216 -
   4.217 -#ifdef EMULATED_ATOMIC_OPERATIONS
   4.218 -#define HAVE_ALL_64_BIT_OPS
   4.219 -
   4.220 -#define nativeExchange64(ptr, value)			()
   4.221 -#define nativeCompareThenSet64(ptr, oldvalue, newvalue) ()
   4.222 -#define nativeTestThenSet64(ptr)    	     		()
   4.223 -#define nativeClear64(ptr)				()
   4.224 -#define nativeFetchThenIncrement64(ptr)   		()
   4.225 -#define nativeFetchThenDecrement64(ptr) 		()
   4.226 -#define nativeFetchThenAdd64(ptr, value) 		()
   4.227 -#define nativeFetchThenSubtract64(ptr, value) 		()
   4.228 -#define nativeIncrementThenFetch64(ptr) 		()
   4.229 -#define nativeDecrementThenFetch64(ptr) 		()
   4.230 -#define nativeAddThenFetch64(ptr, value) 		()
   4.231 -#define nativeSubtractThenFetch64(ptr, value) 		()
   4.232 -#endif
   4.233 -
   4.234 -/* 
   4.235 -If any of the operations are not provided then we must emulate some of
   4.236 -them.
   4.237 - */
   4.238 -
   4.239 -#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)
   4.240 -
   4.241 -#include "SDL_mutex.h"
   4.242 -#include "SDL_error.h"
   4.243 -
   4.244 -static SDL_mutex * lock = NULL;
   4.245 -
   4.246 -static __inline__ void
   4.247 -privateWaitLock()
   4.248 -{
   4.249 -   if(NULL == lock)
   4.250 -   {
   4.251 -      lock = SDL_CreateMutex();
   4.252 -      if (NULL == lock)
   4.253 -      {
   4.254 -	 SDL_SetError("SDL_atomic.c: can't create a mutex");
   4.255 -	 return;
   4.256 -      }
   4.257 -   }
   4.258 -
   4.259 -   if (-1 == SDL_LockMutex(lock))
   4.260 -   {
   4.261 -      SDL_SetError("SDL_atomic.c: can't lock mutex");
   4.262 -   }
   4.263 +   SDL_AtomicLock(&locks[index]);
   4.264  }
   4.265  
   4.266  static __inline__ void
   4.267 -privateUnlock()
   4.268 +privateUnlock(volatile void *ptr)
   4.269  {
   4.270 -   if (-1 == SDL_UnlockMutex(lock))
   4.271 -   {
   4.272 -      SDL_SetError("SDL_atomic.c: can't unlock mutex");
   4.273 -   }
   4.274 +#if SIZEOF_VOIDP == 4
   4.275 +   Uint32 index = ((((Uint32)ptr) >> 3) & 0x1f);
   4.276 +#elif SIZEOF_VOIDP == 8
   4.277 +   Uint64 index = ((((Uint64)ptr) >> 3) & 0x1f);
   4.278 +#endif
   4.279 +
   4.280 +   SDL_AtomicUnlock(&locks[index]);
   4.281  }
   4.282  
   4.283 -#endif
   4.284 -
   4.285 -/* 8 bit atomic operations */
   4.286 -
   4.287 -Uint8
   4.288 -SDL_AtomicExchange8(volatile Uint8 * ptr, Uint8 value)
   4.289 -{
   4.290 -#ifdef nativeExchange8
   4.291 -   return nativeExchange8(ptr, value);
   4.292 -#else
   4.293 -   Uint8 tmp = 0;
   4.294 -
   4.295 -   privateWaitLock();
   4.296 -   tmp = *ptr;
   4.297 -   *ptr = value;
   4.298 -   privateUnlock();
   4.299 -
   4.300 -   return tmp;
   4.301 -#endif
   4.302 -}
   4.303 +/* 32 bit atomic operations */
   4.304  
   4.305  SDL_bool
   4.306 -SDL_AtomicCompareThenSet8(volatile Uint8 * ptr, Uint8 oldvalue, Uint8 newvalue)
   4.307 +SDL_AtomicTestThenSet32(volatile Uint32 * ptr)
   4.308  {
   4.309 -#ifdef nativeCompareThenSet8
   4.310 -   return (SDL_bool)nativeCompareThenSet8(ptr, oldvalue, newvalue);
   4.311 +#ifdef nativeTestThenSet32
   4.312  #else
   4.313     SDL_bool result = SDL_FALSE;
   4.314  
   4.315 -   privateWaitLock();
   4.316 -   result = (*ptr == oldvalue);
   4.317 -   if (result)
   4.318 -   {
   4.319 -      *ptr = newvalue;
   4.320 -   }
   4.321 -   privateUnlock();
   4.322 -
   4.323 -   return result;
   4.324 -#endif
   4.325 -}
   4.326 -
   4.327 -SDL_bool
   4.328 -SDL_AtomicTestThenSet8(volatile Uint8 * ptr)
   4.329 -{
   4.330 -#ifdef nativeTestThenSet8
   4.331 -   return (SDL_bool)nativeTestThenSet8(ptr);
   4.332 -#else
   4.333 -   SDL_bool result = SDL_FALSE;
   4.334 -
   4.335 -   privateWaitLock();
   4.336 +   privateWaitLock(ptr);
   4.337     result = (*ptr == 0);
   4.338     if (result)
   4.339     {
   4.340        *ptr = 1;
   4.341     }
   4.342 -   privateUnlock();
   4.343 -
   4.344 -   return result;
   4.345 -#endif
   4.346 -}
   4.347 -
   4.348 -void
   4.349 -SDL_AtomicClear8(volatile Uint8 * ptr)
   4.350 -{
   4.351 -#ifdef nativeClear8
   4.352 -   nativeClear8(ptr);
   4.353 -#else
   4.354 -   privateWaitLock();
   4.355 -   *ptr = 0;
   4.356 -   privateUnlock();
   4.357 -
   4.358 -   return;
   4.359 -#endif
   4.360 -}
   4.361 -
   4.362 -Uint8
   4.363 -SDL_AtomicFetchThenIncrement8(volatile Uint8 * ptr)
   4.364 -{
   4.365 -#ifdef nativeFetchThenIncrement8
   4.366 -   return nativeFetchThenIncrement8(ptr);
   4.367 -#else
   4.368 -   Uint8 tmp = 0;
   4.369 -
   4.370 -   privateWaitLock();
   4.371 -   tmp = *ptr;
   4.372 -   (*ptr)+= 1;
   4.373 -   privateUnlock();
   4.374 -
   4.375 -   return tmp;
   4.376 -#endif
   4.377 -}
   4.378 -
   4.379 -Uint8
   4.380 -SDL_AtomicFetchThenDecrement8(volatile Uint8 * ptr)
   4.381 -{
   4.382 -#ifdef nativeFetchThenDecrement8
   4.383 -   return nativeFetchThenDecrement8(ptr);
   4.384 -#else
   4.385 -   Uint8 tmp = 0;
   4.386 -
   4.387 -   privateWaitLock();
   4.388 -   tmp = *ptr;
   4.389 -   (*ptr) -= 1;
   4.390 -   privateUnlock();
   4.391 -
   4.392 -   return tmp;
   4.393 -#endif
   4.394 -}
   4.395 -
   4.396 -Uint8
   4.397 -SDL_AtomicFetchThenAdd8(volatile Uint8 * ptr, Uint8 value)
   4.398 -{
   4.399 -#ifdef nativeFetchThenAdd8
   4.400 -   return nativeFetchThenAdd8(ptr, value);
   4.401 -#else
   4.402 -   Uint8 tmp = 0;
   4.403 -
   4.404 -   privateWaitLock();
   4.405 -   tmp = *ptr;
   4.406 -   (*ptr)+= value;
   4.407 -   privateUnlock();
   4.408 -
   4.409 -   return tmp;
   4.410 -#endif
   4.411 -}
   4.412 -
   4.413 -Uint8
   4.414 -SDL_AtomicFetchThenSubtract8(volatile Uint8 * ptr, Uint8 value)
   4.415 -{
   4.416 -#ifdef nativeFetchThenSubtract8
   4.417 -   return nativeFetchThenSubtract8(ptr, value);
   4.418 -#else
   4.419 -   Uint8 tmp = 0;
   4.420 -
   4.421 -   privateWaitLock();
   4.422 -   tmp = *ptr;
   4.423 -   (*ptr)-= value;
   4.424 -   privateUnlock();
   4.425 -
   4.426 -   return tmp;
   4.427 -#endif
   4.428 -}
   4.429 -
   4.430 -Uint8
   4.431 -SDL_AtomicIncrementThenFetch8(volatile Uint8 * ptr)
   4.432 -{
   4.433 -#ifdef nativeIncrementThenFetch8
   4.434 -   return nativeIncrementThenFetch8(ptr);
   4.435 -#else
   4.436 -   Uint8 tmp = 0;
   4.437 -
   4.438 -   privateWaitLock();
   4.439 -   (*ptr)+= 1;
   4.440 -   tmp = *ptr;
   4.441 -   privateUnlock();
   4.442 -
   4.443 -   return tmp;
   4.444 -#endif
   4.445 -}
   4.446 -
   4.447 -Uint8
   4.448 -SDL_AtomicDecrementThenFetch8(volatile Uint8 * ptr)
   4.449 -{
   4.450 -#ifdef nativeDecrementThenFetch8
   4.451 -   return nativeDecrementThenFetch8(ptr);
   4.452 -#else
   4.453 -   Uint8 tmp = 0;
   4.454 -
   4.455 -   privateWaitLock();
   4.456 -   (*ptr)-= 1;
   4.457 -   tmp = *ptr;
   4.458 -   privateUnlock();
   4.459 -
   4.460 -   return tmp;
   4.461 -#endif
   4.462 -}
   4.463 -
   4.464 -Uint8
   4.465 -SDL_AtomicAddThenFetch8(volatile Uint8 * ptr, Uint8 value)
   4.466 -{
   4.467 -#ifdef nativeAddThenFetch8
   4.468 -   return nativeAddThenFetch8(ptr, value);
   4.469 -#else
   4.470 -   Uint8 tmp = 0;
   4.471 -
   4.472 -   privateWaitLock();
   4.473 -   (*ptr)+= value;
   4.474 -   tmp = *ptr;
   4.475 -   privateUnlock();
   4.476 -
   4.477 -   return tmp;
   4.478 -#endif
   4.479 -}
   4.480 -
   4.481 -Uint8
   4.482 -SDL_AtomicSubtractThenFetch8(volatile Uint8 * ptr, Uint8 value)
   4.483 -{
   4.484 -#ifdef nativeSubtractThenFetch8
   4.485 -   return nativeSubtractThenFetch8(ptr, value);
   4.486 -#else
   4.487 -   Uint8 tmp = 0;
   4.488 -
   4.489 -   privateWaitLock();
   4.490 -   (*ptr)-= value;
   4.491 -   tmp = *ptr;
   4.492 -   privateUnlock();
   4.493 -
   4.494 -   return tmp;
   4.495 -#endif
   4.496 -}
   4.497 -
   4.498 -/* 16 bit atomic operations */
   4.499 -
   4.500 -Uint16
   4.501 -SDL_AtomicExchange16(volatile Uint16 * ptr, Uint16 value)
   4.502 -{
   4.503 -#ifdef nativeExchange16
   4.504 -   return nativeExchange16(ptr, value);
   4.505 -#else
   4.506 -   Uint16 tmp = 0;
   4.507 -
   4.508 -   privateWaitLock();
   4.509 -   tmp = *ptr;
   4.510 -   *ptr = value;
   4.511 -   privateUnlock();
   4.512 -
   4.513 -   return tmp;
   4.514 -#endif
   4.515 -}
   4.516 -
   4.517 -SDL_bool
   4.518 -SDL_AtomicCompareThenSet16(volatile Uint16 * ptr, Uint16 oldvalue, Uint16 newvalue)
   4.519 -{
   4.520 -#ifdef nativeCompareThenSet16
   4.521 -   return (SDL_bool)nativeCompareThenSet16(ptr, oldvalue, newvalue);
   4.522 -#else
   4.523 -   SDL_bool result = SDL_FALSE;
   4.524 -
   4.525 -   privateWaitLock();
   4.526 -   result = (*ptr == oldvalue);
   4.527 -   if (result)
   4.528 -   {
   4.529 -      *ptr = newvalue;
   4.530 -   }
   4.531 -   privateUnlock();
   4.532 -
   4.533 -   return result;
   4.534 -#endif
   4.535 -}
   4.536 -
   4.537 -SDL_bool
   4.538 -SDL_AtomicTestThenSet16(volatile Uint16 * ptr)
   4.539 -{
   4.540 -#ifdef nativeTestThenSet16
   4.541 -   return (SDL_bool)nativeTestThenSet16(ptr);
   4.542 -#else
   4.543 -   SDL_bool result = SDL_FALSE;
   4.544 -
   4.545 -   privateWaitLock();
   4.546 -   result = (*ptr == 0);
   4.547 -   if (result)
   4.548 -   {
   4.549 -      *ptr = 1;
   4.550 -   }
   4.551 -   privateUnlock();
   4.552 -
   4.553 -   return result;
   4.554 -#endif
   4.555 -}
   4.556 -
   4.557 -void
   4.558 -SDL_AtomicClear16(volatile Uint16 * ptr)
   4.559 -{
   4.560 -#ifdef nativeClear16
   4.561 -   nativeClear16(ptr);
   4.562 -#else
   4.563 -   privateWaitLock();
   4.564 -   *ptr = 0;
   4.565 -   privateUnlock();
   4.566 -
   4.567 -   return;
   4.568 -#endif
   4.569 -}
   4.570 -
   4.571 -Uint16
   4.572 -SDL_AtomicFetchThenIncrement16(volatile Uint16 * ptr)
   4.573 -{
   4.574 -#ifdef nativeFetchThenIncrement16
   4.575 -   return nativeFetchThenIncrement16(ptr);
   4.576 -#else
   4.577 -   Uint16 tmp = 0;
   4.578 -
   4.579 -   privateWaitLock();
   4.580 -   tmp = *ptr;
   4.581 -   (*ptr)+= 1;
   4.582 -   privateUnlock();
   4.583 -
   4.584 -   return tmp;
   4.585 -#endif
   4.586 -}
   4.587 -
   4.588 -Uint16
   4.589 -SDL_AtomicFetchThenDecrement16(volatile Uint16 * ptr)
   4.590 -{
   4.591 -#ifdef nativeFetchThenDecrement16
   4.592 -   return nativeFetchThenDecrement16(ptr);
   4.593 -#else
   4.594 -   Uint16 tmp = 0;
   4.595 -
   4.596 -   privateWaitLock();
   4.597 -   tmp = *ptr;
   4.598 -   (*ptr) -= 1;
   4.599 -   privateUnlock();
   4.600 -
   4.601 -   return tmp;
   4.602 -#endif
   4.603 -}
   4.604 -
   4.605 -Uint16
   4.606 -SDL_AtomicFetchThenAdd16(volatile Uint16 * ptr, Uint16 value)
   4.607 -{
   4.608 -#ifdef nativeFetchThenAdd16
   4.609 -   return nativeFetchThenAdd16(ptr, value);
   4.610 -#else
   4.611 -   Uint16 tmp = 0;
   4.612 -
   4.613 -   privateWaitLock();
   4.614 -   tmp = *ptr;
   4.615 -   (*ptr)+= value;
   4.616 -   privateUnlock();
   4.617 -
   4.618 -   return tmp;
   4.619 -#endif
   4.620 -}
   4.621 -
   4.622 -Uint16
   4.623 -SDL_AtomicFetchThenSubtract16(volatile Uint16 * ptr, Uint16 value)
   4.624 -{
   4.625 -#ifdef nativeFetchThenSubtract16
   4.626 -   return nativeFetchThenSubtract16(ptr, value);
   4.627 -#else
   4.628 -   Uint16 tmp = 0;
   4.629 -
   4.630 -   privateWaitLock();
   4.631 -   tmp = *ptr;
   4.632 -   (*ptr)-= value;
   4.633 -   privateUnlock();
   4.634 -
   4.635 -   return tmp;
   4.636 -#endif
   4.637 -}
   4.638 -
   4.639 -Uint16
   4.640 -SDL_AtomicIncrementThenFetch16(volatile Uint16 * ptr)
   4.641 -{
   4.642 -#ifdef nativeIncrementThenFetch16
   4.643 -   return nativeIncrementThenFetch16(ptr);
   4.644 -#else
   4.645 -   Uint16 tmp = 0;
   4.646 -
   4.647 -   privateWaitLock();
   4.648 -   (*ptr)+= 1;
   4.649 -   tmp = *ptr;
   4.650 -   privateUnlock();
   4.651 -
   4.652 -   return tmp;
   4.653 -#endif
   4.654 -}
   4.655 -
   4.656 -Uint16
   4.657 -SDL_AtomicDecrementThenFetch16(volatile Uint16 * ptr)
   4.658 -{
   4.659 -#ifdef nativeDecrementThenFetch16
   4.660 -   return nativeDecrementThenFetch16(ptr);
   4.661 -#else
   4.662 -   Uint16 tmp = 0;
   4.663 -
   4.664 -   privateWaitLock();
   4.665 -   (*ptr)-= 1;
   4.666 -   tmp = *ptr;
   4.667 -   privateUnlock();
   4.668 -
   4.669 -   return tmp;
   4.670 -#endif
   4.671 -}
   4.672 -
   4.673 -Uint16
   4.674 -SDL_AtomicAddThenFetch16(volatile Uint16 * ptr, Uint16 value)
   4.675 -{
   4.676 -#ifdef nativeAddThenFetch16
   4.677 -   return nativeAddThenFetch16(ptr, value);
   4.678 -#else
   4.679 -   Uint16 tmp = 0;
   4.680 -
   4.681 -   privateWaitLock();
   4.682 -   (*ptr)+= value;
   4.683 -   tmp = *ptr;
   4.684 -   privateUnlock();
   4.685 -
   4.686 -   return tmp;
   4.687 -#endif
   4.688 -}
   4.689 -
   4.690 -Uint16
   4.691 -SDL_AtomicSubtractThenFetch16(volatile Uint16 * ptr, Uint16 value)
   4.692 -{
   4.693 -#ifdef nativeSubtractThenFetch16
   4.694 -   return nativeSubtractThenFetch16(ptr, value);
   4.695 -#else
   4.696 -   Uint16 tmp = 0;
   4.697 -
   4.698 -   privateWaitLock();
   4.699 -   (*ptr)-= value;
   4.700 -   tmp = *ptr;
   4.701 -   privateUnlock();
   4.702 -
   4.703 -   return tmp;
   4.704 -#endif
   4.705 -}
   4.706 -
   4.707 -/* 32 bit atomic operations */
   4.708 -
   4.709 -Uint32
   4.710 -SDL_AtomicExchange32(volatile Uint32 * ptr, Uint32 value)
   4.711 -{
   4.712 -#ifdef nativeExchange32
   4.713 -   return nativeExchange32(ptr, value);
   4.714 -#else
   4.715 -   Uint32 tmp = 0;
   4.716 -
   4.717 -   privateWaitLock();
   4.718 -   tmp = *ptr;
   4.719 -   *ptr = value;
   4.720 -   privateUnlock();
   4.721 -
   4.722 -   return tmp;
   4.723 -#endif
   4.724 -}
   4.725 -
   4.726 -SDL_bool
   4.727 -SDL_AtomicCompareThenSet32(volatile Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
   4.728 -{
   4.729 -#ifdef nativeCompareThenSet32
   4.730 -   return (SDL_bool)nativeCompareThenSet32(ptr, oldvalue, newvalue);
   4.731 -#else
   4.732 -   SDL_bool result = SDL_FALSE;
   4.733 -
   4.734 -   privateWaitLock();
   4.735 -   result = (*ptr == oldvalue);
   4.736 -   if (result)
   4.737 -   {
   4.738 -      *ptr = newvalue;
   4.739 -   }
   4.740 -   privateUnlock();
   4.741 -
   4.742 -   return result;
   4.743 -#endif
   4.744 -}
   4.745 -
   4.746 -SDL_bool
   4.747 -SDL_AtomicTestThenSet32(volatile Uint32 * ptr)
   4.748 -{
   4.749 -#ifdef nativeTestThenSet32
   4.750 -   return (SDL_bool)nativeTestThenSet32(ptr);
   4.751 -#else
   4.752 -   SDL_bool result = SDL_FALSE;
   4.753 -
   4.754 -   privateWaitLock();
   4.755 -   result = (*ptr == 0);
   4.756 -   if (result)
   4.757 -   {
   4.758 -      *ptr = 1;
   4.759 -   }
   4.760 -   privateUnlock();
   4.761 +   privateUnlock(ptr);
   4.762  
   4.763     return result;
   4.764  #endif
   4.765 @@ -643,11 +166,10 @@
   4.766  SDL_AtomicClear32(volatile Uint32 * ptr)
   4.767  {
   4.768  #ifdef nativeClear32
   4.769 -   nativeClear32(ptr);
   4.770  #else
   4.771 -   privateWaitLock();
   4.772 +   privateWaitLock(ptr);
   4.773     *ptr = 0;
   4.774 -   privateUnlock();
   4.775 +   privateUnlock(ptr);
   4.776  
   4.777     return;
   4.778  #endif
   4.779 @@ -657,14 +179,13 @@
   4.780  SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr)
   4.781  {
   4.782  #ifdef nativeFetchThenIncrement32
   4.783 -   return nativeFetchThenIncrement32(ptr);
   4.784  #else
   4.785     Uint32 tmp = 0;
   4.786  
   4.787 -   privateWaitLock();
   4.788 +   privateWaitLock(ptr);
   4.789     tmp = *ptr;
   4.790     (*ptr)+= 1;
   4.791 -   privateUnlock();
   4.792 +   privateUnlock(ptr);
   4.793  
   4.794     return tmp;
   4.795  #endif
   4.796 @@ -674,14 +195,13 @@
   4.797  SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr)
   4.798  {
   4.799  #ifdef nativeFetchThenDecrement32
   4.800 -   return nativeFetchThenDecrement32(ptr);
   4.801  #else
   4.802     Uint32 tmp = 0;
   4.803  
   4.804 -   privateWaitLock();
   4.805 +   privateWaitLock(ptr);
   4.806     tmp = *ptr;
   4.807     (*ptr) -= 1;
   4.808 -   privateUnlock();
   4.809 +   privateUnlock(ptr);
   4.810  
   4.811     return tmp;
   4.812  #endif
   4.813 @@ -691,14 +211,13 @@
   4.814  SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value)
   4.815  {
   4.816  #ifdef nativeFetchThenAdd32
   4.817 -   return nativeFetchThenAdd32(ptr, value);
   4.818  #else
   4.819     Uint32 tmp = 0;
   4.820  
   4.821 -   privateWaitLock();
   4.822 +   privateWaitLock(ptr);
   4.823     tmp = *ptr;
   4.824     (*ptr)+= value;
   4.825 -   privateUnlock();
   4.826 +   privateUnlock(ptr);
   4.827  
   4.828     return tmp;
   4.829  #endif
   4.830 @@ -708,14 +227,13 @@
   4.831  SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value)
   4.832  {
   4.833  #ifdef nativeFetchThenSubtract32
   4.834 -   return nativeFetchThenSubtract32(ptr, value);
   4.835  #else
   4.836     Uint32 tmp = 0;
   4.837  
   4.838 -   privateWaitLock();
   4.839 +   privateWaitLock(ptr);
   4.840     tmp = *ptr;
   4.841     (*ptr)-= value;
   4.842 -   privateUnlock();
   4.843 +   privateUnlock(ptr);
   4.844  
   4.845     return tmp;
   4.846  #endif
   4.847 @@ -725,14 +243,13 @@
   4.848  SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr)
   4.849  {
   4.850  #ifdef nativeIncrementThenFetch32
   4.851 -   return nativeIncrementThenFetch32(ptr);
   4.852  #else
   4.853     Uint32 tmp = 0;
   4.854  
   4.855 -   privateWaitLock();
   4.856 +   privateWaitLock(ptr);
   4.857     (*ptr)+= 1;
   4.858     tmp = *ptr;
   4.859 -   privateUnlock();
   4.860 +   privateUnlock(ptr);
   4.861  
   4.862     return tmp;
   4.863  #endif
   4.864 @@ -742,14 +259,13 @@
   4.865  SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr)
   4.866  {
   4.867  #ifdef nativeDecrementThenFetch32
   4.868 -   return nativeDecrementThenFetch32(ptr);
   4.869  #else
   4.870     Uint32 tmp = 0;
   4.871  
   4.872 -   privateWaitLock();
   4.873 +   privateWaitLock(ptr);
   4.874     (*ptr)-= 1;
   4.875     tmp = *ptr;
   4.876 -   privateUnlock();
   4.877 +   privateUnlock(ptr);
   4.878  
   4.879     return tmp;
   4.880  #endif
   4.881 @@ -759,14 +275,13 @@
   4.882  SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value)
   4.883  {
   4.884  #ifdef nativeAddThenFetch32
   4.885 -   return nativeAddThenFetch32(ptr, value);
   4.886  #else
   4.887     Uint32 tmp = 0;
   4.888  
   4.889 -   privateWaitLock();
   4.890 +   privateWaitLock(ptr);
   4.891     (*ptr)+= value;
   4.892     tmp = *ptr;
   4.893 -   privateUnlock();
   4.894 +   privateUnlock(ptr);
   4.895  
   4.896     return tmp;
   4.897  #endif
   4.898 @@ -776,14 +291,13 @@
   4.899  SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value)
   4.900  {
   4.901  #ifdef nativeSubtractThenFetch32
   4.902 -   return nativeSubtractThenFetch32(ptr, value);
   4.903  #else
   4.904     Uint32 tmp = 0;
   4.905  
   4.906 -   privateWaitLock();
   4.907 +   privateWaitLock(ptr);
   4.908     (*ptr)-= value;
   4.909     tmp = *ptr;
   4.910 -   privateUnlock();
   4.911 +   privateUnlock(ptr);
   4.912  
   4.913     return tmp;
   4.914  #endif
   4.915 @@ -792,58 +306,20 @@
   4.916  /* 64 bit atomic operations */
   4.917  #ifdef SDL_HAS_64BIT_TYPE
   4.918  
   4.919 -Uint64
   4.920 -SDL_AtomicExchange64(volatile Uint64 * ptr, Uint64 value)
   4.921 -{
   4.922 -#ifdef nativeExchange64
   4.923 -   return nativeExchange64(ptr, value);
   4.924 -#else
   4.925 -   Uint64 tmp = 0;
   4.926 -
   4.927 -   privateWaitLock();
   4.928 -   tmp = *ptr;
   4.929 -   *ptr = value;
   4.930 -   privateUnlock();
   4.931 -
   4.932 -   return tmp;
   4.933 -#endif
   4.934 -}
   4.935 -
   4.936 -SDL_bool
   4.937 -SDL_AtomicCompareThenSet64(volatile Uint64 * ptr, Uint64 oldvalue, Uint64 newvalue)
   4.938 -{
   4.939 -#ifdef nativeCompareThenSet64
   4.940 -   return (SDL_bool)nativeCompareThenSet64(ptr, oldvalue, newvalue);
   4.941 -#else
   4.942 -   SDL_bool result = SDL_FALSE;
   4.943 -
   4.944 -   privateWaitLock();
   4.945 -   result = (*ptr == oldvalue);
   4.946 -   if (result)
   4.947 -   {
   4.948 -      *ptr = newvalue;
   4.949 -   }
   4.950 -   privateUnlock();
   4.951 -
   4.952 -   return result;
   4.953 -#endif
   4.954 -}
   4.955 -
   4.956  SDL_bool
   4.957  SDL_AtomicTestThenSet64(volatile Uint64 * ptr)
   4.958  {
   4.959  #ifdef nativeTestThenSet64
   4.960 -   return (SDL_bool)nativeTestThenSet64(ptr);
   4.961  #else
   4.962     SDL_bool result = SDL_FALSE;
   4.963  
   4.964 -   privateWaitLock();
   4.965 +   privateWaitLock(ptr);
   4.966     result = (*ptr == 0);
   4.967     if (result)
   4.968     {
   4.969        *ptr = 1;
   4.970     }
   4.971 -   privateUnlock();
   4.972 +   privateUnlock(ptr);
   4.973  
   4.974     return result;
   4.975  #endif
   4.976 @@ -853,11 +329,10 @@
   4.977  SDL_AtomicClear64(volatile Uint64 * ptr)
   4.978  {
   4.979  #ifdef nativeClear64
   4.980 -   nativeClear64(ptr);
   4.981  #else
   4.982 -   privateWaitLock();
   4.983 +   privateWaitLock(ptr);
   4.984     *ptr = 0;
   4.985 -   privateUnlock();
   4.986 +   privateUnlock(ptr);
   4.987  
   4.988     return;
   4.989  #endif
   4.990 @@ -867,14 +342,13 @@
   4.991  SDL_AtomicFetchThenIncrement64(volatile Uint64 * ptr)
   4.992  {
   4.993  #ifdef nativeFetchThenIncrement64
   4.994 -   return nativeFetchThenIncrement64(ptr);
   4.995  #else
   4.996     Uint64 tmp = 0;
   4.997  
   4.998 -   privateWaitLock();
   4.999 +   privateWaitLock(ptr);
  4.1000     tmp = *ptr;
  4.1001     (*ptr)+= 1;
  4.1002 -   privateUnlock();
  4.1003 +   privateUnlock(ptr);
  4.1004  
  4.1005     return tmp;
  4.1006  #endif
  4.1007 @@ -884,14 +358,13 @@
  4.1008  SDL_AtomicFetchThenDecrement64(volatile Uint64 * ptr)
  4.1009  {
  4.1010  #ifdef nativeFetchThenDecrement64
  4.1011 -   return nativeFetchThenDecrement64(ptr);
  4.1012  #else
  4.1013     Uint64 tmp = 0;
  4.1014  
  4.1015 -   privateWaitLock();
  4.1016 +   privateWaitLock(ptr);
  4.1017     tmp = *ptr;
  4.1018     (*ptr) -= 1;
  4.1019 -   privateUnlock();
  4.1020 +   privateUnlock(ptr);
  4.1021  
  4.1022     return tmp;
  4.1023  #endif
  4.1024 @@ -901,14 +374,13 @@
  4.1025  SDL_AtomicFetchThenAdd64(volatile Uint64 * ptr, Uint64 value)
  4.1026  {
  4.1027  #ifdef nativeFetchThenAdd64
  4.1028 -   return nativeFetchThenAdd64(ptr, value);
  4.1029  #else
  4.1030     Uint64 tmp = 0;
  4.1031  
  4.1032 -   privateWaitLock();
  4.1033 +   privateWaitLock(ptr);
  4.1034     tmp = *ptr;
  4.1035     (*ptr)+= value;
  4.1036 -   privateUnlock();
  4.1037 +   privateUnlock(ptr);
  4.1038  
  4.1039     return tmp;
  4.1040  #endif
  4.1041 @@ -918,14 +390,13 @@
  4.1042  SDL_AtomicFetchThenSubtract64(volatile Uint64 * ptr, Uint64 value)
  4.1043  {
  4.1044  #ifdef nativeFetchThenSubtract64
  4.1045 -   return nativeFetchThenSubtract64(ptr, value);
  4.1046  #else
  4.1047     Uint64 tmp = 0;
  4.1048  
  4.1049 -   privateWaitLock();
  4.1050 +   privateWaitLock(ptr);
  4.1051     tmp = *ptr;
  4.1052     (*ptr)-= value;
  4.1053 -   privateUnlock();
  4.1054 +   privateUnlock(ptr);
  4.1055  
  4.1056     return tmp;
  4.1057  #endif
  4.1058 @@ -935,14 +406,13 @@
  4.1059  SDL_AtomicIncrementThenFetch64(volatile Uint64 * ptr)
  4.1060  {
  4.1061  #ifdef nativeIncrementThenFetch64
  4.1062 -   return nativeIncrementThenFetch64(ptr);
  4.1063  #else
  4.1064     Uint64 tmp = 0;
  4.1065  
  4.1066 -   privateWaitLock();
  4.1067 +   privateWaitLock(ptr);
  4.1068     (*ptr)+= 1;
  4.1069     tmp = *ptr;
  4.1070 -   privateUnlock();
  4.1071 +   privateUnlock(ptr);
  4.1072  
  4.1073     return tmp;
  4.1074  #endif
  4.1075 @@ -952,14 +422,13 @@
  4.1076  SDL_AtomicDecrementThenFetch64(volatile Uint64 * ptr)
  4.1077  {
  4.1078  #ifdef nativeDecrementThenFetch64
  4.1079 -   return nativeDecrementThenFetch64(ptr);
  4.1080  #else
  4.1081     Uint64 tmp = 0;
  4.1082  
  4.1083 -   privateWaitLock();
  4.1084 +   privateWaitLock(ptr);
  4.1085     (*ptr)-= 1;
  4.1086     tmp = *ptr;
  4.1087 -   privateUnlock();
  4.1088 +   privateUnlock(ptr);
  4.1089  
  4.1090     return tmp;
  4.1091  #endif
  4.1092 @@ -969,14 +438,13 @@
  4.1093  SDL_AtomicAddThenFetch64(volatile Uint64 * ptr, Uint64 value)
  4.1094  {
  4.1095  #ifdef nativeAddThenFetch64
  4.1096 -   return nativeAddThenFetch64(ptr, value);
  4.1097  #else
  4.1098     Uint64 tmp = 0;
  4.1099  
  4.1100 -   privateWaitLock();
  4.1101 +   privateWaitLock(ptr);
  4.1102     (*ptr)+= value;
  4.1103     tmp = *ptr;
  4.1104 -   privateUnlock();
  4.1105 +   privateUnlock(ptr);
  4.1106  
  4.1107     return tmp;
  4.1108  #endif
  4.1109 @@ -986,14 +454,13 @@
  4.1110  SDL_AtomicSubtractThenFetch64(volatile Uint64 * ptr, Uint64 value)
  4.1111  {
  4.1112  #ifdef nativeSubtractThenFetch64
  4.1113 -   return nativeSubtractThenFetch64(ptr, value);
  4.1114  #else
  4.1115     Uint64 tmp = 0;
  4.1116  
  4.1117 -   privateWaitLock();
  4.1118 +   privateWaitLock(ptr);
  4.1119     (*ptr)-= value;
  4.1120     tmp = *ptr;
  4.1121 -   privateUnlock();
  4.1122 +   privateUnlock(ptr);
  4.1123  
  4.1124     return tmp;
  4.1125  #endif
     5.1 --- a/src/atomic/win32/SDL_atomic.c	Mon Sep 07 16:04:44 2009 +0000
     5.2 +++ b/src/atomic/win32/SDL_atomic.c	Thu Sep 17 20:35:12 2009 +0000
     5.3 @@ -1,789 +1,303 @@
     5.4  /*
     5.5 -    SDL - Simple DirectMedia Layer
     5.6 -    Copyright (C) 1997-2009 Sam Lantinga
     5.7 +  SDL - Simple DirectMedia Layer
     5.8 +  Copyright (C) 1997-2009 Sam Lantinga
     5.9  
    5.10 -    This library is free software; you can redistribute it and/or
    5.11 -    modify it under the terms of the GNU Lesser General Public
    5.12 -    License as published by the Free Software Foundation; either
    5.13 -    version 2.1 of the License, or (at your option) any later version.
    5.14 +  This library is free software; you can redistribute it and/or
    5.15 +  modify it under the terms of the GNU Lesser General Public
    5.16 +  License as published by the Free Software Foundation; either
    5.17 +  version 2.1 of the License, or (at your option) any later version.
    5.18  
    5.19 -    This library is distributed in the hope that it will be useful,
    5.20 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.21 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    5.22 -    Lesser General Public License for more details.
    5.23 +  This library is distributed in the hope that it will be useful,
    5.24 +  but WITHOUT ANY WARRANTY; without even the implied warranty of
    5.25 +  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    5.26 +  Lesser General Public License for more details.
    5.27  
    5.28 -    You should have received a copy of the GNU Lesser General Public
    5.29 -    License along with this library; if not, write to the Free Software
    5.30 -    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    5.31 +  You should have received a copy of the GNU Lesser General Public
    5.32 +  License along with this library; if not, write to the Free Software
    5.33 +  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    5.34  
    5.35 -    Sam Lantinga
    5.36 -    slouken@libsdl.org
    5.37 +  Sam Lantinga
    5.38 +  slouken@libsdl.org
    5.39 +
    5.40 +  Contributed by Bob Pendleton, bob@pendleton.com
    5.41  */
    5.42  
    5.43  #include "SDL_stdinc.h"
    5.44  #include "SDL_atomic.h"
    5.45  
    5.46 +#include "SDL_error.h"
    5.47 +
    5.48  /*
    5.49 -  This file provides 8, 16, 32, and 64 bit atomic operations. If the
    5.50 +  This file provides 32, and 64 bit atomic operations. If the
    5.51    operations are provided by the native hardware and operating system
    5.52    they are used. If they are not then the operations are emulated
    5.53 -  using the SDL mutex operations. 
    5.54 - */
    5.55 -
    5.56 -/* 
    5.57 -  First, detect whether the operations are supported and create
    5.58 -  #defines that indicate that they do exist. The goal is to have all
    5.59 -  the system dependent code in the top part of the file so that the
    5.60 -  bottom can be use unchanged across all platforms.
    5.61 -
    5.62 -  Second, #define all the operations in each size class that are
    5.63 -  supported. Doing this allows supported operations to be used along
    5.64 -  side of emulated operations.
    5.65 +  using the SDL spin lock operations. If spin lock can not be
    5.66 +  implemented then these functions must fail.
    5.67  */
    5.68  
    5.69  /* 
    5.70 -   Emmulated version.
    5.71 +  DUMMY VERSION.
    5.72  
    5.73 -   Assume there is no support for atomic operations. All such
    5.74 -   operations are implemented using SDL mutex operations.
    5.75 - */
    5.76 +  This version of the code assumes there is no support for atomic
    5.77 +  operations. Therefore, every function sets the SDL error
    5.78 +  message. Oddly enough, if you only have one thread then this
    5.79 +  version actuallys works.
    5.80 +*/
    5.81  
    5.82 -#ifdef EMULATED_ATOMIC_OPERATIONS
    5.83 -#undef EMULATED_ATOMIC_OPERATIONS
    5.84 +/*
    5.85 +  Native spinlock routines. Because this is the dummy implementation
    5.86 +  these will always call SDL_SetError() and do nothing.
    5.87 +*/
    5.88 +
    5.89 +void 
    5.90 +SDL_AtomicLock(SDL_SpinLock *lock)
    5.91 +{
    5.92 +   SDL_SetError("SDL_atomic.c: is not implemented on this platform");
    5.93 +}
    5.94 +
    5.95 +void 
    5.96 +SDL_AtomicUnlock(SDL_SpinLock *lock)
    5.97 +{
    5.98 +   SDL_SetError("SDL_atomic.c: is not implemented on this platform");
    5.99 +}
   5.100 +
   5.101 +/*
   5.102 +  Note that platform specific versions can be built from this version
   5.103 +  by changing the #undefs to #defines and adding platform specific
   5.104 +  code.
   5.105 +*/
   5.106 +
   5.107 +#undef  nativeTestThenSet32
   5.108 +#undef  nativeClear32
   5.109 +#undef  nativeFetchThenIncrement32
   5.110 +#undef  nativeFetchThenDecrement32
   5.111 +#undef  nativeFetchThenAdd32
   5.112 +#undef  nativeFetchThenSubtract32
   5.113 +#undef  nativeIncrementThenFetch32
   5.114 +#undef  nativeDecrementThenFetch32
   5.115 +#undef  nativeAddThenFetch32
   5.116 +#undef  nativeSubtractThenFetch32
   5.117 +
   5.118 +#undef  nativeTestThenSet64
   5.119 +#undef  nativeClear64
   5.120 +#undef  nativeFetchThenIncrement64
   5.121 +#undef  nativeFetchThenDecrement64
   5.122 +#undef  nativeFetchThenAdd64
   5.123 +#undef  nativeFetchThenSubtract64
   5.124 +#undef  nativeIncrementThenFetch64
   5.125 +#undef  nativeDecrementThenFetch64
   5.126 +#undef  nativeAddThenFetch64
   5.127 +#undef  nativeSubtractThenFetch64
   5.128 +
   5.129 +/* 
   5.130 +  If any of the operations are not provided then we must emulate some
   5.131 +  of them. That means we need a nice implementation of spin locks
   5.132 +  that avoids the "one big lock" problem. We use a vector of spin
   5.133 +  locks and pick which one to use based on the address of the operand
   5.134 +  of the function.
   5.135 +
   5.136 +  To generate the index of the lock we first shift by 3 bits to get
   5.137 +  rid on the zero bits that result from 32 and 64 bit allignment of
   5.138 +  data. We then mask off all but 5 bits and use those 5 bits as an
   5.139 +  index into the table. 
   5.140 +
   5.141 +  Picking the lock this way insures that accesses to the same data at
   5.142 +  the same time will go to the same lock. OTOH, accesses to different
   5.143 +  data have only a 1/32 chance of hitting the same lock. That should
   5.144 +  pretty much eliminate the chances of several atomic operations on
   5.145 +  different data from waiting on the same "big lock". If it isn't
   5.146 +  then the table of locks can be expanded to a new size so long as
   5.147 +  the new size if a power of two.
   5.148 +*/
   5.149 +
   5.150 +static SDL_SpinLock locks[32] = {
   5.151 +   0, 0, 0, 0, 0, 0, 0, 0,
   5.152 +   0, 0, 0, 0, 0, 0, 0, 0,
   5.153 +   0, 0, 0, 0, 0, 0, 0, 0,
   5.154 +   0, 0, 0, 0, 0, 0, 0, 0,
   5.155 +};
   5.156 +
   5.157 +static __inline__ void
   5.158 +privateWaitLock(volatile void *ptr)
   5.159 +{
   5.160 +#if SIZEOF_VOIDP == 4
   5.161 +   Uint32 index = ((((Uint32)ptr) >> 3) & 0x1f);
   5.162 +#elif SIZEOF_VOIDP == 8
   5.163 +   Uint64 index = ((((Uint64)ptr) >> 3) & 0x1f);
   5.164  #endif
   5.165  
   5.166 -#ifdef EMULATED_ATOMIC_OPERATIONS
   5.167 -#define HAVE_ALL_8_BIT_OPS
   5.168 -
   5.169 -#define nativeExchange8(ptr, value)			()
   5.170 -#define nativeCompareThenSet8(ptr, oldvalue, newvalue) 	()
   5.171 -#define nativeTestThenSet8(ptr)    	     		()
   5.172 -#define nativeClear8(ptr)				()
   5.173 -#define nativeFetchThenIncrement8(ptr)   		()
   5.174 -#define nativeFetchThenDecrement8(ptr) 			()
   5.175 -#define nativeFetchThenAdd8(ptr, value) 		()
   5.176 -#define nativeFetchThenSubtract8(ptr, value) 		()
   5.177 -#define nativeIncrementThenFetch8(ptr) 			()
   5.178 -#define nativeDecrementThenFetch8(ptr) 			()
   5.179 -#define nativeAddThenFetch8(ptr, value) 		()
   5.180 -#define nativeSubtractThenFetch8(ptr, value) 		()
   5.181 -#endif
   5.182 -
   5.183 -#ifdef EMULATED_ATOMIC_OPERATIONS
   5.184 -#define HAVE_ALL_16_BIT_OPS
   5.185 -
   5.186 -#define nativeExchange16(ptr, value)			()
   5.187 -#define nativeCompareThenSet16(ptr, oldvalue, newvalue) ()
   5.188 -#define nativeTestThenSet16(ptr)    	     		()
   5.189 -#define nativeClear16(ptr)				()
   5.190 -#define nativeFetchThenIncrement16(ptr)   		()
   5.191 -#define nativeFetchThenDecrement16(ptr) 		()
   5.192 -#define nativeFetchThenAdd16(ptr, value) 		()
   5.193 -#define nativeFetchThenSubtract16(ptr, value) 		()
   5.194 -#define nativeIncrementThenFetch16(ptr) 		()
   5.195 -#define nativeDecrementThenFetch16(ptr) 		()
   5.196 -#define nativeAddThenFetch16(ptr, value) 		()
   5.197 -#define nativeSubtractThenFetch16(ptr, value) 		()
   5.198 -#endif
   5.199 -
   5.200 -#ifdef EMULATED_ATOMIC_OPERATIONS
   5.201 -#define HAVE_ALL_32_BIT_OPS
   5.202 -
   5.203 -#define nativeExchange32(ptr, value)			()
   5.204 -#define nativeCompareThenSet32(ptr, oldvalue, newvalue) ()
   5.205 -#define nativeTestThenSet32(ptr)    	     		()
   5.206 -#define nativeClear32(ptr)				()
   5.207 -#define nativeFetchThenIncrement32(ptr)   		()
   5.208 -#define nativeFetchThenDecrement32(ptr) 		()
   5.209 -#define nativeFetchThenAdd32(ptr, value) 		()
   5.210 -#define nativeFetchThenSubtract32(ptr, value) 		()
   5.211 -#define nativeIncrementThenFetch32(ptr) 		()
   5.212 -#define nativeDecrementThenFetch32(ptr) 		()
   5.213 -#define nativeAddThenFetch32(ptr, value) 		()
   5.214 -#define nativeSubtractThenFetch32(ptr, value) 		()
   5.215 -#endif
   5.216 -
   5.217 -#ifdef EMULATED_ATOMIC_OPERATIONS
   5.218 -#define HAVE_ALL_64_BIT_OPS
   5.219 -
   5.220 -#define nativeExchange64(ptr, value)			()
   5.221 -#define nativeCompareThenSet64(ptr, oldvalue, newvalue) ()
   5.222 -#define nativeTestThenSet64(ptr)    	     		()
   5.223 -#define nativeClear64(ptr)				()
   5.224 -#define nativeFetchThenIncrement64(ptr)   		()
   5.225 -#define nativeFetchThenDecrement64(ptr) 		()
   5.226 -#define nativeFetchThenAdd64(ptr, value) 		()
   5.227 -#define nativeFetchThenSubtract64(ptr, value) 		()
   5.228 -#define nativeIncrementThenFetch64(ptr) 		()
   5.229 -#define nativeDecrementThenFetch64(ptr) 		()
   5.230 -#define nativeAddThenFetch64(ptr, value) 		()
   5.231 -#define nativeSubtractThenFetch64(ptr, value) 		()
   5.232 -#endif
   5.233 -
   5.234 -/* 
   5.235 -If any of the operations are not provided then we must emulate some of
   5.236 -them.
   5.237 - */
   5.238 -
   5.239 -#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)
   5.240 -
   5.241 -#include "SDL_mutex.h"
   5.242 -#include "SDL_error.h"
   5.243 -
   5.244 -static SDL_mutex * lock = NULL;
   5.245 -
   5.246 -static __inline__ void
   5.247 -privateWaitLock()
   5.248 -{
   5.249 -   if(NULL == lock)
   5.250 -   {
   5.251 -      lock = SDL_CreateMutex();
   5.252 -      if (NULL == lock)
   5.253 -      {
   5.254 -	 SDL_SetError("SDL_atomic.c: can't create a mutex");
   5.255 -	 return;
   5.256 -      }
   5.257 -   }
   5.258 -
   5.259 -   if (-1 == SDL_LockMutex(lock))
   5.260 -   {
   5.261 -      SDL_SetError("SDL_atomic.c: can't lock mutex");
   5.262 -   }
   5.263 +   SDL_AtomicLock(&locks[index]);
   5.264  }
   5.265  
   5.266  static __inline__ void
   5.267 -privateUnlock()
   5.268 +privateUnlock(volatile void *ptr)
   5.269  {
   5.270 -   if (-1 == SDL_UnlockMutex(lock))
   5.271 -   {
   5.272 -      SDL_SetError("SDL_atomic.c: can't unlock mutex");
   5.273 -   }
   5.274 +#if SIZEOF_VOIDP == 4
   5.275 +   Uint32 index = ((((Uint32)ptr) >> 3) & 0x1f);
   5.276 +#elif SIZEOF_VOIDP == 8
   5.277 +   Uint64 index = ((((Uint64)ptr) >> 3) & 0x1f);
   5.278 +#endif
   5.279 +
   5.280 +   SDL_AtomicUnlock(&locks[index]);
   5.281  }
   5.282  
   5.283 -#endif
   5.284 -
   5.285 -/* 8 bit atomic operations */
   5.286 -
   5.287 -Uint8
   5.288 -SDL_AtomicExchange8(Uint8 * ptr, Uint8 value)
   5.289 -{
   5.290 -#ifdef nativeExchange8
   5.291 -   return nativeExchange8(ptr, value);
   5.292 -#else
   5.293 -   Uint8 tmp = 0;
   5.294 -
   5.295 -   privateWaitLock();
   5.296 -   tmp = *ptr;
   5.297 -   *ptr = value;
   5.298 -   privateUnlock();
   5.299 -
   5.300 -   return tmp;
   5.301 -#endif
   5.302 -}
   5.303 +/* 32 bit atomic operations */
   5.304  
   5.305  SDL_bool
   5.306 -SDL_AtomicCompareThenSet8(Uint8 * ptr, Uint8 oldvalue, Uint8 newvalue)
   5.307 +SDL_AtomicTestThenSet32(volatile Uint32 * ptr)
   5.308  {
   5.309 -#ifdef nativeCompareThenSet8
   5.310 -   return (SDL_bool)nativeCompareThenSet8(ptr, oldvalue, newvalue);
   5.311 +#ifdef nativeTestThenSet32
   5.312  #else
   5.313     SDL_bool result = SDL_FALSE;
   5.314  
   5.315 -   privateWaitLock();
   5.316 -   result = (*ptr == oldvalue);
   5.317 -   if (result)
   5.318 -   {
   5.319 -      *ptr = newvalue;
   5.320 -   }
   5.321 -   privateUnlock();
   5.322 -
   5.323 -   return result;
   5.324 -#endif
   5.325 -}
   5.326 -
   5.327 -SDL_bool
   5.328 -SDL_AtomicTestThenSet8(Uint8 * ptr)
   5.329 -{
   5.330 -#ifdef nativeTestThenSet8
   5.331 -   return (SDL_bool)nativeTestThenSet8(ptr);
   5.332 -#else
   5.333 -   SDL_bool result = SDL_FALSE;
   5.334 -
   5.335 -   privateWaitLock();
   5.336 +   privateWaitLock(ptr);
   5.337     result = (*ptr == 0);
   5.338     if (result)
   5.339     {
   5.340        *ptr = 1;
   5.341     }
   5.342 -   privateUnlock();
   5.343 +   privateUnlock(ptr);
   5.344  
   5.345     return result;
   5.346  #endif
   5.347  }
   5.348  
   5.349  void
   5.350 -SDL_AtomicClear8(Uint8 * ptr)
   5.351 -{
   5.352 -#ifdef nativeClear8
   5.353 -   nativeClear8(ptr);
   5.354 -#else
   5.355 -   privateWaitLock();
   5.356 -   *ptr = 0;
   5.357 -   privateUnlock();
   5.358 -
   5.359 -   return;
   5.360 -#endif
   5.361 -}
   5.362 -
   5.363 -Uint8
   5.364 -SDL_AtomicFetchThenIncrement8(Uint8 * ptr)
   5.365 -{
   5.366 -#ifdef nativeFetchThenIncrement8
   5.367 -   return nativeFetchThenIncrement8(ptr);
   5.368 -#else
   5.369 -   Uint8 tmp = 0;
   5.370 -
   5.371 -   privateWaitLock();
   5.372 -   tmp = *ptr;
   5.373 -   (*ptr)+= 1;
   5.374 -   privateUnlock();
   5.375 -
   5.376 -   return tmp;
   5.377 -#endif
   5.378 -}
   5.379 -
   5.380 -Uint8
   5.381 -SDL_AtomicFetchThenDecrement8(Uint8 * ptr)
   5.382 -{
   5.383 -#ifdef nativeFetchThenDecrement8
   5.384 -   return nativeFetchThenDecrement8(ptr);
   5.385 -#else
   5.386 -   Uint8 tmp = 0;
   5.387 -
   5.388 -   privateWaitLock();
   5.389 -   tmp = *ptr;
   5.390 -   (*ptr) -= 1;
   5.391 -   privateUnlock();
   5.392 -
   5.393 -   return tmp;
   5.394 -#endif
   5.395 -}
   5.396 -
   5.397 -Uint8
   5.398 -SDL_AtomicFetchThenAdd8(Uint8 * ptr, Uint8 value)
   5.399 -{
   5.400 -#ifdef nativeFetchThenAdd8
   5.401 -   return nativeFetchThenAdd8(ptr, value);
   5.402 -#else
   5.403 -   Uint8 tmp = 0;
   5.404 -
   5.405 -   privateWaitLock();
   5.406 -   tmp = *ptr;
   5.407 -   (*ptr)+= value;
   5.408 -   privateUnlock();
   5.409 -
   5.410 -   return tmp;
   5.411 -#endif
   5.412 -}
   5.413 -
   5.414 -Uint8
   5.415 -SDL_AtomicFetchThenSubtract8(Uint8 * ptr, Uint8 value)
   5.416 -{
   5.417 -#ifdef nativeFetchThenSubtract8
   5.418 -   return nativeFetchThenSubtract8(ptr, value);
   5.419 -#else
   5.420 -   Uint8 tmp = 0;
   5.421 -
   5.422 -   privateWaitLock();
   5.423 -   tmp = *ptr;
   5.424 -   (*ptr)-= value;
   5.425 -   privateUnlock();
   5.426 -
   5.427 -   return tmp;
   5.428 -#endif
   5.429 -}
   5.430 -
   5.431 -Uint8
   5.432 -SDL_AtomicIncrementThenFetch8(Uint8 * ptr)
   5.433 -{
   5.434 -#ifdef nativeIncrementThenFetch8
   5.435 -   return nativeIncrementThenFetch8(ptr);
   5.436 -#else
   5.437 -   Uint8 tmp = 0;
   5.438 -
   5.439 -   privateWaitLock();
   5.440 -   (*ptr)+= 1;
   5.441 -   tmp = *ptr;
   5.442 -   privateUnlock();
   5.443 -
   5.444 -   return tmp;
   5.445 -#endif
   5.446 -}
   5.447 -
   5.448 -Uint8
   5.449 -SDL_AtomicDecrementThenFetch8(Uint8 * ptr)
   5.450 -{
   5.451 -#ifdef nativeDecrementThenFetch8
   5.452 -   return nativeDecrementThenFetch8(ptr);
   5.453 -#else
   5.454 -   Uint8 tmp = 0;
   5.455 -
   5.456 -   privateWaitLock();
   5.457 -   (*ptr)-= 1;
   5.458 -   tmp = *ptr;
   5.459 -   privateUnlock();
   5.460 -
   5.461 -   return tmp;
   5.462 -#endif
   5.463 -}
   5.464 -
   5.465 -Uint8
   5.466 -SDL_AtomicAddThenFetch8(Uint8 * ptr, Uint8 value)
   5.467 -{
   5.468 -#ifdef nativeAddThenFetch8
   5.469 -   return nativeAddThenFetch8(ptr, value);
   5.470 -#else
   5.471 -   Uint8 tmp = 0;
   5.472 -
   5.473 -   privateWaitLock();
   5.474 -   (*ptr)+= value;
   5.475 -   tmp = *ptr;
   5.476 -   privateUnlock();
   5.477 -
   5.478 -   return tmp;
   5.479 -#endif
   5.480 -}
   5.481 -
   5.482 -Uint8
   5.483 -SDL_AtomicSubtractThenFetch8(Uint8 * ptr, Uint8 value)
   5.484 -{
   5.485 -#ifdef nativeSubtractThenFetch8
   5.486 -   return nativeSubtractThenFetch8(ptr, value);
   5.487 -#else
   5.488 -   Uint8 tmp = 0;
   5.489 -
   5.490 -   privateWaitLock();
   5.491 -   (*ptr)-= value;
   5.492 -   tmp = *ptr;
   5.493 -   privateUnlock();
   5.494 -
   5.495 -   return tmp;
   5.496 -#endif
   5.497 -}
   5.498 -
   5.499 -/* 16 bit atomic operations */
   5.500 -
   5.501 -Uint16
   5.502 -SDL_AtomicExchange16(Uint16 * ptr, Uint16 value)
   5.503 -{
   5.504 -#ifdef nativeExchange16
   5.505 -   return nativeExchange16(ptr, value);
   5.506 -#else
   5.507 -   Uint16 tmp = 0;
   5.508 -
   5.509 -   privateWaitLock();
   5.510 -   tmp = *ptr;
   5.511 -   *ptr = value;
   5.512 -   privateUnlock();
   5.513 -
   5.514 -   return tmp;
   5.515 -#endif
   5.516 -}
   5.517 -
   5.518 -SDL_bool
   5.519 -SDL_AtomicCompareThenSet16(Uint16 * ptr, Uint16 oldvalue, Uint16 newvalue)
   5.520 -{
   5.521 -#ifdef nativeCompareThenSet16
   5.522 -   return (SDL_bool)nativeCompareThenSet16(ptr, oldvalue, newvalue);
   5.523 -#else
   5.524 -   SDL_bool result = SDL_FALSE;
   5.525 -
   5.526 -   privateWaitLock();
   5.527 -   result = (*ptr == oldvalue);
   5.528 -   if (result)
   5.529 -   {
   5.530 -      *ptr = newvalue;
   5.531 -   }
   5.532 -   privateUnlock();
   5.533 -
   5.534 -   return result;
   5.535 -#endif
   5.536 -}
   5.537 -
   5.538 -SDL_bool
   5.539 -SDL_AtomicTestThenSet16(Uint16 * ptr)
   5.540 -{
   5.541 -#ifdef nativeTestThenSet16
   5.542 -   return (SDL_bool)nativeTestThenSet16(ptr);
   5.543 -#else
   5.544 -   SDL_bool result = SDL_FALSE;
   5.545 -
   5.546 -   privateWaitLock();
   5.547 -   result = (*ptr == 0);
   5.548 -   if (result)
   5.549 -   {
   5.550 -      *ptr = 1;
   5.551 -   }
   5.552 -   privateUnlock();
   5.553 -
   5.554 -   return result;
   5.555 -#endif
   5.556 -}
   5.557 -
   5.558 -void
   5.559 -SDL_AtomicClear16(Uint16 * ptr)
   5.560 -{
   5.561 -#ifdef nativeClear16
   5.562 -   nativeClear16(ptr);
   5.563 -#else
   5.564 -   privateWaitLock();
   5.565 -   *ptr = 0;
   5.566 -   privateUnlock();
   5.567 -
   5.568 -   return;
   5.569 -#endif
   5.570 -}
   5.571 -
   5.572 -Uint16
   5.573 -SDL_AtomicFetchThenIncrement16(Uint16 * ptr)
   5.574 -{
   5.575 -#ifdef nativeFetchThenIncrement16
   5.576 -   return nativeFetchThenIncrement16(ptr);
   5.577 -#else
   5.578 -   Uint16 tmp = 0;
   5.579 -
   5.580 -   privateWaitLock();
   5.581 -   tmp = *ptr;
   5.582 -   (*ptr)+= 1;
   5.583 -   privateUnlock();
   5.584 -
   5.585 -   return tmp;
   5.586 -#endif
   5.587 -}
   5.588 -
   5.589 -Uint16
   5.590 -SDL_AtomicFetchThenDecrement16(Uint16 * ptr)
   5.591 -{
   5.592 -#ifdef nativeFetchThenDecrement16
   5.593 -   return nativeFetchThenDecrement16(ptr);
   5.594 -#else
   5.595 -   Uint16 tmp = 0;
   5.596 -
   5.597 -   privateWaitLock();
   5.598 -   tmp = *ptr;
   5.599 -   (*ptr) -= 1;
   5.600 -   privateUnlock();
   5.601 -
   5.602 -   return tmp;
   5.603 -#endif
   5.604 -}
   5.605 -
   5.606 -Uint16
   5.607 -SDL_AtomicFetchThenAdd16(Uint16 * ptr, Uint16 value)
   5.608 -{
   5.609 -#ifdef nativeFetchThenAdd16
   5.610 -   return nativeFetchThenAdd16(ptr, value);
   5.611 -#else
   5.612 -   Uint16 tmp = 0;
   5.613 -
   5.614 -   privateWaitLock();
   5.615 -   tmp = *ptr;
   5.616 -   (*ptr)+= value;
   5.617 -   privateUnlock();
   5.618 -
   5.619 -   return tmp;
   5.620 -#endif
   5.621 -}
   5.622 -
   5.623 -Uint16
   5.624 -SDL_AtomicFetchThenSubtract16(Uint16 * ptr, Uint16 value)
   5.625 -{
   5.626 -#ifdef nativeFetchThenSubtract16
   5.627 -   return nativeFetchThenSubtract16(ptr, value);
   5.628 -#else
   5.629 -   Uint16 tmp = 0;
   5.630 -
   5.631 -   privateWaitLock();
   5.632 -   tmp = *ptr;
   5.633 -   (*ptr)-= value;
   5.634 -   privateUnlock();
   5.635 -
   5.636 -   return tmp;
   5.637 -#endif
   5.638 -}
   5.639 -
   5.640 -Uint16
   5.641 -SDL_AtomicIncrementThenFetch16(Uint16 * ptr)
   5.642 -{
   5.643 -#ifdef nativeIncrementThenFetch16
   5.644 -   return nativeIncrementThenFetch16(ptr);
   5.645 -#else
   5.646 -   Uint16 tmp = 0;
   5.647 -
   5.648 -   privateWaitLock();
   5.649 -   (*ptr)+= 1;
   5.650 -   tmp = *ptr;
   5.651 -   privateUnlock();
   5.652 -
   5.653 -   return tmp;
   5.654 -#endif
   5.655 -}
   5.656 -
   5.657 -Uint16
   5.658 -SDL_AtomicDecrementThenFetch16(Uint16 * ptr)
   5.659 -{
   5.660 -#ifdef nativeDecrementThenFetch16
   5.661 -   return nativeDecrementThenFetch16(ptr);
   5.662 -#else
   5.663 -   Uint16 tmp = 0;
   5.664 -
   5.665 -   privateWaitLock();
   5.666 -   (*ptr)-= 1;
   5.667 -   tmp = *ptr;
   5.668 -   privateUnlock();
   5.669 -
   5.670 -   return tmp;
   5.671 -#endif
   5.672 -}
   5.673 -
   5.674 -Uint16
   5.675 -SDL_AtomicAddThenFetch16(Uint16 * ptr, Uint16 value)
   5.676 -{
   5.677 -#ifdef nativeAddThenFetch16
   5.678 -   return nativeAddThenFetch16(ptr, value);
   5.679 -#else
   5.680 -   Uint16 tmp = 0;
   5.681 -
   5.682 -   privateWaitLock();
   5.683 -   (*ptr)+= value;
   5.684 -   tmp = *ptr;
   5.685 -   privateUnlock();
   5.686 -
   5.687 -   return tmp;
   5.688 -#endif
   5.689 -}
   5.690 -
   5.691 -Uint16
   5.692 -SDL_AtomicSubtractThenFetch16(Uint16 * ptr, Uint16 value)
   5.693 -{
   5.694 -#ifdef nativeSubtractThenFetch16
   5.695 -   return nativeSubtractThenFetch16(ptr, value);
   5.696 -#else
   5.697 -   Uint16 tmp = 0;
   5.698 -
   5.699 -   privateWaitLock();
   5.700 -   (*ptr)-= value;
   5.701 -   tmp = *ptr;
   5.702 -   privateUnlock();
   5.703 -
   5.704 -   return tmp;
   5.705 -#endif
   5.706 -}
   5.707 -
   5.708 -/* 32 bit atomic operations */
   5.709 -
   5.710 -Uint32
   5.711 -SDL_AtomicExchange32(Uint32 * ptr, Uint32 value)
   5.712 -{
   5.713 -#ifdef nativeExchange32
   5.714 -   return nativeExchange32(ptr, value);
   5.715 -#else
   5.716 -   Uint32 tmp = 0;
   5.717 -
   5.718 -   privateWaitLock();
   5.719 -   tmp = *ptr;
   5.720 -   *ptr = value;
   5.721 -   privateUnlock();
   5.722 -
   5.723 -   return tmp;
   5.724 -#endif
   5.725 -}
   5.726 -
   5.727 -SDL_bool
   5.728 -SDL_AtomicCompareThenSet32(Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
   5.729 -{
   5.730 -#ifdef nativeCompareThenSet32
   5.731 -   return (SDL_bool)nativeCompareThenSet32(ptr, oldvalue, newvalue);
   5.732 -#else
   5.733 -   SDL_bool result = SDL_FALSE;
   5.734 -
   5.735 -   privateWaitLock();
   5.736 -   result = (*ptr == oldvalue);
   5.737 -   if (result)
   5.738 -   {
   5.739 -      *ptr = newvalue;
   5.740 -   }
   5.741 -   privateUnlock();
   5.742 -
   5.743 -   return result;
   5.744 -#endif
   5.745 -}
   5.746 -
   5.747 -SDL_bool
   5.748 -SDL_AtomicTestThenSet32(Uint32 * ptr)
   5.749 -{
   5.750 -#ifdef nativeTestThenSet32
   5.751 -   return (SDL_bool)nativeTestThenSet32(ptr);
   5.752 -#else
   5.753 -   SDL_bool result = SDL_FALSE;
   5.754 -
   5.755 -   privateWaitLock();
   5.756 -   result = (*ptr == 0);
   5.757 -   if (result)
   5.758 -   {
   5.759 -      *ptr = 1;
   5.760 -   }
   5.761 -   privateUnlock();
   5.762 -
   5.763 -   return result;
   5.764 -#endif
   5.765 -}
   5.766 -
   5.767 -void
   5.768 -SDL_AtomicClear32(Uint32 * ptr)
   5.769 +SDL_AtomicClear32(volatile Uint32 * ptr)
   5.770  {
   5.771  #ifdef nativeClear32
   5.772 -   nativeClear32(ptr);
   5.773  #else
   5.774 -   privateWaitLock();
   5.775 +   privateWaitLock(ptr);
   5.776     *ptr = 0;
   5.777 -   privateUnlock();
   5.778 +   privateUnlock(ptr);
   5.779  
   5.780     return;
   5.781  #endif
   5.782  }
   5.783  
   5.784  Uint32
   5.785 -SDL_AtomicFetchThenIncrement32(Uint32 * ptr)
   5.786 +SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr)
   5.787  {
   5.788  #ifdef nativeFetchThenIncrement32
   5.789 -   return nativeFetchThenIncrement32(ptr);
   5.790  #else
   5.791     Uint32 tmp = 0;
   5.792  
   5.793 -   privateWaitLock();
   5.794 +   privateWaitLock(ptr);
   5.795     tmp = *ptr;
   5.796     (*ptr)+= 1;
   5.797 -   privateUnlock();
   5.798 +   privateUnlock(ptr);
   5.799  
   5.800     return tmp;
   5.801  #endif
   5.802  }
   5.803  
   5.804  Uint32
   5.805 -SDL_AtomicFetchThenDecrement32(Uint32 * ptr)
   5.806 +SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr)
   5.807  {
   5.808  #ifdef nativeFetchThenDecrement32
   5.809 -   return nativeFetchThenDecrement32(ptr);
   5.810  #else
   5.811     Uint32 tmp = 0;
   5.812  
   5.813 -   privateWaitLock();
   5.814 +   privateWaitLock(ptr);
   5.815     tmp = *ptr;
   5.816     (*ptr) -= 1;
   5.817 -   privateUnlock();
   5.818 +   privateUnlock(ptr);
   5.819  
   5.820     return tmp;
   5.821  #endif
   5.822  }
   5.823  
   5.824  Uint32
   5.825 -SDL_AtomicFetchThenAdd32(Uint32 * ptr, Uint32 value)
   5.826 +SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value)
   5.827  {
   5.828  #ifdef nativeFetchThenAdd32
   5.829 -   return nativeFetchThenAdd32(ptr, value);
   5.830  #else
   5.831     Uint32 tmp = 0;
   5.832  
   5.833 -   privateWaitLock();
   5.834 +   privateWaitLock(ptr);
   5.835     tmp = *ptr;
   5.836     (*ptr)+= value;
   5.837 -   privateUnlock();
   5.838 +   privateUnlock(ptr);
   5.839  
   5.840     return tmp;
   5.841  #endif
   5.842  }
   5.843  
   5.844  Uint32
   5.845 -SDL_AtomicFetchThenSubtract32(Uint32 * ptr, Uint32 value)
   5.846 +SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value)
   5.847  {
   5.848  #ifdef nativeFetchThenSubtract32
   5.849 -   return nativeFetchThenSubtract32(ptr, value);
   5.850  #else
   5.851     Uint32 tmp = 0;
   5.852  
   5.853 -   privateWaitLock();
   5.854 +   privateWaitLock(ptr);
   5.855     tmp = *ptr;
   5.856     (*ptr)-= value;
   5.857 -   privateUnlock();
   5.858 +   privateUnlock(ptr);
   5.859  
   5.860     return tmp;
   5.861  #endif
   5.862  }
   5.863  
   5.864  Uint32
   5.865 -SDL_AtomicIncrementThenFetch32(Uint32 * ptr)
   5.866 +SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr)
   5.867  {
   5.868  #ifdef nativeIncrementThenFetch32
   5.869 -   return nativeIncrementThenFetch32(ptr);
   5.870  #else
   5.871     Uint32 tmp = 0;
   5.872  
   5.873 -   privateWaitLock();
   5.874 +   privateWaitLock(ptr);
   5.875     (*ptr)+= 1;
   5.876     tmp = *ptr;
   5.877 -   privateUnlock();
   5.878 +   privateUnlock(ptr);
   5.879  
   5.880     return tmp;
   5.881  #endif
   5.882  }
   5.883  
   5.884  Uint32
   5.885 -SDL_AtomicDecrementThenFetch32(Uint32 * ptr)
   5.886 +SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr)
   5.887  {
   5.888  #ifdef nativeDecrementThenFetch32
   5.889 -   return nativeDecrementThenFetch32(ptr);
   5.890  #else
   5.891     Uint32 tmp = 0;
   5.892  
   5.893 -   privateWaitLock();
   5.894 +   privateWaitLock(ptr);
   5.895     (*ptr)-= 1;
   5.896     tmp = *ptr;
   5.897 -   privateUnlock();
   5.898 +   privateUnlock(ptr);
   5.899  
   5.900     return tmp;
   5.901  #endif
   5.902  }
   5.903  
   5.904  Uint32
   5.905 -SDL_AtomicAddThenFetch32(Uint32 * ptr, Uint32 value)
   5.906 +SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value)
   5.907  {
   5.908  #ifdef nativeAddThenFetch32
   5.909 -   return nativeAddThenFetch32(ptr, value);
   5.910  #else
   5.911     Uint32 tmp = 0;
   5.912  
   5.913 -   privateWaitLock();
   5.914 +   privateWaitLock(ptr);
   5.915     (*ptr)+= value;
   5.916     tmp = *ptr;
   5.917 -   privateUnlock();
   5.918 +   privateUnlock(ptr);
   5.919  
   5.920     return tmp;
   5.921  #endif
   5.922  }
   5.923  
   5.924  Uint32
   5.925 -SDL_AtomicSubtractThenFetch32(Uint32 * ptr, Uint32 value)
   5.926 +SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value)
   5.927  {
   5.928  #ifdef nativeSubtractThenFetch32
   5.929 -   return nativeSubtractThenFetch32(ptr, value);
   5.930  #else
   5.931     Uint32 tmp = 0;
   5.932  
   5.933 -   privateWaitLock();
   5.934 +   privateWaitLock(ptr);
   5.935     (*ptr)-= value;
   5.936     tmp = *ptr;
   5.937 -   privateUnlock();
   5.938 +   privateUnlock(ptr);
   5.939  
   5.940     return tmp;
   5.941  #endif
   5.942 @@ -792,208 +306,161 @@
   5.943  /* 64 bit atomic operations */
   5.944  #ifdef SDL_HAS_64BIT_TYPE
   5.945  
   5.946 -Uint64
   5.947 -SDL_AtomicExchange64(Uint64 * ptr, Uint64 value)
   5.948 +SDL_bool
   5.949 +SDL_AtomicTestThenSet64(volatile Uint64 * ptr)
   5.950  {
   5.951 -#ifdef nativeExchange64
   5.952 -   return nativeExchange64(ptr, value);
   5.953 -#else
   5.954 -   Uint64 tmp = 0;
   5.955 -
   5.956 -   privateWaitLock();
   5.957 -   tmp = *ptr;
   5.958 -   *ptr = value;
   5.959 -   privateUnlock();
   5.960 -
   5.961 -   return tmp;
   5.962 -#endif
   5.963 -}
   5.964 -
   5.965 -SDL_bool
   5.966 -SDL_AtomicCompareThenSet64(Uint64 * ptr, Uint64 oldvalue, Uint64 newvalue)
   5.967 -{
   5.968 -#ifdef nativeCompareThenSet64
   5.969 -   return (SDL_bool)nativeCompareThenSet64(ptr, oldvalue, newvalue);
   5.970 +#ifdef nativeTestThenSet64
   5.971  #else
   5.972     SDL_bool result = SDL_FALSE;
   5.973  
   5.974 -   privateWaitLock();
   5.975 -   result = (*ptr == oldvalue);
   5.976 -   if (result)
   5.977 -   {
   5.978 -      *ptr = newvalue;
   5.979 -   }
   5.980 -   privateUnlock();
   5.981 -
   5.982 -   return result;
   5.983 -#endif
   5.984 -}
   5.985 -
   5.986 -SDL_bool
   5.987 -SDL_AtomicTestThenSet64(Uint64 * ptr)
   5.988 -{
   5.989 -#ifdef nativeTestThenSet64
   5.990 -   return (SDL_bool)nativeTestThenSet64(ptr);
   5.991 -#else
   5.992 -   SDL_bool result = SDL_FALSE;
   5.993 -
   5.994 -   privateWaitLock();
   5.995 +   privateWaitLock(ptr);
   5.996     result = (*ptr == 0);
   5.997     if (result)
   5.998     {
   5.999        *ptr = 1;
  5.1000     }
  5.1001 -   privateUnlock();
  5.1002 +   privateUnlock(ptr);
  5.1003  
  5.1004     return result;
  5.1005  #endif
  5.1006  }
  5.1007  
  5.1008  void
  5.1009 -SDL_AtomicClear64(Uint64 * ptr)
  5.1010 +SDL_AtomicClear64(volatile Uint64 * ptr)
  5.1011  {
  5.1012  #ifdef nativeClear64
  5.1013 -   nativeClear64(ptr);
  5.1014  #else
  5.1015 -   privateWaitLock();
  5.1016 +   privateWaitLock(ptr);
  5.1017     *ptr = 0;
  5.1018 -   privateUnlock();
  5.1019 +   privateUnlock(ptr);
  5.1020  
  5.1021     return;
  5.1022  #endif
  5.1023  }
  5.1024  
  5.1025  Uint64
  5.1026 -SDL_AtomicFetchThenIncrement64(Uint64 * ptr)
  5.1027 +SDL_AtomicFetchThenIncrement64(volatile Uint64 * ptr)
  5.1028  {
  5.1029  #ifdef nativeFetchThenIncrement64
  5.1030 -   return nativeFetchThenIncrement64(ptr);
  5.1031  #else
  5.1032     Uint64 tmp = 0;
  5.1033  
  5.1034 -   privateWaitLock();
  5.1035 +   privateWaitLock(ptr);
  5.1036     tmp = *ptr;
  5.1037     (*ptr)+= 1;
  5.1038 -   privateUnlock();
  5.1039 +   privateUnlock(ptr);
  5.1040  
  5.1041     return tmp;
  5.1042  #endif
  5.1043  }
  5.1044  
  5.1045  Uint64
  5.1046 -SDL_AtomicFetchThenDecrement64(Uint64 * ptr)
  5.1047 +SDL_AtomicFetchThenDecrement64(volatile Uint64 * ptr)
  5.1048  {
  5.1049  #ifdef nativeFetchThenDecrement64
  5.1050 -   return nativeFetchThenDecrement64(ptr);
  5.1051  #else
  5.1052     Uint64 tmp = 0;
  5.1053  
  5.1054 -   privateWaitLock();
  5.1055 +   privateWaitLock(ptr);
  5.1056     tmp = *ptr;
  5.1057     (*ptr) -= 1;
  5.1058 -   privateUnlock();
  5.1059 +   privateUnlock(ptr);
  5.1060  
  5.1061     return tmp;
  5.1062  #endif
  5.1063  }
  5.1064  
  5.1065  Uint64
  5.1066 -SDL_AtomicFetchThenAdd64(Uint64 * ptr, Uint64 value)
  5.1067 +SDL_AtomicFetchThenAdd64(volatile Uint64 * ptr, Uint64 value)
  5.1068  {
  5.1069  #ifdef nativeFetchThenAdd64
  5.1070 -   return nativeFetchThenAdd64(ptr, value);
  5.1071  #else
  5.1072     Uint64 tmp = 0;
  5.1073  
  5.1074 -   privateWaitLock();
  5.1075 +   privateWaitLock(ptr);
  5.1076     tmp = *ptr;
  5.1077     (*ptr)+= value;
  5.1078 -   privateUnlock();
  5.1079 +   privateUnlock(ptr);
  5.1080  
  5.1081     return tmp;
  5.1082  #endif
  5.1083  }
  5.1084  
  5.1085  Uint64
  5.1086 -SDL_AtomicFetchThenSubtract64(Uint64 * ptr, Uint64 value)
  5.1087 +SDL_AtomicFetchThenSubtract64(volatile Uint64 * ptr, Uint64 value)
  5.1088  {
  5.1089  #ifdef nativeFetchThenSubtract64
  5.1090 -   return nativeFetchThenSubtract64(ptr, value);
  5.1091  #else
  5.1092     Uint64 tmp = 0;
  5.1093  
  5.1094 -   privateWaitLock();
  5.1095 +   privateWaitLock(ptr);
  5.1096     tmp = *ptr;
  5.1097     (*ptr)-= value;
  5.1098 -   privateUnlock();
  5.1099 +   privateUnlock(ptr);
  5.1100  
  5.1101     return tmp;
  5.1102  #endif
  5.1103  }
  5.1104  
  5.1105  Uint64
  5.1106 -SDL_AtomicIncrementThenFetch64(Uint64 * ptr)
  5.1107 +SDL_AtomicIncrementThenFetch64(volatile Uint64 * ptr)
  5.1108  {
  5.1109  #ifdef nativeIncrementThenFetch64
  5.1110 -   return nativeIncrementThenFetch64(ptr);
  5.1111  #else
  5.1112     Uint64 tmp = 0;
  5.1113  
  5.1114 -   privateWaitLock();
  5.1115 +   privateWaitLock(ptr);
  5.1116     (*ptr)+= 1;
  5.1117     tmp = *ptr;
  5.1118 -   privateUnlock();
  5.1119 +   privateUnlock(ptr);
  5.1120  
  5.1121     return tmp;
  5.1122  #endif
  5.1123  }
  5.1124  
  5.1125  Uint64
  5.1126 -SDL_AtomicDecrementThenFetch64(Uint64 * ptr)
  5.1127 +SDL_AtomicDecrementThenFetch64(volatile Uint64 * ptr)
  5.1128  {
  5.1129  #ifdef nativeDecrementThenFetch64
  5.1130 -   return nativeDecrementThenFetch64(ptr);
  5.1131  #else
  5.1132     Uint64 tmp = 0;
  5.1133  
  5.1134 -   privateWaitLock();
  5.1135 +   privateWaitLock(ptr);
  5.1136     (*ptr)-= 1;
  5.1137     tmp = *ptr;
  5.1138 -   privateUnlock();
  5.1139 +   privateUnlock(ptr);
  5.1140  
  5.1141     return tmp;
  5.1142  #endif
  5.1143  }
  5.1144  
  5.1145  Uint64
  5.1146 -SDL_AtomicAddThenFetch64(Uint64 * ptr, Uint64 value)
  5.1147 +SDL_AtomicAddThenFetch64(volatile Uint64 * ptr, Uint64 value)
  5.1148  {
  5.1149  #ifdef nativeAddThenFetch64
  5.1150 -   return nativeAddThenFetch64(ptr, value);
  5.1151  #else
  5.1152     Uint64 tmp = 0;
  5.1153  
  5.1154 -   privateWaitLock();
  5.1155 +   privateWaitLock(ptr);
  5.1156     (*ptr)+= value;
  5.1157     tmp = *ptr;
  5.1158 -   privateUnlock();
  5.1159 +   privateUnlock(ptr);
  5.1160  
  5.1161     return tmp;
  5.1162  #endif
  5.1163  }
  5.1164  
  5.1165  Uint64
  5.1166 -SDL_AtomicSubtractThenFetch64(Uint64 * ptr, Uint64 value)
  5.1167 +SDL_AtomicSubtractThenFetch64(volatile Uint64 * ptr, Uint64 value)
  5.1168  {
  5.1169  #ifdef nativeSubtractThenFetch64
  5.1170 -   return nativeSubtractThenFetch64(ptr, value);
  5.1171  #else
  5.1172     Uint64 tmp = 0;
  5.1173  
  5.1174 -   privateWaitLock();
  5.1175 +   privateWaitLock(ptr);
  5.1176     (*ptr)-= value;
  5.1177     tmp = *ptr;
  5.1178 -   privateUnlock();
  5.1179 +   privateUnlock(ptr);
  5.1180  
  5.1181     return tmp;
  5.1182  #endif
     6.1 --- a/test/testatomic.c	Mon Sep 07 16:04:44 2009 +0000
     6.2 +++ b/test/testatomic.c	Thu Sep 17 20:35:12 2009 +0000
     6.3 @@ -29,21 +29,18 @@
     6.4     volatile Uint64 val64 = 0;
     6.5     Uint64 ret64 = 0;
     6.6  
     6.7 +   SDL_SpinLock lock = 0;
     6.8 +
     6.9     SDL_bool tfret = SDL_FALSE;
    6.10  
    6.11 -   printf("32 bit -----------------------------------------\n\n");
    6.12 +   printf("\nspin lock---------------------------------------\n\n");
    6.13  
    6.14 -   ret32 = SDL_AtomicExchange32(&val32, 10);
    6.15 -   printf("Exchange32           ret=%d val=%d\n", ret32, val32);
    6.16 -   ret32 = SDL_AtomicExchange32(&val32, 0);
    6.17 -   printf("Exchange32           ret=%d val=%d\n", ret32, val32);
    6.18 +   SDL_AtomicLock(&lock);
    6.19 +   printf("AtomicLock                   lock=%d\n", lock);
    6.20 +   SDL_AtomicUnlock(&lock);
    6.21 +   printf("AtomicUnlock                 lock=%d\n", lock);
    6.22  
    6.23 -   val32 = 10;
    6.24 -   tfret = SDL_AtomicCompareThenSet32(&val32, 10, 20);
    6.25 -   printf("CompareThenSet32     tfret=%s val=%d\n", tf(tfret), val32);
    6.26 -   val32 = 10;
    6.27 -   tfret = SDL_AtomicCompareThenSet32(&val32, 0, 20);
    6.28 -   printf("CompareThenSet32     tfret=%s val=%d\n", tf(tfret), val32);
    6.29 +   printf("\n32 bit -----------------------------------------\n\n");
    6.30  
    6.31     val32 = 0;
    6.32     tfret = SDL_AtomicTestThenSet32(&val32);
    6.33 @@ -79,19 +76,7 @@
    6.34     printf("SubtractThenFetch32  ret=%d val=%d\n", ret32, val32);
    6.35  
    6.36  #ifdef SDL_HAS_64BIT_TYPE
    6.37 -   printf("64 bit -----------------------------------------\n\n");
    6.38 -
    6.39 -   ret64 = SDL_AtomicExchange64(&val64, 10);
    6.40 -   printf("Exchange64           ret=%lld val=%lld\n", ret64, val64);
    6.41 -   ret64 = SDL_AtomicExchange64(&val64, 0);
    6.42 -   printf("Exchange64           ret=%lld val=%lld\n", ret64, val64);
    6.43 -
    6.44 -   val64 = 10;
    6.45 -   tfret = SDL_AtomicCompareThenSet64(&val64, 10, 20);
    6.46 -   printf("CompareThenSet64     tfret=%s val=%lld\n", tf(tfret), val64);
    6.47 -   val64 = 10;
    6.48 -   tfret = SDL_AtomicCompareThenSet64(&val64, 0, 20);
    6.49 -   printf("CompareThenSet64     tfret=%s val=%lld\n", tf(tfret), val64);
    6.50 +   printf("\n64 bit -----------------------------------------\n\n");
    6.51  
    6.52     val64 = 0;
    6.53     tfret = SDL_AtomicTestThenSet64(&val64);