src/atomic/SDL_atomic.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 13 Apr 2017 15:28:56 -0400
changeset 10974 10e16b1151b0
parent 10973 b141211c5d0a
child 11318 06fd8421e8f6
permissions -rw-r--r--
atomic: Patched to compile on Android.
slouken@5003
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
binarycrusader@8979
    34
#if !defined(HAVE_GCC_ATOMICS) && defined(__SOLARIS__)
binarycrusader@8979
    35
#include <atomic.h>
binarycrusader@8979
    36
#endif
binarycrusader@8979
    37
icculus@10972
    38
/* The __atomic_load_n() intrinsic showed up in different times for different compilers. */
icculus@10972
    39
#if defined(HAVE_GCC_ATOMICS)
icculus@10972
    40
# if defined(__clang__)
icculus@10972
    41
#   if __has_builtin(__atomic_load_n)
icculus@10974
    42
      /* !!! FIXME: this advertises as available in the NDK but uses an external symbol we don't have.
icculus@10974
    43
         It might be in a later NDK or we might need an extra library? --ryan. */
icculus@10974
    44
#     if !defined(__ANDROID__)
icculus@10974
    45
#       define HAVE_ATOMIC_LOAD_N 1
icculus@10974
    46
#     endif
icculus@10972
    47
#   endif
icculus@10972
    48
# elif defined(__GNUC__)
icculus@10972
    49
#   if (__GNUC__ >= 5)
icculus@10972
    50
#     define HAVE_ATOMIC_LOAD_N 1
icculus@10972
    51
#   endif
icculus@10972
    52
# endif
icculus@10972
    53
#endif
icculus@10972
    54
slouken@7191
    55
/*
slouken@5003
    56
  If any of the operations are not provided then we must emulate some
slouken@5003
    57
  of them. That means we need a nice implementation of spin locks
slouken@5003
    58
  that avoids the "one big lock" problem. We use a vector of spin
slouken@5003
    59
  locks and pick which one to use based on the address of the operand
slouken@5003
    60
  of the function.
slouken@5003
    61
slouken@5003
    62
  To generate the index of the lock we first shift by 3 bits to get
slouken@5003
    63
  rid on the zero bits that result from 32 and 64 bit allignment of
slouken@5003
    64
  data. We then mask off all but 5 bits and use those 5 bits as an
slouken@7191
    65
  index into the table.
slouken@5003
    66
slouken@5003
    67
  Picking the lock this way insures that accesses to the same data at
slouken@5003
    68
  the same time will go to the same lock. OTOH, accesses to different
slouken@5003
    69
  data have only a 1/32 chance of hitting the same lock. That should
slouken@5003
    70
  pretty much eliminate the chances of several atomic operations on
slouken@5003
    71
  different data from waiting on the same "big lock". If it isn't
slouken@5003
    72
  then the table of locks can be expanded to a new size so long as
slouken@5003
    73
  the new size is a power of two.
slouken@5003
    74
slouken@5003
    75
  Contributed by Bob Pendleton, bob@pendleton.com
slouken@5003
    76
*/
slouken@5003
    77
binarycrusader@8979
    78
#if !defined(HAVE_MSC_ATOMICS) && !defined(HAVE_GCC_ATOMICS) && !defined(__MACOSX__) && !defined(__SOLARIS__)
icculus@8088
    79
#define EMULATE_CAS 1
icculus@8088
    80
#endif
icculus@8088
    81
icculus@8088
    82
#if EMULATE_CAS
slouken@5003
    83
static SDL_SpinLock locks[32];
slouken@5003
    84
slouken@7860
    85
static SDL_INLINE void
slouken@5003
    86
enterLock(void *a)
slouken@5003
    87
{
slouken@5004
    88
    uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
slouken@5003
    89
slouken@5004
    90
    SDL_AtomicLock(&locks[index]);
slouken@5003
    91
}
slouken@5003
    92
slouken@7860
    93
static SDL_INLINE void
slouken@5003
    94
leaveLock(void *a)
slouken@5003
    95
{
slouken@5004
    96
    uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
slouken@5003
    97
slouken@5004
    98
    SDL_AtomicUnlock(&locks[index]);
slouken@5003
    99
}
icculus@8088
   100
#endif
slouken@5003
   101
icculus@8088
   102
icculus@8088
   103
SDL_bool
slouken@6978
   104
SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
slouken@5003
   105
{
icculus@8088
   106
#ifdef HAVE_MSC_ATOMICS
icculus@8088
   107
    return (_InterlockedCompareExchange((long*)&a->value, (long)newval, (long)oldval) == (long)oldval);
icculus@8088
   108
#elif defined(HAVE_GCC_ATOMICS)
icculus@8088
   109
    return (SDL_bool) __sync_bool_compare_and_swap(&a->value, oldval, newval);
icculus@10973
   110
#elif defined(__MACOSX__)  /* this is deprecated in 10.12 sdk; favor gcc atomics. */
icculus@10973
   111
    return (SDL_bool) OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value);
binarycrusader@8979
   112
#elif defined(__SOLARIS__) && defined(_LP64)
binarycrusader@8979
   113
    return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)&a->value, (uint64_t)oldval, (uint64_t)newval) == oldval);
binarycrusader@8979
   114
#elif defined(__SOLARIS__) && !defined(_LP64)
binarycrusader@8979
   115
    return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)&a->value, (uint32_t)oldval, (uint32_t)newval) == oldval);
icculus@8088
   116
#elif EMULATE_CAS
slouken@5004
   117
    SDL_bool retval = SDL_FALSE;
slouken@5003
   118
slouken@5003
   119
    enterLock(a);
slouken@5004
   120
    if (a->value == oldval) {
slouken@5003
   121
        a->value = newval;
slouken@5004
   122
        retval = SDL_TRUE;
slouken@5003
   123
    }
slouken@5003
   124
    leaveLock(a);
slouken@5003
   125
slouken@5004
   126
    return retval;
icculus@8088
   127
#else
icculus@8088
   128
    #error Please define your platform.
icculus@8088
   129
#endif
slouken@5003
   130
}
slouken@5003
   131
icculus@8088
   132
SDL_bool
slouken@6978
   133
SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
slouken@5003
   134
{
icculus@8088
   135
#if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
icculus@8088
   136
    return (_InterlockedCompareExchange((long*)a, (long)newval, (long)oldval) == (long)oldval);
icculus@8088
   137
#elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
icculus@8088
   138
    return (_InterlockedCompareExchangePointer(a, newval, oldval) == oldval);
icculus@8088
   139
#elif defined(HAVE_GCC_ATOMICS)
icculus@8088
   140
    return __sync_bool_compare_and_swap(a, oldval, newval);
icculus@10973
   141
#elif defined(__MACOSX__) && defined(__LP64__)  /* this is deprecated in 10.12 sdk; favor gcc atomics. */
icculus@10973
   142
    return (SDL_bool) OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t*) a);
icculus@10973
   143
#elif defined(__MACOSX__) && !defined(__LP64__)  /* this is deprecated in 10.12 sdk; favor gcc atomics. */
icculus@10973
   144
    return (SDL_bool) OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t*) a);
binarycrusader@8979
   145
#elif defined(__SOLARIS__)
binarycrusader@8979
   146
    return (SDL_bool) (atomic_cas_ptr(a, oldval, newval) == oldval);
icculus@8088
   147
#elif EMULATE_CAS
slouken@5004
   148
    SDL_bool retval = SDL_FALSE;
slouken@5003
   149
slouken@5003
   150
    enterLock(a);
slouken@5003
   151
    if (*a == oldval) {
slouken@5003
   152
        *a = newval;
slouken@5004
   153
        retval = SDL_TRUE;
slouken@5003
   154
    }
slouken@5003
   155
    leaveLock(a);
slouken@5003
   156
slouken@5004
   157
    return retval;
icculus@8088
   158
#else
icculus@8088
   159
    #error Please define your platform.
icculus@8088
   160
#endif
slouken@5003
   161
}
slouken@5003
   162
icculus@8088
   163
int
icculus@8088
   164
SDL_AtomicSet(SDL_atomic_t *a, int v)
icculus@8088
   165
{
icculus@8088
   166
#ifdef HAVE_MSC_ATOMICS
icculus@8088
   167
    return _InterlockedExchange((long*)&a->value, v);
icculus@8088
   168
#elif defined(HAVE_GCC_ATOMICS)
icculus@8088
   169
    return __sync_lock_test_and_set(&a->value, v);
binarycrusader@8979
   170
#elif defined(__SOLARIS__) && defined(_LP64)
binarycrusader@8979
   171
    return (int) atomic_swap_64((volatile uint64_t*)&a->value, (uint64_t)v);
binarycrusader@8979
   172
#elif defined(__SOLARIS__) && !defined(_LP64)
binarycrusader@8979
   173
    return (int) atomic_swap_32((volatile uint32_t*)&a->value, (uint32_t)v);
icculus@8088
   174
#else
icculus@8088
   175
    int value;
icculus@8088
   176
    do {
icculus@8088
   177
        value = a->value;
icculus@8088
   178
    } while (!SDL_AtomicCAS(a, value, v));
icculus@8088
   179
    return value;
icculus@8088
   180
#endif
icculus@8088
   181
}
icculus@8088
   182
