diff options
author | Tim Angus <tim@ngus.net> | 2013-08-20 18:30:56 +0100 |
---|---|---|
committer | Tim Angus <tim@ngus.net> | 2014-08-28 11:03:18 +0100 |
commit | 675c6f2daa7ba85d6e5cddbcbfdd01aaaa8c3601 (patch) | |
tree | 2ee3322d87431f3fada83cb8341b69edc38b4504 /src/SDL2/include/SDL_atomic.h | |
parent | 4780cef266bfcad933e10129544a0eb5583991b6 (diff) |
Update SDL2 headers... oops
Diffstat (limited to 'src/SDL2/include/SDL_atomic.h')
-rw-r--r-- | src/SDL2/include/SDL_atomic.h | 89 |
1 files changed, 65 insertions, 24 deletions
diff --git a/src/SDL2/include/SDL_atomic.h b/src/SDL2/include/SDL_atomic.h index 4b737f54..7bd669d1 100644 --- a/src/SDL2/include/SDL_atomic.h +++ b/src/SDL2/include/SDL_atomic.h @@ -1,6 +1,6 @@ /* Simple DirectMedia Layer - Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org> + Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org> This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -21,30 +21,31 @@ /** * \file SDL_atomic.h - * + * * Atomic operations. - * + * * IMPORTANT: * If you are not an expert in concurrent lockless programming, you should * only be using the atomic lock and reference counting functions in this * file. In all other cases you should be protecting your data structures * with full mutexes. - * + * * The list of "safe" functions to use are: * SDL_AtomicLock() * SDL_AtomicUnlock() * SDL_AtomicIncRef() * SDL_AtomicDecRef() - * + * * Seriously, here be dragons! * ^^^^^^^^^^^^^^^^^^^^^^^^^^^ * - * You can find out a little more about lockless programming and the + * You can find out a little more about lockless programming and the * subtle issues that can arise here: * http://msdn.microsoft.com/en-us/library/ee418650%28v=vs.85%29.aspx * * There's also lots of good information here: * http://www.1024cores.net/home/lock-free-algorithms + * http://preshing.com/ * * These operations may or may not actually be implemented using * processor specific atomic operations. When possible they are @@ -72,14 +73,12 @@ /* Set up for C function definitions, even when using C++ */ #ifdef __cplusplus -/* *INDENT-OFF* */ extern "C" { -/* *INDENT-ON* */ #endif /** * \name SDL AtomicLock - * + * * The atomic locks are efficient spinlocks using CPU instructions, * but are vulnerable to starvation and can spin forever if a thread * holding a lock has been terminated. For this reason you should @@ -98,7 +97,7 @@ typedef int SDL_SpinLock; /** * \brief Try to lock a spin lock by setting it to a non-zero value. - * + * * \param lock Points to the lock. * * \return SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held. @@ -107,7 +106,7 @@ extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock); /** * \brief Lock a spin lock by setting it to a non-zero value. - * + * * \param lock Points to the lock. */ extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock); @@ -126,7 +125,7 @@ extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock); * The compiler barrier prevents the compiler from reordering * reads and writes to globally visible variables across the call. */ -#ifdef _MSC_VER +#if defined(_MSC_VER) && (_MSC_VER > 1200) void _ReadWriteBarrier(void); #pragma intrinsic(_ReadWriteBarrier) #define SDL_CompilerBarrier() _ReadWriteBarrier() @@ -134,9 +133,55 @@ void _ReadWriteBarrier(void); #define SDL_CompilerBarrier() __asm__ __volatile__ ("" : : : "memory") #else #define SDL_CompilerBarrier() \ -({ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); }) +{ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); } +#endif + +/** + * Memory barriers are designed to prevent reads and writes from being + * reordered by the compiler and being seen out of order on multi-core CPUs. + * + * A typical pattern would be for thread A to write some data and a flag, + * and for thread B to read the flag and get the data. In this case you + * would insert a release barrier between writing the data and the flag, + * guaranteeing that the data write completes no later than the flag is + * written, and you would insert an acquire barrier between reading the + * flag and reading the data, to ensure that all the reads associated + * with the flag have completed. + * + * In this pattern you should always see a release barrier paired with + * an acquire barrier and you should gate the data reads/writes with a + * single flag variable. + * + * For more information on these semantics, take a look at the blog post: + * http://preshing.com/20120913/acquire-and-release-semantics + */ +#if defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__)) +#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("lwsync" : : : "memory") +#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("lwsync" : : : "memory") +#elif defined(__GNUC__) && defined(__arm__) +#if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7EM__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__) +#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("dmb ish" : : : "memory") +#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("dmb ish" : : : "memory") +#elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) +#ifdef __thumb__ +/* The mcr instruction isn't available in thumb mode, use real functions */ +extern DECLSPEC void SDLCALL SDL_MemoryBarrierRelease(); +extern DECLSPEC void SDLCALL SDL_MemoryBarrierAcquire(); +#else +#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory") +#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory") +#endif /* __thumb__ */ +#else +#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("" : : : "memory") +#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("" : : : "memory") +#endif /* __GNUC__ && __arm__ */ +#else +/* This is correct for the x86 and x64 CPUs, and we'll expand this over time. */ +#define SDL_MemoryBarrierRelease() SDL_CompilerBarrier() +#define SDL_MemoryBarrierAcquire() SDL_CompilerBarrier() #endif + /* Platform specific optimized versions of the atomic functions, * you can disable these by defining SDL_DISABLE_ATOMIC_INLINE */ @@ -196,9 +241,8 @@ typedef struct { int value; } SDL_atomic_t; * \note If you don't know what this function is for, you shouldn't use it! */ #ifndef SDL_AtomicCAS -#define SDL_AtomicCAS SDL_AtomicCAS_ +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval); #endif -extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval); /** * \brief Set an atomic variable to a value. @@ -206,7 +250,7 @@ extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int * \return The previous value of the atomic variable. */ #ifndef SDL_AtomicSet -static __inline__ int SDL_AtomicSet(SDL_atomic_t *a, int v) +SDL_FORCE_INLINE int SDL_AtomicSet(SDL_atomic_t *a, int v) { int value; do { @@ -220,7 +264,7 @@ static __inline__ int SDL_AtomicSet(SDL_atomic_t *a, int v) * \brief Get the value of an atomic variable */ #ifndef SDL_AtomicGet -static __inline__ int SDL_AtomicGet(SDL_atomic_t *a) +SDL_FORCE_INLINE int SDL_AtomicGet(SDL_atomic_t *a) { int value = a->value; SDL_CompilerBarrier(); @@ -236,7 +280,7 @@ static __inline__ int SDL_AtomicGet(SDL_atomic_t *a) * \note This same style can be used for any number operation */ #ifndef SDL_AtomicAdd -static __inline__ int SDL_AtomicAdd(SDL_atomic_t *a, int v) +SDL_FORCE_INLINE int SDL_AtomicAdd(SDL_atomic_t *a, int v) { int value; do { @@ -271,9 +315,8 @@ static __inline__ int SDL_AtomicAdd(SDL_atomic_t *a, int v) * \note If you don't know what this function is for, you shouldn't use it! */ #ifndef SDL_AtomicCASPtr -#define SDL_AtomicCASPtr SDL_AtomicCASPtr_ +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void* *a, void *oldval, void *newval); #endif -extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr_(void* *a, void *oldval, void *newval); /** * \brief Set a pointer to a value atomically. @@ -281,7 +324,7 @@ extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr_(void* *a, void *oldval, void * \return The previous value of the pointer. */ #ifndef SDL_AtomicSetPtr -static __inline__ void* SDL_AtomicSetPtr(void* *a, void* v) +SDL_FORCE_INLINE void* SDL_AtomicSetPtr(void* *a, void* v) { void* value; do { @@ -295,7 +338,7 @@ static __inline__ void* SDL_AtomicSetPtr(void* *a, void* v) * \brief Get the value of a pointer atomically. */ #ifndef SDL_AtomicGetPtr -static __inline__ void* SDL_AtomicGetPtr(void* *a) +SDL_FORCE_INLINE void* SDL_AtomicGetPtr(void* *a) { void* value = *a; SDL_CompilerBarrier(); @@ -306,9 +349,7 @@ static __inline__ void* SDL_AtomicGetPtr(void* *a) /* Ends C function definitions when using C++ */ #ifdef __cplusplus -/* *INDENT-OFF* */ } -/* *INDENT-ON* */ #endif #include "close_code.h" |