| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #ifndef _SDL_atomic_h_ |
| #define _SDL_atomic_h_ |
|
|
| #include "SDL_stdinc.h" |
| #include "SDL_platform.h" |
|
|
| #include "begin_code.h" |
|
|
| |
| |
| #if defined(_MSC_VER) && (_MSC_VER >= 1500) && !defined(_WIN32_WCE) |
| #include <intrin.h> |
| #define HAVE_MSC_ATOMICS 1 |
| #endif |
|
|
| |
| #ifdef __cplusplus |
| |
| extern "C" { |
| |
| #endif |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| typedef int SDL_SpinLock; |
|
|
| |
| |
| |
| |
| |
| |
| |
| extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock); |
|
|
| |
| |
| |
| |
| |
| extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock); |
|
|
| |
| |
| |
| |
| |
| extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock); |
|
|
| |
|
|
|
|
| |
| |
| |
| |
| #ifdef _MSC_VER |
| void _ReadWriteBarrier(void); |
| #pragma intrinsic(_ReadWriteBarrier) |
| #define SDL_CompilerBarrier() _ReadWriteBarrier() |
| #elif defined(__GNUC__) |
| #define SDL_CompilerBarrier() __asm__ __volatile__ ("" : : : "memory") |
| #else |
| #define SDL_CompilerBarrier() \ |
| ({ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); }) |
| #endif |
|
|
| |
| |
| |
| #if defined(SDL_ATOMIC_DISABLED) && SDL_ATOMIC_DISABLED |
| #define SDL_DISABLE_ATOMIC_INLINE |
| #endif |
| #ifndef SDL_DISABLE_ATOMIC_INLINE |
|
|
| #ifdef HAVE_MSC_ATOMICS |
|
|
| #define SDL_AtomicSet(a, v) _InterlockedExchange((long*)&(a)->value, (v)) |
| #define SDL_AtomicAdd(a, v) _InterlockedExchangeAdd((long*)&(a)->value, (v)) |
| #define SDL_AtomicCAS(a, oldval, newval) (_InterlockedCompareExchange((long*)&(a)->value, (newval), (oldval)) == (oldval)) |
| #define SDL_AtomicSetPtr(a, v) _InterlockedExchangePointer((a), (v)) |
| #if _M_IX86 |
| #define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchange((long*)(a), (long)(newval), (long)(oldval)) == (long)(oldval)) |
| #else |
| #define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchangePointer((a), (newval), (oldval)) == (oldval)) |
| #endif |
|
|
| #elif defined(__MACOSX__) |
| #include <libkern/OSAtomic.h> |
|
|
| #define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((oldval), (newval), &(a)->value) |
| #if SIZEOF_VOIDP == 4 |
| #define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a)) |
| #elif SIZEOF_VOIDP == 8 |
| #define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a)) |
| #endif |
|
|
| #elif defined(HAVE_GCC_ATOMICS) |
|
|
| #define SDL_AtomicSet(a, v) __sync_lock_test_and_set(&(a)->value, v) |
| #define SDL_AtomicAdd(a, v) __sync_fetch_and_add(&(a)->value, v) |
| #define SDL_AtomicSetPtr(a, v) __sync_lock_test_and_set(a, v) |
| #define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval) |
| #define SDL_AtomicCASPtr(a, oldval, newval) __sync_bool_compare_and_swap(a, oldval, newval) |
|
|
| #endif |
|
|
| #endif |
|
|
|
|
| |
| |
| |
| |
| #ifndef SDL_atomic_t_defined |
| typedef struct { int value; } SDL_atomic_t; |
| #endif |
|
|
| |
| |
| |
| |
| |
| |
| |
| #ifndef SDL_AtomicCAS |
| #define SDL_AtomicCAS SDL_AtomicCAS_ |
| #endif |
| extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval); |
|
|
| |
| |
| |
| |
| |
| #ifndef SDL_AtomicSet |
| static __inline__ int SDL_AtomicSet(SDL_atomic_t *a, int v) |
| { |
| int value; |
| do { |
| value = a->value; |
| } while (!SDL_AtomicCAS(a, value, v)); |
| return value; |
| } |
| #endif |
|
|
| |
| |
| |
| #ifndef SDL_AtomicGet |
| static __inline__ int SDL_AtomicGet(SDL_atomic_t *a) |
| { |
| int value = a->value; |
| SDL_CompilerBarrier(); |
| return value; |
| } |
| #endif |
|
|
| |
| |
| |
| |
| |
| |
| |
| #ifndef SDL_AtomicAdd |
| static __inline__ int SDL_AtomicAdd(SDL_atomic_t *a, int v) |
| { |
| int value; |
| do { |
| value = a->value; |
| } while (!SDL_AtomicCAS(a, value, (value + v))); |
| return value; |
| } |
| #endif |
|
|
| |
| |
| |
| #ifndef SDL_AtomicIncRef |
| #define SDL_AtomicIncRef(a) SDL_AtomicAdd(a, 1) |
| #endif |
|
|
| |
| |
| |
| |
| |
| |
| #ifndef SDL_AtomicDecRef |
| #define SDL_AtomicDecRef(a) (SDL_AtomicAdd(a, -1) == 1) |
| #endif |
|
|
| |
| |
| |
| |
| |
| |
| |
| #ifndef SDL_AtomicCASPtr |
| #define SDL_AtomicCASPtr SDL_AtomicCASPtr_ |
| #endif |
| extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr_(void* *a, void *oldval, void *newval); |
|
|
| |
| |
| |
| |
| |
| #ifndef SDL_AtomicSetPtr |
| static __inline__ void* SDL_AtomicSetPtr(void* *a, void* v) |
| { |
| void* value; |
| do { |
| value = *a; |
| } while (!SDL_AtomicCASPtr(a, value, v)); |
| return value; |
| } |
| #endif |
|
|
| |
| |
| |
| #ifndef SDL_AtomicGetPtr |
| static __inline__ void* SDL_AtomicGetPtr(void* *a) |
| { |
| void* value = *a; |
| SDL_CompilerBarrier(); |
| return value; |
| } |
| #endif |
|
|
|
|
| |
| #ifdef __cplusplus |
| |
| } |
| |
| #endif |
|
|
| #include "close_code.h" |
|
|
| #endif |
|
|
| |
|
|