include/SDL_atomic.h
changeset 5097 b938ad843e52
parent 5096 e4301cde4de1
child 5098 470ede30189c
     1.1 --- a/include/SDL_atomic.h	Tue Jan 25 17:44:17 2011 -0800
     1.2 +++ b/include/SDL_atomic.h	Tue Jan 25 18:02:41 2011 -0800
     1.3 @@ -127,7 +127,7 @@
     1.4  void _ReadWriteBarrier(void);
     1.5  #pragma intrinsic(_ReadWriteBarrier)
     1.6  #define SDL_CompilerBarrier()   _ReadWriteBarrier()
     1.7 -#elif __GNUC__
     1.8 +#elif defined(__GNUC__)
     1.9  #define SDL_CompilerBarrier()   __asm__ __volatile__ ("" : : : "memory")
    1.10  #else
    1.11  #define SDL_CompilerBarrier()   \
    1.12 @@ -139,7 +139,7 @@
    1.13   */
    1.14  #ifndef SDL_DISABLE_ATOMIC_INLINE
    1.15  
    1.16 -#if HAVE_MSC_ATOMICS
    1.17 +#ifdef HAVE_MSC_ATOMICS
    1.18  
    1.19  #define SDL_AtomicSet(a, v)     _InterlockedExchange((long*)&(a)->value, (v))
    1.20  #define SDL_AtomicAdd(a, v)     _InterlockedExchangeAdd((long*)&(a)->value, (v))
    1.21 @@ -151,7 +151,7 @@
    1.22  #define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchangePointer((a), (newval), (oldval)) == (oldval))
    1.23  #endif
    1.24  
    1.25 -#elif __MACOSX__
    1.26 +#elif defined(__MACOSX__)
    1.27  #include <libkern/OSAtomic.h>
    1.28  
    1.29  #define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((oldval), (newval), &(a)->value)
    1.30 @@ -161,7 +161,7 @@
    1.31  #define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
    1.32  #endif
    1.33  
    1.34 -#elif HAVE_GCC_ATOMICS
    1.35 +#elif defined(HAVE_GCC_ATOMICS)
    1.36  
    1.37  #define SDL_AtomicSet(a, v)     __sync_lock_test_and_set(&(a)->value, v)
    1.38  #define SDL_AtomicAdd(a, v)     __sync_fetch_and_add(&(a)->value, v)
    1.39 @@ -183,31 +183,43 @@
    1.40  #endif
    1.41  
    1.42  /**
    1.43 + * \brief Set an atomic variable to a new value if it is currently an old value.
    1.44 + *
    1.45 + * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
    1.46 + *
    1.47 + * \note If you don't know what this function is for, you shouldn't use it!
    1.48 +*/
    1.49 +#ifndef SDL_AtomicCAS
    1.50 +#define SDL_AtomicCAS SDL_AtomicCAS_
    1.51 +#endif
    1.52 +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval);
    1.53 +
    1.54 +/**
    1.55   * \brief Set an atomic variable to a value.
    1.56   *
    1.57   * \return The previous value of the atomic variable.
    1.58   */
    1.59  #ifndef SDL_AtomicSet
    1.60 -#define SDL_AtomicSet(a, v) \
    1.61 -({                              \
    1.62 -    int _value;                 \
    1.63 -    do {                        \
    1.64 -        _value = (a)->value;    \
    1.65 -    } while (!SDL_AtomicCAS(a, _value, (v))); \
    1.66 -    _value;                     \
    1.67 -})
    1.68 +static __inline__ int SDL_AtomicSet(SDL_atomic_t *a, int v)
    1.69 +{
    1.70 +    int value;
    1.71 +    do {
    1.72 +        value = a->value;
    1.73 +    } while (!SDL_AtomicCAS(a, value, v));
    1.74 +    return value;
    1.75 +}
    1.76  #endif
    1.77  
    1.78  /**
    1.79   * \brief Get the value of an atomic variable
    1.80   */
    1.81  #ifndef SDL_AtomicGet
    1.82 -#define SDL_AtomicGet(a)        \
    1.83 -({                              \
    1.84 -    int _value = (a)->value;    \
    1.85 -    SDL_CompilerBarrier();      \
    1.86 -    _value;                     \
    1.87 -})
    1.88 +static __inline__ int SDL_AtomicGet(SDL_atomic_t *a)
    1.89 +{
    1.90 +    int value = a->value;
    1.91 +    SDL_CompilerBarrier();
    1.92 +    return value;
    1.93 +}
    1.94  #endif
    1.95  
    1.96  /**
    1.97 @@ -218,14 +230,14 @@
    1.98   * \note This same style can be used for any number operation
    1.99   */
   1.100  #ifndef SDL_AtomicAdd
   1.101 -#define SDL_AtomicAdd(a, v)     \
   1.102 -({                              \
   1.103 -    int _value;                 \
   1.104 -    do {                        \
   1.105 -        _value = (a)->value;    \
   1.106 -    } while (!SDL_AtomicCAS(a, _value, (_value + (v)))); \
   1.107 -    _value;                     \
   1.108 -})
   1.109 +static __inline__ int SDL_AtomicAdd(SDL_atomic_t *a, int v)
   1.110 +{
   1.111 +    int value;
   1.112 +    do {
   1.113 +        value = a->value;
   1.114 +    } while (!SDL_AtomicCAS(a, value, (value + v)));
   1.115 +    return value;
   1.116 +}
   1.117  #endif
   1.118  
   1.119  /**
   1.120 @@ -246,46 +258,6 @@
   1.121  #endif
   1.122  
   1.123  /**
   1.124 - * \brief Set an atomic variable to a new value if it is currently an old value.
   1.125 - *
   1.126 - * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
   1.127 - *
   1.128 - * \note If you don't know what this function is for, you shouldn't use it!
   1.129 -*/
   1.130 -#ifndef SDL_AtomicCAS
   1.131 -#define SDL_AtomicCAS SDL_AtomicCAS_
   1.132 -#endif
   1.133 -extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval);
   1.134 -
   1.135 -/**
   1.136 - * \brief Set a pointer to a value atomically.
   1.137 - *
   1.138 - * \return The previous value of the pointer.
   1.139 - */
   1.140 -#ifndef SDL_AtomicSetPtr
   1.141 -#define SDL_AtomicSetPtr(a, v)  \
   1.142 -({                              \
   1.143 -    void* _value;               \
   1.144 -    do {                        \
   1.145 -        _value = *(a);          \
   1.146 -    } while (!SDL_AtomicCASPtr(a, _value, (v))); \
   1.147 -    _value;                     \
   1.148 -})
   1.149 -#endif
   1.150 -
   1.151 -/**
   1.152 - * \brief Get the value of a pointer atomically.
   1.153 - */
   1.154 -#ifndef SDL_AtomicGetPtr
   1.155 -#define SDL_AtomicGetPtr(a)     \
   1.156 -({                              \
   1.157 -    void* _value = *(a);        \
   1.158 -    SDL_CompilerBarrier();      \
   1.159 -    _value;                     \
   1.160 -})
   1.161 -#endif
   1.162 -
   1.163 -/**
   1.164   * \brief Set a pointer to a new value if it is currently an old value.
   1.165   *
   1.166   * \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
   1.167 @@ -297,6 +269,35 @@
   1.168  #endif
   1.169  extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr_(void **a, void *oldval, void *newval);
   1.170  
   1.171 +/**
   1.172 + * \brief Set a pointer to a value atomically.
   1.173 + *
   1.174 + * \return The previous value of the pointer.
   1.175 + */
   1.176 +#ifndef SDL_AtomicSetPtr
   1.177 +static __inline__ void* SDL_AtomicSetPtr(void* *a, void* v)
   1.178 +{
   1.179 +    void* value;
   1.180 +    do {
   1.181 +        value = *a;
   1.182 +    } while (!SDL_AtomicCASPtr(a, value, v));
   1.183 +    return value;
   1.184 +}
   1.185 +#endif
   1.186 +
   1.187 +/**
   1.188 + * \brief Get the value of a pointer atomically.
   1.189 + */
   1.190 +#ifndef SDL_AtomicGetPtr
   1.191 +static __inline__ void* SDL_AtomicGetPtr(void* *a)
   1.192 +{
   1.193 +    void* value = *a;
   1.194 +    SDL_CompilerBarrier();
   1.195 +    return value;
   1.196 +}
   1.197 +#endif
   1.198 +
   1.199 +
   1.200  /* Ends C function definitions when using C++ */
   1.201  #ifdef __cplusplus
   1.202  /* *INDENT-OFF* */