icculus@8088
   183
void*
icculus@8088
   184
SDL_AtomicSetPtr(void **a, void *v)
icculus@8088
   185
{
icculus@8092
   186
#if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
icculus@8092
   187
    return (void *) _InterlockedExchange((long *)a, (long) v);
icculus@8092
   188
#elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
icculus@8088
   189
    return _InterlockedExchangePointer(a, v);
icculus@8088
   190
#elif defined(HAVE_GCC_ATOMICS)
icculus@8088
   191
    return __sync_lock_test_and_set(a, v);
binarycrusader@8979
   192
#elif defined(__SOLARIS__)
binarycrusader@8979
   193
    return atomic_swap_ptr(a, v);
icculus@8088
   194
#else
icculus@8088
   195
    void *value;
icculus@8088
   196
    do {
icculus@8088
   197
        value = *a;
icculus@8088
   198
    } while (!SDL_AtomicCASPtr(a, value, v));
icculus@8088
   199
    return value;
icculus@8088
   200
#endif
icculus@8088
   201
}
icculus@8088
   202
icculus@8088
   203
int
icculus@8088
   204
SDL_AtomicAdd(SDL_atomic_t *a, int v)
icculus@8088
   205
{
icculus@8088
   206
#ifdef HAVE_MSC_ATOMICS
icculus@8088
   207
    return _InterlockedExchangeAdd((long*)&a->value, v);
icculus@8088
   208
#elif defined(HAVE_GCC_ATOMICS)
icculus@8088
   209
    return __sync_fetch_and_add(&a->value, v);
binarycrusader@8979
   210
#elif defined(__SOLARIS__)
binarycrusader@8979
   211
    int pv = a->value;
binarycrusader@8979
   212
    membar_consumer();
binarycrusader@8979
   213
#if defined(_LP64)
binarycrusader@8979
   214
    atomic_add_64((volatile uint64_t*)&a->value, v);
binarycrusader@8979
   215
#elif !defined(_LP64)
binarycrusader@8979
   216
    atomic_add_32((volatile uint32_t*)&a->value, v);
binarycrusader@8979
   217
#endif
binarycrusader@8979
   218
    return pv;
icculus@8088
   219
#else
icculus@8088
   220
    int value;
icculus@8088
   221
    do {
icculus@8088
   222
        value = a->value;
icculus@8088
   223
    } while (!SDL_AtomicCAS(a, value, (value + v)));
icculus@8088
   224
    return value;
icculus@8088
   225
#endif
icculus@8088
   226
}
icculus@8088
   227
icculus@8088
   228
int
icculus@8088
   229
SDL_AtomicGet(SDL_atomic_t *a)
icculus@8088
   230
{
icculus@10972
   231
#ifdef HAVE_ATOMIC_LOAD_N
jlegg@10946
   232
    return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
jlegg@10946
   233
#else
icculus@8088
   234
    int value;
icculus@8088
   235
    do {
icculus@8088
   236
        value = a->value;
icculus@8088
   237
    } while (!SDL_AtomicCAS(a, value, value));
icculus@8088
   238
    return value;
jlegg@10946
   239
#endif
icculus@8088
   240
}
icculus@8088
   241
icculus@8088
   242
void *
icculus@8088
   243
SDL_AtomicGetPtr(void **a)
icculus@8088
   244
{
icculus@10972
   245
#ifdef HAVE_ATOMIC_LOAD_N
jlegg@10946
   246
    return __atomic_load_n(a, __ATOMIC_SEQ_CST);
jlegg@10946
   247
#else
icculus@8088
   248
    void *value;
icculus@8088
   249
    do {
icculus@8088
   250
        value = *a;
icculus@8088
   251
    } while (!SDL_AtomicCASPtr(a, value, value));
icculus@8088
   252
    return value;
jlegg@10946
   253
#endif
icculus@8088
   254
}
icculus@8088
   255
slouken@10879
   256
void
slouken@10879
   257
SDL_MemoryBarrierReleaseFunction(void)
slouken@10879
   258
{
slouken@10879
   259
    SDL_MemoryBarrierRelease();
slouken@10879
   260
}
slouken@10879
   261
slouken@10879
   262
void
slouken@10879
   263
SDL_MemoryBarrierAcquireFunction(void)
slouken@10879
   264
{
slouken@10879
   265
    SDL_MemoryBarrierAcquire();
slouken@10879
   266
}
slouken@7394
   267
slouken@5003
   268
/* vi: set ts=4 sw=4 expandtab: */