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