src/atomic/SDL_atomic.c
changeset 5004 0c72ae7b7cb2
parent 5003 3a95a2b93eb3
child 5095 dceec93471e7
     1.1 --- a/src/atomic/SDL_atomic.c	Sat Jan 15 12:41:59 2011 -0800
     1.2 +++ b/src/atomic/SDL_atomic.c	Sun Jan 16 15:16:39 2011 -0800
     1.3 @@ -23,6 +23,12 @@
     1.4  
     1.5  #include "SDL_atomic.h"
     1.6  
     1.7 +/* Note that we undefine the atomic operations here, in case they are
     1.8 +   defined as compiler intrinsics while building SDL but the library user
     1.9 +   doesn't have that compiler.  That way we always have a working set of
    1.10 +   atomic operations built into the library.
    1.11 +*/
    1.12 + 
    1.13  /* 
    1.14    If any of the operations are not provided then we must emulate some
    1.15    of them. That means we need a nice implementation of spin locks
    1.16 @@ -51,20 +57,20 @@
    1.17  static __inline__ void
    1.18  enterLock(void *a)
    1.19  {
    1.20 -   uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
    1.21 +    uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
    1.22  
    1.23 -   SDL_AtomicLock(&locks[index]);
    1.24 +    SDL_AtomicLock(&locks[index]);
    1.25  }
    1.26  
    1.27  static __inline__ void
    1.28  leaveLock(void *a)
    1.29  {
    1.30 -   uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
    1.31 +    uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
    1.32  
    1.33 -   SDL_AtomicUnlock(&locks[index]);
    1.34 +    SDL_AtomicUnlock(&locks[index]);
    1.35  }
    1.36  
    1.37 -#ifndef SDL_AtomicSet
    1.38 +#undef SDL_AtomicSet
    1.39  int
    1.40  SDL_AtomicSet(SDL_atomic_t *a, int value)
    1.41  {
    1.42 @@ -77,9 +83,8 @@
    1.43  
    1.44      return oldvalue;
    1.45  }
    1.46 -#endif
    1.47  
    1.48 -#ifndef SDL_AtomicGet
    1.49 +#undef SDL_AtomicGet
    1.50  int
    1.51  SDL_AtomicGet(SDL_atomic_t *a)
    1.52  {
    1.53 @@ -88,9 +93,8 @@
    1.54      */
    1.55      return a->value;
    1.56  }
    1.57 -#endif
    1.58  
    1.59 -#ifndef SDL_AtomicAdd
    1.60 +#undef SDL_AtomicAdd
    1.61  int
    1.62  SDL_AtomicAdd(SDL_atomic_t *a, int value)
    1.63  {
    1.64 @@ -103,53 +107,48 @@
    1.65  
    1.66      return oldvalue;
    1.67  }
    1.68 -#endif
    1.69  
    1.70 -#ifndef SDL_AtomicIncRef
    1.71 +#undef SDL_AtomicIncRef
    1.72  void
    1.73  SDL_AtomicIncRef(SDL_atomic_t *a)
    1.74  {
    1.75      SDL_AtomicAdd(a, 1);
    1.76  }
    1.77 -#endif
    1.78  
    1.79 -#ifndef SDL_AtomicDecRef
    1.80 +#undef SDL_AtomicDecRef
    1.81  SDL_bool
    1.82  SDL_AtomicDecRef(SDL_atomic_t *a)
    1.83  {
    1.84      return SDL_AtomicAdd(a, -1) == 1;
    1.85  }
    1.86 -#endif
    1.87  
    1.88 -#ifndef SDL_AtomicCAS
    1.89 -int
    1.90 +#undef SDL_AtomicCAS
    1.91 +SDL_bool
    1.92  SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
    1.93  {
    1.94 -    int prevval;
    1.95 +    SDL_bool retval = SDL_FALSE;
    1.96  
    1.97      enterLock(a);
    1.98 -    prevval = a->value;
    1.99 -    if (prevval == oldval) {
   1.100 +    if (a->value == oldval) {
   1.101          a->value = newval;
   1.102 +        retval = SDL_TRUE;
   1.103      }
   1.104      leaveLock(a);
   1.105  
   1.106 -    return prevval;
   1.107 +    return retval;
   1.108  }
   1.109 -#endif
   1.110  
   1.111 -#ifndef SDL_AtomicSetPtr
   1.112 +#undef SDL_AtomicSetPtr
   1.113  void
   1.114  SDL_AtomicSetPtr(void** a, void* value)
   1.115  {
   1.116      void *prevval;
   1.117      do {
   1.118          prevval = *a;
   1.119 -    } while (SDL_AtomicCASPtr(a, prevval, value) != prevval);
   1.120 +    } while (!SDL_AtomicCASPtr(a, prevval, value));
   1.121  }
   1.122 -#endif
   1.123  
   1.124 -#ifndef SDL_AtomicGetPtr
   1.125 +#undef SDL_AtomicGetPtr
   1.126  void*
   1.127  SDL_AtomicGetPtr(void** a)
   1.128  {
   1.129 @@ -158,22 +157,20 @@
   1.130      */
   1.131      return *a;
   1.132  }
   1.133 -#endif
   1.134  
   1.135 -#ifndef SDL_AtomicCASPtr
   1.136 -void* SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
   1.137 +#undef SDL_AtomicCASPtr
   1.138 +SDL_bool SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
   1.139  {
   1.140 -    void *prevval;
   1.141 +    SDL_bool retval = SDL_FALSE;
   1.142  
   1.143      enterLock(a);
   1.144 -    prevval = *a;
   1.145      if (*a == oldval) {
   1.146          *a = newval;
   1.147 +        retval = SDL_TRUE;
   1.148      }
   1.149      leaveLock(a);
   1.150  
   1.151 -    return prevval;
   1.152 +    return retval;
   1.153  }
   1.154 -#endif
   1.155  
   1.156  /* vi: set ts=4 sw=4 expandtab: */