I've made so many changes I don't dare continue until I check the current stuff in.
authorBob Pendleton <bob@pendleton.com>
Mon, 29 Jun 2009 19:54:43 +0000
changeset 32023aa519a5c676
parent 3201 c297230efc75
child 3203 790cbbda6429
I've made so many changes I don't dare continue until I check the current stuff in.

/test/testatomic.c performs absolutely basic tests to show that the function work as expected. Need a second test to do more detailed tests.

/include/SDL_atomic.h provides declarations for all included functions.

/src/atomic/linux/SDL_atomic.c provided all the functions. On a generic built the 64 bit functions work, but they are emulated. On a build for -march=pentium and above the 64 bit functions use native instructions
/src/atomic/dummy/SDL_atomic.c emulates all the operations using SDL_mutex.h.
/src/atomic/win32/SDL_atomic.c is a copy of dummy
/src/atomic/macosx/SDL_atomic.s is a copy of dummy

These versions of SDL_atomic.c provide a frame work for building the library with a mixture of native and emulated functions. This allows the whole library to be provided on all platforms. (I hope.)
I hope this fits with the SDL philosophy of either providing a common subset or emulating when the platform is missing a feature.

I have not added dummy, macosx, or win32 to the build. They are there as place holders for future work.

I have modified congifure.in to compile sources in /src/atomic/linux. (The SDL configure.in file is an amazing piece of work and I hope I didn't mess it up. :-)
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	Wed Jun 24 22:24:23 2009 +0000
     1.2 +++ b/include/SDL_atomic.h	Mon Jun 29 19:54:43 2009 +0000
     1.3 @@ -29,7 +29,6 @@
     1.4  #ifndef _SDL_atomic_h_
     1.5  #define _SDL_atomic_h_
     1.6  
     1.7 -
     1.8  #include "SDL_stdinc.h"
     1.9  #include "SDL_platform.h"
    1.10  
    1.11 @@ -42,72 +41,51 @@
    1.12  /* *INDENT-ON* */
    1.13  #endif
    1.14  
    1.15 -/* *INDENT-OFF* */
    1.16  /**
    1.17 - * \def SDL_AtomicBusyWait32 (ptr)
    1.18 - *
    1.19 - * \brief Implements a simple busy wait for use with
    1.20 - * SDL_AtomicTestThenSet and SDL_AtomicClear.
    1.21 - *
    1.22 - * Note: This can be an infinite loop.
    1.23 - *
    1.24 + * These operations may, or may not, actually be implemented using
    1.25 + * processor specific atomic operations. When possible they are
    1.26 + * implemented as true processor specific atomic operations. When that
    1.27 + * is not possible the are implemented using locks that *do* use the
    1.28 + * available atomic operations. In rare cases they may be implemented
    1.29 + * using SDL's mutex fuctions.
    1.30   */
    1.31 -#define SDL_AtomicBusyWait32(ptr)		\
    1.32 -   {						\
    1.33 -   while (!SDL_AtomicTestThenSet32(ptr)		\
    1.34 -      {						\
    1.35 -      };					\
    1.36 -   };
    1.37 -
    1.38 -/**
    1.39 - * \def SDL_AtomicWait32(ptr)
    1.40 - *
    1.41 - * \brief A safer way to wait for a test-then-set lock to be cleared.
    1.42 - *
    1.43 - * This assumes that the SDL_Sleep(0) call acts as a thread_yeild
    1.44 - * operation. 
    1.45 - *
    1.46 - */
    1.47 -#define SDL_AtomicWait32(ptr)			\
    1.48 -   {						\
    1.49 -   while (!SDL_AtomicTestThenSet32(ptr)		\
    1.50 -      {						\
    1.51 -	 SDL_Sleep(0);				\
    1.52 -      };                                        \
    1.53 -   };
    1.54 -
    1.55 -/**
    1.56 - * \def SDL_AtomicBusyWait64(ptr)
    1.57 - *
    1.58 - * \brief 64 bit version of busy wait
    1.59 - *
    1.60 - * \sa SDL_AtomicBusyWait32
    1.61 - */
    1.62 -#define SDL_AtomicBusyWait64(ptr)		\
    1.63 -   {						\
    1.64 -   while (!SDL_AtomicTestThenSet64(ptr)		\
    1.65 -      {						\
    1.66 -      };					\
    1.67 -   };
    1.68 -
    1.69 -/**
    1.70 - * \def SDL_AtomicWait64(ptr)
    1.71 - *
    1.72 - * \brief 64 bit version of SDL_AtomicWait32
    1.73 - *
    1.74 - * \sa SDL_AtomicWait32
    1.75 - */
    1.76 -#define SDL_AtomicWait64(ptr)			\
    1.77 -   {						\
    1.78 -   while (!SDL_AtomicTestThenSet64(ptr)		\
    1.79 -      {						\
    1.80 -	 SDL_Sleep(0);				\
    1.81 -      };                                        \
    1.82 -   };
    1.83 -/* *INDENT-ON* */
    1.84  
    1.85  /* Function prototypes */
    1.86  
    1.87 +/* 8 bit atomic operations */
    1.88 +
    1.89 +extern DECLSPEC Uint8 SDLCALL SDL_AtomicExchange8(Uint8 * ptr, Uint8 value);
    1.90 +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet8(Uint8 * ptr,
    1.91 +                                                            Uint8 oldvalue, Uint8 newvalue);
    1.92 +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet8(Uint8 * ptr);
    1.93 +extern DECLSPEC void SDLCALL SDL_AtomicClear8(Uint8 * ptr);
    1.94 +extern DECLSPEC Uint8 SDLCALL SDL_AtomicFetchThenIncrement8(Uint8 * ptr);
    1.95 +extern DECLSPEC Uint8 SDLCALL SDL_AtomicFetchThenDecrement8(Uint8 * ptr);
    1.96 +extern DECLSPEC Uint8 SDLCALL SDL_AtomicFetchThenAdd8(Uint8 * ptr, Uint8 value);
    1.97 +extern DECLSPEC Uint8 SDLCALL SDL_AtomicFetchThenSubtract8(Uint8 * ptr, Uint8 value);
    1.98 +extern DECLSPEC Uint8 SDLCALL SDL_AtomicIncrementThenFetch8(Uint8 * ptr);
    1.99 +extern DECLSPEC Uint8 SDLCALL SDL_AtomicDecrementThenFetch8(Uint8 * ptr);
   1.100 +extern DECLSPEC Uint8 SDLCALL SDL_AtomicAddThenFetch8(Uint8 * ptr, Uint8 value);
   1.101 +extern DECLSPEC Uint8 SDLCALL SDL_AtomicSubtractThenFetch8(Uint8 * ptr, Uint8 value);
   1.102 +
   1.103 +/* 16 bit atomic operations */
   1.104 +
   1.105 +extern DECLSPEC Uint16 SDLCALL SDL_AtomicExchange16(Uint16 * ptr, Uint16 value);
   1.106 +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareThenSet16(Uint16 * ptr,
   1.107 +                                                            Uint16 oldvalue, Uint16 newvalue);
   1.108 +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTestThenSet16(Uint16 * ptr);
   1.109 +extern DECLSPEC void SDLCALL SDL_AtomicClear16(Uint16 * ptr);
   1.110 +extern DECLSPEC Uint16 SDLCALL SDL_AtomicFetchThenIncrement16(Uint16 * ptr);
   1.111 +extern DECLSPEC Uint16 SDLCALL SDL_AtomicFetchThenDecrement16(Uint16 * ptr);
   1.112 +extern DECLSPEC Uint16 SDLCALL SDL_AtomicFetchThenAdd16(Uint16 * ptr, Uint16 value);
   1.113 +extern DECLSPEC Uint16 SDLCALL SDL_AtomicFetchThenSubtract16(Uint16 * ptr, Uint16 value);
   1.114 +extern DECLSPEC Uint16 SDLCALL SDL_AtomicIncrementThenFetch16(Uint16 * ptr);
   1.115 +extern DECLSPEC Uint16 SDLCALL SDL_AtomicDecrementThenFetch16(Uint16 * ptr);
   1.116 +extern DECLSPEC Uint16 SDLCALL SDL_AtomicAddThenFetch16(Uint16 * ptr, Uint16 value);
   1.117 +extern DECLSPEC Uint16 SDLCALL SDL_AtomicSubtractThenFetch16(Uint16 * ptr, Uint16 value);
   1.118 +
   1.119 +/* 32 bit atomic operations */
   1.120 +
   1.121  /**
   1.122   * \fn int SDL_AtomicExchange32(Uint32 * ptr, Uint32 value)
   1.123   *
   1.124 @@ -252,6 +230,7 @@
   1.125   */
   1.126  extern DECLSPEC Uint32 SDLCALL SDL_AtomicSubtractThenFetch32(Uint32 * ptr, Uint32 value);
   1.127  
   1.128 +/* 64 bit atomic operations */
   1.129  #ifdef SDL_HAS_64BIT_TYPE
   1.130  
   1.131  extern DECLSPEC Uint64 SDLCALL SDL_AtomicExchange64(Uint64 * ptr, Uint64 value);
   1.132 @@ -267,7 +246,7 @@
   1.133  extern DECLSPEC Uint64 SDLCALL SDL_AtomicDecrementThenFetch64(Uint64 * ptr);
   1.134  extern DECLSPEC Uint64 SDLCALL SDL_AtomicAddThenFetch64(Uint64 * ptr, Uint64 value);
   1.135  extern DECLSPEC Uint64 SDLCALL SDL_AtomicSubtractThenFetch64(Uint64 * ptr, Uint64 value);
   1.136 -#endif
   1.137 +#endif /*  SDL_HAS_64BIT_TYPE */
   1.138  
   1.139  /* Ends C function definitions when using C++ */
   1.140  #ifdef __cplusplus
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/atomic/dummy/SDL_atomic.c	Mon Jun 29 19:54:43 2009 +0000
     2.3 @@ -0,0 +1,1002 @@
     2.4 +/*
     2.5 +    SDL - Simple DirectMedia Layer
     2.6 +    Copyright (C) 1997-2009 Sam Lantinga
     2.7 +
     2.8 +    This library is free software; you can redistribute it and/or
     2.9 +    modify it under the terms of the GNU Lesser General Public
    2.10 +    License as published by the Free Software Foundation; either
    2.11 +    version 2.1 of the License, or (at your option) any later version.
    2.12 +
    2.13 +    This library is distributed in the hope that it will be useful,
    2.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.16 +    Lesser General Public License for more details.
    2.17 +
    2.18 +    You should have received a copy of the GNU Lesser General Public
    2.19 +    License along with this library; if not, write to the Free Software
    2.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2.21 +
    2.22 +    Sam Lantinga
    2.23 +    slouken@libsdl.org
    2.24 +*/
    2.25 +
    2.26 +#include "SDL_stdinc.h"
    2.27 +#include "SDL_atomic.h"
    2.28 +
    2.29 +/*
    2.30 +  This file provides 8, 16, 32, and 64 bit atomic operations. If the
    2.31 +  operations are provided by the native hardware and operating system
    2.32 +  they are used. If they are not then the operations are emulated
    2.33 +  using the SDL mutex operations. 
    2.34 + */
    2.35 +
    2.36 +/* 
    2.37 +  First, detect whether the operations are supported and create
    2.38 +  #defines that indicate that they do exist. The goal is to have all
    2.39 +  the system dependent code in the top part of the file so that the
    2.40 +  bottom can be use unchanged across all platforms.
    2.41 +
    2.42 +  Second, #define all the operations in each size class that are
    2.43 +  supported. Doing this allows supported operations to be used along
    2.44 +  side of emulated operations.
    2.45 +*/
    2.46 +
    2.47 +/* 
    2.48 +   Emmulated version.
    2.49 +
    2.50 +   Assume there is no support for atomic operations. All such
    2.51 +   operations are implemented using SDL mutex operations.
    2.52 + */
    2.53 +
    2.54 +#ifdef EMULATED_ATOMIC_OPERATIONS
    2.55 +#undef EMULATED_ATOMIC_OPERATIONS
    2.56 +#endif
    2.57 +
    2.58 +#ifdef EMULATED_ATOMIC_OPERATIONS
    2.59 +#define HAVE_ALL_8_BIT_OPS
    2.60 +
    2.61 +#define nativeExchange8(ptr, value)			()
    2.62 +#define nativeCompareThenSet8(ptr, oldvalue, newvalue) 	()
    2.63 +#define nativeTestThenSet8(ptr)    	     		()
    2.64 +#define nativeClear8(ptr)				()
    2.65 +#define nativeFetchThenIncrement8(ptr)   		()
    2.66 +#define nativeFetchThenDecrement8(ptr) 			()
    2.67 +#define nativeFetchThenAdd8(ptr, value) 		()
    2.68 +#define nativeFetchThenSubtract8(ptr, value) 		()
    2.69 +#define nativeIncrementThenFetch8(ptr) 			()
    2.70 +#define nativeDecrementThenFetch8(ptr) 			()
    2.71 +#define nativeAddThenFetch8(ptr, value) 		()
    2.72 +#define nativeSubtractThenFetch8(ptr, value) 		()
    2.73 +#endif
    2.74 +
    2.75 +#ifdef EMULATED_ATOMIC_OPERATIONS
    2.76 +#define HAVE_ALL_16_BIT_OPS
    2.77 +
    2.78 +#define nativeExchange16(ptr, value)			()
    2.79 +#define nativeCompareThenSet16(ptr, oldvalue, newvalue) ()
    2.80 +#define nativeTestThenSet16(ptr)    	     		()
    2.81 +#define nativeClear16(ptr)				()
    2.82 +#define nativeFetchThenIncrement16(ptr)   		()
    2.83 +#define nativeFetchThenDecrement16(ptr) 		()
    2.84 +#define nativeFetchThenAdd16(ptr, value) 		()
    2.85 +#define nativeFetchThenSubtract16(ptr, value) 		()
    2.86 +#define nativeIncrementThenFetch16(ptr) 		()
    2.87 +#define nativeDecrementThenFetch16(ptr) 		()
    2.88 +#define nativeAddThenFetch16(ptr, value) 		()
    2.89 +#define nativeSubtractThenFetch16(ptr, value) 		()
    2.90 +#endif
    2.91 +
    2.92 +#ifdef EMULATED_ATOMIC_OPERATIONS
    2.93 +#define HAVE_ALL_32_BIT_OPS
    2.94 +
    2.95 +#define nativeExchange32(ptr, value)			()
    2.96 +#define nativeCompareThenSet32(ptr, oldvalue, newvalue) ()
    2.97 +#define nativeTestThenSet32(ptr)    	     		()
    2.98 +#define nativeClear32(ptr)				()
    2.99 +#define nativeFetchThenIncrement32(ptr)   		()
   2.100 +#define nativeFetchThenDecrement32(ptr) 		()
   2.101 +#define nativeFetchThenAdd32(ptr, value) 		()
   2.102 +#define nativeFetchThenSubtract32(ptr, value) 		()
   2.103 +#define nativeIncrementThenFetch32(ptr) 		()
   2.104 +#define nativeDecrementThenFetch32(ptr) 		()
   2.105 +#define nativeAddThenFetch32(ptr, value) 		()
   2.106 +#define nativeSubtractThenFetch32(ptr, value) 		()
   2.107 +#endif
   2.108 +
   2.109 +#ifdef EMULATED_ATOMIC_OPERATIONS
   2.110 +#define HAVE_ALL_64_BIT_OPS
   2.111 +
   2.112 +#define nativeExchange64(ptr, value)			()
   2.113 +#define nativeCompareThenSet64(ptr, oldvalue, newvalue) ()
   2.114 +#define nativeTestThenSet64(ptr)    	     		()
   2.115 +#define nativeClear64(ptr)				()
   2.116 +#define nativeFetchThenIncrement64(ptr)   		()
   2.117 +#define nativeFetchThenDecrement64(ptr) 		()
   2.118 +#define nativeFetchThenAdd64(ptr, value) 		()
   2.119 +#define nativeFetchThenSubtract64(ptr, value) 		()
   2.120 +#define nativeIncrementThenFetch64(ptr) 		()
   2.121 +#define nativeDecrementThenFetch64(ptr) 		()
   2.122 +#define nativeAddThenFetch64(ptr, value) 		()
   2.123 +#define nativeSubtractThenFetch64(ptr, value) 		()
   2.124 +#endif
   2.125 +
   2.126 +/* 
   2.127 +If any of the operations are not provided then we must emulate some of
   2.128 +them.
   2.129 + */
   2.130 +
   2.131 +#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.132 +
   2.133 +#include "SDL_mutex.h"
   2.134 +#include "SDL_error.h"
   2.135 +
   2.136 +static SDL_mutex * lock = NULL;
   2.137 +
   2.138 +static __inline__ void
   2.139 +privateWaitLock()
   2.140 +{
   2.141 +   if(NULL == lock)
   2.142 +   {
   2.143 +      lock = SDL_CreateMutex();
   2.144 +      if (NULL == lock)
   2.145 +      {
   2.146 +	 SDL_SetError("SDL_atomic.c: can't create a mutex");
   2.147 +	 return;
   2.148 +      }
   2.149 +   }
   2.150 +
   2.151 +   if (-1 == SDL_LockMutex(lock))
   2.152 +   {
   2.153 +      SDL_SetError("SDL_atomic.c: can't lock mutex");
   2.154 +   }
   2.155 +}
   2.156 +
   2.157 +static __inline__ void
   2.158 +privateUnlock()
   2.159 +{
   2.160 +   if (-1 == SDL_UnlockMutex(lock))
   2.161 +   {
   2.162 +      SDL_SetError("SDL_atomic.c: can't unlock mutex");
   2.163 +   }
   2.164 +}
   2.165 +
   2.166 +#endif
   2.167 +
   2.168 +/* 8 bit atomic operations */
   2.169 +
   2.170 +Uint8
   2.171 +SDL_AtomicExchange8(Uint8 * ptr, Uint8 value)
   2.172 +{
   2.173 +#ifdef nativeExchange8
   2.174 +   return nativeExchange8(ptr, value);
   2.175 +#else
   2.176 +   Uint8 tmp = 0;;
   2.177 +
   2.178 +   privateWaitLock();
   2.179 +   tmp = *ptr;
   2.180 +   *ptr = value;
   2.181 +   privateUnlock();
   2.182 +
   2.183 +   return tmp;
   2.184 +#endif
   2.185 +}
   2.186 +
   2.187 +SDL_bool
   2.188 +SDL_AtomicCompareThenSet8(Uint8 * ptr, Uint8 oldvalue, Uint8 newvalue)
   2.189 +{
   2.190 +#ifdef nativeCompareThenSet8
   2.191 +   return (SDL_bool)nativeCompareThenSet8(ptr, oldvalue, newvalue);
   2.192 +#else
   2.193 +   SDL_bool result = SDL_FALSE;
   2.194 +
   2.195 +   privateWaitLock();
   2.196 +   result = (*ptr == oldvalue);
   2.197 +   if (result)
   2.198 +   {
   2.199 +      *ptr = newvalue;
   2.200 +   }
   2.201 +   privateUnlock();
   2.202 +
   2.203 +   return result;
   2.204 +#endif
   2.205 +}
   2.206 +
   2.207 +SDL_bool
   2.208 +SDL_AtomicTestThenSet8(Uint8 * ptr)
   2.209 +{
   2.210 +#ifdef nativeTestThenSet8
   2.211 +   return (SDL_bool)nativeTestThenSet8(ptr);
   2.212 +#else
   2.213 +   SDL_bool result = SDL_FALSE;
   2.214 +
   2.215 +   privateWaitLock();
   2.216 +   result = (*ptr == 0);
   2.217 +   if (result)
   2.218 +   {
   2.219 +      *ptr = 1;
   2.220 +   }
   2.221 +   privateUnlock();
   2.222 +
   2.223 +   return result;
   2.224 +#endif
   2.225 +}
   2.226 +
   2.227 +void
   2.228 +SDL_AtomicClear8(Uint8 * ptr)
   2.229 +{
   2.230 +#ifdef nativeClear8
   2.231 +   nativeClear8(ptr);
   2.232 +#else
   2.233 +   privateWaitLock();
   2.234 +   *ptr = 0;
   2.235 +   privateUnlock();
   2.236 +
   2.237 +   return;
   2.238 +#endif
   2.239 +}
   2.240 +
   2.241 +Uint8
   2.242 +SDL_AtomicFetchThenIncrement8(Uint8 * ptr)
   2.243 +{
   2.244 +#ifdef nativeFetchThenIncrement8
   2.245 +   return nativeFetchThenIncrement8(ptr);
   2.246 +#else
   2.247 +   Uint8 tmp = 0;;
   2.248 +
   2.249 +   privateWaitLock();
   2.250 +   tmp = *ptr;
   2.251 +   (*ptr)+= 1;
   2.252 +   privateUnlock();
   2.253 +
   2.254 +   return tmp;
   2.255 +#endif
   2.256 +}
   2.257 +
   2.258 +Uint8
   2.259 +SDL_AtomicFetchThenDecrement8(Uint8 * ptr)
   2.260 +{
   2.261 +#ifdef nativeFetchThenDecrement8
   2.262 +   return nativeFetchThenDecrement8(ptr);
   2.263 +#else
   2.264 +   Uint8 tmp = 0;;
   2.265 +
   2.266 +   privateWaitLock();
   2.267 +   tmp = *ptr;
   2.268 +   (*ptr) -= 1;
   2.269 +   privateUnlock();
   2.270 +
   2.271 +   return tmp;
   2.272 +#endif
   2.273 +}
   2.274 +
   2.275 +Uint8
   2.276 +SDL_AtomicFetchThenAdd8(Uint8 * ptr, Uint8 value)
   2.277 +{
   2.278 +#ifdef nativeFetchThenAdd8
   2.279 +   return nativeFetchThenAdd8(ptr, value);
   2.280 +#else
   2.281 +   Uint8 tmp = 0;;
   2.282 +
   2.283 +   privateWaitLock();
   2.284 +   tmp = *ptr;
   2.285 +   (*ptr)+= value;
   2.286 +   privateUnlock();
   2.287 +
   2.288 +   return tmp;
   2.289 +#endif
   2.290 +}
   2.291 +
   2.292 +Uint8
   2.293 +SDL_AtomicFetchThenSubtract8(Uint8 * ptr, Uint8 value)
   2.294 +{
   2.295 +#ifdef nativeFetchThenSubtract8
   2.296 +   return nativeFetchThenSubtract8(ptr, value);
   2.297 +#else
   2.298 +   Uint8 tmp = 0;;
   2.299 +
   2.300 +   privateWaitLock();
   2.301 +   tmp = *ptr;
   2.302 +   (*ptr)-= value;
   2.303 +   privateUnlock();
   2.304 +
   2.305 +   return tmp;
   2.306 +#endif
   2.307 +}
   2.308 +
   2.309 +Uint8
   2.310 +SDL_AtomicIncrementThenFetch8(Uint8 * ptr)
   2.311 +{
   2.312 +#ifdef nativeIncrementThenFetch8
   2.313 +   return nativeIncrementThenFetch8(ptr);
   2.314 +#else
   2.315 +   Uint8 tmp = 0;;
   2.316 +
   2.317 +   privateWaitLock();
   2.318 +   (*ptr)+= 1;
   2.319 +   tmp = *ptr;
   2.320 +   privateUnlock();
   2.321 +
   2.322 +   return tmp;
   2.323 +#endif
   2.324 +}
   2.325 +
   2.326 +Uint8
   2.327 +SDL_AtomicDecrementThenFetch8(Uint8 * ptr)
   2.328 +{
   2.329 +#ifdef nativeDecrementThenFetch8
   2.330 +   return nativeDecrementThenFetch8(ptr);
   2.331 +#else
   2.332 +   Uint8 tmp = 0;;
   2.333 +
   2.334 +   privateWaitLock();
   2.335 +   (*ptr)-= 1;
   2.336 +   tmp = *ptr;
   2.337 +   privateUnlock();
   2.338 +
   2.339 +   return tmp;
   2.340 +#endif
   2.341 +}
   2.342 +
   2.343 +Uint8
   2.344 +SDL_AtomicAddThenFetch8(Uint8 * ptr, Uint8 value)
   2.345 +{
   2.346 +#ifdef nativeAddThenFetch8
   2.347 +   return nativeAddThenFetch8(ptr, value);
   2.348 +#else
   2.349 +   Uint8 tmp = 0;;
   2.350 +
   2.351 +   privateWaitLock();
   2.352 +   (*ptr)+= value;
   2.353 +   tmp = *ptr;
   2.354 +   privateUnlock();
   2.355 +
   2.356 +   return tmp;
   2.357 +#endif
   2.358 +}
   2.359 +
   2.360 +Uint8
   2.361 +SDL_AtomicSubtractThenFetch8(Uint8 * ptr, Uint8 value)
   2.362 +{
   2.363 +#ifdef nativeSubtractThenFetch8
   2.364 +   return nativeSubtractThenFetch8(ptr, value);
   2.365 +#else
   2.366 +   Uint8 tmp = 0;;
   2.367 +
   2.368 +   privateWaitLock();
   2.369 +   (*ptr)-= value;
   2.370 +   tmp = *ptr;
   2.371 +   privateUnlock();
   2.372 +
   2.373 +   return tmp;
   2.374 +#endif
   2.375 +}
   2.376 +
   2.377 +/* 16 bit atomic operations */
   2.378 +
   2.379 +Uint16
   2.380 +SDL_AtomicExchange16(Uint16 * ptr, Uint16 value)
   2.381 +{
   2.382 +#ifdef nativeExchange16
   2.383 +   return nativeExchange16(ptr, value);
   2.384 +#else
   2.385 +   Uint16 tmp = 0;;
   2.386 +
   2.387 +   privateWaitLock();
   2.388 +   tmp = *ptr;
   2.389 +   *ptr = value;
   2.390 +   privateUnlock();
   2.391 +
   2.392 +   return tmp;
   2.393 +#endif
   2.394 +}
   2.395 +
   2.396 +SDL_bool
   2.397 +SDL_AtomicCompareThenSet16(Uint16 * ptr, Uint16 oldvalue, Uint16 newvalue)
   2.398 +{
   2.399 +#ifdef nativeCompareThenSet16
   2.400 +   return (SDL_bool)nativeCompareThenSet16(ptr, oldvalue, newvalue);
   2.401 +#else
   2.402 +   SDL_bool result = SDL_FALSE;
   2.403 +
   2.404 +   privateWaitLock();
   2.405 +   result = (*ptr == oldvalue);
   2.406 +   if (result)
   2.407 +   {
   2.408 +      *ptr = newvalue;
   2.409 +   }
   2.410 +   privateUnlock();
   2.411 +
   2.412 +   return result;
   2.413 +#endif
   2.414 +}
   2.415 +
   2.416 +SDL_bool
   2.417 +SDL_AtomicTestThenSet16(Uint16 * ptr)
   2.418 +{
   2.419 +#ifdef nativeTestThenSet16
   2.420 +   return (SDL_bool)nativeTestThenSet16(ptr);
   2.421 +#else
   2.422 +   SDL_bool result = SDL_FALSE;
   2.423 +
   2.424 +   privateWaitLock();
   2.425 +   result = (*ptr == 0);
   2.426 +   if (result)
   2.427 +   {
   2.428 +      *ptr = 1;
   2.429 +   }
   2.430 +   privateUnlock();
   2.431 +
   2.432 +   return result;
   2.433 +#endif
   2.434 +}
   2.435 +
   2.436 +void
   2.437 +SDL_AtomicClear16(Uint16 * ptr)
   2.438 +{
   2.439 +#ifdef nativeClear16
   2.440 +   nativeClear16(ptr);
   2.441 +#else
   2.442 +   privateWaitLock();
   2.443 +   *ptr = 0;
   2.444 +   privateUnlock();
   2.445 +
   2.446 +   return;
   2.447 +#endif
   2.448 +}
   2.449 +
   2.450 +Uint16
   2.451 +SDL_AtomicFetchThenIncrement16(Uint16 * ptr)
   2.452 +{
   2.453 +#ifdef nativeFetchThenIncrement16
   2.454 +   return nativeFetchThenIncrement16(ptr);
   2.455 +#else
   2.456 +   Uint16 tmp = 0;;
   2.457 +
   2.458 +   privateWaitLock();
   2.459 +   tmp = *ptr;
   2.460 +   (*ptr)+= 1;
   2.461 +   privateUnlock();
   2.462 +
   2.463 +   return tmp;
   2.464 +#endif
   2.465 +}
   2.466 +
   2.467 +Uint16
   2.468 +SDL_AtomicFetchThenDecrement16(Uint16 * ptr)
   2.469 +{
   2.470 +#ifdef nativeFetchThenDecrement16
   2.471 +   return nativeFetchThenDecrement16(ptr);
   2.472 +#else
   2.473 +   Uint16 tmp = 0;;
   2.474 +
   2.475 +   privateWaitLock();
   2.476 +   tmp = *ptr;
   2.477 +   (*ptr) -= 1;
   2.478 +   privateUnlock();
   2.479 +
   2.480 +   return tmp;
   2.481 +#endif
   2.482 +}
   2.483 +
   2.484 +Uint16
   2.485 +SDL_AtomicFetchThenAdd16(Uint16 * ptr, Uint16 value)
   2.486 +{
   2.487 +#ifdef nativeFetchThenAdd16
   2.488 +   return nativeFetchThenAdd16(ptr, value);
   2.489 +#else
   2.490 +   Uint16 tmp = 0;;
   2.491 +
   2.492 +   privateWaitLock();
   2.493 +   tmp = *ptr;
   2.494 +   (*ptr)+= value;
   2.495 +   privateUnlock();
   2.496 +
   2.497 +   return tmp;
   2.498 +#endif
   2.499 +}
   2.500 +
   2.501 +Uint16
   2.502 +SDL_AtomicFetchThenSubtract16(Uint16 * ptr, Uint16 value)
   2.503 +{
   2.504 +#ifdef nativeFetchThenSubtract16
   2.505 +   return nativeFetchThenSubtract16(ptr, value);
   2.506 +#else
   2.507 +   Uint16 tmp = 0;;
   2.508 +
   2.509 +   privateWaitLock();
   2.510 +   tmp = *ptr;
   2.511 +   (*ptr)-= value;
   2.512 +   privateUnlock();
   2.513 +
   2.514 +   return tmp;
   2.515 +#endif
   2.516 +}
   2.517 +
   2.518 +Uint16
   2.519 +SDL_AtomicIncrementThenFetch16(Uint16 * ptr)
   2.520 +{
   2.521 +#ifdef nativeIncrementThenFetch16
   2.522 +   return nativeIncrementThenFetch16(ptr);
   2.523 +#else
   2.524 +   Uint16 tmp = 0;;
   2.525 +
   2.526 +   privateWaitLock();
   2.527 +   (*ptr)+= 1;
   2.528 +   tmp = *ptr;
   2.529 +   privateUnlock();
   2.530 +
   2.531 +   return tmp;
   2.532 +#endif
   2.533 +}
   2.534 +
   2.535 +Uint16
   2.536 +SDL_AtomicDecrementThenFetch16(Uint16 * ptr)
   2.537 +{
   2.538 +#ifdef nativeDecrementThenFetch16
   2.539 +   return nativeDecrementThenFetch16(ptr);
   2.540 +#else
   2.541 +   Uint16 tmp = 0;;
   2.542 +
   2.543 +   privateWaitLock();
   2.544 +   (*ptr)-= 1;
   2.545 +   tmp = *ptr;
   2.546 +   privateUnlock();
   2.547 +
   2.548 +   return tmp;
   2.549 +#endif
   2.550 +}
   2.551 +
   2.552 +Uint16
   2.553 +SDL_AtomicAddThenFetch16(Uint16 * ptr, Uint16 value)
   2.554 +{
   2.555 +#ifdef nativeAddThenFetch16
   2.556 +   return nativeAddThenFetch16(ptr, value);
   2.557 +#else
   2.558 +   Uint16 tmp = 0;;
   2.559 +
   2.560 +   privateWaitLock();
   2.561 +   (*ptr)+= value;
   2.562 +   tmp = *ptr;
   2.563 +   privateUnlock();
   2.564 +
   2.565 +   return tmp;
   2.566 +#endif
   2.567 +}
   2.568 +
   2.569 +Uint16
   2.570 +SDL_AtomicSubtractThenFetch16(Uint16 * ptr, Uint16 value)
   2.571 +{
   2.572 +#ifdef nativeSubtractThenFetch16
   2.573 +   return nativeSubtractThenFetch16(ptr, value);
   2.574 +#else
   2.575 +   Uint16 tmp = 0;;
   2.576 +
   2.577 +   privateWaitLock();
   2.578 +   (*ptr)-= value;
   2.579 +   tmp = *ptr;
   2.580 +   privateUnlock();
   2.581 +
   2.582 +   return tmp;
   2.583 +#endif
   2.584 +}
   2.585 +
   2.586 +/* 32 bit atomic operations */
   2.587 +
   2.588 +Uint32
   2.589 +SDL_AtomicExchange32(Uint32 * ptr, Uint32 value)
   2.590 +{
   2.591 +#ifdef nativeExchange32
   2.592 +   return nativeExchange32(ptr, value);
   2.593 +#else
   2.594 +   Uint32 tmp = 0;;
   2.595 +
   2.596 +   privateWaitLock();
   2.597 +   tmp = *ptr;
   2.598 +   *ptr = value;
   2.599 +   privateUnlock();
   2.600 +
   2.601 +   return tmp;
   2.602 +#endif
   2.603 +}
   2.604 +
   2.605 +SDL_bool
   2.606 +SDL_AtomicCompareThenSet32(Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
   2.607 +{
   2.608 +#ifdef nativeCompareThenSet32
   2.609 +   return (SDL_bool)nativeCompareThenSet32(ptr, oldvalue, newvalue);
   2.610 +#else
   2.611 +   SDL_bool result = SDL_FALSE;
   2.612 +
   2.613 +   privateWaitLock();
   2.614 +   result = (*ptr == oldvalue);
   2.615 +   if (result)
   2.616 +   {
   2.617 +      *ptr = newvalue;
   2.618 +   }
   2.619 +   privateUnlock();
   2.620 +
   2.621 +   return result;
   2.622 +#endif
   2.623 +}
   2.624 +
   2.625 +SDL_bool
   2.626 +SDL_AtomicTestThenSet32(Uint32 * ptr)
   2.627 +{
   2.628 +#ifdef nativeTestThenSet32
   2.629 +   return (SDL_bool)nativeTestThenSet32(ptr);
   2.630 +#else
   2.631 +   SDL_bool result = SDL_FALSE;
   2.632 +
   2.633 +   privateWaitLock();
   2.634 +   result = (*ptr == 0);
   2.635 +   if (result)
   2.636 +   {
   2.637 +      *ptr = 1;
   2.638 +   }
   2.639 +   privateUnlock();
   2.640 +
   2.641 +   return result;
   2.642 +#endif
   2.643 +}
   2.644 +
   2.645 +void
   2.646 +SDL_AtomicClear32(Uint32 * ptr)
   2.647 +{
   2.648 +#ifdef nativeClear32
   2.649 +   nativeClear32(ptr);
   2.650 +#else
   2.651 +   privateWaitLock();
   2.652 +   *ptr = 0;
   2.653 +   privateUnlock();
   2.654 +
   2.655 +   return;
   2.656 +#endif
   2.657 +}
   2.658 +
   2.659 +Uint32
   2.660 +SDL_AtomicFetchThenIncrement32(Uint32 * ptr)
   2.661 +{
   2.662 +#ifdef nativeFetchThenIncrement32
   2.663 +   return nativeFetchThenIncrement32(ptr);
   2.664 +#else
   2.665 +   Uint32 tmp = 0;;
   2.666 +
   2.667 +   privateWaitLock();
   2.668 +   tmp = *ptr;
   2.669 +   (*ptr)+= 1;
   2.670 +   privateUnlock();
   2.671 +
   2.672 +   return tmp;
   2.673 +#endif
   2.674 +}
   2.675 +
   2.676 +Uint32
   2.677 +SDL_AtomicFetchThenDecrement32(Uint32 * ptr)
   2.678 +{
   2.679 +#ifdef nativeFetchThenDecrement32
   2.680 +   return nativeFetchThenDecrement32(ptr);
   2.681 +#else
   2.682 +   Uint32 tmp = 0;;
   2.683 +
   2.684 +   privateWaitLock();
   2.685 +   tmp = *ptr;
   2.686 +   (*ptr) -= 1;
   2.687 +   privateUnlock();
   2.688 +
   2.689 +   return tmp;
   2.690 +#endif
   2.691 +}
   2.692 +
   2.693 +Uint32
   2.694 +SDL_AtomicFetchThenAdd32(Uint32 * ptr, Uint32 value)
   2.695 +{
   2.696 +#ifdef nativeFetchThenAdd32
   2.697 +   return nativeFetchThenAdd32(ptr, value);
   2.698 +#else
   2.699 +   Uint32 tmp = 0;;
   2.700 +
   2.701 +   privateWaitLock();
   2.702 +   tmp = *ptr;
   2.703 +   (*ptr)+= value;
   2.704 +   privateUnlock();
   2.705 +
   2.706 +   return tmp;
   2.707 +#endif
   2.708 +}
   2.709 +
   2.710 +Uint32
   2.711 +SDL_AtomicFetchThenSubtract32(Uint32 * ptr, Uint32 value)
   2.712 +{
   2.713 +#ifdef nativeFetchThenSubtract32
   2.714 +   return nativeFetchThenSubtract32(ptr, value);
   2.715 +#else
   2.716 +   Uint32 tmp = 0;;
   2.717 +
   2.718 +   privateWaitLock();
   2.719 +   tmp = *ptr;
   2.720 +   (*ptr)-= value;
   2.721 +   privateUnlock();
   2.722 +
   2.723 +   return tmp;
   2.724 +#endif
   2.725 +}
   2.726 +
   2.727 +Uint32
   2.728 +SDL_AtomicIncrementThenFetch32(Uint32 * ptr)
   2.729 +{
   2.730 +#ifdef nativeIncrementThenFetch32
   2.731 +   return nativeIncrementThenFetch32(ptr);
   2.732 +#else
   2.733 +   Uint32 tmp = 0;;
   2.734 +
   2.735 +   privateWaitLock();
   2.736 +   (*ptr)+= 1;
   2.737 +   tmp = *ptr;
   2.738 +   privateUnlock();
   2.739 +
   2.740 +   return tmp;
   2.741 +#endif
   2.742 +}
   2.743 +
   2.744 +Uint32
   2.745 +SDL_AtomicDecrementThenFetch32(Uint32 * ptr)
   2.746 +{
   2.747 +#ifdef nativeDecrementThenFetch32
   2.748 +   return nativeDecrementThenFetch32(ptr);
   2.749 +#else
   2.750 +   Uint32 tmp = 0;;
   2.751 +
   2.752 +   privateWaitLock();
   2.753 +   (*ptr)-= 1;
   2.754 +   tmp = *ptr;
   2.755 +   privateUnlock();
   2.756 +
   2.757 +   return tmp;
   2.758 +#endif
   2.759 +}
   2.760 +
   2.761 +Uint32
   2.762 +SDL_AtomicAddThenFetch32(Uint32 * ptr, Uint32 value)
   2.763 +{
   2.764 +#ifdef nativeAddThenFetch32
   2.765 +   return nativeAddThenFetch32(ptr, value);
   2.766 +#else
   2.767 +   Uint32 tmp = 0;;
   2.768 +
   2.769 +   privateWaitLock();
   2.770 +   (*ptr)+= value;
   2.771 +   tmp = *ptr;
   2.772 +   privateUnlock();
   2.773 +
   2.774 +   return tmp;
   2.775 +#endif
   2.776 +}
   2.777 +
   2.778 +Uint32
   2.779 +SDL_AtomicSubtractThenFetch32(Uint32 * ptr, Uint32 value)
   2.780 +{
   2.781 +#ifdef nativeSubtractThenFetch32
   2.782 +   return nativeSubtractThenFetch32(ptr, value);
   2.783 +#else
   2.784 +   Uint32 tmp = 0;;
   2.785 +
   2.786 +   privateWaitLock();
   2.787 +   (*ptr)-= value;
   2.788 +   tmp = *ptr;
   2.789 +   privateUnlock();
   2.790 +
   2.791 +   return tmp;
   2.792 +#endif
   2.793 +}
   2.794 +
   2.795 +/* 64 bit atomic operations */
   2.796 +#ifdef SDL_HAS_64BIT_TYPE
   2.797 +
   2.798 +Uint64
   2.799 +SDL_AtomicExchange64(Uint64 * ptr, Uint64 value)
   2.800 +{
   2.801 +#ifdef nativeExchange64
   2.802 +   return nativeExchange64(ptr, value);
   2.803 +#else
   2.804 +   Uint64 tmp = 0;;
   2.805 +
   2.806 +   privateWaitLock();
   2.807 +   tmp = *ptr;
   2.808 +   *ptr = value;
   2.809 +   privateUnlock();
   2.810 +
   2.811 +   return tmp;
   2.812 +#endif
   2.813 +}
   2.814 +
   2.815 +SDL_bool
   2.816 +SDL_AtomicCompareThenSet64(Uint64 * ptr, Uint64 oldvalue, Uint64 newvalue)
   2.817 +{
   2.818 +#ifdef nativeCompareThenSet64
   2.819 +   return (SDL_bool)nativeCompareThenSet64(ptr, oldvalue, newvalue);
   2.820 +#else
   2.821 +   SDL_bool result = SDL_FALSE;
   2.822 +
   2.823 +   privateWaitLock();
   2.824 +   result = (*ptr == oldvalue);
   2.825 +   if (result)
   2.826 +   {
   2.827 +      *ptr = newvalue;
   2.828 +   }
   2.829 +   privateUnlock();
   2.830 +
   2.831 +   return result;
   2.832 +#endif
   2.833 +}
   2.834 +
   2.835 +SDL_bool
   2.836 +SDL_AtomicTestThenSet64(Uint64 * ptr)
   2.837 +{
   2.838 +#ifdef nativeTestThenSet64
   2.839 +   return (SDL_bool)nativeTestThenSet64(ptr);
   2.840 +#else
   2.841 +   SDL_bool result = SDL_FALSE;
   2.842 +
   2.843 +   privateWaitLock();
   2.844 +   result = (*ptr == 0);
   2.845 +   if (result)
   2.846 +   {
   2.847 +      *ptr = 1;
   2.848 +   }
   2.849 +   privateUnlock();
   2.850 +
   2.851 +   return result;
   2.852 +#endif
   2.853 +}
   2.854 +
   2.855 +void
   2.856 +SDL_AtomicClear64(Uint64 * ptr)
   2.857 +{
   2.858 +#ifdef nativeClear64
   2.859 +   nativeClear64(ptr);
   2.860 +#else
   2.861 +   privateWaitLock();
   2.862 +   *ptr = 0;
   2.863 +   privateUnlock();
   2.864 +
   2.865 +   return;
   2.866 +#endif
   2.867 +}
   2.868 +
   2.869 +Uint64
   2.870 +SDL_AtomicFetchThenIncrement64(Uint64 * ptr)
   2.871 +{
   2.872 +#ifdef nativeFetchThenIncrement64
   2.873 +   return nativeFetchThenIncrement64(ptr);
   2.874 +#else
   2.875 +   Uint64 tmp = 0;;
   2.876 +
   2.877 +   privateWaitLock();
   2.878 +   tmp = *ptr;
   2.879 +   (*ptr)+= 1;
   2.880 +   privateUnlock();
   2.881 +
   2.882 +   return tmp;
   2.883 +#endif
   2.884 +}
   2.885 +
   2.886 +Uint64
   2.887 +SDL_AtomicFetchThenDecrement64(Uint64 * ptr)
   2.888 +{
   2.889 +#ifdef nativeFetchThenDecrement64
   2.890 +   return nativeFetchThenDecrement64(ptr);
   2.891 +#else
   2.892 +   Uint64 tmp = 0;;
   2.893 +
   2.894 +   privateWaitLock();
   2.895 +   tmp = *ptr;
   2.896 +   (*ptr) -= 1;
   2.897 +   privateUnlock();
   2.898 +
   2.899 +   return tmp;
   2.900 +#endif
   2.901 +}
   2.902 +
   2.903 +Uint64
   2.904 +SDL_AtomicFetchThenAdd64(Uint64 * ptr, Uint64 value)
   2.905 +{
   2.906 +#ifdef nativeFetchThenAdd64
   2.907 +   return nativeFetchThenAdd64(ptr, value);
   2.908 +#else
   2.909 +   Uint64 tmp = 0;;
   2.910 +
   2.911 +   privateWaitLock();
   2.912 +   tmp = *ptr;
   2.913 +   (*ptr)+= value;
   2.914 +   privateUnlock();
   2.915 +
   2.916 +   return tmp;
   2.917 +#endif
   2.918 +}
   2.919 +
   2.920 +Uint64
   2.921 +SDL_AtomicFetchThenSubtract64(Uint64 * ptr, Uint64 value)
   2.922 +{
   2.923 +#ifdef nativeFetchThenSubtract64
   2.924 +   return nativeFetchThenSubtract64(ptr, value);
   2.925 +#else
   2.926 +   Uint64 tmp = 0;;
   2.927 +
   2.928 +   privateWaitLock();
   2.929 +   tmp = *ptr;
   2.930 +   (*ptr)-= value;
   2.931 +   privateUnlock();
   2.932 +
   2.933 +   return tmp;
   2.934 +#endif
   2.935 +}
   2.936 +
   2.937 +Uint64
   2.938 +SDL_AtomicIncrementThenFetch64(Uint64 * ptr)
   2.939 +{
   2.940 +#ifdef nativeIncrementThenFetch64
   2.941 +   return nativeIncrementThenFetch64(ptr);
   2.942 +#else
   2.943 +   Uint64 tmp = 0;;
   2.944 +
   2.945 +   privateWaitLock();
   2.946 +   (*ptr)+= 1;
   2.947 +   tmp = *ptr;
   2.948 +   privateUnlock();
   2.949 +
   2.950 +   return tmp;
   2.951 +#endif
   2.952 +}
   2.953 +
   2.954 +Uint64
   2.955 +SDL_AtomicDecrementThenFetch64(Uint64 * ptr)
   2.956 +{
   2.957 +#ifdef nativeDecrementThenFetch64
   2.958 +   return nativeDecrementThenFetch64(ptr);
   2.959 +#else
   2.960 +   Uint64 tmp = 0;;
   2.961 +
   2.962 +   privateWaitLock();
   2.963 +   (*ptr)-= 1;
   2.964 +   tmp = *ptr;
   2.965 +   privateUnlock();
   2.966 +
   2.967 +   return tmp;
   2.968 +#endif
   2.969 +}
   2.970 +
   2.971 +Uint64
   2.972 +SDL_AtomicAddThenFetch64(Uint64 * ptr, Uint64 value)
   2.973 +{
   2.974 +#ifdef nativeAddThenFetch64
   2.975 +   return nativeAddThenFetch64(ptr, value);
   2.976 +#else
   2.977 +   Uint64 tmp = 0;;
   2.978 +
   2.979 +   privateWaitLock();
   2.980 +   (*ptr)+= value;
   2.981 +   tmp = *ptr;
   2.982 +   privateUnlock();
   2.983 +
   2.984 +   return tmp;
   2.985 +#endif
   2.986 +}
   2.987 +
   2.988 +Uint64
   2.989 +SDL_AtomicSubtractThenFetch64(Uint64 * ptr, Uint64 value)
   2.990 +{
   2.991 +#ifdef nativeSubtractThenFetch64
   2.992 +   return nativeSubtractThenFetch64(ptr, value);
   2.993 +#else
   2.994 +   Uint64 tmp = 0;;
   2.995 +
   2.996 +   privateWaitLock();
   2.997 +   (*ptr)-= value;
   2.998 +   tmp = *ptr;
   2.999 +   privateUnlock();
  2.1000 +
  2.1001 +   return tmp;
  2.1002 +#endif
  2.1003 +}
  2.1004 +#endif
  2.1005 +
     3.1 --- a/src/atomic/linux/SDL_atomic.c	Wed Jun 24 22:24:23 2009 +0000
     3.2 +++ b/src/atomic/linux/SDL_atomic.c	Mon Jun 29 19:54:43 2009 +0000
     3.3 @@ -20,154 +20,955 @@
     3.4      slouken@libsdl.org
     3.5  */
     3.6  
     3.7 -#include "SDL.h"
     3.8 -#include "SDL_config.h"
     3.9 +#include "SDL_stdinc.h"
    3.10  #include "SDL_atomic.h"
    3.11  
    3.12 +/*
    3.13 +  This file provides 8, 16, 32, and 64 bit atomic operations. If the
    3.14 +  operations are provided by the native hardware and operating system
    3.15 +  they are used. If they are not then the operations are emulated
    3.16 +  using the SDL mutex operations. 
    3.17 + */
    3.18 +
    3.19 +/* 
    3.20 +  First, detect whether the operations are supported and create
    3.21 +  #defines that indicate that they do exist. The goal is to have all
    3.22 +  the system dependent code in the top part of the file so that the
    3.23 +  bottom can be use unchanged across all platforms.
    3.24 +
    3.25 +  Second, #define all the operations in each size class that are
    3.26 +  supported. Doing this allows supported operations to be used along
    3.27 +  side of emulated operations.
    3.28 +*/
    3.29 +
    3.30 +/* 
    3.31 +   Linux version.
    3.32 +
    3.33 +   Test for gnu C builtin support for atomic operations. The only way
    3.34 +   I know of is to check to see if the
    3.35 +   __GCC_HAVE_SYNC_COMPARE_AND_SWAP_* macros are defined.
    3.36 + */
    3.37 +
    3.38 +#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_1
    3.39 +#define HAVE_ALL_8_BIT_OPS
    3.40 +
    3.41 +#define nativeExchange8(ptr, value)			(__sync_lock_test_and_set(ptr, value))
    3.42 +#define nativeCompareThenSet8(ptr, oldvalue, newvalue) 	(oldvalue == __sync_val_compare_and_swap(ptr, oldvalue, newvalue))
    3.43 +#define nativeTestThenSet8(ptr)    	     		(0 == __sync_lock_test_and_set(ptr, 1))
    3.44 +#define nativeClear8(ptr)				(__sync_lock_release(ptr))
    3.45 +#define nativeFetchThenIncrement8(ptr)   		(__sync_fetch_and_add(ptr, 1))
    3.46 +#define nativeFetchThenDecrement8(ptr) 			(__sync_fetch_and_sub(ptr, 1))
    3.47 +#define nativeFetchThenAdd8(ptr, value) 		(__sync_fetch_and_add(ptr, value))
    3.48 +#define nativeFetchThenSubtract8(ptr, value) 		(__sync_fetch_and_sub(ptr, value))
    3.49 +#define nativeIncrementThenFetch8(ptr) 			(__sync_add_and_fetch(ptr, 1))
    3.50 +#define nativeDecrementThenFetch8(ptr) 			(__sync_sub_and_fetch(ptr, 1))
    3.51 +#define nativeAddThenFetch8(ptr, value) 		(__sync_add_and_fetch(ptr, value))
    3.52 +#define nativeSubtractThenFetch8(ptr, value) 		(__sync_sub_and_fetch(ptr, value))
    3.53 +#endif
    3.54 +
    3.55 +#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
    3.56 +#define HAVE_ALL_16_BIT_OPS
    3.57 +
    3.58 +#define nativeExchange16(ptr, value)			(__sync_lock_test_and_set(ptr, value))
    3.59 +#define nativeCompareThenSet16(ptr, oldvalue, newvalue) (oldvalue == __sync_val_compare_and_swap(ptr, oldvalue, newvalue))
    3.60 +#define nativeTestThenSet16(ptr)    	     		(0 == __sync_lock_test_and_set(ptr, 1))
    3.61 +#define nativeClear16(ptr)				(__sync_lock_release(ptr))
    3.62 +#define nativeFetchThenIncrement16(ptr)   		(__sync_fetch_and_add(ptr, 1))
    3.63 +#define nativeFetchThenDecrement16(ptr) 		(__sync_fetch_and_sub(ptr, 1))
    3.64 +#define nativeFetchThenAdd16(ptr, value) 		(__sync_fetch_and_add(ptr, value))
    3.65 +#define nativeFetchThenSubtract16(ptr, value) 		(__sync_fetch_and_sub(ptr, value))
    3.66 +#define nativeIncrementThenFetch16(ptr) 		(__sync_add_and_fetch(ptr, 1))
    3.67 +#define nativeDecrementThenFetch16(ptr) 		(__sync_sub_and_fetch(ptr, 1))
    3.68 +#define nativeAddThenFetch16(ptr, value) 		(__sync_add_and_fetch(ptr, value))
    3.69 +#define nativeSubtractThenFetch16(ptr, value) 		(__sync_sub_and_fetch(ptr, value))
    3.70 +#endif
    3.71 +
    3.72 +#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
    3.73 +#define HAVE_ALL_32_BIT_OPS
    3.74 +
    3.75 +#define nativeExchange32(ptr, value)			(__sync_lock_test_and_set(ptr, value))
    3.76 +#define nativeCompareThenSet32(ptr, oldvalue, newvalue) (oldvalue == __sync_val_compare_and_swap(ptr, oldvalue, newvalue))
    3.77 +#define nativeTestThenSet32(ptr)    	     		(0 == __sync_lock_test_and_set(ptr, 1))
    3.78 +#define nativeClear32(ptr)				(__sync_lock_release(ptr))
    3.79 +#define nativeFetchThenIncrement32(ptr)   		(__sync_fetch_and_add(ptr, 1))
    3.80 +#define nativeFetchThenDecrement32(ptr) 		(__sync_fetch_and_sub(ptr, 1))
    3.81 +#define nativeFetchThenAdd32(ptr, value) 		(__sync_fetch_and_add(ptr, value))
    3.82 +#define nativeFetchThenSubtract32(ptr, value) 		(__sync_fetch_and_sub(ptr, value))
    3.83 +#define nativeIncrementThenFetch32(ptr) 		(__sync_add_and_fetch(ptr, 1))
    3.84 +#define nativeDecrementThenFetch32(ptr) 		(__sync_sub_and_fetch(ptr, 1))
    3.85 +#define nativeAddThenFetch32(ptr, value) 		(__sync_add_and_fetch(ptr, value))
    3.86 +#define nativeSubtractThenFetch32(ptr, value) 		(__sync_sub_and_fetch(ptr, value))
    3.87 +#endif
    3.88 +
    3.89 +#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
    3.90 +#define HAVE_ALL_64_BIT_OPS
    3.91 +
    3.92 +#define nativeExchange64(ptr, value)			(__sync_lock_test_and_set(ptr, value))
    3.93 +#define nativeCompareThenSet64(ptr, oldvalue, newvalue) (oldvalue == __sync_val_compare_and_swap(ptr, oldvalue, newvalue))
    3.94 +#define nativeTestThenSet64(ptr)    	     		(0 == __sync_lock_test_and_set(ptr, 1))
    3.95 +#define nativeClear64(ptr)				(__sync_lock_release(ptr))
    3.96 +#define nativeFetchThenIncrement64(ptr)   		(__sync_fetch_and_add(ptr, 1))
    3.97 +#define nativeFetchThenDecrement64(ptr) 		(__sync_fetch_and_sub(ptr, 1))
    3.98 +#define nativeFetchThenAdd64(ptr, value) 		(__sync_fetch_and_add(ptr, value))
    3.99 +#define nativeFetchThenSubtract64(ptr, value) 		(__sync_fetch_and_sub(ptr, value))
   3.100 +#define nativeIncrementThenFetch64(ptr) 		(__sync_add_and_fetch(ptr, 1))
   3.101 +#define nativeDecrementThenFetch64(ptr) 		(__sync_sub_and_fetch(ptr, 1))
   3.102 +#define nativeAddThenFetch64(ptr, value) 		(__sync_add_and_fetch(ptr, value))
   3.103 +#define nativeSubtractThenFetch64(ptr, value) 		(__sync_sub_and_fetch(ptr, value))
   3.104 +#endif
   3.105 +
   3.106 +/* 
   3.107 +If any of the operations are not provided then we must emulate some of
   3.108 +them.
   3.109 + */
   3.110 +
   3.111 +#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.112 +
   3.113 +static Uint32 lock = 0;
   3.114 +
   3.115 +#define privateWaitLock()	       \
   3.116 +   while (nativeTestThenSet32(&lock))  \
   3.117 +   {				       \
   3.118 +   };
   3.119 +
   3.120 +#define privateUnlock() (nativeClear32(&lock))
   3.121 +#endif
   3.122 +
   3.123 +/* 8 bit atomic operations */
   3.124 +
   3.125 +Uint8
   3.126 +SDL_AtomicExchange8(Uint8 * ptr, Uint8 value)
   3.127 +{
   3.128 +#ifdef nativeExchange8
   3.129 +   return nativeExchange8(ptr, value);
   3.130 +#else
   3.131 +   Uint8 tmp = 0;;
   3.132 +
   3.133 +   privateWaitLock();
   3.134 +   tmp = *ptr;
   3.135 +   *ptr = value;
   3.136 +   privateUnlock();
   3.137 +
   3.138 +   return tmp;
   3.139 +#endif
   3.140 +}
   3.141 +
   3.142 +SDL_bool
   3.143 +SDL_AtomicCompareThenSet8(Uint8 * ptr, Uint8 oldvalue, Uint8 newvalue)
   3.144 +{
   3.145 +#ifdef nativeCompareThenSet8
   3.146 +   return (SDL_bool)nativeCompareThenSet8(ptr, oldvalue, newvalue);
   3.147 +#else
   3.148 +   SDL_bool result = SDL_FALSE;
   3.149 +
   3.150 +   privateWaitLock();
   3.151 +   result = (*ptr == oldvalue);
   3.152 +   if (result)
   3.153 +   {
   3.154 +      *ptr = newvalue;
   3.155 +   }
   3.156 +   privateUnlock();
   3.157 +
   3.158 +   return result;
   3.159 +#endif
   3.160 +}
   3.161 +
   3.162 +SDL_bool
   3.163 +SDL_AtomicTestThenSet8(Uint8 * ptr)
   3.164 +{
   3.165 +#ifdef nativeTestThenSet8
   3.166 +   return (SDL_bool)nativeTestThenSet8(ptr);
   3.167 +#else
   3.168 +   SDL_bool result = SDL_FALSE;
   3.169 +
   3.170 +   privateWaitLock();
   3.171 +   result = (*ptr == 0);
   3.172 +   if (result)
   3.173 +   {
   3.174 +      *ptr = 1;
   3.175 +   }
   3.176 +   privateUnlock();
   3.177 +
   3.178 +   return result;
   3.179 +#endif
   3.180 +}
   3.181 +
   3.182 +void
   3.183 +SDL_AtomicClear8(Uint8 * ptr)
   3.184 +{
   3.185 +#ifdef nativeClear8
   3.186 +   nativeClear8(ptr);
   3.187 +#else
   3.188 +   privateWaitLock();
   3.189 +   *ptr = 0;
   3.190 +   privateUnlock();
   3.191 +
   3.192 +   return;
   3.193 +#endif
   3.194 +}
   3.195 +
   3.196 +Uint8
   3.197 +SDL_AtomicFetchThenIncrement8(Uint8 * ptr)
   3.198 +{
   3.199 +#ifdef nativeFetchThenIncrement8
   3.200 +   return nativeFetchThenIncrement8(ptr);
   3.201 +#else
   3.202 +   Uint8 tmp = 0;;
   3.203 +
   3.204 +   privateWaitLock();
   3.205 +   tmp = *ptr;
   3.206 +   (*ptr)+= 1;
   3.207 +   privateUnlock();
   3.208 +
   3.209 +   return tmp;
   3.210 +#endif
   3.211 +}
   3.212 +
   3.213 +Uint8
   3.214 +SDL_AtomicFetchThenDecrement8(Uint8 * ptr)
   3.215 +{
   3.216 +#ifdef nativeFetchThenDecrement8
   3.217 +   return nativeFetchThenDecrement8(ptr);
   3.218 +#else
   3.219 +   Uint8 tmp = 0;;
   3.220 +
   3.221 +   privateWaitLock();
   3.222 +   tmp = *ptr;
   3.223 +   (*ptr) -= 1;
   3.224 +   privateUnlock();
   3.225 +
   3.226 +   return tmp;
   3.227 +#endif
   3.228 +}
   3.229 +
   3.230 +Uint8
   3.231 +SDL_AtomicFetchThenAdd8(Uint8 * ptr, Uint8 value)
   3.232 +{
   3.233 +#ifdef nativeFetchThenAdd8
   3.234 +   return nativeFetchThenAdd8(ptr, value);
   3.235 +#else
   3.236 +   Uint8 tmp = 0;;
   3.237 +
   3.238 +   privateWaitLock();
   3.239 +   tmp = *ptr;
   3.240 +   (*ptr)+= value;
   3.241 +   privateUnlock();
   3.242 +
   3.243 +   return tmp;
   3.244 +#endif
   3.245 +}
   3.246 +
   3.247 +Uint8
   3.248 +SDL_AtomicFetchThenSubtract8(Uint8 * ptr, Uint8 value)
   3.249 +{
   3.250 +#ifdef nativeFetchThenSubtract8
   3.251 +   return nativeFetchThenSubtract8(ptr, value);
   3.252 +#else
   3.253 +   Uint8 tmp = 0;;
   3.254 +
   3.255 +   privateWaitLock();
   3.256 +   tmp = *ptr;
   3.257 +   (*ptr)-= value;
   3.258 +   privateUnlock();
   3.259 +
   3.260 +   return tmp;
   3.261 +#endif
   3.262 +}
   3.263 +
   3.264 +Uint8
   3.265 +SDL_AtomicIncrementThenFetch8(Uint8 * ptr)
   3.266 +{
   3.267 +#ifdef nativeIncrementThenFetch8
   3.268 +   return nativeIncrementThenFetch8(ptr);
   3.269 +#else
   3.270 +   Uint8 tmp = 0;;
   3.271 +
   3.272 +   privateWaitLock();
   3.273 +   (*ptr)+= 1;
   3.274 +   tmp = *ptr;
   3.275 +   privateUnlock();
   3.276 +
   3.277 +   return tmp;
   3.278 +#endif
   3.279 +}
   3.280 +
   3.281 +Uint8
   3.282 +SDL_AtomicDecrementThenFetch8(Uint8 * ptr)
   3.283 +{
   3.284 +#ifdef nativeDecrementThenFetch8
   3.285 +   return nativeDecrementThenFetch8(ptr);
   3.286 +#else
   3.287 +   Uint8 tmp = 0;;
   3.288 +
   3.289 +   privateWaitLock();
   3.290 +   (*ptr)-= 1;
   3.291 +   tmp = *ptr;
   3.292 +   privateUnlock();
   3.293 +
   3.294 +   return tmp;
   3.295 +#endif
   3.296 +}
   3.297 +
   3.298 +Uint8
   3.299 +SDL_AtomicAddThenFetch8(Uint8 * ptr, Uint8 value)
   3.300 +{
   3.301 +#ifdef nativeAddThenFetch8
   3.302 +   return nativeAddThenFetch8(ptr, value);
   3.303 +#else
   3.304 +   Uint8 tmp = 0;;
   3.305 +
   3.306 +   privateWaitLock();
   3.307 +   (*ptr)+= value;
   3.308 +   tmp = *ptr;
   3.309 +   privateUnlock();
   3.310 +
   3.311 +   return tmp;
   3.312 +#endif
   3.313 +}
   3.314 +
   3.315 +Uint8
   3.316 +SDL_AtomicSubtractThenFetch8(Uint8 * ptr, Uint8 value)
   3.317 +{
   3.318 +#ifdef nativeSubtractThenFetch8
   3.319 +   return nativeSubtractThenFetch8(ptr, value);
   3.320 +#else
   3.321 +   Uint8 tmp = 0;;
   3.322 +
   3.323 +   privateWaitLock();
   3.324 +   (*ptr)-= value;
   3.325 +   tmp = *ptr;
   3.326 +   privateUnlock();
   3.327 +
   3.328 +   return tmp;
   3.329 +#endif
   3.330 +}
   3.331 +
   3.332 +/* 16 bit atomic operations */
   3.333 +
   3.334 +Uint16
   3.335 +SDL_AtomicExchange16(Uint16 * ptr, Uint16 value)
   3.336 +{
   3.337 +#ifdef nativeExchange16
   3.338 +   return nativeExchange16(ptr, value);
   3.339 +#else
   3.340 +   Uint16 tmp = 0;;
   3.341 +
   3.342 +   privateWaitLock();
   3.343 +   tmp = *ptr;
   3.344 +   *ptr = value;
   3.345 +   privateUnlock();
   3.346 +
   3.347 +   return tmp;
   3.348 +#endif
   3.349 +}
   3.350 +
   3.351 +SDL_bool
   3.352 +SDL_AtomicCompareThenSet16(Uint16 * ptr, Uint16 oldvalue, Uint16 newvalue)
   3.353 +{
   3.354 +#ifdef nativeCompareThenSet16
   3.355 +   return (SDL_bool)nativeCompareThenSet16(ptr, oldvalue, newvalue);
   3.356 +#else
   3.357 +   SDL_bool result = SDL_FALSE;
   3.358 +
   3.359 +   privateWaitLock();
   3.360 +   result = (*ptr == oldvalue);
   3.361 +   if (result)
   3.362 +   {
   3.363 +      *ptr = newvalue;
   3.364 +   }
   3.365 +   privateUnlock();
   3.366 +
   3.367 +   return result;
   3.368 +#endif
   3.369 +}
   3.370 +
   3.371 +SDL_bool
   3.372 +SDL_AtomicTestThenSet16(Uint16 * ptr)
   3.373 +{
   3.374 +#ifdef nativeTestThenSet16
   3.375 +   return (SDL_bool)nativeTestThenSet16(ptr);
   3.376 +#else
   3.377 +   SDL_bool result = SDL_FALSE;
   3.378 +
   3.379 +   privateWaitLock();
   3.380 +   result = (*ptr == 0);
   3.381 +   if (result)
   3.382 +   {
   3.383 +      *ptr = 1;
   3.384 +   }
   3.385 +   privateUnlock();
   3.386 +
   3.387 +   return result;
   3.388 +#endif
   3.389 +}
   3.390 +
   3.391 +void
   3.392 +SDL_AtomicClear16(Uint16 * ptr)
   3.393 +{
   3.394 +#ifdef nativeClear16
   3.395 +   nativeClear16(ptr);
   3.396 +#else
   3.397 +   privateWaitLock();
   3.398 +   *ptr = 0;
   3.399 +   privateUnlock();
   3.400 +
   3.401 +   return;
   3.402 +#endif
   3.403 +}
   3.404 +
   3.405 +Uint16
   3.406 +SDL_AtomicFetchThenIncrement16(Uint16 * ptr)
   3.407 +{
   3.408 +#ifdef nativeFetchThenIncrement16
   3.409 +   return nativeFetchThenIncrement16(ptr);
   3.410 +#else
   3.411 +   Uint16 tmp = 0;;
   3.412 +
   3.413 +   privateWaitLock();
   3.414 +   tmp = *ptr;
   3.415 +   (*ptr)+= 1;
   3.416 +   privateUnlock();
   3.417 +
   3.418 +   return tmp;
   3.419 +#endif
   3.420 +}
   3.421 +
   3.422 +Uint16
   3.423 +SDL_AtomicFetchThenDecrement16(Uint16 * ptr)
   3.424 +{
   3.425 +#ifdef nativeFetchThenDecrement16
   3.426 +   return nativeFetchThenDecrement16(ptr);
   3.427 +#else
   3.428 +   Uint16 tmp = 0;;
   3.429 +
   3.430 +   privateWaitLock();
   3.431 +   tmp = *ptr;
   3.432 +   (*ptr) -= 1;
   3.433 +   privateUnlock();
   3.434 +
   3.435 +   return tmp;
   3.436 +#endif
   3.437 +}
   3.438 +
   3.439 +Uint16
   3.440 +SDL_AtomicFetchThenAdd16(Uint16 * ptr, Uint16 value)
   3.441 +{
   3.442 +#ifdef nativeFetchThenAdd16
   3.443 +   return nativeFetchThenAdd16(ptr, value);
   3.444 +#else
   3.445 +   Uint16 tmp = 0;;
   3.446 +
   3.447 +   privateWaitLock();
   3.448 +   tmp = *ptr;
   3.449 +   (*ptr)+= value;
   3.450 +   privateUnlock();
   3.451 +
   3.452 +   return tmp;
   3.453 +#endif
   3.454 +}
   3.455 +
   3.456 +Uint16
   3.457 +SDL_AtomicFetchThenSubtract16(Uint16 * ptr, Uint16 value)
   3.458 +{
   3.459 +#ifdef nativeFetchThenSubtract16
   3.460 +   return nativeFetchThenSubtract16(ptr, value);
   3.461 +#else
   3.462 +   Uint16 tmp = 0;;
   3.463 +
   3.464 +   privateWaitLock();
   3.465 +   tmp = *ptr;
   3.466 +   (*ptr)-= value;
   3.467 +   privateUnlock();
   3.468 +
   3.469 +   return tmp;
   3.470 +#endif
   3.471 +}
   3.472 +
   3.473 +Uint16
   3.474 +SDL_AtomicIncrementThenFetch16(Uint16 * ptr)
   3.475 +{
   3.476 +#ifdef nativeIncrementThenFetch16
   3.477 +   return nativeIncrementThenFetch16(ptr);
   3.478 +#else
   3.479 +   Uint16 tmp = 0;;
   3.480 +
   3.481 +   privateWaitLock();
   3.482 +   (*ptr)+= 1;
   3.483 +   tmp = *ptr;
   3.484 +   privateUnlock();
   3.485 +
   3.486 +   return tmp;
   3.487 +#endif
   3.488 +}
   3.489 +
   3.490 +Uint16
   3.491 +SDL_AtomicDecrementThenFetch16(Uint16 * ptr)
   3.492 +{
   3.493 +#ifdef nativeDecrementThenFetch16
   3.494 +   return nativeDecrementThenFetch16(ptr);
   3.495 +#else
   3.496 +   Uint16 tmp = 0;;
   3.497 +
   3.498 +   privateWaitLock();
   3.499 +   (*ptr)-= 1;
   3.500 +   tmp = *ptr;
   3.501 +   privateUnlock();
   3.502 +
   3.503 +   return tmp;
   3.504 +#endif
   3.505 +}
   3.506 +
   3.507 +Uint16
   3.508 +SDL_AtomicAddThenFetch16(Uint16 * ptr, Uint16 value)
   3.509 +{
   3.510 +#ifdef nativeAddThenFetch16
   3.511 +   return nativeAddThenFetch16(ptr, value);
   3.512 +#else
   3.513 +   Uint16 tmp = 0;;
   3.514 +
   3.515 +   privateWaitLock();
   3.516 +   (*ptr)+= value;
   3.517 +   tmp = *ptr;
   3.518 +   privateUnlock();
   3.519 +
   3.520 +   return tmp;
   3.521 +#endif
   3.522 +}
   3.523 +
   3.524 +Uint16
   3.525 +SDL_AtomicSubtractThenFetch16(Uint16 * ptr, Uint16 value)
   3.526 +{
   3.527 +#ifdef nativeSubtractThenFetch16
   3.528 +   return nativeSubtractThenFetch16(ptr, value);
   3.529 +#else
   3.530 +   Uint16 tmp = 0;;
   3.531 +
   3.532 +   privateWaitLock();
   3.533 +   (*ptr)-= value;
   3.534 +   tmp = *ptr;
   3.535 +   privateUnlock();
   3.536 +
   3.537 +   return tmp;
   3.538 +#endif
   3.539 +}
   3.540 +
   3.541 +/* 32 bit atomic operations */
   3.542 +
   3.543  Uint32
   3.544  SDL_AtomicExchange32(Uint32 * ptr, Uint32 value)
   3.545  {
   3.546 -   return __sync_lock_test_and_set(ptr, value);
   3.547 +#ifdef nativeExchange32
   3.548 +   return nativeExchange32(ptr, value);
   3.549 +#else
   3.550 +   Uint32 tmp = 0;;
   3.551 +
   3.552 +   privateWaitLock();
   3.553 +   tmp = *ptr;
   3.554 +   *ptr = value;
   3.555 +   privateUnlock();
   3.556 +
   3.557 +   return tmp;
   3.558 +#endif
   3.559  }
   3.560  
   3.561  SDL_bool
   3.562  SDL_AtomicCompareThenSet32(Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
   3.563  {
   3.564 -   return (SDL_bool)__sync_bool_compare_and_swap(ptr, oldvalue, newvalue);
   3.565 +#ifdef nativeCompareThenSet32
   3.566 +   return (SDL_bool)nativeCompareThenSet32(ptr, oldvalue, newvalue);
   3.567 +#else
   3.568 +   SDL_bool result = SDL_FALSE;
   3.569 +
   3.570 +   privateWaitLock();
   3.571 +   result = (*ptr == oldvalue);
   3.572 +   if (result)
   3.573 +   {
   3.574 +      *ptr = newvalue;
   3.575 +   }
   3.576 +   privateUnlock();
   3.577 +
   3.578 +   return result;
   3.579 +#endif
   3.580  }
   3.581  
   3.582  SDL_bool
   3.583  SDL_AtomicTestThenSet32(Uint32 * ptr)
   3.584  {
   3.585 -   return (SDL_bool)(0 == __sync_lock_test_and_set(ptr, 1));
   3.586 +#ifdef nativeTestThenSet32
   3.587 +   return (SDL_bool)nativeTestThenSet32(ptr);
   3.588 +#else
   3.589 +   SDL_bool result = SDL_FALSE;
   3.590 +
   3.591 +   privateWaitLock();
   3.592 +   result = (*ptr == 0);
   3.593 +   if (result)
   3.594 +   {
   3.595 +      *ptr = 1;
   3.596 +   }
   3.597 +   privateUnlock();
   3.598 +
   3.599 +   return result;
   3.600 +#endif
   3.601  }
   3.602  
   3.603  void
   3.604  SDL_AtomicClear32(Uint32 * ptr)
   3.605  {
   3.606 -   __sync_lock_release(ptr);
   3.607 +#ifdef nativeClear32
   3.608 +   nativeClear32(ptr);
   3.609 +#else
   3.610 +   privateWaitLock();
   3.611 +   *ptr = 0;
   3.612 +   privateUnlock();
   3.613 +
   3.614 +   return;
   3.615 +#endif
   3.616  }
   3.617  
   3.618  Uint32
   3.619  SDL_AtomicFetchThenIncrement32(Uint32 * ptr)
   3.620  {
   3.621 -   return __sync_fetch_and_add(ptr, 1);
   3.622 +#ifdef nativeFetchThenIncrement32
   3.623 +   return nativeFetchThenIncrement32(ptr);
   3.624 +#else
   3.625 +   Uint32 tmp = 0;;
   3.626 +
   3.627 +   privateWaitLock();
   3.628 +   tmp = *ptr;
   3.629 +   (*ptr)+= 1;
   3.630 +   privateUnlock();
   3.631 +
   3.632 +   return tmp;
   3.633 +#endif
   3.634  }
   3.635  
   3.636  Uint32
   3.637  SDL_AtomicFetchThenDecrement32(Uint32 * ptr)
   3.638  {
   3.639 -   return __sync_fetch_and_sub(ptr, 1);
   3.640 +#ifdef nativeFetchThenDecrement32
   3.641 +   return nativeFetchThenDecrement32(ptr);
   3.642 +#else
   3.643 +   Uint32 tmp = 0;;
   3.644 +
   3.645 +   privateWaitLock();
   3.646 +   tmp = *ptr;
   3.647 +   (*ptr) -= 1;
   3.648 +   privateUnlock();
   3.649 +
   3.650 +   return tmp;
   3.651 +#endif
   3.652  }
   3.653  
   3.654  Uint32
   3.655  SDL_AtomicFetchThenAdd32(Uint32 * ptr, Uint32 value)
   3.656  {
   3.657 -   return __sync_fetch_and_add(ptr, value);
   3.658 +#ifdef nativeFetchThenAdd32
   3.659 +   return nativeFetchThenAdd32(ptr, value);
   3.660 +#else
   3.661 +   Uint32 tmp = 0;;
   3.662 +
   3.663 +   privateWaitLock();
   3.664 +   tmp = *ptr;
   3.665 +   (*ptr)+= value;
   3.666 +   privateUnlock();
   3.667 +
   3.668 +   return tmp;
   3.669 +#endif
   3.670  }
   3.671  
   3.672  Uint32
   3.673  SDL_AtomicFetchThenSubtract32(Uint32 * ptr, Uint32 value)
   3.674  {
   3.675 -   return __sync_fetch_and_sub(ptr, value);
   3.676 +#ifdef nativeFetchThenSubtract32
   3.677 +   return nativeFetchThenSubtract32(ptr, value);
   3.678 +#else
   3.679 +   Uint32 tmp = 0;;
   3.680 +
   3.681 +   privateWaitLock();
   3.682 +   tmp = *ptr;
   3.683 +   (*ptr)-= value;
   3.684 +   privateUnlock();
   3.685 +
   3.686 +   return tmp;
   3.687 +#endif
   3.688  }
   3.689  
   3.690  Uint32
   3.691  SDL_AtomicIncrementThenFetch32(Uint32 * ptr)
   3.692  {
   3.693 -   return __sync_add_and_fetch(ptr, 1);
   3.694 +#ifdef nativeIncrementThenFetch32
   3.695 +   return nativeIncrementThenFetch32(ptr);
   3.696 +#else
   3.697 +   Uint32 tmp = 0;;
   3.698 +
   3.699 +   privateWaitLock();
   3.700 +   (*ptr)+= 1;
   3.701 +   tmp = *ptr;
   3.702 +   privateUnlock();
   3.703 +
   3.704 +   return tmp;
   3.705 +#endif
   3.706  }
   3.707  
   3.708  Uint32
   3.709  SDL_AtomicDecrementThenFetch32(Uint32 * ptr)
   3.710  {
   3.711 -   return __sync_sub_and_fetch(ptr, 1);
   3.712 +#ifdef nativeDecrementThenFetch32
   3.713 +   return nativeDecrementThenFetch32(ptr);
   3.714 +#else
   3.715 +   Uint32 tmp = 0;;
   3.716 +
   3.717 +   privateWaitLock();
   3.718 +   (*ptr)-= 1;
   3.719 +   tmp = *ptr;
   3.720 +   privateUnlock();
   3.721 +
   3.722 +   return tmp;
   3.723 +#endif
   3.724  }
   3.725  
   3.726  Uint32
   3.727  SDL_AtomicAddThenFetch32(Uint32 * ptr, Uint32 value)
   3.728  {
   3.729 -   return __sync_add_and_fetch(ptr, value);
   3.730 +#ifdef nativeAddThenFetch32
   3.731 +   return nativeAddThenFetch32(ptr, value);
   3.732 +#else
   3.733 +   Uint32 tmp = 0;;
   3.734 +
   3.735 +   privateWaitLock();
   3.736 +   (*ptr)+= value;
   3.737 +   tmp = *ptr;
   3.738 +   privateUnlock();
   3.739 +
   3.740 +   return tmp;
   3.741 +#endif
   3.742  }
   3.743  
   3.744  Uint32
   3.745  SDL_AtomicSubtractThenFetch32(Uint32 * ptr, Uint32 value)
   3.746  {
   3.747 -   return __sync_sub_and_fetch(ptr, value);
   3.748 +#ifdef nativeSubtractThenFetch32
   3.749 +   return nativeSubtractThenFetch32(ptr, value);
   3.750 +#else
   3.751 +   Uint32 tmp = 0;;
   3.752 +
   3.753 +   privateWaitLock();
   3.754 +   (*ptr)-= value;
   3.755 +   tmp = *ptr;
   3.756 +   privateUnlock();
   3.757 +
   3.758 +   return tmp;
   3.759 +#endif
   3.760  }
   3.761  
   3.762 -/* #ifdef SDL_HAS_64BIT_TYPE */
   3.763 -#if 0
   3.764 +/* 64 bit atomic operations */
   3.765 +#ifdef SDL_HAS_64BIT_TYPE
   3.766  
   3.767  Uint64
   3.768  SDL_AtomicExchange64(Uint64 * ptr, Uint64 value)
   3.769  {
   3.770 -   return __sync_lock_test_and_set(ptr, value);
   3.771 +#ifdef nativeExchange64
   3.772 +   return nativeExchange64(ptr, value);
   3.773 +#else
   3.774 +   Uint64 tmp = 0;;
   3.775 +
   3.776 +   privateWaitLock();
   3.777 +   tmp = *ptr;
   3.778 +   *ptr = value;
   3.779 +   privateUnlock();
   3.780 +
   3.781 +   return tmp;
   3.782 +#endif
   3.783  }
   3.784  
   3.785  SDL_bool
   3.786  SDL_AtomicCompareThenSet64(Uint64 * ptr, Uint64 oldvalue, Uint64 newvalue)
   3.787  {
   3.788 -   return (SDL_bool)__sync_bool_compare_and_swap(ptr, oldvalue, newvalue);
   3.789 +#ifdef nativeCompareThenSet64
   3.790 +   return (SDL_bool)nativeCompareThenSet64(ptr, oldvalue, newvalue);
   3.791 +#else
   3.792 +   SDL_bool result = SDL_FALSE;
   3.793 +
   3.794 +   privateWaitLock();
   3.795 +   result = (*ptr == oldvalue);
   3.796 +   if (result)
   3.797 +   {
   3.798 +      *ptr = newvalue;
   3.799 +   }
   3.800 +   privateUnlock();
   3.801 +
   3.802 +   return result;
   3.803 +#endif
   3.804  }
   3.805  
   3.806  SDL_bool
   3.807  SDL_AtomicTestThenSet64(Uint64 * ptr)
   3.808  {
   3.809 -   return (SDL_bool)(0 == __sync_lock_test_and_set(ptr, 1));
   3.810 +#ifdef nativeTestThenSet64
   3.811 +   return (SDL_bool)nativeTestThenSet64(ptr);
   3.812 +#else
   3.813 +   SDL_bool result = SDL_FALSE;
   3.814 +
   3.815 +   privateWaitLock();
   3.816 +   result = (*ptr == 0);
   3.817 +   if (result)
   3.818 +   {
   3.819 +      *ptr = 1;
   3.820 +   }
   3.821 +   privateUnlock();
   3.822 +
   3.823 +   return result;
   3.824 +#endif
   3.825  }
   3.826  
   3.827  void
   3.828  SDL_AtomicClear64(Uint64 * ptr)
   3.829  {
   3.830 -   __sync_lock_release(ptr);
   3.831 +#ifdef nativeClear64
   3.832 +   nativeClear64(ptr);
   3.833 +#else
   3.834 +   privateWaitLock();
   3.835 +   *ptr = 0;
   3.836 +   privateUnlock();
   3.837 +
   3.838 +   return;
   3.839 +#endif
   3.840  }
   3.841  
   3.842  Uint64
   3.843  SDL_AtomicFetchThenIncrement64(Uint64 * ptr)
   3.844  {
   3.845 -   return __sync_fetch_and_add(ptr, 1);
   3.846 +#ifdef nativeFetchThenIncrement64
   3.847 +   return nativeFetchThenIncrement64(ptr);
   3.848 +#else
   3.849 +   Uint64 tmp = 0;;
   3.850 +
   3.851 +   privateWaitLock();
   3.852 +   tmp = *ptr;
   3.853 +   (*ptr)+= 1;
   3.854 +   privateUnlock();
   3.855 +
   3.856 +   return tmp;
   3.857 +#endif
   3.858  }
   3.859  
   3.860  Uint64
   3.861  SDL_AtomicFetchThenDecrement64(Uint64 * ptr)
   3.862  {
   3.863 -   return __sync_fetch_and_sub(ptr, 1);
   3.864 +#ifdef nativeFetchThenDecrement64
   3.865 +   return nativeFetchThenDecrement64(ptr);
   3.866 +#else
   3.867 +   Uint64 tmp = 0;;
   3.868 +
   3.869 +   privateWaitLock();
   3.870 +   tmp = *ptr;
   3.871 +   (*ptr) -= 1;
   3.872 +   privateUnlock();
   3.873 +
   3.874 +   return tmp;
   3.875 +#endif
   3.876  }
   3.877  
   3.878  Uint64
   3.879  SDL_AtomicFetchThenAdd64(Uint64 * ptr, Uint64 value)
   3.880  {
   3.881 -   return __sync_fetch_and_add(ptr, value);
   3.882 +#ifdef nativeFetchThenAdd64
   3.883 +   return nativeFetchThenAdd64(ptr, value);
   3.884 +#else
   3.885 +   Uint64 tmp = 0;;
   3.886 +
   3.887 +   privateWaitLock();
   3.888 +   tmp = *ptr;
   3.889 +   (*ptr)+= value;
   3.890 +   privateUnlock();
   3.891 +
   3.892 +   return tmp;
   3.893 +#endif
   3.894  }
   3.895  
   3.896  Uint64
   3.897  SDL_AtomicFetchThenSubtract64(Uint64 * ptr, Uint64 value)
   3.898  {
   3.899 -   return __sync_fetch_and_sub(ptr, value);
   3.900 +#ifdef nativeFetchThenSubtract64
   3.901 +   return nativeFetchThenSubtract64(ptr, value);
   3.902 +#else
   3.903 +   Uint64 tmp = 0;;
   3.904 +
   3.905 +   privateWaitLock();
   3.906 +   tmp = *ptr;
   3.907 +   (*ptr)-= value;
   3.908 +   privateUnlock();
   3.909 +
   3.910 +   return tmp;
   3.911 +#endif
   3.912  }
   3.913  
   3.914  Uint64
   3.915  SDL_AtomicIncrementThenFetch64(Uint64 * ptr)
   3.916  {
   3.917 -   return __sync_add_and_fetch(ptr, 1);
   3.918 +#ifdef nativeIncrementThenFetch64
   3.919 +   return nativeIncrementThenFetch64(ptr);
   3.920 +#else
   3.921 +   Uint64 tmp = 0;;
   3.922 +
   3.923 +   privateWaitLock();
   3.924 +   (*ptr)+= 1;
   3.925 +   tmp = *ptr;
   3.926 +   privateUnlock();
   3.927 +
   3.928 +   return tmp;
   3.929 +#endif
   3.930  }
   3.931  
   3.932  Uint64
   3.933  SDL_AtomicDecrementThenFetch64(Uint64 * ptr)
   3.934  {
   3.935 -   return __sync_sub_and_fetch(ptr, 1);
   3.936 +#ifdef nativeDecrementThenFetch64
   3.937 +   return nativeDecrementThenFetch64(ptr);
   3.938 +#else
   3.939 +   Uint64 tmp = 0;;
   3.940 +
   3.941 +   privateWaitLock();
   3.942 +   (*ptr)-= 1;
   3.943 +   tmp = *ptr;
   3.944 +   privateUnlock();
   3.945 +
   3.946 +   return tmp;
   3.947 +#endif
   3.948  }
   3.949  
   3.950  Uint64
   3.951  SDL_AtomicAddThenFetch64(Uint64 * ptr, Uint64 value)
   3.952  {
   3.953 -   return __sync_add_and_fetch(ptr, value);
   3.954 +#ifdef nativeAddThenFetch64
   3.955 +   return nativeAddThenFetch64(ptr, value);
   3.956 +#else
   3.957 +   Uint64 tmp = 0;;
   3.958 +
   3.959 +   privateWaitLock();
   3.960 +   (*ptr)+= value;
   3.961 +   tmp = *ptr;
   3.962 +   privateUnlock();
   3.963 +
   3.964 +   return tmp;
   3.965 +#endif
   3.966  }
   3.967  
   3.968  Uint64
   3.969  SDL_AtomicSubtractThenFetch64(Uint64 * ptr, Uint64 value)
   3.970  {
   3.971 -   return __sync_sub_and_fetch(ptr, value);
   3.972 +#ifdef nativeSubtractThenFetch64
   3.973 +   return nativeSubtractThenFetch64(ptr, value);
   3.974 +#else
   3.975 +   Uint64 tmp = 0;;
   3.976 +
   3.977 +   privateWaitLock();
   3.978 +   (*ptr)-= value;
   3.979 +   tmp = *ptr;
   3.980 +   privateUnlock();
   3.981 +
   3.982 +   return tmp;
   3.983 +#endif
   3.984  }
   3.985  #endif
   3.986 +
     4.1 --- a/src/atomic/macosx/SDL_atomic.c	Wed Jun 24 22:24:23 2009 +0000
     4.2 +++ b/src/atomic/macosx/SDL_atomic.c	Mon Jun 29 19:54:43 2009 +0000
     4.3 @@ -20,157 +20,983 @@
     4.4      slouken@libsdl.org
     4.5  */
     4.6  
     4.7 -#ifdef SDL_ATOMIC_LINUX
     4.8 +#include "SDL_stdinc.h"
     4.9 +#include "SDL_atomic.h"
    4.10  
    4.11 -#include "SDL.h"
    4.12 -#include "SDL_config.h"
    4.13 -#include "SDL_atomic.h"
    4.14 +/*
    4.15 +  This file provides 8, 16, 32, and 64 bit atomic operations. If the
    4.16 +  operations are provided by the native hardware and operating system
    4.17 +  they are used. If they are not then the operations are emulated
    4.18 +  using the SDL mutex operations. 
    4.19 + */
    4.20 +
    4.21 +/* 
    4.22 +  First, detect whether the operations are supported and create
    4.23 +  #defines that indicate that they do exist. The goal is to have all
    4.24 +  the system dependent code in the top part of the file so that the
    4.25 +  bottom can be use unchanged across all platforms.
    4.26 +
    4.27 +  Second, #define all the operations in each size class that are
    4.28 +  supported. Doing this allows supported operations to be used along
    4.29 +  side of emulated operations.
    4.30 +*/
    4.31 +
    4.32 +/* 
    4.33 +   Emmulated version.
    4.34 +
    4.35 +   Assume there is no support for atomic operations. All such
    4.36 +   operations are implemented using SDL mutex operations.
    4.37 + */
    4.38 +
    4.39 +#ifdef EMULATED_ATOMIC_OPERATIONS
    4.40 +#undef EMULATED_ATOMIC_OPERATIONS
    4.41 +#endif
    4.42 +
    4.43 +#ifdef EMULATED_ATOMIC_OPERATIONS
    4.44 +#define HAVE_ALL_8_BIT_OPS
    4.45 +
    4.46 +#define nativeExchange8(ptr, value)			()
    4.47 +#define nativeCompareThenSet8(ptr, oldvalue, newvalue) 	()
    4.48 +#define nativeTestThenSet8(ptr)    	     		()
    4.49 +#define nativeClear8(ptr)				()
    4.50 +#define nativeFetchThenIncrement8(ptr)   		()
    4.51 +#define nativeFetchThenDecrement8(ptr) 			()
    4.52 +#define nativeFetchThenAdd8(ptr, value) 		()
    4.53 +#define nativeFetchThenSubtract8(ptr, value) 		()
    4.54 +#define nativeIncrementThenFetch8(ptr) 			()
    4.55 +#define nativeDecrementThenFetch8(ptr) 			()
    4.56 +#define nativeAddThenFetch8(ptr, value) 		()
    4.57 +#define nativeSubtractThenFetch8(ptr, value) 		()
    4.58 +#endif
    4.59 +
    4.60 +#ifdef EMULATED_ATOMIC_OPERATIONS
    4.61 +#define HAVE_ALL_16_BIT_OPS
    4.62 +
    4.63 +#define nativeExchange16(ptr, value)			()
    4.64 +#define nativeCompareThenSet16(ptr, oldvalue, newvalue) ()
    4.65 +#define nativeTestThenSet16(ptr)    	     		()
    4.66 +#define nativeClear16(ptr)				()
    4.67 +#define nativeFetchThenIncrement16(ptr)   		()
    4.68 +#define nativeFetchThenDecrement16(ptr) 		()
    4.69 +#define nativeFetchThenAdd16(ptr, value) 		()
    4.70 +#define nativeFetchThenSubtract16(ptr, value) 		()
    4.71 +#define nativeIncrementThenFetch16(ptr) 		()
    4.72 +#define nativeDecrementThenFetch16(ptr) 		()
    4.73 +#define nativeAddThenFetch16(ptr, value) 		()
    4.74 +#define nativeSubtractThenFetch16(ptr, value) 		()
    4.75 +#endif
    4.76 +
    4.77 +#ifdef EMULATED_ATOMIC_OPERATIONS
    4.78 +#define HAVE_ALL_32_BIT_OPS
    4.79 +
    4.80 +#define nativeExchange32(ptr, value)			()
    4.81 +#define nativeCompareThenSet32(ptr, oldvalue, newvalue) ()
    4.82 +#define nativeTestThenSet32(ptr)    	     		()
    4.83 +#define nativeClear32(ptr)				()
    4.84 +#define nativeFetchThenIncrement32(ptr)   		()
    4.85 +#define nativeFetchThenDecrement32(ptr) 		()
    4.86 +#define nativeFetchThenAdd32(ptr, value) 		()
    4.87 +#define nativeFetchThenSubtract32(ptr, value) 		()
    4.88 +#define nativeIncrementThenFetch32(ptr) 		()
    4.89 +#define nativeDecrementThenFetch32(ptr) 		()
    4.90 +#define nativeAddThenFetch32(ptr, value) 		()
    4.91 +#define nativeSubtractThenFetch32(ptr, value) 		()
    4.92 +#endif
    4.93 +
    4.94 +#ifdef EMULATED_ATOMIC_OPERATIONS
    4.95 +#define HAVE_ALL_64_BIT_OPS
    4.96 +
    4.97 +#define nativeExchange64(ptr, value)			()
    4.98 +#define nativeCompareThenSet64(ptr, oldvalue, newvalue) ()
    4.99 +#define nativeTestThenSet64(ptr)    	     		()
   4.100 +#define nativeClear64(ptr)				()
   4.101 +#define nativeFetchThenIncrement64(ptr)   		()
   4.102 +#define nativeFetchThenDecrement64(ptr) 		()
   4.103 +#define nativeFetchThenAdd64(ptr, value) 		()
   4.104 +#define nativeFetchThenSubtract64(ptr, value) 		()
   4.105 +#define nativeIncrementThenFetch64(ptr) 		()
   4.106 +#define nativeDecrementThenFetch64(ptr) 		()
   4.107 +#define nativeAddThenFetch64(ptr, value) 		()
   4.108 +#define nativeSubtractThenFetch64(ptr, value) 		()
   4.109 +#endif
   4.110 +
   4.111 +/* 
   4.112 +If any of the operations are not provided then we must emulate some of
   4.113 +them.
   4.114 + */
   4.115 +
   4.116 +#if !defined(HAVE_ALL_8_BIT_OPS) || !defined(HAVE_ALL_16_BIT_OPS) || !defined(HAVE_ALL_32_BIT_OPS) || !defined(HAVE_ALL_64_BIT_OPS)
   4.117 +
   4.118 +#include "SDL_mutex.h"
   4.119 +#include "SDL_error.h"
   4.120 +
   4.121 +static SDL_mutex * lock = NULL;
   4.122 +
   4.123 +static __inline__ void
   4.124 +privateWaitLock()
   4.125 +{
   4.126 +   if(NULL == lock)
   4.127 +   {
   4.128 +      lock = SDL_CreateMutex();
   4.129 +      if (NULL == lock)
   4.130 +      {
   4.131 +	 SDL_SetError("SDL_atomic.c: can't create a mutex");
   4.132 +	 return;
   4.133 +      }
   4.134 +   }
   4.135 +
   4.136 +   if (-1 == SDL_LockMutex(lock))
   4.137 +   {
   4.138 +      SDL_SetError("SDL_atomic.c: can't lock mutex");
   4.139 +   }
   4.140 +}
   4.141 +
   4.142 +static __inline__ void
   4.143 +privateUnlock()
   4.144 +{
   4.145 +   if (-1 == SDL_UnlockMutex(lock))
   4.146 +   {
   4.147 +      SDL_SetError("SDL_atomic.c: can't unlock mutex");
   4.148 +   }
   4.149 +}
   4.150 +
   4.151 +#endif
   4.152 +
   4.153 +/* 8 bit atomic operations */
   4.154 +
   4.155 +Uint8
   4.156 +SDL_AtomicExchange8(Uint8 * ptr, Uint8 value)
   4.157 +{
   4.158 +#ifdef nativeExchange8
   4.159 +   return nativeExchange8(ptr, value);
   4.160 +#else
   4.161 +   Uint8 tmp = 0;;
   4.162 +
   4.163 +   privateWaitLock();
   4.164 +   tmp = *ptr;
   4.165 +   *ptr = value;
   4.166 +   privateUnlock();
   4.167 +
   4.168 +   return tmp;
   4.169 +#endif
   4.170 +}
   4.171 +
   4.172 +SDL_bool
   4.173 +SDL_AtomicCompareThenSet8(Uint8 * ptr, Uint8 oldvalue, Uint8 newvalue)
   4.174 +{
   4.175 +#ifdef nativeCompareThenSet8
   4.176 +   return (SDL_bool)nativeCompareThenSet8(ptr, oldvalue, newvalue);
   4.177 +#else
   4.178 +   SDL_bool result = SDL_FALSE;
   4.179 +
   4.180 +   privateWaitLock();
   4.181 +   result = (*ptr == oldvalue);
   4.182 +   if (result)
   4.183 +   {
   4.184 +      *ptr = newvalue;
   4.185 +   }
   4.186 +   privateUnlock();
   4.187 +
   4.188 +   return result;
   4.189 +#endif
   4.190 +}
   4.191 +
   4.192 +SDL_bool
   4.193 +SDL_AtomicTestThenSet8(Uint8 * ptr)
   4.194 +{
   4.195 +#ifdef nativeTestThenSet8
   4.196 +   return (SDL_bool)nativeTestThenSet8(ptr);
   4.197 +#else
   4.198 +   SDL_bool result = SDL_FALSE;
   4.199 +
   4.200 +   privateWaitLock();
   4.201 +   result = (*ptr == 0);
   4.202 +   if (result)
   4.203 +   {
   4.204 +      *ptr = 1;
   4.205 +   }
   4.206 +   privateUnlock();
   4.207 +
   4.208 +   return result;
   4.209 +#endif
   4.210 +}
   4.211 +
   4.212 +void
   4.213 +SDL_AtomicClear8(Uint8 * ptr)
   4.214 +{
   4.215 +#ifdef nativeClear8
   4.216 +   nativeClear8(ptr);
   4.217 +#else
   4.218 +   privateWaitLock();
   4.219 +   *ptr = 0;
   4.220 +   privateUnlock();
   4.221 +
   4.222 +   return;
   4.223 +#endif
   4.224 +}
   4.225 +
   4.226 +Uint8
   4.227 +SDL_AtomicFetchThenIncrement8(Uint8 * ptr)
   4.228 +{
   4.229 +#ifdef nativeFetchThenIncrement8
   4.230 +   return nativeFetchThenIncrement8(ptr);
   4.231 +#else
   4.232 +   Uint8 tmp = 0;;
   4.233 +
   4.234 +   privateWaitLock();
   4.235 +   tmp = *ptr;
   4.236 +   (*ptr)+= 1;
   4.237 +   privateUnlock();
   4.238 +
   4.239 +   return tmp;
   4.240 +#endif
   4.241 +}
   4.242 +
   4.243 +Uint8
   4.244 +SDL_AtomicFetchThenDecrement8(Uint8 * ptr)
   4.245 +{
   4.246 +#ifdef nativeFetchThenDecrement8
   4.247 +   return nativeFetchThenDecrement8(ptr);
   4.248 +#else
   4.249 +   Uint8 tmp = 0;;
   4.250 +
   4.251 +   privateWaitLock();
   4.252 +   tmp = *ptr;
   4.253 +   (*ptr) -= 1;
   4.254 +   privateUnlock();
   4.255 +
   4.256 +   return tmp;
   4.257 +#endif
   4.258 +}
   4.259 +
   4.260 +Uint8
   4.261 +SDL_AtomicFetchThenAdd8(Uint8 * ptr, Uint8 value)
   4.262 +{
   4.263 +#ifdef nativeFetchThenAdd8
   4.264 +   return nativeFetchThenAdd8(ptr, value);
   4.265 +#else
   4.266 +   Uint8 tmp = 0;;
   4.267 +
   4.268 +   privateWaitLock();
   4.269 +   tmp = *ptr;
   4.270 +   (*ptr)+= value;
   4.271 +   privateUnlock();
   4.272 +
   4.273 +   return tmp;
   4.274 +#endif
   4.275 +}
   4.276 +
   4.277 +Uint8
   4.278 +SDL_AtomicFetchThenSubtract8(Uint8 * ptr, Uint8 value)
   4.279 +{
   4.280 +#ifdef nativeFetchThenSubtract8
   4.281 +   return nativeFetchThenSubtract8(ptr, value);
   4.282 +#else
   4.283 +   Uint8 tmp = 0;;
   4.284 +
   4.285 +   privateWaitLock();
   4.286 +   tmp = *ptr;
   4.287 +   (*ptr)-= value;
   4.288 +   privateUnlock();
   4.289 +
   4.290 +   return tmp;
   4.291 +#endif
   4.292 +}
   4.293 +
   4.294 +Uint8
   4.295 +SDL_AtomicIncrementThenFetch8(Uint8 * ptr)
   4.296 +{
   4.297 +#ifdef nativeIncrementThenFetch8
   4.298 +   return nativeIncrementThenFetch8(ptr);
   4.299 +#else
   4.300 +   Uint8 tmp = 0;;
   4.301 +
   4.302 +   privateWaitLock();
   4.303 +   (*ptr)+= 1;
   4.304 +   tmp = *ptr;
   4.305 +   privateUnlock();
   4.306 +
   4.307 +   return tmp;
   4.308 +#endif
   4.309 +}
   4.310 +
   4.311 +Uint8
   4.312 +SDL_AtomicDecrementThenFetch8(Uint8 * ptr)
   4.313 +{
   4.314 +#ifdef nativeDecrementThenFetch8
   4.315 +   return nativeDecrementThenFetch8(ptr);
   4.316 +#else
   4.317 +   Uint8 tmp = 0;;
   4.318 +
   4.319 +   privateWaitLock();
   4.320 +   (*ptr)-= 1;
   4.321 +   tmp = *ptr;
   4.322 +   privateUnlock();
   4.323 +
   4.324 +   return tmp;
   4.325 +#endif
   4.326 +}
   4.327 +
   4.328 +Uint8
   4.329 +SDL_AtomicAddThenFetch8(Uint8 * ptr, Uint8 value)
   4.330 +{
   4.331 +#ifdef nativeAddThenFetch8
   4.332 +   return nativeAddThenFetch8(ptr, value);
   4.333 +#else
   4.334 +   Uint8 tmp = 0;;
   4.335 +
   4.336 +   privateWaitLock();
   4.337 +   (*ptr)+= value;
   4.338 +   tmp = *ptr;
   4.339 +   privateUnlock();
   4.340 +
   4.341 +   return tmp;
   4.342 +#endif
   4.343 +}
   4.344 +
   4.345 +Uint8
   4.346 +SDL_AtomicSubtractThenFetch8(Uint8 * ptr, Uint8 value)
   4.347 +{
   4.348 +#ifdef nativeSubtractThenFetch8
   4.349 +   return nativeSubtractThenFetch8(ptr, value);
   4.350 +#else
   4.351 +   Uint8 tmp = 0;;
   4.352 +
   4.353 +   privateWaitLock();
   4.354 +   (*ptr)-= value;
   4.355 +   tmp = *ptr;
   4.356 +   privateUnlock();
   4.357 +
   4.358 +   return tmp;
   4.359 +#endif
   4.360 +}
   4.361 +
   4.362 +/* 16 bit atomic operations */
   4.363 +
   4.364 +Uint16
   4.365 +SDL_AtomicExchange16(Uint16 * ptr, Uint16 value)
   4.366 +{
   4.367 +#ifdef nativeExchange16
   4.368 +   return nativeExchange16(ptr, value);
   4.369 +#else
   4.370 +   Uint16 tmp = 0;;
   4.371 +
   4.372 +   privateWaitLock();
   4.373 +   tmp = *ptr;
   4.374 +   *ptr = value;
   4.375 +   privateUnlock();
   4.376 +
   4.377 +   return tmp;
   4.378 +#endif
   4.379 +}
   4.380 +
   4.381 +SDL_bool
   4.382 +SDL_AtomicCompareThenSet16(Uint16 * ptr, Uint16 oldvalue, Uint16 newvalue)
   4.383 +{
   4.384 +#ifdef nativeCompareThenSet16
   4.385 +   return (SDL_bool)nativeCompareThenSet16(ptr, oldvalue, newvalue);
   4.386 +#else
   4.387 +   SDL_bool result = SDL_FALSE;
   4.388 +
   4.389 +   privateWaitLock();
   4.390 +   result = (*ptr == oldvalue);
   4.391 +   if (result)
   4.392 +   {
   4.393 +      *ptr = newvalue;
   4.394 +   }
   4.395 +   privateUnlock();
   4.396 +
   4.397 +   return result;
   4.398 +#endif
   4.399 +}
   4.400 +
   4.401 +SDL_bool
   4.402 +SDL_AtomicTestThenSet16(Uint16 * ptr)
   4.403 +{
   4.404 +#ifdef nativeTestThenSet16
   4.405 +   return (SDL_bool)nativeTestThenSet16(ptr);
   4.406 +#else
   4.407 +   SDL_bool result = SDL_FALSE;
   4.408 +
   4.409 +   privateWaitLock();
   4.410 +   result = (*ptr == 0);
   4.411 +   if (result)
   4.412 +   {
   4.413 +      *ptr = 1;
   4.414 +   }
   4.415 +   privateUnlock();
   4.416 +
   4.417 +   return result;
   4.418 +#endif
   4.419 +}
   4.420 +
   4.421 +void
   4.422 +SDL_AtomicClear16(Uint16 * ptr)
   4.423 +{
   4.424 +#ifdef nativeClear16
   4.425 +   nativeClear16(ptr);
   4.426 +#else
   4.427 +   privateWaitLock();
   4.428 +   *ptr = 0;
   4.429 +   privateUnlock();
   4.430 +
   4.431 +   return;
   4.432 +#endif
   4.433 +}
   4.434 +
   4.435 +Uint16
   4.436 +SDL_AtomicFetchThenIncrement16(Uint16 * ptr)
   4.437 +{
   4.438 +#ifdef nativeFetchThenIncrement16
   4.439 +   return nativeFetchThenIncrement16(ptr);
   4.440 +#else
   4.441 +   Uint16 tmp = 0;;
   4.442 +
   4.443 +   privateWaitLock();
   4.444 +   tmp = *ptr;
   4.445 +   (*ptr)+= 1;
   4.446 +   privateUnlock();
   4.447 +
   4.448 +   return tmp;
   4.449 +#endif
   4.450 +}
   4.451 +
   4.452 +Uint16
   4.453 +SDL_AtomicFetchThenDecrement16(Uint16 * ptr)
   4.454 +{
   4.455 +#ifdef nativeFetchThenDecrement16
   4.456 +   return nativeFetchThenDecrement16(ptr);
   4.457 +#else
   4.458 +   Uint16 tmp = 0;;
   4.459 +
   4.460 +   privateWaitLock();
   4.461 +   tmp = *ptr;
   4.462 +   (*ptr) -= 1;
   4.463 +   privateUnlock();
   4.464 +
   4.465 +   return tmp;
   4.466 +#endif
   4.467 +}
   4.468 +
   4.469 +Uint16
   4.470 +SDL_AtomicFetchThenAdd16(Uint16 * ptr, Uint16 value)
   4.471 +{
   4.472 +#ifdef nativeFetchThenAdd16
   4.473 +   return nativeFetchThenAdd16(ptr, value);
   4.474 +#else
   4.475 +   Uint16 tmp = 0;;
   4.476 +
   4.477 +   privateWaitLock();
   4.478 +   tmp = *ptr;
   4.479 +   (*ptr)+= value;
   4.480 +   privateUnlock();
   4.481 +
   4.482 +   return tmp;
   4.483 +#endif
   4.484 +}
   4.485 +
   4.486 +Uint16
   4.487 +SDL_AtomicFetchThenSubtract16(Uint16 * ptr, Uint16 value)
   4.488 +{
   4.489 +#ifdef nativeFetchThenSubtract16
   4.490 +   return nativeFetchThenSubtract16(ptr, value);
   4.491 +#else
   4.492 +   Uint16 tmp = 0;;
   4.493 +
   4.494 +   privateWaitLock();
   4.495 +   tmp = *ptr;
   4.496 +   (*ptr)-= value;
   4.497 +   privateUnlock();
   4.498 +
   4.499 +   return tmp;
   4.500 +#endif
   4.501 +}
   4.502 +
   4.503 +Uint16
   4.504 +SDL_AtomicIncrementThenFetch16(Uint16 * ptr)
   4.505 +{
   4.506 +#ifdef nativeIncrementThenFetch16
   4.507 +   return nativeIncrementThenFetch16(ptr);
   4.508 +#else
   4.509 +   Uint16 tmp = 0;;
   4.510 +
   4.511 +   privateWaitLock();
   4.512 +   (*ptr)+= 1;
   4.513 +   tmp = *ptr;
   4.514 +   privateUnlock();
   4.515 +
   4.516 +   return tmp;
   4.517 +#endif
   4.518 +}
   4.519 +
   4.520 +Uint16
   4.521 +SDL_AtomicDecrementThenFetch16(Uint16 * ptr)
   4.522 +{
   4.523 +#ifdef nativeDecrementThenFetch16
   4.524 +   return nativeDecrementThenFetch16(ptr);
   4.525 +#else
   4.526 +   Uint16 tmp = 0;;
   4.527 +
   4.528 +   privateWaitLock();
   4.529 +   (*ptr)-= 1;
   4.530 +   tmp = *ptr;
   4.531 +   privateUnlock();
   4.532 +
   4.533 +   return tmp;
   4.534 +#endif
   4.535 +}
   4.536 +
   4.537 +Uint16
   4.538 +SDL_AtomicAddThenFetch16(Uint16 * ptr, Uint16 value)
   4.539 +{
   4.540 +#ifdef nativeAddThenFetch16
   4.541 +   return nativeAddThenFetch16(ptr, value);
   4.542 +#else
   4.543 +   Uint16 tmp = 0;;
   4.544 +
   4.545 +   privateWaitLock();
   4.546 +   (*ptr)+= value;
   4.547 +   tmp = *ptr;
   4.548 +   privateUnlock();
   4.549 +
   4.550 +   return tmp;
   4.551 +#endif
   4.552 +}
   4.553 +
   4.554 +Uint16
   4.555 +SDL_AtomicSubtractThenFetch16(Uint16 * ptr, Uint16 value)
   4.556 +{
   4.557 +#ifdef nativeSubtractThenFetch16
   4.558 +   return nativeSubtractThenFetch16(ptr, value);
   4.559 +#else
   4.560 +   Uint16 tmp = 0;;
   4.561 +
   4.562 +   privateWaitLock();
   4.563 +   (*ptr)-= value;
   4.564 +   tmp = *ptr;
   4.565 +   privateUnlock();
   4.566 +
   4.567 +   return tmp;
   4.568 +#endif
   4.569 +}
   4.570 +
   4.571 +/* 32 bit atomic operations */
   4.572  
   4.573  Uint32
   4.574  SDL_AtomicExchange32(Uint32 * ptr, Uint32 value)
   4.575  {
   4.576 -   return 0;
   4.577 +#ifdef nativeExchange32
   4.578 +   return nativeExchange32(ptr, value);
   4.579 +#else
   4.580 +   Uint32 tmp = 0;;
   4.581 +
   4.582 +   privateWaitLock();
   4.583 +   tmp = *ptr;
   4.584 +   *ptr = value;
   4.585 +   privateUnlock();
   4.586 +
   4.587 +   return tmp;
   4.588 +#endif
   4.589  }
   4.590  
   4.591  SDL_bool
   4.592  SDL_AtomicCompareThenSet32(Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
   4.593  {
   4.594 -   return SDL_false;
   4.595 +#ifdef nativeCompareThenSet32
   4.596 +   return (SDL_bool)nativeCompareThenSet32(ptr, oldvalue, newvalue);
   4.597 +#else
   4.598 +   SDL_bool result = SDL_FALSE;
   4.599 +
   4.600 +   privateWaitLock();
   4.601 +   result = (*ptr == oldvalue);
   4.602 +   if (result)
   4.603 +   {
   4.604 +      *ptr = newvalue;
   4.605 +   }
   4.606 +   privateUnlock();
   4.607 +
   4.608 +   return result;
   4.609 +#endif
   4.610  }
   4.611  
   4.612  SDL_bool
   4.613  SDL_AtomicTestThenSet32(Uint32 * ptr)
   4.614  {
   4.615 -   return SDL_false;
   4.616 +#ifdef nativeTestThenSet32
   4.617 +   return (SDL_bool)nativeTestThenSet32(ptr);
   4.618 +#else
   4.619 +   SDL_bool result = SDL_FALSE;
   4.620 +
   4.621 +   privateWaitLock();
   4.622 +   result = (*ptr == 0);
   4.623 +   if (result)
   4.624 +   {
   4.625 +      *ptr = 1;
   4.626 +   }
   4.627 +   privateUnlock();
   4.628 +
   4.629 +   return result;
   4.630 +#endif
   4.631  }
   4.632  
   4.633  void
   4.634  SDL_AtomicClear32(Uint32 * ptr)
   4.635  {
   4.636 +#ifdef nativeClear32
   4.637 +   nativeClear32(ptr);
   4.638 +#else
   4.639 +   privateWaitLock();
   4.640 +   *ptr = 0;
   4.641 +   privateUnlock();
   4.642 +
   4.643 +   return;
   4.644 +#endif
   4.645  }
   4.646  
   4.647  Uint32
   4.648  SDL_AtomicFetchThenIncrement32(Uint32 * ptr)
   4.649  {
   4.650 -   return 0;
   4.651 +#ifdef nativeFetchThenIncrement32
   4.652 +   return nativeFetchThenIncrement32(ptr);
   4.653 +#else
   4.654 +   Uint32 tmp = 0;;
   4.655 +
   4.656 +   privateWaitLock();
   4.657 +   tmp = *ptr;
   4.658 +   (*ptr)+= 1;
   4.659 +   privateUnlock();
   4.660 +
   4.661 +   return tmp;
   4.662 +#endif
   4.663  }
   4.664  
   4.665  Uint32
   4.666  SDL_AtomicFetchThenDecrement32(Uint32 * ptr)
   4.667  {
   4.668 -   return 0;
   4.669 +#ifdef nativeFetchThenDecrement32
   4.670 +   return nativeFetchThenDecrement32(ptr);
   4.671 +#else
   4.672 +   Uint32 tmp = 0;;
   4.673 +
   4.674 +   privateWaitLock();
   4.675 +   tmp = *ptr;
   4.676 +   (*ptr) -= 1;
   4.677 +   privateUnlock();
   4.678 +
   4.679 +   return tmp;
   4.680 +#endif
   4.681  }
   4.682  
   4.683  Uint32
   4.684  SDL_AtomicFetchThenAdd32(Uint32 * ptr, Uint32 value)
   4.685  {
   4.686 -   return 0;
   4.687 +#ifdef nativeFetchThenAdd32
   4.688 +   return nativeFetchThenAdd32(ptr, value);
   4.689 +#else
   4.690 +   Uint32 tmp = 0;;
   4.691 +
   4.692 +   privateWaitLock();
   4.693 +   tmp = *ptr;
   4.694 +   (*ptr)+= value;
   4.695 +   privateUnlock();
   4.696 +
   4.697 +   return tmp;
   4.698 +#endif
   4.699  }
   4.700  
   4.701  Uint32
   4.702  SDL_AtomicFetchThenSubtract32(Uint32 * ptr, Uint32 value)
   4.703  {
   4.704 -   return 0;
   4.705 +#ifdef nativeFetchThenSubtract32
   4.706 +   return nativeFetchThenSubtract32(ptr, value);
   4.707 +#else
   4.708 +   Uint32 tmp = 0;;
   4.709 +
   4.710 +   privateWaitLock();
   4.711 +   tmp = *ptr;
   4.712 +   (*ptr)-= value;
   4.713 +   privateUnlock();
   4.714 +
   4.715 +   return tmp;
   4.716 +#endif
   4.717  }
   4.718  
   4.719  Uint32
   4.720  SDL_AtomicIncrementThenFetch32(Uint32 * ptr)
   4.721  {
   4.722 -   return 0;
   4.723 +#ifdef nativeIncrementThenFetch32
   4.724 +   return nativeIncrementThenFetch32(ptr);
   4.725 +#else
   4.726 +   Uint32 tmp = 0;;
   4.727 +
   4.728 +   privateWaitLock();
   4.729 +   (*ptr)+= 1;
   4.730 +   tmp = *ptr;
   4.731 +   privateUnlock();
   4.732 +
   4.733 +   return tmp;
   4.734 +#endif
   4.735  }
   4.736  
   4.737  Uint32
   4.738  SDL_AtomicDecrementThenFetch32(Uint32 * ptr)
   4.739  {
   4.740 -   return 0;
   4.741 +#ifdef nativeDecrementThenFetch32
   4.742 +   return nativeDecrementThenFetch32(ptr);
   4.743 +#else
   4.744 +   Uint32 tmp = 0;;
   4.745 +
   4.746 +   privateWaitLock();
   4.747 +   (*ptr)-= 1;
   4.748 +   tmp = *ptr;
   4.749 +   privateUnlock();
   4.750 +
   4.751 +   return tmp;
   4.752 +#endif
   4.753  }
   4.754  
   4.755  Uint32
   4.756  SDL_AtomicAddThenFetch32(Uint32 * ptr, Uint32 value)
   4.757  {
   4.758 -   return 0;
   4.759 +#ifdef nativeAddThenFetch32
   4.760 +   return nativeAddThenFetch32(ptr, value);
   4.761 +#else
   4.762 +   Uint32 tmp = 0;;
   4.763 +
   4.764 +   privateWaitLock();
   4.765 +   (*ptr)+= value;
   4.766 +   tmp = *ptr;
   4.767 +   privateUnlock();
   4.768 +
   4.769 +   return tmp;
   4.770 +#endif
   4.771  }
   4.772  
   4.773  Uint32
   4.774  SDL_AtomicSubtractThenFetch32(Uint32 * ptr, Uint32 value)
   4.775  {
   4.776 -   return 0;
   4.777 +#ifdef nativeSubtractThenFetch32
   4.778 +   return nativeSubtractThenFetch32(ptr, value);
   4.779 +#else
   4.780 +   Uint32 tmp = 0;;
   4.781 +
   4.782 +   privateWaitLock();
   4.783 +   (*ptr)-= value;
   4.784 +   tmp = *ptr;
   4.785 +   privateUnlock();
   4.786 +
   4.787 +   return tmp;
   4.788 +#endif
   4.789  }
   4.790  
   4.791 -
   4.792 +/* 64 bit atomic operations */
   4.793  #ifdef SDL_HAS_64BIT_TYPE
   4.794  
   4.795  Uint64
   4.796  SDL_AtomicExchange64(Uint64 * ptr, Uint64 value)
   4.797  {
   4.798 -   return 0;
   4.799 +#ifdef nativeExchange64
   4.800 +   return nativeExchange64(ptr, value);
   4.801 +#else
   4.802 +   Uint64 tmp = 0;;
   4.803 +
   4.804 +   privateWaitLock();
   4.805 +   tmp = *ptr;
   4.806 +   *ptr = value;
   4.807 +   privateUnlock();
   4.808 +
   4.809 +   return tmp;
   4.810 +#endif
   4.811  }
   4.812  
   4.813  SDL_bool
   4.814 -SDL_AtomicCompareThenSet64(Uint64 * ptr,
   4.815 -Uint64 oldvalue, Uint64 newvalue)
   4.816 +SDL_AtomicCompareThenSet64(Uint64 * ptr, Uint64 oldvalue, Uint64 newvalue)
   4.817  {
   4.818 -   return SDL_false;
   4.819 +#ifdef nativeCompareThenSet64
   4.820 +   return (SDL_bool)nativeCompareThenSet64(ptr, oldvalue, newvalue);
   4.821 +#else
   4.822 +   SDL_bool result = SDL_FALSE;
   4.823 +
   4.824 +   privateWaitLock();
   4.825 +   result = (*ptr == oldvalue);
   4.826 +   if (result)
   4.827 +   {
   4.828 +      *ptr = newvalue;
   4.829 +   }
   4.830 +   privateUnlock();
   4.831 +
   4.832 +   return result;
   4.833 +#endif
   4.834  }
   4.835  
   4.836  SDL_bool
   4.837  SDL_AtomicTestThenSet64(Uint64 * ptr)
   4.838  {
   4.839 -   return SDL_false;
   4.840 +#ifdef nativeTestThenSet64
   4.841 +   return (SDL_bool)nativeTestThenSet64(ptr);
   4.842 +#else
   4.843 +   SDL_bool result = SDL_FALSE;
   4.844 +
   4.845 +   privateWaitLock();
   4.846 +   result = (*ptr == 0);
   4.847 +   if (result)
   4.848 +   {
   4.849 +      *ptr = 1;
   4.850 +   }
   4.851 +   privateUnlock();
   4.852 +
   4.853 +   return result;
   4.854 +#endif
   4.855  }
   4.856  
   4.857  void
   4.858  SDL_AtomicClear64(Uint64 * ptr)
   4.859  {
   4.860 +#ifdef nativeClear64
   4.861 +   nativeClear64(ptr);
   4.862 +#else
   4.863 +   privateWaitLock();
   4.864 +   *ptr = 0;
   4.865 +   privateUnlock();
   4.866 +
   4.867 +   return;
   4.868 +#endif
   4.869  }
   4.870  
   4.871  Uint64
   4.872  SDL_AtomicFetchThenIncrement64(Uint64 * ptr)
   4.873  {
   4.874 -   return 0;
   4.875 +#ifdef nativeFetchThenIncrement64
   4.876 +   return nativeFetchThenIncrement64(ptr);
   4.877 +#else
   4.878 +   Uint64 tmp = 0;;
   4.879 +
   4.880 +   privateWaitLock();
   4.881 +   tmp = *ptr;
   4.882 +   (*ptr)+= 1;
   4.883 +   privateUnlock();
   4.884 +
   4.885 +   return tmp;
   4.886 +#endif
   4.887  }
   4.888  
   4.889  Uint64
   4.890  SDL_AtomicFetchThenDecrement64(Uint64 * ptr)
   4.891  {
   4.892 -   return 0;
   4.893 +#ifdef nativeFetchThenDecrement64
   4.894 +   return nativeFetchThenDecrement64(ptr);
   4.895 +#else
   4.896 +   Uint64 tmp = 0;;
   4.897 +
   4.898 +   privateWaitLock();
   4.899 +   tmp = *ptr;
   4.900 +   (*ptr) -= 1;
   4.901 +   privateUnlock();
   4.902 +
   4.903 +   return tmp;
   4.904 +#endif
   4.905  }
   4.906  
   4.907  Uint64
   4.908  SDL_AtomicFetchThenAdd64(Uint64 * ptr, Uint64 value)
   4.909  {
   4.910 -   return 0;
   4.911 +#ifdef nativeFetchThenAdd64
   4.912 +   return nativeFetchThenAdd64(ptr, value);
   4.913 +#else
   4.914 +   Uint64 tmp = 0;;
   4.915 +
   4.916 +   privateWaitLock();
   4.917 +   tmp = *ptr;
   4.918 +   (*ptr)+= value;
   4.919 +   privateUnlock();
   4.920 +
   4.921 +   return tmp;
   4.922 +#endif
   4.923  }
   4.924  
   4.925  Uint64
   4.926  SDL_AtomicFetchThenSubtract64(Uint64 * ptr, Uint64 value)
   4.927  {
   4.928 -   return 0;
   4.929 +#ifdef nativeFetchThenSubtract64
   4.930 +   return nativeFetchThenSubtract64(ptr, value);
   4.931 +#else
   4.932 +   Uint64 tmp = 0;;
   4.933 +
   4.934 +   privateWaitLock();
   4.935 +   tmp = *ptr;
   4.936 +   (*ptr)-= value;
   4.937 +   privateUnlock();
   4.938 +
   4.939 +   return tmp;
   4.940 +#endif
   4.941  }
   4.942  
   4.943  Uint64
   4.944  SDL_AtomicIncrementThenFetch64(Uint64 * ptr)
   4.945  {
   4.946 -   return 0;
   4.947 +#ifdef nativeIncrementThenFetch64
   4.948 +   return nativeIncrementThenFetch64(ptr);
   4.949 +#else
   4.950 +   Uint64 tmp = 0;;
   4.951 +
   4.952 +   privateWaitLock();
   4.953 +   (*ptr)+= 1;
   4.954 +   tmp = *ptr;
   4.955 +   privateUnlock();
   4.956 +
   4.957 +   return tmp;
   4.958 +#endif
   4.959  }
   4.960  
   4.961  Uint64
   4.962  SDL_AtomicDecrementThenFetch64(Uint64 * ptr)
   4.963  {
   4.964 -   return 0;
   4.965 +#ifdef nativeDecrementThenFetch64
   4.966 +   return nativeDecrementThenFetch64(ptr);
   4.967 +#else
   4.968 +   Uint64 tmp = 0;;
   4.969 +
   4.970 +   privateWaitLock();
   4.971 +   (*ptr)-= 1;
   4.972 +   tmp = *ptr;
   4.973 +   privateUnlock();
   4.974 +
   4.975 +   return tmp;
   4.976 +#endif
   4.977  }
   4.978  
   4.979  Uint64
   4.980  SDL_AtomicAddThenFetch64(Uint64 * ptr, Uint64 value)
   4.981  {
   4.982 -   return 0;
   4.983 +#ifdef nativeAddThenFetch64
   4.984 +   return nativeAddThenFetch64(ptr, value);
   4.985 +#else
   4.986 +   Uint64 tmp = 0;;
   4.987 +
   4.988 +   privateWaitLock();
   4.989 +   (*ptr)+= value;
   4.990 +   tmp = *ptr;
   4.991 +   privateUnlock();
   4.992 +
   4.993 +   return tmp;
   4.994 +#endif
   4.995  }
   4.996  
   4.997  Uint64
   4.998  SDL_AtomicSubtractThenFetch64(Uint64 * ptr, Uint64 value)
   4.999  {
  4.1000 -   return 0;
  4.1001 +#ifdef nativeSubtractThenFetch64
  4.1002 +   return nativeSubtractThenFetch64(ptr, value);
  4.1003 +#else
  4.1004 +   Uint64 tmp = 0;;
  4.1005 +
  4.1006 +   privateWaitLock();
  4.1007 +   (*ptr)-= value;
  4.1008 +   tmp = *ptr;
  4.1009 +   privateUnlock();
  4.1010 +
  4.1011 +   return tmp;
  4.1012 +#endif
  4.1013  }
  4.1014 +#endif
  4.1015  
  4.1016 -#endif
  4.1017 -#endif
     5.1 --- a/src/atomic/win32/SDL_atomic.c	Wed Jun 24 22:24:23 2009 +0000
     5.2 +++ b/src/atomic/win32/SDL_atomic.c	Mon Jun 29 19:54:43 2009 +0000
     5.3 @@ -20,157 +20,983 @@
     5.4      slouken@libsdl.org
     5.5  */
     5.6  
     5.7 -#ifdef SDL_ATOMIC_LINUX
     5.8 +#include "SDL_stdinc.h"
     5.9 +#include "SDL_atomic.h"
    5.10  
    5.11 -#include "SDL.h"
    5.12 -#include "SDL_config.h"
    5.13 -#include "SDL_atomic.h"
    5.14 +/*
    5.15 +  This file provides 8, 16, 32, and 64 bit atomic operations. If the
    5.16 +  operations are provided by the native hardware and operating system
    5.17 +  they are used. If they are not then the operations are emulated
    5.18 +  using the SDL mutex operations. 
    5.19 + */
    5.20 +
    5.21 +/* 
    5.22 +  First, detect whether the operations are supported and create
    5.23 +  #defines that indicate that they do exist. The goal is to have all
    5.24 +  the system dependent code in the top part of the file so that the
    5.25 +  bottom can be use unchanged across all platforms.
    5.26 +
    5.27 +  Second, #define all the operations in each size class that are
    5.28 +  supported. Doing this allows supported operations to be used along
    5.29 +  side of emulated operations.
    5.30 +*/
    5.31 +
    5.32 +/* 
    5.33 +   Emmulated version.
    5.34 +
    5.35 +   Assume there is no support for atomic operations. All such
    5.36 +   operations are implemented using SDL mutex operations.
    5.37 + */
    5.38 +
    5.39 +#ifdef EMULATED_ATOMIC_OPERATIONS
    5.40 +#undef EMULATED_ATOMIC_OPERATIONS
    5.41 +#endif
    5.42 +
    5.43 +#ifdef EMULATED_ATOMIC_OPERATIONS
    5.44 +#define HAVE_ALL_8_BIT_OPS
    5.45 +
    5.46 +#define nativeExchange8(ptr, value)			()
    5.47 +#define nativeCompareThenSet8(ptr, oldvalue, newvalue) 	()
    5.48 +#define nativeTestThenSet8(ptr)    	     		()
    5.49 +#define nativeClear8(ptr)				()
    5.50 +#define nativeFetchThenIncrement8(ptr)   		()
    5.51 +#define nativeFetchThenDecrement8(ptr) 			()
    5.52 +#define nativeFetchThenAdd8(ptr, value) 		()
    5.53 +#define nativeFetchThenSubtract8(ptr, value) 		()
    5.54 +#define nativeIncrementThenFetch8(ptr) 			()
    5.55 +#define nativeDecrementThenFetch8(ptr) 			()
    5.56 +#define nativeAddThenFetch8(ptr, value) 		()
    5.57 +#define nativeSubtractThenFetch8(ptr, value) 		()
    5.58 +#endif
    5.59 +
    5.60 +#ifdef EMULATED_ATOMIC_OPERATIONS
    5.61 +#define HAVE_ALL_16_BIT_OPS
    5.62 +
    5.63 +#define nativeExchange16(ptr, value)			()
    5.64 +#define nativeCompareThenSet16(ptr, oldvalue, newvalue) ()
    5.65 +#define nativeTestThenSet16(ptr)    	     		()
    5.66 +#define nativeClear16(ptr)				()
    5.67 +#define nativeFetchThenIncrement16(ptr)   		()
    5.68 +#define nativeFetchThenDecrement16(ptr) 		()
    5.69 +#define nativeFetchThenAdd16(ptr, value) 		()
    5.70 +#define nativeFetchThenSubtract16(ptr, value) 		()
    5.71 +#define nativeIncrementThenFetch16(ptr) 		()
    5.72 +#define nativeDecrementThenFetch16(ptr) 		()
    5.73 +#define nativeAddThenFetch16(ptr, value) 		()
    5.74 +#define nativeSubtractThenFetch16(ptr, value) 		()
    5.75 +#endif
    5.76 +
    5.77 +#ifdef EMULATED_ATOMIC_OPERATIONS
    5.78 +#define HAVE_ALL_32_BIT_OPS
    5.79 +
    5.80 +#define nativeExchange32(ptr, value)			()
    5.81 +#define nativeCompareThenSet32(ptr, oldvalue, newvalue) ()
    5.82 +#define nativeTestThenSet32(ptr)    	     		()
    5.83 +#define nativeClear32(ptr)				()
    5.84 +#define nativeFetchThenIncrement32(ptr)   		()
    5.85 +#define nativeFetchThenDecrement32(ptr) 		()
    5.86 +#define nativeFetchThenAdd32(ptr, value) 		()
    5.87 +#define nativeFetchThenSubtract32(ptr, value) 		()
    5.88 +#define nativeIncrementThenFetch32(ptr) 		()
    5.89 +#define nativeDecrementThenFetch32(ptr) 		()
    5.90 +#define nativeAddThenFetch32(ptr, value) 		()
    5.91 +#define nativeSubtractThenFetch32(ptr, value) 		()
    5.92 +#endif
    5.93 +
    5.94 +#ifdef EMULATED_ATOMIC_OPERATIONS
    5.95 +#define HAVE_ALL_64_BIT_OPS
    5.96 +
    5.97 +#define nativeExchange64(ptr, value)			()
    5.98 +#define nativeCompareThenSet64(ptr, oldvalue, newvalue) ()
    5.99 +#define nativeTestThenSet64(ptr)    	     		()
   5.100 +#define nativeClear64(ptr)				()
   5.101 +#define nativeFetchThenIncrement64(ptr)   		()
   5.102 +#define nativeFetchThenDecrement64(ptr) 		()
   5.103 +#define nativeFetchThenAdd64(ptr, value) 		()
   5.104 +#define nativeFetchThenSubtract64(ptr, value) 		()
   5.105 +#define nativeIncrementThenFetch64(ptr) 		()
   5.106 +#define nativeDecrementThenFetch64(ptr) 		()
   5.107 +#define nativeAddThenFetch64(ptr, value) 		()
   5.108 +#define nativeSubtractThenFetch64(ptr, value) 		()
   5.109 +#endif
   5.110 +
   5.111 +/* 
   5.112 +If any of the operations are not provided then we must emulate some of
   5.113 +them.
   5.114 + */
   5.115 +
   5.116 +#if !defined(HAVE_ALL_8_BIT_OPS) || !defined(HAVE_ALL_16_BIT_OPS) || !defined(HAVE_ALL_32_BIT_OPS) || !defined(HAVE_ALL_64_BIT_OPS)
   5.117 +
   5.118 +#include "SDL_mutex.h"
   5.119 +#include "SDL_error.h"
   5.120 +
   5.121 +static SDL_mutex * lock = NULL;
   5.122 +
   5.123 +static __inline__ void
   5.124 +privateWaitLock()
   5.125 +{
   5.126 +   if(NULL == lock)
   5.127 +   {
   5.128 +      lock = SDL_CreateMutex();
   5.129 +      if (NULL == lock)
   5.130 +      {
   5.131 +	 SDL_SetError("SDL_atomic.c: can't create a mutex");
   5.132 +	 return;
   5.133 +      }
   5.134 +   }
   5.135 +
   5.136 +   if (-1 == SDL_LockMutex(lock))
   5.137 +   {
   5.138 +      SDL_SetError("SDL_atomic.c: can't lock mutex");
   5.139 +   }
   5.140 +}
   5.141 +
   5.142 +static __inline__ void
   5.143 +privateUnlock()
   5.144 +{
   5.145 +   if (-1 == SDL_UnlockMutex(lock))
   5.146 +   {
   5.147 +      SDL_SetError("SDL_atomic.c: can't unlock mutex");
   5.148 +   }
   5.149 +}
   5.150 +
   5.151 +#endif
   5.152 +
   5.153 +/* 8 bit atomic operations */
   5.154 +
   5.155 +Uint8
   5.156 +SDL_AtomicExchange8(Uint8 * ptr, Uint8 value)
   5.157 +{
   5.158 +#ifdef nativeExchange8
   5.159 +   return nativeExchange8(ptr, value);
   5.160 +#else
   5.161 +   Uint8 tmp = 0;;
   5.162 +
   5.163 +   privateWaitLock();
   5.164 +   tmp = *ptr;
   5.165 +   *ptr = value;
   5.166 +   privateUnlock();
   5.167 +
   5.168 +   return tmp;
   5.169 +#endif
   5.170 +}
   5.171 +
   5.172 +SDL_bool
   5.173 +SDL_AtomicCompareThenSet8(Uint8 * ptr, Uint8 oldvalue, Uint8 newvalue)
   5.174 +{
   5.175 +#ifdef nativeCompareThenSet8
   5.176 +   return (SDL_bool)nativeCompareThenSet8(ptr, oldvalue, newvalue);
   5.177 +#else
   5.178 +   SDL_bool result = SDL_FALSE;
   5.179 +
   5.180 +   privateWaitLock();
   5.181 +   result = (*ptr == oldvalue);
   5.182 +   if (result)
   5.183 +   {
   5.184 +      *ptr = newvalue;
   5.185 +   }
   5.186 +   privateUnlock();
   5.187 +
   5.188 +   return result;
   5.189 +#endif
   5.190 +}
   5.191 +
   5.192 +SDL_bool
   5.193 +SDL_AtomicTestThenSet8(Uint8 * ptr)
   5.194 +{
   5.195 +#ifdef nativeTestThenSet8
   5.196 +   return (SDL_bool)nativeTestThenSet8(ptr);
   5.197 +#else
   5.198 +   SDL_bool result = SDL_FALSE;
   5.199 +
   5.200 +   privateWaitLock();
   5.201 +   result = (*ptr == 0);
   5.202 +   if (result)
   5.203 +   {
   5.204 +      *ptr = 1;
   5.205 +   }
   5.206 +   privateUnlock();
   5.207 +
   5.208 +   return result;
   5.209 +#endif
   5.210 +}
   5.211 +
   5.212 +void
   5.213 +SDL_AtomicClear8(Uint8 * ptr)
   5.214 +{
   5.215 +#ifdef nativeClear8
   5.216 +   nativeClear8(ptr);
   5.217 +#else
   5.218 +   privateWaitLock();
   5.219 +   *ptr = 0;
   5.220 +   privateUnlock();
   5.221 +
   5.222 +   return;
   5.223 +#endif
   5.224 +}
   5.225 +
   5.226 +Uint8
   5.227 +SDL_AtomicFetchThenIncrement8(Uint8 * ptr)
   5.228 +{
   5.229 +#ifdef nativeFetchThenIncrement8
   5.230 +   return nativeFetchThenIncrement8(ptr);
   5.231 +#else
   5.232 +   Uint8 tmp = 0;;
   5.233 +
   5.234 +   privateWaitLock();
   5.235 +   tmp = *ptr;
   5.236 +   (*ptr)+= 1;
   5.237 +   privateUnlock();
   5.238 +
   5.239 +   return tmp;
   5.240 +#endif
   5.241 +}
   5.242 +
   5.243 +Uint8
   5.244 +SDL_AtomicFetchThenDecrement8(Uint8 * ptr)
   5.245 +{
   5.246 +#ifdef nativeFetchThenDecrement8
   5.247 +   return nativeFetchThenDecrement8(ptr);
   5.248 +#else
   5.249 +   Uint8 tmp = 0;;
   5.250 +
   5.251 +   privateWaitLock();
   5.252 +   tmp = *ptr;
   5.253 +   (*ptr) -= 1;
   5.254 +   privateUnlock();
   5.255 +
   5.256 +   return tmp;
   5.257 +#endif
   5.258 +}
   5.259 +
   5.260 +Uint8
   5.261 +SDL_AtomicFetchThenAdd8(Uint8 * ptr, Uint8 value)
   5.262 +{
   5.263 +#ifdef nativeFetchThenAdd8
   5.264 +   return nativeFetchThenAdd8(ptr, value);
   5.265 +#else
   5.266 +   Uint8 tmp = 0;;
   5.267 +
   5.268 +   privateWaitLock();
   5.269 +   tmp = *ptr;
   5.270 +   (*ptr)+= value;
   5.271 +   privateUnlock();
   5.272 +
   5.273 +   return tmp;
   5.274 +#endif
   5.275 +}
   5.276 +
   5.277 +Uint8
   5.278 +SDL_AtomicFetchThenSubtract8(Uint8 * ptr, Uint8 value)
   5.279 +{
   5.280 +#ifdef nativeFetchThenSubtract8
   5.281 +   return nativeFetchThenSubtract8(ptr, value);
   5.282 +#else
   5.283 +   Uint8 tmp = 0;;
   5.284 +
   5.285 +   privateWaitLock();
   5.286 +   tmp = *ptr;
   5.287 +   (*ptr)-= value;
   5.288 +   privateUnlock();
   5.289 +
   5.290 +   return tmp;
   5.291 +#endif
   5.292 +}
   5.293 +
   5.294 +Uint8
   5.295 +SDL_AtomicIncrementThenFetch8(Uint8 * ptr)
   5.296 +{
   5.297 +#ifdef nativeIncrementThenFetch8
   5.298 +   return nativeIncrementThenFetch8(ptr);
   5.299 +#else
   5.300 +   Uint8 tmp = 0;;
   5.301 +
   5.302 +   privateWaitLock();
   5.303 +   (*ptr)+= 1;
   5.304 +   tmp = *ptr;
   5.305 +   privateUnlock();
   5.306 +
   5.307 +   return tmp;
   5.308 +#endif
   5.309 +}
   5.310 +
   5.311 +Uint8
   5.312 +SDL_AtomicDecrementThenFetch8(Uint8 * ptr)
   5.313 +{
   5.314 +#ifdef nativeDecrementThenFetch8
   5.315 +   return nativeDecrementThenFetch8(ptr);
   5.316 +#else
   5.317 +   Uint8 tmp = 0;;
   5.318 +
   5.319 +   privateWaitLock();
   5.320 +   (*ptr)-= 1;
   5.321 +   tmp = *ptr;
   5.322 +   privateUnlock();
   5.323 +
   5.324 +   return tmp;
   5.325 +#endif
   5.326 +}
   5.327 +
   5.328 +Uint8
   5.329 +SDL_AtomicAddThenFetch8(Uint8 * ptr, Uint8 value)
   5.330 +{
   5.331 +#ifdef nativeAddThenFetch8
   5.332 +   return nativeAddThenFetch8(ptr, value);
   5.333 +#else
   5.334 +   Uint8 tmp = 0;;
   5.335 +
   5.336 +   privateWaitLock();
   5.337 +   (*ptr)+= value;
   5.338 +   tmp = *ptr;
   5.339 +   privateUnlock();
   5.340 +
   5.341 +   return tmp;
   5.342 +#endif
   5.343 +}
   5.344 +
   5.345 +Uint8
   5.346 +SDL_AtomicSubtractThenFetch8(Uint8 * ptr, Uint8 value)
   5.347 +{
   5.348 +#ifdef nativeSubtractThenFetch8
   5.349 +   return nativeSubtractThenFetch8(ptr, value);
   5.350 +#else
   5.351 +   Uint8 tmp = 0;;
   5.352 +
   5.353 +   privateWaitLock();
   5.354 +   (*ptr)-= value;
   5.355 +   tmp = *ptr;
   5.356 +   privateUnlock();
   5.357 +
   5.358 +   return tmp;
   5.359 +#endif
   5.360 +}
   5.361 +
   5.362 +/* 16 bit atomic operations */
   5.363 +
   5.364 +Uint16
   5.365 +SDL_AtomicExchange16(Uint16 * ptr, Uint16 value)
   5.366 +{
   5.367 +#ifdef nativeExchange16
   5.368 +   return nativeExchange16(ptr, value);
   5.369 +#else
   5.370 +   Uint16 tmp = 0;;
   5.371 +
   5.372 +   privateWaitLock();
   5.373 +   tmp = *ptr;
   5.374 +   *ptr = value;
   5.375 +   privateUnlock();
   5.376 +
   5.377 +   return tmp;
   5.378 +#endif
   5.379 +}
   5.380 +
   5.381 +SDL_bool
   5.382 +SDL_AtomicCompareThenSet16(Uint16 * ptr, Uint16 oldvalue, Uint16 newvalue)
   5.383 +{
   5.384 +#ifdef nativeCompareThenSet16
   5.385 +   return (SDL_bool)nativeCompareThenSet16(ptr, oldvalue, newvalue);
   5.386 +#else
   5.387 +   SDL_bool result = SDL_FALSE;
   5.388 +
   5.389 +   privateWaitLock();
   5.390 +   result = (*ptr == oldvalue);
   5.391 +   if (result)
   5.392 +   {
   5.393 +      *ptr = newvalue;
   5.394 +   }
   5.395 +   privateUnlock();
   5.396 +
   5.397 +   return result;
   5.398 +#endif
   5.399 +}
   5.400 +
   5.401 +SDL_bool
   5.402 +SDL_AtomicTestThenSet16(Uint16 * ptr)
   5.403 +{
   5.404 +#ifdef nativeTestThenSet16
   5.405 +   return (SDL_bool)nativeTestThenSet16(ptr);
   5.406 +#else
   5.407 +   SDL_bool result = SDL_FALSE;
   5.408 +
   5.409 +   privateWaitLock();
   5.410 +   result = (*ptr == 0);
   5.411 +   if (result)
   5.412 +   {
   5.413 +      *ptr = 1;
   5.414 +   }
   5.415 +   privateUnlock();
   5.416 +
   5.417 +   return result;
   5.418 +#endif
   5.419 +}
   5.420 +
   5.421 +void
   5.422 +SDL_AtomicClear16(Uint16 * ptr)
   5.423 +{
   5.424 +#ifdef nativeClear16
   5.425 +   nativeClear16(ptr);
   5.426 +#else
   5.427 +   privateWaitLock();
   5.428 +   *ptr = 0;
   5.429 +   privateUnlock();
   5.430 +
   5.431 +   return;
   5.432 +#endif
   5.433 +}
   5.434 +
   5.435 +Uint16
   5.436 +SDL_AtomicFetchThenIncrement16(Uint16 * ptr)
   5.437 +{
   5.438 +#ifdef nativeFetchThenIncrement16
   5.439 +   return nativeFetchThenIncrement16(ptr);
   5.440 +#else
   5.441 +   Uint16 tmp = 0;;
   5.442 +
   5.443 +   privateWaitLock();
   5.444 +   tmp = *ptr;
   5.445 +   (*ptr)+= 1;
   5.446 +   privateUnlock();
   5.447 +
   5.448 +   return tmp;
   5.449 +#endif
   5.450 +}
   5.451 +
   5.452 +Uint16
   5.453 +SDL_AtomicFetchThenDecrement16(Uint16 * ptr)
   5.454 +{
   5.455 +#ifdef nativeFetchThenDecrement16
   5.456 +   return nativeFetchThenDecrement16(ptr);
   5.457 +#else
   5.458 +   Uint16 tmp = 0;;
   5.459 +
   5.460 +   privateWaitLock();
   5.461 +   tmp = *ptr;
   5.462 +   (*ptr) -= 1;
   5.463 +   privateUnlock();
   5.464 +
   5.465 +   return tmp;
   5.466 +#endif
   5.467 +}
   5.468 +
   5.469 +Uint16
   5.470 +SDL_AtomicFetchThenAdd16(Uint16 * ptr, Uint16 value)
   5.471 +{
   5.472 +#ifdef nativeFetchThenAdd16
   5.473 +   return nativeFetchThenAdd16(ptr, value);
   5.474 +#else
   5.475 +   Uint16 tmp = 0;;
   5.476 +
   5.477 +   privateWaitLock();
   5.478 +   tmp = *ptr;
   5.479 +   (*ptr)+= value;
   5.480 +   privateUnlock();
   5.481 +
   5.482 +   return tmp;
   5.483 +#endif
   5.484 +}
   5.485 +
   5.486 +Uint16
   5.487 +SDL_AtomicFetchThenSubtract16(Uint16 * ptr, Uint16 value)
   5.488 +{
   5.489 +#ifdef nativeFetchThenSubtract16
   5.490 +   return nativeFetchThenSubtract16(ptr, value);
   5.491 +#else
   5.492 +   Uint16 tmp = 0;;
   5.493 +
   5.494 +   privateWaitLock();
   5.495 +   tmp = *ptr;
   5.496 +   (*ptr)-= value;
   5.497 +   privateUnlock();
   5.498 +
   5.499 +   return tmp;
   5.500 +#endif
   5.501 +}
   5.502 +
   5.503 +Uint16
   5.504 +SDL_AtomicIncrementThenFetch16(Uint16 * ptr)
   5.505 +{
   5.506 +#ifdef nativeIncrementThenFetch16
   5.507 +   return nativeIncrementThenFetch16(ptr);
   5.508 +#else
   5.509 +   Uint16 tmp = 0;;
   5.510 +
   5.511 +   privateWaitLock();
   5.512 +   (*ptr)+= 1;
   5.513 +   tmp = *ptr;
   5.514 +   privateUnlock();
   5.515 +
   5.516 +   return tmp;
   5.517 +#endif
   5.518 +}
   5.519 +
   5.520 +Uint16
   5.521 +SDL_AtomicDecrementThenFetch16(Uint16 * ptr)
   5.522 +{
   5.523 +#ifdef nativeDecrementThenFetch16
   5.524 +   return nativeDecrementThenFetch16(ptr);
   5.525 +#else
   5.526 +   Uint16 tmp = 0;;
   5.527 +
   5.528 +   privateWaitLock();
   5.529 +   (*ptr)-= 1;
   5.530 +   tmp = *ptr;
   5.531 +   privateUnlock();
   5.532 +
   5.533 +   return tmp;
   5.534 +#endif
   5.535 +}
   5.536 +
   5.537 +Uint16
   5.538 +SDL_AtomicAddThenFetch16(Uint16 * ptr, Uint16 value)
   5.539 +{
   5.540 +#ifdef nativeAddThenFetch16
   5.541 +   return nativeAddThenFetch16(ptr, value);
   5.542 +#else
   5.543 +   Uint16 tmp = 0;;
   5.544 +
   5.545 +   privateWaitLock();
   5.546 +   (*ptr)+= value;
   5.547 +   tmp = *ptr;
   5.548 +   privateUnlock();
   5.549 +
   5.550 +   return tmp;
   5.551 +#endif
   5.552 +}
   5.553 +
   5.554 +Uint16
   5.555 +SDL_AtomicSubtractThenFetch16(Uint16 * ptr, Uint16 value)
   5.556 +{
   5.557 +#ifdef nativeSubtractThenFetch16
   5.558 +   return nativeSubtractThenFetch16(ptr, value);
   5.559 +#else
   5.560 +   Uint16 tmp = 0;;
   5.561 +
   5.562 +   privateWaitLock();
   5.563 +   (*ptr)-= value;
   5.564 +   tmp = *ptr;
   5.565 +   privateUnlock();
   5.566 +
   5.567 +   return tmp;
   5.568 +#endif
   5.569 +}
   5.570 +
   5.571 +/* 32 bit atomic operations */
   5.572  
   5.573  Uint32
   5.574  SDL_AtomicExchange32(Uint32 * ptr, Uint32 value)
   5.575  {
   5.576 -   return 0;
   5.577 +#ifdef nativeExchange32
   5.578 +   return nativeExchange32(ptr, value);
   5.579 +#else
   5.580 +   Uint32 tmp = 0;;
   5.581 +
   5.582 +   privateWaitLock();
   5.583 +   tmp = *ptr;
   5.584 +   *ptr = value;
   5.585 +   privateUnlock();
   5.586 +
   5.587 +   return tmp;
   5.588 +#endif
   5.589  }
   5.590  
   5.591  SDL_bool
   5.592  SDL_AtomicCompareThenSet32(Uint32 * ptr, Uint32 oldvalue, Uint32 newvalue)
   5.593  {
   5.594 -   return SDL_false;
   5.595 +#ifdef nativeCompareThenSet32
   5.596 +   return (SDL_bool)nativeCompareThenSet32(ptr, oldvalue, newvalue);
   5.597 +#else
   5.598 +   SDL_bool result = SDL_FALSE;
   5.599 +
   5.600 +   privateWaitLock();
   5.601 +   result = (*ptr == oldvalue);
   5.602 +   if (result)
   5.603 +   {
   5.604 +      *ptr = newvalue;
   5.605 +   }
   5.606 +   privateUnlock();
   5.607 +
   5.608 +   return result;
   5.609 +#endif
   5.610  }
   5.611  
   5.612  SDL_bool
   5.613  SDL_AtomicTestThenSet32(Uint32 * ptr)
   5.614  {
   5.615 -   return SDL_false;
   5.616 +#ifdef nativeTestThenSet32
   5.617 +   return (SDL_bool)nativeTestThenSet32(ptr);
   5.618 +#else
   5.619 +   SDL_bool result = SDL_FALSE;
   5.620 +
   5.621 +   privateWaitLock();
   5.622 +   result = (*ptr == 0);
   5.623 +   if (result)
   5.624 +   {
   5.625 +      *ptr = 1;
   5.626 +   }
   5.627 +   privateUnlock();
   5.628 +
   5.629 +   return result;
   5.630 +#endif
   5.631  }
   5.632  
   5.633  void
   5.634  SDL_AtomicClear32(Uint32 * ptr)
   5.635  {
   5.636 +#ifdef nativeClear32
   5.637 +   nativeClear32(ptr);
   5.638 +#else
   5.639 +   privateWaitLock();
   5.640 +   *ptr = 0;
   5.641 +   privateUnlock();
   5.642 +
   5.643 +   return;
   5.644 +#endif
   5.645  }
   5.646  
   5.647  Uint32
   5.648  SDL_AtomicFetchThenIncrement32(Uint32 * ptr)
   5.649  {
   5.650 -   return 0;
   5.651 +#ifdef nativeFetchThenIncrement32
   5.652 +   return nativeFetchThenIncrement32(ptr);
   5.653 +#else
   5.654 +   Uint32 tmp = 0;;
   5.655 +
   5.656 +   privateWaitLock();
   5.657 +   tmp = *ptr;
   5.658 +   (*ptr)+= 1;
   5.659 +   privateUnlock();
   5.660 +
   5.661 +   return tmp;
   5.662 +#endif
   5.663  }
   5.664  
   5.665  Uint32
   5.666  SDL_AtomicFetchThenDecrement32(Uint32 * ptr)
   5.667  {
   5.668 -   return 0;
   5.669 +#ifdef nativeFetchThenDecrement32
   5.670 +   return nativeFetchThenDecrement32(ptr);
   5.671 +#else
   5.672 +   Uint32 tmp = 0;;
   5.673 +
   5.674 +   privateWaitLock();
   5.675 +   tmp = *ptr;
   5.676 +   (*ptr) -= 1;
   5.677 +   privateUnlock();
   5.678 +
   5.679 +   return tmp;
   5.680 +#endif
   5.681  }
   5.682  
   5.683  Uint32
   5.684  SDL_AtomicFetchThenAdd32(Uint32 * ptr, Uint32 value)
   5.685  {
   5.686 -   return 0;
   5.687 +#ifdef nativeFetchThenAdd32
   5.688 +   return nativeFetchThenAdd32(ptr, value);
   5.689 +#else
   5.690 +   Uint32 tmp = 0;;
   5.691 +
   5.692 +   privateWaitLock();
   5.693 +   tmp = *ptr;
   5.694 +   (*ptr)+= value;
   5.695 +   privateUnlock();
   5.696 +
   5.697 +   return tmp;
   5.698 +#endif
   5.699  }
   5.700  
   5.701  Uint32
   5.702  SDL_AtomicFetchThenSubtract32(Uint32 * ptr, Uint32 value)
   5.703  {
   5.704 -   return 0;
   5.705 +#ifdef nativeFetchThenSubtract32
   5.706 +   return nativeFetchThenSubtract32(ptr, value);
   5.707 +#else
   5.708 +   Uint32 tmp = 0;;
   5.709 +
   5.710 +   privateWaitLock();
   5.711 +   tmp = *ptr;
   5.712 +   (*ptr)-= value;
   5.713 +   privateUnlock();
   5.714 +
   5.715 +   return tmp;
   5.716 +#endif
   5.717  }
   5.718  
   5.719  Uint32
   5.720  SDL_AtomicIncrementThenFetch32(Uint32 * ptr)
   5.721  {
   5.722 -   return 0;
   5.723 +#ifdef nativeIncrementThenFetch32
   5.724 +   return nativeIncrementThenFetch32(ptr);
   5.725 +#else
   5.726 +   Uint32 tmp = 0;;
   5.727 +
   5.728 +   privateWaitLock();
   5.729 +   (*ptr)+= 1;
   5.730 +   tmp = *ptr;
   5.731 +   privateUnlock();
   5.732 +
   5.733 +   return tmp;
   5.734 +#endif
   5.735  }
   5.736  
   5.737  Uint32
   5.738  SDL_AtomicDecrementThenFetch32(Uint32 * ptr)
   5.739  {
   5.740 -   return 0;
   5.741 +#ifdef nativeDecrementThenFetch32
   5.742 +   return nativeDecrementThenFetch32(ptr);
   5.743 +#else
   5.744 +   Uint32 tmp = 0;;
   5.745 +
   5.746 +   privateWaitLock();
   5.747 +   (*ptr)-= 1;
   5.748 +   tmp = *ptr;
   5.749 +   privateUnlock();
   5.750 +
   5.751 +   return tmp;
   5.752 +#endif
   5.753  }
   5.754  
   5.755  Uint32
   5.756  SDL_AtomicAddThenFetch32(Uint32 * ptr, Uint32 value)
   5.757  {
   5.758 -   return 0;
   5.759 +#ifdef nativeAddThenFetch32
   5.760 +   return nativeAddThenFetch32(ptr, value);
   5.761 +#else
   5.762 +   Uint32 tmp = 0;;
   5.763 +
   5.764 +   privateWaitLock();
   5.765 +   (*ptr)+= value;
   5.766 +   tmp = *ptr;
   5.767 +   privateUnlock();
   5.768 +
   5.769 +   return tmp;
   5.770 +#endif
   5.771  }
   5.772  
   5.773  Uint32
   5.774  SDL_AtomicSubtractThenFetch32(Uint32 * ptr, Uint32 value)
   5.775  {
   5.776 -   return 0;
   5.777 +#ifdef nativeSubtractThenFetch32
   5.778 +   return nativeSubtractThenFetch32(ptr, value);
   5.779 +#else
   5.780 +   Uint32 tmp = 0;;
   5.781 +
   5.782 +   privateWaitLock();
   5.783 +   (*ptr)-= value;
   5.784 +   tmp = *ptr;
   5.785 +   privateUnlock();
   5.786 +
   5.787 +   return tmp;
   5.788 +#endif
   5.789  }
   5.790  
   5.791 -
   5.792 +/* 64 bit atomic operations */
   5.793  #ifdef SDL_HAS_64BIT_TYPE
   5.794  
   5.795  Uint64
   5.796  SDL_AtomicExchange64(Uint64 * ptr, Uint64 value)
   5.797  {
   5.798 -   return 0;
   5.799 +#ifdef nativeExchange64
   5.800 +   return nativeExchange64(ptr, value);
   5.801 +#else
   5.802 +   Uint64 tmp = 0;;
   5.803 +
   5.804 +   privateWaitLock();
   5.805 +   tmp = *ptr;
   5.806 +   *ptr = value;
   5.807 +   privateUnlock();
   5.808 +
   5.809 +   return tmp;
   5.810 +#endif
   5.811  }
   5.812  
   5.813  SDL_bool
   5.814 -SDL_AtomicCompareThenSet64(Uint64 * ptr,
   5.815 -Uint64 oldvalue, Uint64 newvalue)
   5.816 +SDL_AtomicCompareThenSet64(Uint64 * ptr, Uint64 oldvalue, Uint64 newvalue)
   5.817  {
   5.818 -   return SDL_false;
   5.819 +#ifdef nativeCompareThenSet64
   5.820 +   return (SDL_bool)nativeCompareThenSet64(ptr, oldvalue, newvalue);
   5.821 +#else
   5.822 +   SDL_bool result = SDL_FALSE;
   5.823 +
   5.824 +   privateWaitLock();
   5.825 +   result = (*ptr == oldvalue);
   5.826 +   if (result)
   5.827 +   {
   5.828 +      *ptr = newvalue;
   5.829 +   }
   5.830 +   privateUnlock();
   5.831 +
   5.832 +   return result;
   5.833 +#endif
   5.834  }
   5.835  
   5.836  SDL_bool
   5.837  SDL_AtomicTestThenSet64(Uint64 * ptr)
   5.838  {
   5.839 -   return SDL_false;
   5.840 +#ifdef nativeTestThenSet64
   5.841 +   return (SDL_bool)nativeTestThenSet64(ptr);
   5.842 +#else
   5.843 +   SDL_bool result = SDL_FALSE;
   5.844 +
   5.845 +   privateWaitLock();
   5.846 +   result = (*ptr == 0);
   5.847 +   if (result)
   5.848 +   {
   5.849 +      *ptr = 1;
   5.850 +   }
   5.851 +   privateUnlock();
   5.852 +
   5.853 +   return result;
   5.854 +#endif
   5.855  }
   5.856  
   5.857  void
   5.858  SDL_AtomicClear64(Uint64 * ptr)
   5.859  {
   5.860 +#ifdef nativeClear64
   5.861 +   nativeClear64(ptr);
   5.862 +#else
   5.863 +   privateWaitLock();
   5.864 +   *ptr = 0;
   5.865 +   privateUnlock();
   5.866 +
   5.867 +   return;
   5.868 +#endif
   5.869  }
   5.870  
   5.871  Uint64
   5.872  SDL_AtomicFetchThenIncrement64(Uint64 * ptr)
   5.873  {
   5.874 -   return 0;
   5.875 +#ifdef nativeFetchThenIncrement64
   5.876 +   return nativeFetchThenIncrement64(ptr);
   5.877 +#else
   5.878 +   Uint64 tmp = 0;;
   5.879 +
   5.880 +   privateWaitLock();
   5.881 +   tmp = *ptr;
   5.882 +   (*ptr)+= 1;
   5.883 +   privateUnlock();
   5.884 +
   5.885 +   return tmp;
   5.886 +#endif
   5.887  }
   5.888  
   5.889  Uint64
   5.890  SDL_AtomicFetchThenDecrement64(Uint64 * ptr)
   5.891  {
   5.892 -   return 0;
   5.893 +#ifdef nativeFetchThenDecrement64
   5.894 +   return nativeFetchThenDecrement64(ptr);
   5.895 +#else
   5.896 +   Uint64 tmp = 0;;
   5.897 +
   5.898 +   privateWaitLock();
   5.899 +   tmp = *ptr;
   5.900 +   (*ptr) -= 1;
   5.901 +   privateUnlock();
   5.902 +
   5.903 +   return tmp;
   5.904 +#endif
   5.905  }
   5.906  
   5.907  Uint64
   5.908  SDL_AtomicFetchThenAdd64(Uint64 * ptr, Uint64 value)
   5.909  {
   5.910 -   return 0;
   5.911 +#ifdef nativeFetchThenAdd64
   5.912 +   return nativeFetchThenAdd64(ptr, value);
   5.913 +#else
   5.914 +   Uint64 tmp = 0;;
   5.915 +
   5.916 +   privateWaitLock();
   5.917 +   tmp = *ptr;
   5.918 +   (*ptr)+= value;
   5.919 +   privateUnlock();
   5.920 +
   5.921 +   return tmp;
   5.922 +#endif
   5.923  }
   5.924  
   5.925  Uint64
   5.926  SDL_AtomicFetchThenSubtract64(Uint64 * ptr, Uint64 value)
   5.927  {
   5.928 -   return 0;
   5.929 +#ifdef nativeFetchThenSubtract64
   5.930 +   return nativeFetchThenSubtract64(ptr, value);
   5.931 +#else
   5.932 +   Uint64 tmp = 0;;
   5.933 +
   5.934 +   privateWaitLock();
   5.935 +   tmp = *ptr;
   5.936 +   (*ptr)-= value;
   5.937 +   privateUnlock();
   5.938 +
   5.939 +   return tmp;
   5.940 +#endif
   5.941  }
   5.942  
   5.943  Uint64
   5.944  SDL_AtomicIncrementThenFetch64(Uint64 * ptr)
   5.945  {
   5.946 -   return 0;
   5.947 +#ifdef nativeIncrementThenFetch64
   5.948 +   return nativeIncrementThenFetch64(ptr);
   5.949 +#else
   5.950 +   Uint64 tmp = 0;;
   5.951 +
   5.952 +   privateWaitLock();
   5.953 +   (*ptr)+= 1;
   5.954 +   tmp = *ptr;
   5.955 +   privateUnlock();
   5.956 +
   5.957 +   return tmp;
   5.958 +#endif
   5.959  }
   5.960  
   5.961  Uint64
   5.962  SDL_AtomicDecrementThenFetch64(Uint64 * ptr)
   5.963  {
   5.964 -   return 0;
   5.965 +#ifdef nativeDecrementThenFetch64
   5.966 +   return nativeDecrementThenFetch64(ptr);
   5.967 +#else
   5.968 +   Uint64 tmp = 0;;
   5.969 +
   5.970 +   privateWaitLock();
   5.971 +   (*ptr)-= 1;
   5.972 +   tmp = *ptr;
   5.973 +   privateUnlock();
   5.974 +
   5.975 +   return tmp;
   5.976 +#endif
   5.977  }
   5.978  
   5.979  Uint64
   5.980  SDL_AtomicAddThenFetch64(Uint64 * ptr, Uint64 value)
   5.981  {
   5.982 -   return 0;
   5.983 +#ifdef nativeAddThenFetch64
   5.984 +   return nativeAddThenFetch64(ptr, value);
   5.985 +#else
   5.986 +   Uint64 tmp = 0;;
   5.987 +
   5.988 +   privateWaitLock();
   5.989 +   (*ptr)+= value;
   5.990 +   tmp = *ptr;
   5.991 +   privateUnlock();
   5.992 +
   5.993 +   return tmp;
   5.994 +#endif
   5.995  }
   5.996  
   5.997  Uint64
   5.998  SDL_AtomicSubtractThenFetch64(Uint64 * ptr, Uint64 value)
   5.999  {
  5.1000 -   return 0;
  5.1001 +#ifdef nativeSubtractThenFetch64
  5.1002 +   return nativeSubtractThenFetch64(ptr, value);
  5.1003 +#else
  5.1004 +   Uint64 tmp = 0;;
  5.1005 +
  5.1006 +   privateWaitLock();
  5.1007 +   (*ptr)-= value;
  5.1008 +   tmp = *ptr;
  5.1009 +   privateUnlock();
  5.1010 +
  5.1011 +   return tmp;
  5.1012 +#endif
  5.1013  }
  5.1014 +#endif
  5.1015  
  5.1016 -#endif
  5.1017 -#endif
     6.1 --- a/test/testatomic.c	Wed Jun 24 22:24:23 2009 +0000
     6.2 +++ b/test/testatomic.c	Mon Jun 29 19:54:43 2009 +0000
     6.3 @@ -1,50 +1,232 @@
     6.4  #include "SDL.h"
     6.5  
     6.6  /*
     6.7 -  Absolutely basic test just to see if we get the expected value after
     6.8 -  calling each function.
     6.9 +  Absolutely basic tests just to see if we get the expected value
    6.10 +  after calling each function.
    6.11  */
    6.12  
    6.13 +char * 
    6.14 +tf(SDL_bool tf)
    6.15 +{
    6.16 +   static char *t = "true";
    6.17 +   static char *f = "false";
    6.18 +
    6.19 +   if (tf)
    6.20 +   {
    6.21 +      return t;
    6.22 +   }
    6.23 +
    6.24 +   return f;
    6.25 +}
    6.26 +  
    6.27  int
    6.28  main(int argc, char **argv)
    6.29  {
    6.30  
    6.31 +   Uint8 val8 = 0;
    6.32 +   Uint8 ret8 = 0;
    6.33 +
    6.34 +   Uint16 val16 = 0;
    6.35 +   Uint16 ret16 = 0;
    6.36 +
    6.37     Uint32 val32 = 0;
    6.38     Uint32 ret32 = 0;
    6.39  
    6.40     Uint64 val64 = 0;
    6.41     Uint64 ret64 = 0;
    6.42  
    6.43 -   SDL_bool tfval = SDL_FALSE;
    6.44 +   SDL_bool tfret = SDL_FALSE;
    6.45 +
    6.46 +
    6.47 +   printf("8 bit -----------------------------------------\n\n");
    6.48 +
    6.49 +   ret8 = SDL_AtomicExchange8(&val8, 10);
    6.50 +   printf("Exchange8           ret=%d val=%d\n", ret8, val8);
    6.51 +   ret8 = SDL_AtomicExchange8(&val8, 0);
    6.52 +   printf("Exchange8           ret=%d val=%d\n", ret8, val8);
    6.53 +
    6.54 +   val8 = 10;
    6.55 +   tfret = SDL_AtomicCompareThenSet8(&val8, 10, 20);
    6.56 +   printf("CompareThenSet8     tfret=%s val=%d\n", tf(tfret), val8);
    6.57 +   val8 = 10;
    6.58 +   tfret = SDL_AtomicCompareThenSet8(&val8, 0, 20);
    6.59 +   printf("CompareThenSet8     tfret=%s val=%d\n", tf(tfret), val8);
    6.60 +
    6.61 +   val8 = 0;
    6.62 +   tfret = SDL_AtomicTestThenSet8(&val8);
    6.63 +   printf("TestThenSet8        tfret=%s val=%d\n", tf(tfret), val8);
    6.64 +   tfret = SDL_AtomicTestThenSet8(&val8);
    6.65 +   printf("TestThenSet8        tfret=%s val=%d\n", tf(tfret), val8);
    6.66 +
    6.67 +   SDL_AtomicClear8(&val8);
    6.68 +   printf("Clear8              val=%d\n", val8);
    6.69 +
    6.70 +   ret8 = SDL_AtomicFetchThenIncrement8(&val8);
    6.71 +   printf("FetchThenIncrement8 ret=%d val=%d\n", ret8, val8);
    6.72 +
    6.73 +   ret8 = SDL_AtomicFetchThenDecrement8(&val8);
    6.74 +   printf("FetchThenDecrement8 ret=%d val=%d\n", ret8, val8);
    6.75 +
    6.76 +   ret8 = SDL_AtomicFetchThenAdd8(&val8, 10);
    6.77 +   printf("FetchThenAdd8       ret=%d val=%d\n", ret8, val8);
    6.78 +
    6.79 +   ret8 = SDL_AtomicFetchThenSubtract8(&val8, 10);
    6.80 +   printf("FetchThenSubtract8  ret=%d val=%d\n", ret8, val8);
    6.81 +
    6.82 +   ret8 = SDL_AtomicIncrementThenFetch8(&val8);
    6.83 +   printf("IncrementThenFetch8 ret=%d val=%d\n", ret8, val8);
    6.84 +
    6.85 +   ret8 = SDL_AtomicDecrementThenFetch8(&val8);
    6.86 +   printf("DecrementThenFetch8 ret=%d val=%d\n", ret8, val8);
    6.87 +
    6.88 +   ret8 = SDL_AtomicAddThenFetch8(&val8, 10);
    6.89 +   printf("AddThenFetch8       ret=%d val=%d\n", ret8, val8);
    6.90 +
    6.91 +   ret8 = SDL_AtomicSubtractThenFetch8(&val8, 10);
    6.92 +   printf("SubtractThenFetch8  ret=%d val=%d\n", ret8, val8);
    6.93 +
    6.94 +
    6.95 +   printf("16 bit -----------------------------------------\n\n");
    6.96 +
    6.97 +   ret16 = SDL_AtomicExchange16(&val16, 10);
    6.98 +   printf("Exchange16           ret=%d val=%d\n", ret16, val16);
    6.99 +   ret16 = SDL_AtomicExchange16(&val16, 0);
   6.100 +   printf("Exchange16           ret=%d val=%d\n", ret16, val16);
   6.101 +
   6.102 +   val16 = 10;
   6.103 +   tfret = SDL_AtomicCompareThenSet16(&val16, 10, 20);
   6.104 +   printf("CompareThenSet16     tfret=%s val=%d\n", tf(tfret), val16);
   6.105 +   val16 = 10;
   6.106 +   tfret = SDL_AtomicCompareThenSet16(&val16, 0, 20);
   6.107 +   printf("CompareThenSet16     tfret=%s val=%d\n", tf(tfret), val16);
   6.108 +
   6.109 +   val16 = 0;
   6.110 +   tfret = SDL_AtomicTestThenSet16(&val16);
   6.111 +   printf("TestThenSet16        tfret=%s val=%d\n", tf(tfret), val16);
   6.112 +   tfret = SDL_AtomicTestThenSet16(&val16);
   6.113 +   printf("TestThenSet16        tfret=%s val=%d\n", tf(tfret), val16);
   6.114 +
   6.115 +   SDL_AtomicClear16(&val16);
   6.116 +   printf("Clear16              val=%d\n", val16);
   6.117 +
   6.118 +   ret16 = SDL_AtomicFetchThenIncrement16(&val16);
   6.119 +   printf("FetchThenIncrement16 ret=%d val=%d\n", ret16, val16);
   6.120 +
   6.121 +   ret16 = SDL_AtomicFetchThenDecrement16(&val16);
   6.122 +   printf("FetchThenDecrement16 ret=%d val=%d\n", ret16, val16);
   6.123 +
   6.124 +   ret16 = SDL_AtomicFetchThenAdd16(&val16, 10);
   6.125 +   printf("FetchThenAdd16       ret=%d val=%d\n", ret16, val16);
   6.126 +
   6.127 +   ret16 = SDL_AtomicFetchThenSubtract16(&val16, 10);
   6.128 +   printf("FetchThenSubtract16  ret=%d val=%d\n", ret16, val16);
   6.129 +
   6.130 +   ret16 = SDL_AtomicIncrementThenFetch16(&val16);
   6.131 +   printf("IncrementThenFetch16 ret=%d val=%d\n", ret16, val16);
   6.132 +
   6.133 +   ret16 = SDL_AtomicDecrementThenFetch16(&val16);
   6.134 +   printf("DecrementThenFetch16 ret=%d val=%d\n", ret16, val16);
   6.135 +
   6.136 +   ret16 = SDL_AtomicAddThenFetch16(&val16, 10);
   6.137 +   printf("AddThenFetch16       ret=%d val=%d\n", ret16, val16);
   6.138 +
   6.139 +   ret16 = SDL_AtomicSubtractThenFetch16(&val16, 10);
   6.140 +   printf("SubtractThenFetch16  ret=%d val=%d\n", ret16, val16);
   6.141 +
   6.142 +   printf("32 bit -----------------------------------------\n\n");
   6.143  
   6.144     ret32 = SDL_AtomicExchange32(&val32, 10);
   6.145 -   tfval = SDL_AtomicCompareThenSet32(&val32, 10, 20);
   6.146 -   tfval = SDL_AtomicTestThenSet32(&val32);
   6.147 +   printf("Exchange32           ret=%d val=%d\n", ret32, val32);
   6.148 +   ret32 = SDL_AtomicExchange32(&val32, 0);
   6.149 +   printf("Exchange32           ret=%d val=%d\n", ret32, val32);
   6.150 +
   6.151 +   val32 = 10;
   6.152 +   tfret = SDL_AtomicCompareThenSet32(&val32, 10, 20);
   6.153 +   printf("CompareThenSet32     tfret=%s val=%d\n", tf(tfret), val32);
   6.154 +   val32 = 10;
   6.155 +   tfret = SDL_AtomicCompareThenSet32(&val32, 0, 20);
   6.156 +   printf("CompareThenSet32     tfret=%s val=%d\n", tf(tfret), val32);
   6.157 +
   6.158 +   val32 = 0;
   6.159 +   tfret = SDL_AtomicTestThenSet32(&val32);
   6.160 +   printf("TestThenSet32        tfret=%s val=%d\n", tf(tfret), val32);
   6.161 +   tfret = SDL_AtomicTestThenSet32(&val32);
   6.162 +   printf("TestThenSet32        tfret=%s val=%d\n", tf(tfret), val32);
   6.163 +
   6.164     SDL_AtomicClear32(&val32);
   6.165 +   printf("Clear32              val=%d\n", val32);
   6.166 +
   6.167     ret32 = SDL_AtomicFetchThenIncrement32(&val32);
   6.168 +   printf("FetchThenIncrement32 ret=%d val=%d\n", ret32, val32);
   6.169 +
   6.170     ret32 = SDL_AtomicFetchThenDecrement32(&val32);
   6.171 +   printf("FetchThenDecrement32 ret=%d val=%d\n", ret32, val32);
   6.172 +
   6.173     ret32 = SDL_AtomicFetchThenAdd32(&val32, 10);
   6.174 +   printf("FetchThenAdd32       ret=%d val=%d\n", ret32, val32);
   6.175 +
   6.176     ret32 = SDL_AtomicFetchThenSubtract32(&val32, 10);
   6.177 +   printf("FetchThenSubtract32  ret=%d val=%d\n", ret32, val32);
   6.178 +
   6.179     ret32 = SDL_AtomicIncrementThenFetch32(&val32);
   6.180 +   printf("IncrementThenFetch32 ret=%d val=%d\n", ret32, val32);
   6.181 +
   6.182     ret32 = SDL_AtomicDecrementThenFetch32(&val32);
   6.183 +   printf("DecrementThenFetch32 ret=%d val=%d\n", ret32, val32);
   6.184 +
   6.185     ret32 = SDL_AtomicAddThenFetch32(&val32, 10);
   6.186 +   printf("AddThenFetch32       ret=%d val=%d\n", ret32, val32);
   6.187 +
   6.188     ret32 = SDL_AtomicSubtractThenFetch32(&val32, 10);
   6.189 +   printf("SubtractThenFetch32  ret=%d val=%d\n", ret32, val32);
   6.190  
   6.191 -/* #ifdef SDL_HAS_64BIT_TYPE */
   6.192 -#if 0
   6.193 +#ifdef SDL_HAS_64BIT_TYPE
   6.194 +   printf("64 bit -----------------------------------------\n\n");
   6.195  
   6.196     ret64 = SDL_AtomicExchange64(&val64, 10);
   6.197 -   tfval = SDL_AtomicCompareThenSet64(&val64, 10, 20);
   6.198 -   tfval = SDL_AtomicTestThenSet64(&val64);
   6.199 +   printf("Exchange64           ret=%lld val=%lld\n", ret64, val64);
   6.200 +   ret64 = SDL_AtomicExchange64(&val64, 0);
   6.201 +   printf("Exchange64           ret=%lld val=%lld\n", ret64, val64);
   6.202 +
   6.203 +   val64 = 10;
   6.204 +   tfret = SDL_AtomicCompareThenSet64(&val64, 10, 20);
   6.205 +   printf("CompareThenSet64     tfret=%s val=%lld\n", tf(tfret), val64);
   6.206 +   val64 = 10;
   6.207 +   tfret = SDL_AtomicCompareThenSet64(&val64, 0, 20);
   6.208 +   printf("CompareThenSet64     tfret=%s val=%lld\n", tf(tfret), val64);
   6.209 +
   6.210 +   val64 = 0;
   6.211 +   tfret = SDL_AtomicTestThenSet64(&val64);
   6.212 +   printf("TestThenSet64        tfret=%s val=%lld\n", tf(tfret), val64);
   6.213 +   tfret = SDL_AtomicTestThenSet64(&val64);
   6.214 +   printf("TestThenSet64        tfret=%s val=%lld\n", tf(tfret), val64);
   6.215 +
   6.216     SDL_AtomicClear64(&val64);
   6.217 +   printf("Clear64              val=%lld\n", val64);
   6.218 +
   6.219     ret64 = SDL_AtomicFetchThenIncrement64(&val64);
   6.220 +   printf("FetchThenIncrement64 ret=%lld val=%lld\n", ret64, val64);
   6.221 +
   6.222     ret64 = SDL_AtomicFetchThenDecrement64(&val64);
   6.223 +   printf("FetchThenDecrement64 ret=%lld val=%lld\n", ret64, val64);
   6.224 +
   6.225     ret64 = SDL_AtomicFetchThenAdd64(&val64, 10);
   6.226 +   printf("FetchThenAdd64       ret=%lld val=%lld\n", ret64, val64);
   6.227 +
   6.228     ret64 = SDL_AtomicFetchThenSubtract64(&val64, 10);
   6.229 +   printf("FetchThenSubtract64  ret=%lld val=%lld\n", ret64, val64);
   6.230 +
   6.231     ret64 = SDL_AtomicIncrementThenFetch64(&val64);
   6.232 +   printf("IncrementThenFetch64 ret=%lld val=%lld\n", ret64, val64);
   6.233 +
   6.234     ret64 = SDL_AtomicDecrementThenFetch64(&val64);
   6.235 +   printf("DecrementThenFetch64 ret=%lld val=%lld\n", ret64, val64);
   6.236 +
   6.237     ret64 = SDL_AtomicAddThenFetch64(&val64, 10);
   6.238 +   printf("AddThenFetch64       ret=%lld val=%lld\n", ret64, val64);
   6.239 +
   6.240     ret64 = SDL_AtomicSubtractThenFetch64(&val64, 10);
   6.241 +   printf("SubtractThenFetch64  ret=%lld val=%lld\n", ret64, val64);
   6.242  #endif
   6.243  
   6.244     return 0;