src/atomic/SDL_atomic.c
author Ryan C. Gordon
Sun, 24 Nov 2013 21:04:51 -0500
changeset 8088 e5d77d5e0fe2
parent 7860 2b0bcdea3a79
child 8092 46fb898f098d
permissions -rw-r--r--
Moved atomic API implementation out of headers.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 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_config.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 #ifdef HAVE_MSC_ATOMICS
   156     return _InterlockedExchangePointer(a, v);
   157 #elif defined(HAVE_GCC_ATOMICS)
   158     return __sync_lock_test_and_set(a, v);
   159 #else
   160     void *value;
   161     do {
   162         value = *a;
   163     } while (!SDL_AtomicCASPtr(a, value, v));
   164     return value;
   165 #endif
   166 }
   167 
   168 int
   169 SDL_AtomicAdd(SDL_atomic_t *a, int v)
   170 {
   171 #ifdef HAVE_MSC_ATOMICS
   172     return _InterlockedExchangeAdd((long*)&a->value, v);
   173 #elif defined(HAVE_GCC_ATOMICS)
   174     return __sync_fetch_and_add(&a->value, v);
   175 #else
   176     int value;
   177     do {
   178         value = a->value;
   179     } while (!SDL_AtomicCAS(a, value, (value + v)));
   180     return value;
   181 #endif
   182 }
   183 
   184 int
   185 SDL_AtomicGet(SDL_atomic_t *a)
   186 {
   187     int value;
   188     do {
   189         value = a->value;
   190     } while (!SDL_AtomicCAS(a, value, value));
   191     return value;
   192 }
   193 
   194 void *
   195 SDL_AtomicGetPtr(void **a)
   196 {
   197     void *value;
   198     do {
   199         value = *a;
   200     } while (!SDL_AtomicCASPtr(a, value, value));
   201     return value;
   202 }
   203 
   204 #ifdef __thumb__
   205 __asm__(
   206 "   .align 2\n"
   207 "   .globl _SDL_MemoryBarrierRelease\n"
   208 "   .globl _SDL_MemoryBarrierAcquire\n"
   209 "_SDL_MemoryBarrierRelease:\n"
   210 "_SDL_MemoryBarrierAcquire:\n"
   211 "   mov r0, #0\n"
   212 "   mcr p15, 0, r0, c7, c10, 5\n"
   213 "   bx lr\n"
   214 );
   215 #endif
   216 
   217 /* vi: set ts=4 sw=4 expandtab: */