Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
authorSam Lantinga <slouken@libsdl.org>
Sun, 16 Jan 2011 15:16:39 -0800
changeset 50040c72ae7b7cb2
parent 5003 3a95a2b93eb3
child 5005 7f0265279b68
Added native atomic operations for Windows, Mac OS X, and gcc compiler intrinsics.
Changed the CAS return value to bool, so it's efficient with OSAtomicCompareAndSwap32Barrier()
Added an atomic test adapted from code by Michael Davidsaver
Xcode/SDLTest/SDLTest.xcodeproj/project.pbxproj
configure.in
include/SDL_atomic.h
include/SDL_config.h.in
include/SDL_config_iphoneos.h
src/atomic/SDL_atomic.c
src/atomic/SDL_spinlock.c
test/testatomic.c
     1.1 --- a/Xcode/SDLTest/SDLTest.xcodeproj/project.pbxproj	Sat Jan 15 12:41:59 2011 -0800
     1.2 +++ b/Xcode/SDLTest/SDLTest.xcodeproj/project.pbxproj	Sun Jan 16 15:16:39 2011 -0800
     1.3 @@ -3985,7 +3985,14 @@
     1.4  			isa = PBXProject;
     1.5  			buildConfigurationList = 001B5A0C08BDB826006539E9 /* Build configuration list for PBXProject "SDLTest" */;
     1.6  			compatibilityVersion = "Xcode 3.0";
     1.7 +			developmentRegion = English;
     1.8  			hasScannedForEncodings = 1;
     1.9 +			knownRegions = (
    1.10 +				English,
    1.11 +				Japanese,
    1.12 +				French,
    1.13 +				German,
    1.14 +			);
    1.15  			mainGroup = 08FB7794FE84155DC02AAC07 /* SDLTest */;
    1.16  			projectDirPath = "";
    1.17  			projectReferences = (
     2.1 --- a/configure.in	Sat Jan 15 12:41:59 2011 -0800
     2.2 +++ b/configure.in	Sun Jan 16 15:16:39 2011 -0800
     2.3 @@ -181,7 +181,7 @@
     2.4          DEPENDENCY_TRACKING_OPTIONS="-MMD -MT \$@"
     2.5      fi
     2.6  fi
     2.7 -    
     2.8 +
     2.9  dnl See whether we are allowed to use the system C library
    2.10  AC_ARG_ENABLE(libc,
    2.11  AC_HELP_STRING([--enable-libc], [Use the system C library [[default=yes]]]),
    2.12 @@ -285,6 +285,32 @@
    2.13      AC_DEFINE(uintptr_t, unsigned long)
    2.14  fi
    2.15  
    2.16 +dnl See whether we can use gcc atomic operations on this architecture
    2.17 +AC_ARG_ENABLE(gcc-atomics,
    2.18 +AC_HELP_STRING([--enable-gcc-atomics],
    2.19 +               [Use gcc builtin atomics [[default=yes]]]),
    2.20 +              , enable_gcc_atomics=yes)
    2.21 +if test x$enable_gcc_atomics = xyes; then
    2.22 +    have_gcc_atomics=no
    2.23 +    AC_MSG_CHECKING(for GCC builtin atomic operations)
    2.24 +    AC_TRY_LINK([
    2.25 +    ],[
    2.26 +    int a;
    2.27 +    void *x, *y, *z;
    2.28 +    __sync_lock_test_and_set(&a, 4);
    2.29 +    __sync_fetch_and_add(&a, 1);
    2.30 +    __sync_bool_compare_and_swap(&a, 5, 10);
    2.31 +    __sync_bool_compare_and_swap(&x, y, z);
    2.32 +    ],[
    2.33 +    have_gcc_atomics=yes
    2.34 +    ])
    2.35 +    AC_MSG_RESULT($have_gcc_atomics)
    2.36 +
    2.37 +    if test x$have_gcc_mmd_mt = xyes; then
    2.38 +        AC_DEFINE(HAVE_GCC_ATOMICS)
    2.39 +    fi
    2.40 +fi
    2.41 +
    2.42  # Standard C sources
    2.43  SOURCES="$SOURCES $srcdir/src/*.c"
    2.44  SOURCES="$SOURCES $srcdir/src/audio/*.c"
     3.1 --- a/include/SDL_atomic.h	Sat Jan 15 12:41:59 2011 -0800
     3.2 +++ b/include/SDL_atomic.h	Sun Jan 16 15:16:39 2011 -0800
     3.3 @@ -108,7 +108,62 @@
     3.4  /*@}*//*SDL AtomicLock*/
     3.5  
     3.6  /* Platform specific optimized versions of the atomic functions */
     3.7 -/* None yet... */
     3.8 +#if defined(__WIN32__)
     3.9 +#define WIN32_LEAN_AND_MEAN
    3.10 +#include <windows.h>
    3.11 +
    3.12 +#define SDL_AtomicSet(a, v)     InterlockedExchange(&(a)->value, v)
    3.13 +#define SDL_AtomicGet(a)        ((a)->value)
    3.14 +#define SDL_AtomicAdd(a, v)     InterlockedAdd(&(a)->value, v)
    3.15 +#define SDL_AtomicCAS(a, oldval, newval) (InterlockedCompareExchange(&(a)->value, newval, oldval) == (oldval))
    3.16 +#define SDL_AtomicSetPtr(a, v)  InterlockedExchangePointer(a, v)
    3.17 +#define SDL_AtomicGetPtr(a)     (*(a))
    3.18 +#define SDL_AtomicCASPtr(a, oldval, newval) (InterlockedCompareExchangePointer(a, newval, oldval) == (oldval))
    3.19 +
    3.20 +#elif defined(__MACOSX__)
    3.21 +#include <libkern/OSAtomic.h>
    3.22 +
    3.23 +#define SDL_AtomicSet(a, v) \
    3.24 +({                          \
    3.25 +    int oldvalue;           \
    3.26 +                            \
    3.27 +    do {                    \
    3.28 +        oldvalue = (a)->value; \
    3.29 +    } while (!OSAtomicCompareAndSwap32Barrier(oldvalue, v, &(a)->value)); \
    3.30 +                            \
    3.31 +    oldvalue;               \
    3.32 +})
    3.33 +#define SDL_AtomicGet(a)        ((a)->value)
    3.34 +#define SDL_AtomicAdd(a, v) \
    3.35 +({                          \
    3.36 +    int oldvalue;           \
    3.37 +                            \
    3.38 +    do {                    \
    3.39 +        oldvalue = (a)->value; \
    3.40 +    } while (!OSAtomicCompareAndSwap32Barrier(oldvalue, oldvalue+v, &(a)->value)); \
    3.41 +                            \
    3.42 +    oldvalue;               \
    3.43 +})
    3.44 +#define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier(oldval, newval, &(a)->value)
    3.45 +#define SDL_AtomicSetPtr(a, v)  (*(a) = v, OSMemoryBarrier())
    3.46 +#define SDL_AtomicGetPtr(a)     (*(a))
    3.47 +#if SIZEOF_VOIDP == 4
    3.48 +#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a))
    3.49 +#elif SIZEOF_VOIDP == 8
    3.50 +#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
    3.51 +#endif
    3.52 +
    3.53 +#elif defined(HAVE_GCC_ATOMICS)
    3.54 +
    3.55 +#define SDL_AtomicSet(a, v)     __sync_lock_test_and_set(&(a)->value, v)
    3.56 +#define SDL_AtomicGet(a)        ((a)->value)
    3.57 +#define SDL_AtomicAdd(a, v)     __sync_fetch_and_add(&(a)->value, v)
    3.58 +#define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval)
    3.59 +#define SDL_AtomicSetPtr(a, v)  (*(a) = v, __sync_synchronize())
    3.60 +#define SDL_AtomicGetPtr(a)     (*(a))
    3.61 +#define SDL_AtomicCASPtr(a, oldval, newval) __sync_bool_compare_and_swap(a, oldval, newval)
    3.62 +
    3.63 +#endif
    3.64  
    3.65  /**
    3.66   * \brief A type representing an atomic integer value.  It is a struct
    3.67 @@ -163,12 +218,12 @@
    3.68  /**
    3.69   * \brief Set an atomic variable to a new value if it is currently an old value.
    3.70   *
    3.71 - * \return The previous value of the atomic variable
    3.72 + * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
    3.73   *
    3.74   * \note If you don't know what this function is for, you shouldn't use it!
    3.75  */
    3.76  #ifndef SDL_AtomicCAS
    3.77 -extern DECLSPEC int SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
    3.78 +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
    3.79  #endif
    3.80  
    3.81  /**
    3.82 @@ -188,12 +243,12 @@
    3.83  /**
    3.84   * \brief Set a pointer to a new value if it is currently an old value.
    3.85   *
    3.86 - * \return The previous value of the pointer
    3.87 + * \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
    3.88   *
    3.89   * \note If you don't know what this function is for, you shouldn't use it!
    3.90  */
    3.91  #ifndef SDL_AtomicCASPtr
    3.92 -extern DECLSPEC void* SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval);
    3.93 +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval);
    3.94  #endif
    3.95  
    3.96  /* Ends C function definitions when using C++ */
     4.1 --- a/include/SDL_config.h.in	Sat Jan 15 12:41:59 2011 -0800
     4.2 +++ b/include/SDL_config.h.in	Sun Jan 16 15:16:39 2011 -0800
     4.3 @@ -58,6 +58,7 @@
     4.4  
     4.5  #undef SIZEOF_VOIDP
     4.6  #undef SDL_HAS_64BIT_TYPE
     4.7 +#undef HAVE_GCC_ATOMICS
     4.8  
     4.9  /* Comment this if you want to build without any C library requirements */
    4.10  #undef HAVE_LIBC
     5.1 --- a/include/SDL_config_iphoneos.h	Sat Jan 15 12:41:59 2011 -0800
     5.2 +++ b/include/SDL_config_iphoneos.h	Sun Jan 16 15:16:39 2011 -0800
     5.3 @@ -43,6 +43,8 @@
     5.4  
     5.5  #define SDL_HAS_64BIT_TYPE	1
     5.6  
     5.7 +#define HAVE_GCC_ATOMICS	1
     5.8 +
     5.9  #define HAVE_ALLOCA_H		1
    5.10  #define HAVE_SYS_TYPES_H	1
    5.11  #define HAVE_STDIO_H	1
     6.1 --- a/src/atomic/SDL_atomic.c	Sat Jan 15 12:41:59 2011 -0800
     6.2 +++ b/src/atomic/SDL_atomic.c	Sun Jan 16 15:16:39 2011 -0800
     6.3 @@ -23,6 +23,12 @@
     6.4  
     6.5  #include "SDL_atomic.h"
     6.6  
     6.7 +/* Note that we undefine the atomic operations here, in case they are
     6.8 +   defined as compiler intrinsics while building SDL but the library user
     6.9 +   doesn't have that compiler.  That way we always have a working set of
    6.10 +   atomic operations built into the library.
    6.11 +*/
    6.12 + 
    6.13  /* 
    6.14    If any of the operations are not provided then we must emulate some
    6.15    of them. That means we need a nice implementation of spin locks
    6.16 @@ -51,20 +57,20 @@
    6.17  static __inline__ void
    6.18  enterLock(void *a)
    6.19  {
    6.20 -   uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
    6.21 +    uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
    6.22  
    6.23 -   SDL_AtomicLock(&locks[index]);
    6.24 +    SDL_AtomicLock(&locks[index]);
    6.25  }
    6.26  
    6.27  static __inline__ void
    6.28  leaveLock(void *a)
    6.29  {
    6.30 -   uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
    6.31 +    uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
    6.32  
    6.33 -   SDL_AtomicUnlock(&locks[index]);
    6.34 +    SDL_AtomicUnlock(&locks[index]);
    6.35  }
    6.36  
    6.37 -#ifndef SDL_AtomicSet
    6.38 +#undef SDL_AtomicSet
    6.39  int
    6.40  SDL_AtomicSet(SDL_atomic_t *a, int value)
    6.41  {
    6.42 @@ -77,9 +83,8 @@
    6.43  
    6.44      return oldvalue;
    6.45  }
    6.46 -#endif
    6.47  
    6.48 -#ifndef SDL_AtomicGet
    6.49 +#undef SDL_AtomicGet
    6.50  int
    6.51  SDL_AtomicGet(SDL_atomic_t *a)
    6.52  {
    6.53 @@ -88,9 +93,8 @@
    6.54      */
    6.55      return a->value;
    6.56  }
    6.57 -#endif
    6.58  
    6.59 -#ifndef SDL_AtomicAdd
    6.60 +#undef SDL_AtomicAdd
    6.61  int
    6.62  SDL_AtomicAdd(SDL_atomic_t *a, int value)
    6.63  {
    6.64 @@ -103,53 +107,48 @@
    6.65  
    6.66      return oldvalue;
    6.67  }
    6.68 -#endif
    6.69  
    6.70 -#ifndef SDL_AtomicIncRef
    6.71 +#undef SDL_AtomicIncRef
    6.72  void
    6.73  SDL_AtomicIncRef(SDL_atomic_t *a)
    6.74  {
    6.75      SDL_AtomicAdd(a, 1);
    6.76  }
    6.77 -#endif
    6.78  
    6.79 -#ifndef SDL_AtomicDecRef
    6.80 +#undef SDL_AtomicDecRef
    6.81  SDL_bool
    6.82  SDL_AtomicDecRef(SDL_atomic_t *a)
    6.83  {
    6.84      return SDL_AtomicAdd(a, -1) == 1;
    6.85  }
    6.86 -#endif
    6.87  
    6.88 -#ifndef SDL_AtomicCAS
    6.89 -int
    6.90 +#undef SDL_AtomicCAS
    6.91 +SDL_bool
    6.92  SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
    6.93  {
    6.94 -    int prevval;
    6.95 +    SDL_bool retval = SDL_FALSE;
    6.96  
    6.97      enterLock(a);
    6.98 -    prevval = a->value;
    6.99 -    if (prevval == oldval) {
   6.100 +    if (a->value == oldval) {
   6.101          a->value = newval;
   6.102 +        retval = SDL_TRUE;
   6.103      }
   6.104      leaveLock(a);
   6.105  
   6.106 -    return prevval;
   6.107 +    return retval;
   6.108  }
   6.109 -#endif
   6.110  
   6.111 -#ifndef SDL_AtomicSetPtr
   6.112 +#undef SDL_AtomicSetPtr
   6.113  void
   6.114  SDL_AtomicSetPtr(void** a, void* value)
   6.115  {
   6.116      void *prevval;
   6.117      do {
   6.118          prevval = *a;
   6.119 -    } while (SDL_AtomicCASPtr(a, prevval, value) != prevval);
   6.120 +    } while (!SDL_AtomicCASPtr(a, prevval, value));
   6.121  }
   6.122 -#endif
   6.123  
   6.124 -#ifndef SDL_AtomicGetPtr
   6.125 +#undef SDL_AtomicGetPtr
   6.126  void*
   6.127  SDL_AtomicGetPtr(void** a)
   6.128  {
   6.129 @@ -158,22 +157,20 @@
   6.130      */
   6.131      return *a;
   6.132  }
   6.133 -#endif
   6.134  
   6.135 -#ifndef SDL_AtomicCASPtr
   6.136 -void* SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
   6.137 +#undef SDL_AtomicCASPtr
   6.138 +SDL_bool SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
   6.139  {
   6.140 -    void *prevval;
   6.141 +    SDL_bool retval = SDL_FALSE;
   6.142  
   6.143      enterLock(a);
   6.144 -    prevval = *a;
   6.145      if (*a == oldval) {
   6.146          *a = newval;
   6.147 +        retval = SDL_TRUE;
   6.148      }
   6.149      leaveLock(a);
   6.150  
   6.151 -    return prevval;
   6.152 +    return retval;
   6.153  }
   6.154 -#endif
   6.155  
   6.156  /* vi: set ts=4 sw=4 expandtab: */
     7.1 --- a/src/atomic/SDL_spinlock.c	Sat Jan 15 12:41:59 2011 -0800
     7.2 +++ b/src/atomic/SDL_spinlock.c	Sun Jan 16 15:16:39 2011 -0800
     7.3 @@ -44,24 +44,22 @@
     7.4  #elif defined(__MACOSX__)
     7.5      return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
     7.6  
     7.7 -#elif defined(__GNUC__)
     7.8 -#if defined(__arm__)
     7.9 -#ifdef __ARM_ARCH_5__
    7.10 +#elif defined(HAVE_GCC_ATOMICS)
    7.11 +    return (__sync_lock_test_and_set(lock, 1) == 0);
    7.12 +
    7.13 +#elif defined(__GNUC__) && defined(__arm__) && defined(__ARM_ARCH_5__)
    7.14      int result;
    7.15      __asm__ __volatile__ (
    7.16          "swp %0, %1, [%2]\n"
    7.17          : "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
    7.18      return (result == 0);
    7.19 -#else
    7.20 +
    7.21 +#elif defined(__GNUC__) && defined(__arm__)
    7.22      int result;
    7.23      __asm__ __volatile__ (
    7.24          "ldrex %0, [%2]\nteq   %0, #0\nstrexeq %0, %1, [%2]"
    7.25          : "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
    7.26      return (result == 0);
    7.27 -#endif
    7.28 -#else
    7.29 -    return (__sync_lock_test_and_set(lock, 1) == 0);
    7.30 -#endif
    7.31  
    7.32  #else
    7.33      /* Need CPU instructions for spinlock here! */
    7.34 @@ -81,19 +79,8 @@
    7.35  void
    7.36  SDL_AtomicUnlock(SDL_SpinLock *lock)
    7.37  {
    7.38 -#if defined(__WIN32__)
    7.39 +    /* Assuming atomic assignment operation and full memory barrier in lock */
    7.40      *lock = 0;
    7.41 -
    7.42 -#elif defined(__MACOSX__)
    7.43 -    *lock = 0;
    7.44 -
    7.45 -#elif defined(__GNUC__) && !defined(__arm__)
    7.46 -    __sync_lock_release(lock);
    7.47 -
    7.48 -#else
    7.49 -    /* Assuming memory barrier in lock and integral assignment operation */
    7.50 -    *lock = 0;
    7.51 -#endif
    7.52  }
    7.53  
    7.54  /* vi: set ts=4 sw=4 expandtab: */
     8.1 --- a/test/testatomic.c	Sat Jan 15 12:41:59 2011 -0800
     8.2 +++ b/test/testatomic.c	Sun Jan 16 15:16:39 2011 -0800
     8.3 @@ -1,11 +1,13 @@
     8.4  #include <stdio.h>
     8.5  #include "SDL.h"
     8.6 +#include "SDL_assert.h"
     8.7  
     8.8  /*
     8.9    Absolutely basic tests just to see if we get the expected value
    8.10    after calling each function.
    8.11  */
    8.12  
    8.13 +static
    8.14  char *
    8.15  tf(SDL_bool tf)
    8.16  {
    8.17 @@ -20,8 +22,8 @@
    8.18      return f;
    8.19  }
    8.20  
    8.21 -int
    8.22 -main(int argc, char *argv[])
    8.23 +static
    8.24 +void RunBasicTest()
    8.25  {
    8.26      int value;
    8.27      SDL_SpinLock lock = 0;
    8.28 @@ -58,11 +60,173 @@
    8.29      printf("AtomicDecRef()       tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
    8.30  
    8.31      SDL_AtomicSet(&v, 10);
    8.32 -    tfret = (SDL_AtomicCAS(&v, 0, 20) != 0);
    8.33 +    tfret = (SDL_AtomicCAS(&v, 0, 20) == SDL_FALSE);
    8.34      printf("AtomicCAS()          tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
    8.35      value = SDL_AtomicGet(&v);
    8.36 -    tfret = (SDL_AtomicCAS(&v, value, 20) == value);
    8.37 +    tfret = (SDL_AtomicCAS(&v, value, 20) == SDL_TRUE);
    8.38      printf("AtomicCAS()          tfret=%s val=%"PRIu32"\n", tf(tfret), SDL_AtomicGet(&v));
    8.39 +}
    8.40  
    8.41 +/* Atomic operation test, adapted from code by Michael Davidsaver at:
    8.42 +    http://bazaar.launchpad.net/~mdavidsaver/epics-base/atomic/revision/12105#src/libCom/test/epicsAtomicTest.c
    8.43 +*/
    8.44 +
    8.45 +/* Tests semantics of atomic operations.  Also a stress test
    8.46 + * to see if they are really atomic.
    8.47 + *
    8.48 + * Serveral threads adding to the same variable.
    8.49 + * at the end the value is compared with the expected
    8.50 + * and with a non-atomic counter.
    8.51 + */
    8.52 + 
    8.53 +/* Number of concurrent incrementers */
    8.54 +#define NThreads 2
    8.55 +#define CountInc 100
    8.56 +#define VALBITS (sizeof(atomicValue)*8)
    8.57 + 
    8.58 +#define atomicValue int
    8.59 +#define CountTo ((atomicValue)((unsigned int)(1<<(VALBITS-1))-1))
    8.60 +#define NInter (CountTo/CountInc/NThreads)
    8.61 +#define Expect (CountTo-NInter*CountInc*NThreads)
    8.62 + 
    8.63 +SDL_COMPILE_TIME_ASSERT(size, CountTo>0); /* check for rollover */
    8.64 + 
    8.65 +static SDL_atomic_t good = { 42 };
    8.66 + 
    8.67 +static atomicValue bad = 42;
    8.68 + 
    8.69 +static SDL_atomic_t threadsRunning;
    8.70 +
    8.71 +static SDL_sem *threadDone;
    8.72 + 
    8.73 +static
    8.74 +int adder(void* junk)
    8.75 +{
    8.76 +    unsigned long N=NInter;
    8.77 +    printf("Thread subtracting %d %lu times\n",CountInc,N);
    8.78 +    while (N--) {
    8.79 +        SDL_AtomicAdd(&good, -CountInc);
    8.80 +        bad-=CountInc;
    8.81 +    }
    8.82 +    SDL_AtomicAdd(&threadsRunning, -1);
    8.83 +    SDL_SemPost(threadDone);
    8.84      return 0;
    8.85  }
    8.86 + 
    8.87 +static
    8.88 +void runAdder(void)
    8.89 +{
    8.90 +    Uint32 start, end;
    8.91 +    int T=NThreads;
    8.92 + 
    8.93 +    start = SDL_GetTicks();
    8.94 + 
    8.95 +    threadDone = SDL_CreateSemaphore(0);
    8.96 +
    8.97 +    SDL_AtomicSet(&threadsRunning, NThreads);
    8.98 +
    8.99 +    while (T--)
   8.100 +        SDL_CreateThread(adder, NULL);
   8.101 + 
   8.102 +    while (SDL_AtomicGet(&threadsRunning) > 0)
   8.103 +        SDL_SemWait(threadDone);
   8.104 + 
   8.105 +    SDL_DestroySemaphore(threadDone);
   8.106 +
   8.107 +    end = SDL_GetTicks();
   8.108 + 
   8.109 +    printf("Finished in %f sec\n", (end - start) / 1000.f);
   8.110 +}
   8.111 + 
   8.112 +static
   8.113 +void RunEpicTest()
   8.114 +{
   8.115 +    int b;
   8.116 +    atomicValue v;
   8.117 + 
   8.118 +    printf("\nepic test---------------------------------------\n\n");
   8.119 +
   8.120 +    printf("Size asserted to be >= 32-bit\n");
   8.121 +    SDL_assert(sizeof(atomicValue)>=4);
   8.122 + 
   8.123 +    printf("Check static initializer\n");
   8.124 +    v=SDL_AtomicGet(&good);
   8.125 +    SDL_assert(v==42);
   8.126 + 
   8.127 +    SDL_assert(bad==42);
   8.128 + 
   8.129 +    printf("Test negative values\n");
   8.130 +    SDL_AtomicSet(&good, -5);
   8.131 +    v=SDL_AtomicGet(&good);
   8.132 +    SDL_assert(v==-5);
   8.133 + 
   8.134 +    printf("Verify maximum value\n");
   8.135 +    SDL_AtomicSet(&good, CountTo);
   8.136 +    v=SDL_AtomicGet(&good);
   8.137 +    SDL_assert(v==CountTo);
   8.138 + 
   8.139 +    printf("Test compare and exchange\n");
   8.140 + 
   8.141 +    b=SDL_AtomicCAS(&good, 500, 43);
   8.142 +    SDL_assert(!b); /* no swap since CountTo!=500 */
   8.143 +    v=SDL_AtomicGet(&good);
   8.144 +    SDL_assert(v==CountTo); /* ensure no swap */
   8.145 + 
   8.146 +    b=SDL_AtomicCAS(&good, CountTo, 44);
   8.147 +    SDL_assert(!!b); /* will swap */
   8.148 +    v=SDL_AtomicGet(&good);
   8.149 +    SDL_assert(v==44);
   8.150 + 
   8.151 +    printf("Test Add\n");
   8.152 + 
   8.153 +    v=SDL_AtomicAdd(&good, 1);
   8.154 +    SDL_assert(v==44);
   8.155 +    v=SDL_AtomicGet(&good);
   8.156 +    SDL_assert(v==45);
   8.157 + 
   8.158 +    v=SDL_AtomicAdd(&good, 10);
   8.159 +    SDL_assert(v==45);
   8.160 +    v=SDL_AtomicGet(&good);
   8.161 +    SDL_assert(v==55);
   8.162 + 
   8.163 +    printf("Test Add (Negative values)\n");
   8.164 + 
   8.165 +    v=SDL_AtomicAdd(&good, -20);
   8.166 +    SDL_assert(v==55);
   8.167 +    v=SDL_AtomicGet(&good);
   8.168 +    SDL_assert(v==35);
   8.169 + 
   8.170 +    v=SDL_AtomicAdd(&good, -50); /* crossing zero down */
   8.171 +    SDL_assert(v==35);
   8.172 +    v=SDL_AtomicGet(&good);
   8.173 +    SDL_assert(v==-15);
   8.174 + 
   8.175 +    v=SDL_AtomicAdd(&good, 30); /* crossing zero up */
   8.176 +    SDL_assert(v==-15);
   8.177 +    v=SDL_AtomicGet(&good);
   8.178 +    SDL_assert(v==15);
   8.179 + 
   8.180 +    printf("Reset before count down test\n");
   8.181 +    SDL_AtomicSet(&good, CountTo);
   8.182 +    v=SDL_AtomicGet(&good);
   8.183 +    SDL_assert(v==CountTo);
   8.184 + 
   8.185 +    bad=CountTo;
   8.186 +    SDL_assert(bad==CountTo);
   8.187 + 
   8.188 +    printf("Counting down from %d, Expect %d remaining\n",CountTo,Expect);
   8.189 +    runAdder();
   8.190 + 
   8.191 +    v=SDL_AtomicGet(&good);
   8.192 +    printf("Atomic %d Non-Atomic %d\n",v,bad);
   8.193 +    SDL_assert(v==Expect);
   8.194 +    SDL_assert(bad!=Expect);
   8.195 +}
   8.196 +
   8.197 +int
   8.198 +main(int argc, char *argv[])
   8.199 +{
   8.200 +    RunBasicTest();
   8.201 +    RunEpicTest();
   8.202 +    return 0;
   8.203 +}