src/atomic/SDL_atomic.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8096 132b47684fbf
child 8979 1e283b7a1580
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

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