src/atomic/SDL_atomic.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 04 Jun 2014 10:56:56 -0700
changeset 8820 0e935d5b193a
parent 8149 681eb46b8ac4
child 8979 1e283b7a1580
permissions -rw-r--r--
Added annotations to help code analysis tools

CR: Bruce Dawson
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../SDL_internal.h"
    22 
    23 #include "SDL_atomic.h"
    24 
    25 #if defined(_MSC_VER) && (_MSC_VER >= 1500)
    26 #include <intrin.h>
    27 #define HAVE_MSC_ATOMICS 1
    28 #endif
    29 
    30 #if defined(__MACOSX__)  /* !!! FIXME: should we favor gcc atomics? */
    31 #include <libkern/OSAtomic.h>
    32 #endif
    33 
    34 /*
    35   If any of the operations are not provided then we must emulate some
    36   of them. That means we need a nice implementation of spin locks
    37   that avoids the "one big lock" problem. We use a vector of spin
    38   locks and pick which one to use based on the address of the operand
    39   of the function.
    40 
    41   To generate the index of the lock we first shift by 3 bits to get
    42   rid on the zero bits that result from 32 and 64 bit allignment of
    43   data. We then mask off all but 5 bits and use those 5 bits as an
    44   index into the table.
    45 
    46   Picking the lock this way insures that accesses to the same data at
    47   the same time will go to the same lock. OTOH, accesses to different
    48   data have only a 1/32 chance of hitting the same lock. That should
    49   pretty much eliminate the chances of several atomic operations on
    50   different data from waiting on the same "big lock". If it isn't
    51   then the table of locks can be expanded to a new size so long as
    52   the new size is a power of two.
    53 
    54   Contributed by Bob Pendleton, bob@pendleton.com
    55 */
    56 
    57 #if !defined(HAVE_MSC_ATOMICS) && !defined(HAVE_GCC_ATOMICS) && !defined(__MACOSX__)
    58 #define EMULATE_CAS 1
    59 #endif
    60 
    61 #if EMULATE_CAS
    62 static SDL_SpinLock locks[32];
    63 
    64 static SDL_INLINE void
    65 enterLock(void *a)
    66 {
    67     uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
    68 
    69     SDL_AtomicLock(&locks[index]);
    70 }
    71 
    72 static SDL_INLINE void
    73 leaveLock(void *a)
    74 {
    75     uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
    76 
    77     SDL_AtomicUnlock(&locks[index]);
    78 }
    79 #endif
    80 
    81 
    82 SDL_bool
    83 SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
    84 {
    85 #ifdef HAVE_MSC_ATOMICS
    86     return (_InterlockedCompareExchange((long*)&a->value, (long)newval, (long)oldval) == (long)oldval);
    87 #elif defined(__MACOSX__)  /* !!! FIXME: should we favor gcc atomics? */
    88     return (SDL_bool) OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value);
    89 #elif defined(HAVE_GCC_ATOMICS)
    90     return (SDL_bool) __sync_bool_compare_and_swap(&a->value, oldval, newval);
    91 #elif EMULATE_CAS
    92     SDL_bool retval = SDL_FALSE;
    93 
    94     enterLock(a);
    95     if (a->value == oldval) {
    96         a->value = newval;
    97         retval = SDL_TRUE;
    98     }
    99     leaveLock(a);
   100 
   101     return retval;
   102 #else
   103     #error Please define your platform.
   104 #endif
   105 }
   106 
   107 SDL_bool
   108 SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
   109 {
   110 #if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
   111     return (_InterlockedCompareExchange((long*)a, (long)newval, (long)oldval) == (long)oldval);
   112 #elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
   113     return (_InterlockedCompareExchangePointer(a, newval, oldval) == oldval);
   114 #elif defined(__MACOSX__) && defined(__LP64__)   /* !!! FIXME: should we favor gcc atomics? */
   115     return (SDL_bool) OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t*) a);
   116 #elif defined(__MACOSX__) && !defined(__LP64__)  /* !!! FIXME: should we favor gcc atomics? */
   117     return (SDL_bool) OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t*) a);
   118 #elif defined(HAVE_GCC_ATOMICS)
   119     return __sync_bool_compare_and_swap(a, oldval, newval);
   120 #elif EMULATE_CAS
   121     SDL_bool retval = SDL_FALSE;
   122 
   123     enterLock(a);
   124     if (*a == oldval) {
   125         *a = newval;
   126         retval = SDL_TRUE;
   127     }
   128     leaveLock(a);
   129 
   130     return retval;
   131 #else
   132     #error Please define your platform.
   133 #endif
   134 }
   135 
   136 int
   137 SDL_AtomicSet(SDL_atomic_t *a, int v)
   138 {
   139 #ifdef HAVE_MSC_ATOMICS
   140     return _InterlockedExchange((long*)&a->value, v);
   141 #elif defined(HAVE_GCC_ATOMICS)
   142     return __sync_lock_test_and_set(&a->value, v);
   143 #else
   144     int value;
   145     do {
   146         value = a->value;
   147     } while (!SDL_AtomicCAS(a, value, v));
   148     return value;
   149 #endif
   150 }
   151 
   152 void*
   153 SDL_AtomicSetPtr(void **a, void *v)
   154 {
   155 #if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
   156     return (void *) _InterlockedExchange((long *)a, (long) v);
   157 #elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
   158     return _InterlockedExchangePointer(a, v);
   159 #elif defined(HAVE_GCC_ATOMICS)
   160     return __sync_lock_test_and_set(a, v);
   161 #else
   162     void *value;
   163     do {
   164         value = *a;
   165     } while (!SDL_AtomicCASPtr(a, value, v));
   166     return value;
   167 #endif
   168 }
   169 
   170 int
   171 SDL_AtomicAdd(SDL_atomic_t *a, int v)
   172 {
   173 #ifdef HAVE_MSC_ATOMICS
   174     return _InterlockedExchangeAdd((long*)&a->value, v);
   175 #elif defined(HAVE_GCC_ATOMICS)
   176     return __sync_fetch_and_add(&a->value, v);
   177 #else
   178     int value;
   179     do {
   180         value = a->value;
   181     } while (!SDL_AtomicCAS(a, value, (value + v)));
   182     return value;
   183 #endif
   184 }
   185 
   186 int
   187 SDL_AtomicGet(SDL_atomic_t *a)
   188 {
   189     int value;
   190     do {
   191         value = a->value;
   192     } while (!SDL_AtomicCAS(a, value, value));
   193     return value;
   194 }
   195 
   196 void *
   197 SDL_AtomicGetPtr(void **a)
   198 {
   199     void *value;
   200     do {
   201         value = *a;
   202     } while (!SDL_AtomicCASPtr(a, value, value));
   203     return value;
   204 }
   205 
   206 #ifdef __thumb__
   207 #if defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__)
   208 __asm__(
   209 "   .align 2\n"
   210 "   .globl _SDL_MemoryBarrierRelease\n"
   211 "   .globl _SDL_MemoryBarrierAcquire\n"
   212 "_SDL_MemoryBarrierRelease:\n"
   213 "_SDL_MemoryBarrierAcquire:\n"
   214 "   mov r0, #0\n"
   215 "   mcr p15, 0, r0, c7, c10, 5\n"
   216 "   bx lr\n"
   217 );
   218 #endif
   219 #endif
   220 
   221 /* vi: set ts=4 sw=4 expandtab: */