src/atomic/SDL_atomic.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 13 Apr 2017 13:22:23 -0400
changeset 10972 e9d71390bedf
parent 10950 fd1b8fd3b937
child 10973 b141211c5d0a
permissions -rw-r--r--
atomic: let Clang always use atomic_load_n if available.

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