include/SDL_atomic.h
changeset 5004 0c72ae7b7cb2
parent 5003 3a95a2b93eb3
child 5005 7f0265279b68
     1.1 --- a/include/SDL_atomic.h	Sat Jan 15 12:41:59 2011 -0800
     1.2 +++ b/include/SDL_atomic.h	Sun Jan 16 15:16:39 2011 -0800
     1.3 @@ -108,7 +108,62 @@
     1.4  /*@}*//*SDL AtomicLock*/
     1.5  
     1.6  /* Platform specific optimized versions of the atomic functions */
     1.7 -/* None yet... */
     1.8 +#if defined(__WIN32__)
     1.9 +#define WIN32_LEAN_AND_MEAN
    1.10 +#include <windows.h>
    1.11 +
    1.12 +#define SDL_AtomicSet(a, v)     InterlockedExchange(&(a)->value, v)
    1.13 +#define SDL_AtomicGet(a)        ((a)->value)
    1.14 +#define SDL_AtomicAdd(a, v)     InterlockedAdd(&(a)->value, v)
    1.15 +#define SDL_AtomicCAS(a, oldval, newval) (InterlockedCompareExchange(&(a)->value, newval, oldval) == (oldval))
    1.16 +#define SDL_AtomicSetPtr(a, v)  InterlockedExchangePointer(a, v)
    1.17 +#define SDL_AtomicGetPtr(a)     (*(a))
    1.18 +#define SDL_AtomicCASPtr(a, oldval, newval) (InterlockedCompareExchangePointer(a, newval, oldval) == (oldval))
    1.19 +
    1.20 +#elif defined(__MACOSX__)
    1.21 +#include <libkern/OSAtomic.h>
    1.22 +
    1.23 +#define SDL_AtomicSet(a, v) \
    1.24 +({                          \
    1.25 +    int oldvalue;           \
    1.26 +                            \
    1.27 +    do {                    \
    1.28 +        oldvalue = (a)->value; \
    1.29 +    } while (!OSAtomicCompareAndSwap32Barrier(oldvalue, v, &(a)->value)); \
    1.30 +                            \
    1.31 +    oldvalue;               \
    1.32 +})
    1.33 +#define SDL_AtomicGet(a)        ((a)->value)
    1.34 +#define SDL_AtomicAdd(a, v) \
    1.35 +({                          \
    1.36 +    int oldvalue;           \
    1.37 +                            \
    1.38 +    do {                    \
    1.39 +        oldvalue = (a)->value; \
    1.40 +    } while (!OSAtomicCompareAndSwap32Barrier(oldvalue, oldvalue+v, &(a)->value)); \
    1.41 +                            \
    1.42 +    oldvalue;               \
    1.43 +})
    1.44 +#define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier(oldval, newval, &(a)->value)
    1.45 +#define SDL_AtomicSetPtr(a, v)  (*(a) = v, OSMemoryBarrier())
    1.46 +#define SDL_AtomicGetPtr(a)     (*(a))
    1.47 +#if SIZEOF_VOIDP == 4
    1.48 +#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a))
    1.49 +#elif SIZEOF_VOIDP == 8
    1.50 +#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
    1.51 +#endif
    1.52 +
    1.53 +#elif defined(HAVE_GCC_ATOMICS)
    1.54 +
    1.55 +#define SDL_AtomicSet(a, v)     __sync_lock_test_and_set(&(a)->value, v)
    1.56 +#define SDL_AtomicGet(a)        ((a)->value)
    1.57 +#define SDL_AtomicAdd(a, v)     __sync_fetch_and_add(&(a)->value, v)
    1.58 +#define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval)
    1.59 +#define SDL_AtomicSetPtr(a, v)  (*(a) = v, __sync_synchronize())
    1.60 +#define SDL_AtomicGetPtr(a)     (*(a))
    1.61 +#define SDL_AtomicCASPtr(a, oldval, newval) __sync_bool_compare_and_swap(a, oldval, newval)
    1.62 +
    1.63 +#endif
    1.64  
    1.65  /**
    1.66   * \brief A type representing an atomic integer value.  It is a struct
    1.67 @@ -163,12 +218,12 @@
    1.68  /**
    1.69   * \brief Set an atomic variable to a new value if it is currently an old value.
    1.70   *
    1.71 - * \return The previous value of the atomic variable
    1.72 + * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
    1.73   *
    1.74   * \note If you don't know what this function is for, you shouldn't use it!
    1.75  */
    1.76  #ifndef SDL_AtomicCAS
    1.77 -extern DECLSPEC int SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
    1.78 +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
    1.79  #endif
    1.80  
    1.81  /**
    1.82 @@ -188,12 +243,12 @@
    1.83  /**
    1.84   * \brief Set a pointer to a new value if it is currently an old value.
    1.85   *
    1.86 - * \return The previous value of the pointer
    1.87 + * \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
    1.88   *
    1.89   * \note If you don't know what this function is for, you shouldn't use it!
    1.90  */
    1.91  #ifndef SDL_AtomicCASPtr
    1.92 -extern DECLSPEC void* SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval);
    1.93 +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval);
    1.94  #endif
    1.95  
    1.96  /* Ends C function definitions when using C++ */