Use GCC's atomic loads in SDL_AtomicGet and SDL_AtomicGetPtr
authorJames Legg <jlegg@feralinteractive.com>
Wed, 29 Mar 2017 15:48:22 +0100
changeset 10946623e8891e091
parent 10945 1300a3135d61
child 10947 56a0a429d309
Use GCC's atomic loads in SDL_AtomicGet and SDL_AtomicGetPtr

This fixes errors reported by address sanitizer, and generates simpler
code on x86 architectures.
src/atomic/SDL_atomic.c
     1.1 --- a/src/atomic/SDL_atomic.c	Sun Mar 26 21:00:19 2017 +0200
     1.2 +++ b/src/atomic/SDL_atomic.c	Wed Mar 29 15:48:22 2017 +0100
     1.3 @@ -211,21 +211,29 @@
     1.4  int
     1.5  SDL_AtomicGet(SDL_atomic_t *a)
     1.6  {
     1.7 +#ifdef HAVE_GCC_ATOMICS
     1.8 +    return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
     1.9 +#else
    1.10      int value;
    1.11      do {
    1.12          value = a->value;
    1.13      } while (!SDL_AtomicCAS(a, value, value));
    1.14      return value;
    1.15 +#endif
    1.16  }
    1.17  
    1.18  void *
    1.19  SDL_AtomicGetPtr(void **a)
    1.20  {
    1.21 +#ifdef HAVE_GCC_ATOMICS
    1.22 +    return __atomic_load_n(a, __ATOMIC_SEQ_CST);
    1.23 +#else
    1.24      void *value;
    1.25      do {
    1.26          value = *a;
    1.27      } while (!SDL_AtomicCASPtr(a, value, value));
    1.28      return value;
    1.29 +#endif
    1.30  }
    1.31  
    1.32  void