This version actaully should work on Windows.
authorBob Pendleton
Fri, 25 Sep 2009 19:17:16 +0000
changeset 3306a8ec88ff18c7
parent 3305 bd88e323ad02
child 3307 6fb7f38d20b6
This version actaully should work on Windows.
src/atomic/win32/SDL_atomic.c
     1.1 --- a/src/atomic/win32/SDL_atomic.c	Thu Sep 24 15:20:11 2009 +0000
     1.2 +++ b/src/atomic/win32/SDL_atomic.c	Fri Sep 25 19:17:16 2009 +0000
     1.3 @@ -26,6 +26,7 @@
     1.4  #include "SDL_atomic.h"
     1.5  
     1.6  #include "SDL_error.h"
     1.7 +#include "Windows.h"
     1.8  
     1.9  /*
    1.10    This file provides 32, and 64 bit atomic operations. If the
    1.11 @@ -36,12 +37,9 @@
    1.12  */
    1.13  
    1.14  /* 
    1.15 -  DUMMY VERSION.
    1.16 +  WIN32 VERSION.
    1.17  
    1.18 -  This version of the code assumes there is no support for atomic
    1.19 -  operations. Therefore, every function sets the SDL error
    1.20 -  message. Oddly enough, if you only have one thread then this
    1.21 -  version actuallys works.
    1.22 +  This makes use of native Windows atomic operations.
    1.23  */
    1.24  
    1.25  /*
    1.26 @@ -52,13 +50,24 @@
    1.27  void 
    1.28  SDL_AtomicLock(SDL_SpinLock *lock)
    1.29  {
    1.30 -   SDL_SetError("SDL_atomic.c: is not implemented on this platform");
    1.31 +  long volatile * l = (long volatile *)lock;
    1.32 +  Uint32 old = 0;
    1.33 +  Uint32 new = 1;
    1.34 +
    1.35 +  old = InterlockedExchange(l, new);
    1.36 +  while(1 == old)
    1.37 +    {
    1.38 +      old = InterlockedExchange(l, new);
    1.39 +    }
    1.40  }
    1.41  
    1.42  void 
    1.43  SDL_AtomicUnlock(SDL_SpinLock *lock)
    1.44  {
    1.45 -   SDL_SetError("SDL_atomic.c: is not implemented on this platform");
    1.46 +  long volatile * l = (long volatile *)lock;
    1.47 +  Uint32 new = 0;
    1.48 +
    1.49 +  InterlockedExchange(l, new);
    1.50  }
    1.51  
    1.52  /*
    1.53 @@ -67,16 +76,16 @@
    1.54    code.
    1.55  */
    1.56  
    1.57 -#undef  nativeTestThenSet32
    1.58 -#undef  nativeClear32
    1.59 -#undef  nativeFetchThenIncrement32
    1.60 -#undef  nativeFetchThenDecrement32
    1.61 -#undef  nativeFetchThenAdd32
    1.62 -#undef  nativeFetchThenSubtract32
    1.63 -#undef  nativeIncrementThenFetch32
    1.64 -#undef  nativeDecrementThenFetch32
    1.65 -#undef  nativeAddThenFetch32
    1.66 -#undef  nativeSubtractThenFetch32
    1.67 +#define nativeTestThenSet32
    1.68 +#define nativeClear32
    1.69 +#define nativeFetchThenIncrement32
    1.70 +#define nativeFetchThenDecrement32
    1.71 +#define nativeFetchThenAdd32
    1.72 +#define nativeFetchThenSubtract32
    1.73 +#define nativeIncrementThenFetch32
    1.74 +#define nativeDecrementThenFetch32
    1.75 +#define nativeAddThenFetch32
    1.76 +#define nativeSubtractThenFetch32
    1.77  
    1.78  #undef  nativeTestThenSet64
    1.79  #undef  nativeClear64
    1.80 @@ -147,6 +156,10 @@
    1.81  SDL_AtomicTestThenSet32(volatile Uint32 * ptr)
    1.82  {
    1.83  #ifdef nativeTestThenSet32
    1.84 +  long volatile * p = (long volatile *)ptr;
    1.85 +  Uint32 new = 1;
    1.86 +
    1.87 +  return 0 == InterlockedExchange(p, new);
    1.88  #else
    1.89     SDL_bool result = SDL_FALSE;
    1.90  
    1.91 @@ -166,6 +179,10 @@
    1.92  SDL_AtomicClear32(volatile Uint32 * ptr)
    1.93  {
    1.94  #ifdef nativeClear32
    1.95 +  long volatile * p = (long volatile *)ptr;
    1.96 +  Uint32 new = 0;
    1.97 +
    1.98 +  InterlockedExchange(p, new);
    1.99  #else
   1.100     privateWaitLock(ptr);
   1.101     *ptr = 0;
   1.102 @@ -179,6 +196,9 @@
   1.103  SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr)
   1.104  {
   1.105  #ifdef nativeFetchThenIncrement32
   1.106 +  long volatile * p = (long volatile *)ptr;
   1.107 +
   1.108 +  return InterlockedExchangeAdd(p, 1);
   1.109  #else
   1.110     Uint32 tmp = 0;
   1.111  
   1.112 @@ -195,6 +215,9 @@
   1.113  SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr)
   1.114  {
   1.115  #ifdef nativeFetchThenDecrement32
   1.116 +  long volatile * p = (long volatile *)ptr;
   1.117 +
   1.118 +  return InterlockedExchangeAdd(p, -1);
   1.119  #else
   1.120     Uint32 tmp = 0;
   1.121  
   1.122 @@ -211,6 +234,9 @@
   1.123  SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value)
   1.124  {
   1.125  #ifdef nativeFetchThenAdd32
   1.126 +  long volatile * p = (long volatile *)ptr;
   1.127 +
   1.128 +  return InterlockedExchangeAdd(p, value);
   1.129  #else
   1.130     Uint32 tmp = 0;
   1.131  
   1.132 @@ -227,6 +253,9 @@
   1.133  SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value)
   1.134  {
   1.135  #ifdef nativeFetchThenSubtract32
   1.136 +  long volatile * p = (long volatile *)ptr;
   1.137 +
   1.138 +  return InterlockedExchangeAdd(p, (0 - value));
   1.139  #else
   1.140     Uint32 tmp = 0;
   1.141  
   1.142 @@ -243,6 +272,9 @@
   1.143  SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr)
   1.144  {
   1.145  #ifdef nativeIncrementThenFetch32
   1.146 +  long volatile * p = (LONG volatile *)ptr;
   1.147 +
   1.148 +  return InterlockedIncrement(p);
   1.149  #else
   1.150     Uint32 tmp = 0;
   1.151  
   1.152 @@ -259,6 +291,9 @@
   1.153  SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr)
   1.154  {
   1.155  #ifdef nativeDecrementThenFetch32
   1.156 +  long volatile * p = (LONG volatile *)ptr;
   1.157 +
   1.158 +  return InterlockedDecrement(p);
   1.159  #else
   1.160     Uint32 tmp = 0;
   1.161  
   1.162 @@ -275,6 +310,9 @@
   1.163  SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value)
   1.164  {
   1.165  #ifdef nativeAddThenFetch32
   1.166 +  long volatile * p = (long volatile *)ptr;
   1.167 +
   1.168 +  return InterlockedExchangeAdd(p, value) + value;
   1.169  #else
   1.170     Uint32 tmp = 0;
   1.171  
   1.172 @@ -291,6 +329,9 @@
   1.173  SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value)
   1.174  {
   1.175  #ifdef nativeSubtractThenFetch32
   1.176 +  long volatile * p = (long volatile *)ptr;
   1.177 +
   1.178 +  return InterlockedExchangeAdd(p, (0 - value)) - value;
   1.179  #else
   1.180     Uint32 tmp = 0;
   1.181