Commit 6e96a994 authored by Bob Pendleton's avatar Bob Pendleton

This version actaully should work on Windows.

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%403896
parent 9705e19c
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
#include "SDL_atomic.h" #include "SDL_atomic.h"
#include "SDL_error.h" #include "SDL_error.h"
#include "Windows.h"
/* /*
This file provides 32, and 64 bit atomic operations. If the This file provides 32, and 64 bit atomic operations. If the
...@@ -36,12 +37,9 @@ ...@@ -36,12 +37,9 @@
*/ */
/* /*
DUMMY VERSION. WIN32 VERSION.
This version of the code assumes there is no support for atomic This makes use of native Windows atomic operations.
operations. Therefore, every function sets the SDL error
message. Oddly enough, if you only have one thread then this
version actuallys works.
*/ */
/* /*
...@@ -52,13 +50,24 @@ ...@@ -52,13 +50,24 @@
void void
SDL_AtomicLock(SDL_SpinLock *lock) SDL_AtomicLock(SDL_SpinLock *lock)
{ {
SDL_SetError("SDL_atomic.c: is not implemented on this platform"); long volatile * l = (long volatile *)lock;
Uint32 old = 0;
Uint32 new = 1;
old = InterlockedExchange(l, new);
while(1 == old)
{
old = InterlockedExchange(l, new);
}
} }
void void
SDL_AtomicUnlock(SDL_SpinLock *lock) SDL_AtomicUnlock(SDL_SpinLock *lock)
{ {
SDL_SetError("SDL_atomic.c: is not implemented on this platform"); long volatile * l = (long volatile *)lock;
Uint32 new = 0;
InterlockedExchange(l, new);
} }
/* /*
...@@ -67,16 +76,16 @@ SDL_AtomicUnlock(SDL_SpinLock *lock) ...@@ -67,16 +76,16 @@ SDL_AtomicUnlock(SDL_SpinLock *lock)
code. code.
*/ */
#undef nativeTestThenSet32 #define nativeTestThenSet32
#undef nativeClear32 #define nativeClear32
#undef nativeFetchThenIncrement32 #define nativeFetchThenIncrement32
#undef nativeFetchThenDecrement32 #define nativeFetchThenDecrement32
#undef nativeFetchThenAdd32 #define nativeFetchThenAdd32
#undef nativeFetchThenSubtract32 #define nativeFetchThenSubtract32
#undef nativeIncrementThenFetch32 #define nativeIncrementThenFetch32
#undef nativeDecrementThenFetch32 #define nativeDecrementThenFetch32
#undef nativeAddThenFetch32 #define nativeAddThenFetch32
#undef nativeSubtractThenFetch32 #define nativeSubtractThenFetch32
#undef nativeTestThenSet64 #undef nativeTestThenSet64
#undef nativeClear64 #undef nativeClear64
...@@ -147,6 +156,10 @@ SDL_bool ...@@ -147,6 +156,10 @@ SDL_bool
SDL_AtomicTestThenSet32(volatile Uint32 * ptr) SDL_AtomicTestThenSet32(volatile Uint32 * ptr)
{ {
#ifdef nativeTestThenSet32 #ifdef nativeTestThenSet32
long volatile * p = (long volatile *)ptr;
Uint32 new = 1;
return 0 == InterlockedExchange(p, new);
#else #else
SDL_bool result = SDL_FALSE; SDL_bool result = SDL_FALSE;
...@@ -166,6 +179,10 @@ void ...@@ -166,6 +179,10 @@ void
SDL_AtomicClear32(volatile Uint32 * ptr) SDL_AtomicClear32(volatile Uint32 * ptr)
{ {
#ifdef nativeClear32 #ifdef nativeClear32
long volatile * p = (long volatile *)ptr;
Uint32 new = 0;
InterlockedExchange(p, new);
#else #else
privateWaitLock(ptr); privateWaitLock(ptr);
*ptr = 0; *ptr = 0;
...@@ -179,6 +196,9 @@ Uint32 ...@@ -179,6 +196,9 @@ Uint32
SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr) SDL_AtomicFetchThenIncrement32(volatile Uint32 * ptr)
{ {
#ifdef nativeFetchThenIncrement32 #ifdef nativeFetchThenIncrement32
long volatile * p = (long volatile *)ptr;
return InterlockedExchangeAdd(p, 1);
#else #else
Uint32 tmp = 0; Uint32 tmp = 0;
...@@ -195,6 +215,9 @@ Uint32 ...@@ -195,6 +215,9 @@ Uint32
SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr) SDL_AtomicFetchThenDecrement32(volatile Uint32 * ptr)
{ {
#ifdef nativeFetchThenDecrement32 #ifdef nativeFetchThenDecrement32
long volatile * p = (long volatile *)ptr;
return InterlockedExchangeAdd(p, -1);
#else #else
Uint32 tmp = 0; Uint32 tmp = 0;
...@@ -211,6 +234,9 @@ Uint32 ...@@ -211,6 +234,9 @@ Uint32
SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value) SDL_AtomicFetchThenAdd32(volatile Uint32 * ptr, Uint32 value)
{ {
#ifdef nativeFetchThenAdd32 #ifdef nativeFetchThenAdd32
long volatile * p = (long volatile *)ptr;
return InterlockedExchangeAdd(p, value);
#else #else
Uint32 tmp = 0; Uint32 tmp = 0;
...@@ -227,6 +253,9 @@ Uint32 ...@@ -227,6 +253,9 @@ Uint32
SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value) SDL_AtomicFetchThenSubtract32(volatile Uint32 * ptr, Uint32 value)
{ {
#ifdef nativeFetchThenSubtract32 #ifdef nativeFetchThenSubtract32
long volatile * p = (long volatile *)ptr;
return InterlockedExchangeAdd(p, (0 - value));
#else #else
Uint32 tmp = 0; Uint32 tmp = 0;
...@@ -243,6 +272,9 @@ Uint32 ...@@ -243,6 +272,9 @@ Uint32
SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr) SDL_AtomicIncrementThenFetch32(volatile Uint32 * ptr)
{ {
#ifdef nativeIncrementThenFetch32 #ifdef nativeIncrementThenFetch32
long volatile * p = (LONG volatile *)ptr;
return InterlockedIncrement(p);
#else #else
Uint32 tmp = 0; Uint32 tmp = 0;
...@@ -259,6 +291,9 @@ Uint32 ...@@ -259,6 +291,9 @@ Uint32
SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr) SDL_AtomicDecrementThenFetch32(volatile Uint32 * ptr)
{ {
#ifdef nativeDecrementThenFetch32 #ifdef nativeDecrementThenFetch32
long volatile * p = (LONG volatile *)ptr;
return InterlockedDecrement(p);
#else #else
Uint32 tmp = 0; Uint32 tmp = 0;
...@@ -275,6 +310,9 @@ Uint32 ...@@ -275,6 +310,9 @@ Uint32
SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value) SDL_AtomicAddThenFetch32(volatile Uint32 * ptr, Uint32 value)
{ {
#ifdef nativeAddThenFetch32 #ifdef nativeAddThenFetch32
long volatile * p = (long volatile *)ptr;
return InterlockedExchangeAdd(p, value) + value;
#else #else
Uint32 tmp = 0; Uint32 tmp = 0;
...@@ -291,6 +329,9 @@ Uint32 ...@@ -291,6 +329,9 @@ Uint32
SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value) SDL_AtomicSubtractThenFetch32(volatile Uint32 * ptr, Uint32 value)
{ {
#ifdef nativeSubtractThenFetch32 #ifdef nativeSubtractThenFetch32
long volatile * p = (long volatile *)ptr;
return InterlockedExchangeAdd(p, (0 - value)) - value;
#else #else
Uint32 tmp = 0; Uint32 tmp = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment