src/atomic/SDL_atomic.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 30 Jun 2019 23:58:31 -0700
changeset 12916 119d21c68b62
parent 12915 e8773976e7c6
permissions -rw-r--r--
Fixed memory barrier macro check so it isn't quite so fragile
slouken@5003
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 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
sezeroz@11318
    55
#if defined(__WATCOMC__) && defined(__386__)
sezeroz@12241
    56
SDL_COMPILE_TIME_ASSERT(intsize, 4==sizeof(int));
sezeroz@11318
    57
#define HAVE_WATCOM_ATOMICS
sezeroz@11318
    58
extern _inline int _SDL_xchg_watcom(volatile int *a, int v);
sezeroz@11318
    59
#pragma aux _SDL_xchg_watcom = \
sezeroz@12241
    60
  "lock xchg [ecx], eax" \
sezeroz@11318
    61
  parm [ecx] [eax] \
sezeroz@11318
    62
  value [eax] \
sezeroz@11318
    63
  modify exact [eax];
sezeroz@11318
    64
sezeroz@11318
    65
extern _inline unsigned char _SDL_cmpxchg_watcom(volatile int *a, int newval, int oldval);
sezeroz@11318
    66
#pragma aux _SDL_cmpxchg_watcom = \
sezeroz@11318
    67
  "lock cmpxchg [edx], ecx" \
sezeroz@11318
    68
  "setz al" \
sezeroz@11318
    69
  parm [edx] [ecx] [eax] \
sezeroz@11318
    70
  value [al] \
sezeroz@11318
    71
  modify exact [eax];
sezeroz@11318
    72
sezeroz@11318
    73
extern _inline int _SDL_xadd_watcom(volatile int *a, int v);
sezeroz@11318
    74
#pragma aux _SDL_xadd_watcom = \
sezeroz@11318
    75
  "lock xadd [ecx], eax" \
sezeroz@11318
    76
  parm [ecx] [eax] \
sezeroz@11318
    77
  value [eax] \
sezeroz@11318
    78
  modify exact [eax];
sezeroz@11318
    79
#endif /* __WATCOMC__ && __386__ */
sezeroz@11318
    80
slouken@7191
    81
/*
slouken@5003
    82
  If any of the operations are not provided then we must emulate some
slouken@5003
    83
  of them. That means we need a nice implementation of spin locks
slouken@5003
    84
  that avoids the "one big lock" problem. We use a vector of spin
slouken@5003
    85
  locks and pick which one to use based on the address of the operand
slouken@5003
    86
  of the function.
slouken@5003
    87
slouken@5003
    88
  To generate the index of the lock we first shift by 3 bits to get
slouken@5003
    89
  rid on the zero bits that result from 32 and 64 bit allignment of
slouken@5003
    90
  data. We then mask off all but 5 bits and use those 5 bits as an
slouken@7191
    91
  index into the table.
slouken@5003
    92
slouken@5003
    93
  Picking the lock this way insures that accesses to the same data at
slouken@5003
    94
  the same time will go to the same lock. OTOH, accesses to different
slouken@5003
    95
  data have only a 1/32 chance of hitting the same lock. That should
slouken@5003
    96
  pretty much eliminate the chances of several atomic operations on
slouken@5003
    97
  different data from waiting on the same "big lock". If it isn't
slouken@5003
    98
  then the table of locks can be expanded to a new size so long as
slouken@5003
    99
  the new size is a power of two.
slouken@5003
   100
slouken@5003
   101
  Contributed by Bob Pendleton, bob@pendleton.com
slouken@5003
   102
*/
slouken@5003
   103
sezeroz@11318
   104
#if !defined(HAVE_MSC_ATOMICS) && !defined(HAVE_GCC_ATOMICS) && !defined(__MACOSX__) && !defined(__SOLARIS__) && !defined(HAVE_WATCOM_ATOMICS)
icculus@8088
   105
#define EMULATE_CAS 1
icculus@8088
   106
#endif
icculus@8088
   107
icculus@8088
   108
#if EMULATE_CAS
slouken@5003
   109
static SDL_SpinLock locks[32];
slouken@5003
   110
slouken@7860
   111
static SDL_INLINE void
slouken@5003
   112
enterLock(void *a)
slouken@5003
   113
{
slouken@5004
   114
    uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
slouken@5003
   115
slouken@5004
   116
    SDL_AtomicLock(&locks[index]);
slouken@5003
   117
}
slouken@5003
   118
slouken@7860
   119
static SDL_INLINE void
slouken@5003
   120
leaveLock(void *a)
slouken@5003
   121
{
slouken@5004
   122
    uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
slouken@5003
   123
slouken@5004
   124
    SDL_AtomicUnlock(&locks[index]);
slouken@5003
   125
}
icculus@8088
   126
#endif
slouken@5003
   127
icculus@8088
   128
icculus@8088
   129
SDL_bool
slouken@6978
   130
SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
slouken@5003
   131
{
icculus@8088
   132
#ifdef HAVE_MSC_ATOMICS
icculus@8088
   133
    return (_InterlockedCompareExchange((long*)&a->value, (long)newval, (long)oldval) == (long)oldval);
sezeroz@11318
   134
#elif defined(HAVE_WATCOM_ATOMICS)
sezeroz@11318
   135
    return (SDL_bool) _SDL_cmpxchg_watcom(&a->value, newval, oldval);
icculus@8088
   136
#elif defined(HAVE_GCC_ATOMICS)
icculus@8088
   137
    return (SDL_bool) __sync_bool_compare_and_swap(&a->value, oldval, newval);
icculus@10973
   138
#elif defined(__MACOSX__)  /* this is deprecated in 10.12 sdk; favor gcc atomics. */
icculus@10973
   139
    return (SDL_bool) OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value);
binarycrusader@8979
   140
#elif defined(__SOLARIS__) && defined(_LP64)
binarycrusader@8979
   141
    return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)&a->value, (uint64_t)oldval, (uint64_t)newval) == oldval);
binarycrusader@8979
   142
#elif defined(__SOLARIS__) && !defined(_LP64)
binarycrusader@8979
   143
    return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)&a->value, (uint32_t)oldval, (uint32_t)newval) == oldval);
icculus@8088
   144
#elif EMULATE_CAS
slouken@5004
   145
    SDL_bool retval = SDL_FALSE;
slouken@5003
   146
slouken@5003
   147
    enterLock(a);
slouken@5004
   148
    if (a->value == oldval) {
slouken@5003
   149
        a->value = newval;
slouken@5004
   150
        retval = SDL_TRUE;
slouken@5003
   151
    }
slouken@5003
   152
    leaveLock(a);
slouken@5003
   153
slouken@5004
   154
    return retval;
icculus@8088
   155
#else
icculus@8088
   156
    #error Please define your platform.
icculus@8088
   157
#endif
slouken@5003
   158
}
slouken@5003
   159
icculus@8088
   160
SDL_bool
slouken@6978
   161
SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
slouken@5003
   162
{
icculus@8088
   163
#if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
icculus@8088
   164
    return (_InterlockedCompareExchange((long*)a, (long)newval, (long)oldval) == (long)oldval);
icculus@8088
   165
#elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
icculus@8088
   166
    return (_InterlockedCompareExchangePointer(a, newval, oldval) == oldval);
sezeroz@11318
   167
#elif defined(HAVE_WATCOM_ATOMICS)
sezeroz@11318
   168
    return (SDL_bool) _SDL_cmpxchg_watcom((int *)a, (long)newval, (long)oldval);
icculus@8088
   169
#elif defined(HAVE_GCC_ATOMICS)
icculus@8088
   170
    return __sync_bool_compare_and_swap(a, oldval, newval);
icculus@10973
   171
#elif defined(__MACOSX__) && defined(__LP64__)  /* this is deprecated in 10.12 sdk; favor gcc atomics. */
icculus@10973
   172
    return (SDL_bool) OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t*) a);
icculus@10973
   173
#elif defined(__MACOSX__) && !defined(__LP64__)  /* this is deprecated in 10.12 sdk; favor gcc atomics. */
icculus@10973
   174
    return (SDL_bool) OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t*) a);
binarycrusader@8979
   175
#elif defined(__SOLARIS__)
binarycrusader@8979
   176
    return (SDL_bool) (atomic_cas_ptr(a, oldval, newval) == oldval);
icculus@8088
   177
#elif EMULATE_CAS
slouken@5004
   178
    SDL_bool retval = SDL_FALSE;
slouken@5003
   179
slouken@5003
   180
    enterLock(a);
slouken@5003
   181
    if (*a == oldval) {
slouken@5003
   182
        *a = newval;
slouken@5004
   183
        retval = SDL_TRUE;
slouken@5003
   184
    }
slouken@5003
   185
    leaveLock(a);
slouken@5003
   186
slouken@5004
   187
    return retval;
icculus@8088
   188
#else
icculus@8088
   189
    #error Please define your platform.
icculus@8088
   190
#endif
slouken@5003
   191
}
slouken@5003
   192
icculus@8088
   193
int
icculus@8088
   194
SDL_AtomicSet(SDL_atomic_t *a, int v)
icculus@8088
   195
{
icculus@8088
   196
#ifdef HAVE_MSC_ATOMICS
icculus@8088
   197
    return _InterlockedExchange((long*)&a->value, v);
sezeroz@11318
   198
#elif defined(HAVE_WATCOM_ATOMICS)
sezeroz@11318
   199
    return _SDL_xchg_watcom(&a->value, v);
icculus@8088
   200
#elif defined(HAVE_GCC_ATOMICS)
icculus@8088
   201
    return __sync_lock_test_and_set(&a->value, v);
binarycrusader@8979
   202
#elif defined(__SOLARIS__) && defined(_LP64)
binarycrusader@8979
   203
    return (int) atomic_swap_64((volatile uint64_t*)&a->value, (uint64_t)v);
binarycrusader@8979
   204
#elif defined(__SOLARIS__) && !defined(_LP64)
binarycrusader@8979
   205
    return (int) atomic_swap_32((volatile uint32_t*)&a->value, (uint32_t)v);
icculus@8088
   206
#else
icculus@8088
   207
    int value;
icculus@8088
   208
    do {
icculus@8088
   209
        value = a->value;
icculus@8088
   210
    } while (!SDL_AtomicCAS(a, value, v));
icculus@8088
   211
    return value;
icculus@8088
   212
#endif
icculus@8088
   213
}
icculus@8088
   214
icculus@8088
   215
void*
icculus@8088
   216
SDL_AtomicSetPtr(void **a, void *v)
icculus@8088
   217
{
icculus@8092
   218
#if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
icculus@8092
   219
    return (void *) _InterlockedExchange((long *)a, (long) v);
icculus@8092
   220
#elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
icculus@8088
   221
    return _InterlockedExchangePointer(a, v);
sezeroz@11318
   222
#elif defined(HAVE_WATCOM_ATOMICS)
sezeroz@11318
   223
    return (void *) _SDL_xchg_watcom((int *)a, (long)v);
icculus@8088
   224
#elif defined(HAVE_GCC_ATOMICS)
icculus@8088
   225
    return __sync_lock_test_and_set(a, v);
binarycrusader@8979
   226
#elif defined(__SOLARIS__)
binarycrusader@8979
   227
    return atomic_swap_ptr(a, v);
icculus@8088
   228
#else
icculus@8088
   229
    void *value;
icculus@8088
   230
    do {
icculus@8088
   231
        value = *a;
icculus@8088
   232
    } while (!SDL_AtomicCASPtr(a, value, v));
icculus@8088
   233
    return value;
icculus@8088
   234
#endif
icculus@8088
   235
}
icculus@8088
   236
icculus@8088
   237
int
icculus@8088
   238
SDL_AtomicAdd(SDL_atomic_t *a, int v)
icculus@8088
   239
{
icculus@8088
   240
#ifdef HAVE_MSC_ATOMICS
icculus@8088
   241
    return _InterlockedExchangeAdd((long*)&a->value, v);
sezeroz@11318
   242
#elif defined(HAVE_WATCOM_ATOMICS)
sezeroz@11318
   243
    return _SDL_xadd_watcom(&a->value, v);
icculus@8088
   244
#elif defined(HAVE_GCC_ATOMICS)
icculus@8088
   245
    return __sync_fetch_and_add(&a->value, v);
binarycrusader@8979
   246
#elif defined(__SOLARIS__)
binarycrusader@8979
   247
    int pv = a->value;
binarycrusader@8979
   248
    membar_consumer();
binarycrusader@8979
   249
#if defined(_LP64)
binarycrusader@8979
   250
    atomic_add_64((volatile uint64_t*)&a->value, v);
binarycrusader@8979
   251
#elif !defined(_LP64)
binarycrusader@8979
   252
    atomic_add_32((volatile uint32_t*)&a->value, v);
binarycrusader@8979
   253
#endif
binarycrusader@8979
   254
    return pv;
icculus@8088
   255
#else
icculus@8088
   256
    int value;
icculus@8088
   257
    do {
icculus@8088
   258
        value = a->value;
icculus@8088
   259
    } while (!SDL_AtomicCAS(a, value, (value + v)));
icculus@8088
   260
    return value;
icculus@8088
   261
#endif
icculus@8088
   262
}
icculus@8088
   263
icculus@8088
   264
int
icculus@8088
   265
SDL_AtomicGet(SDL_atomic_t *a)
icculus@8088
   266
{
icculus@10972
   267
#ifdef HAVE_ATOMIC_LOAD_N
jlegg@10946
   268
    return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
jlegg@10946
   269
#else
icculus@8088
   270
    int value;
icculus@8088
   271
    do {
icculus@8088
   272
        value = a->value;
icculus@8088
   273
    } while (!SDL_AtomicCAS(a, value, value));
icculus@8088
   274
    return value;
jlegg@10946
   275
#endif
icculus@8088
   276
}
icculus@8088
   277
icculus@8088
   278
void *
icculus@8088
   279
SDL_AtomicGetPtr(void **a)
icculus@8088
   280
{
icculus@10972
   281
#ifdef HAVE_ATOMIC_LOAD_N
jlegg@10946
   282
    return __atomic_load_n(a, __ATOMIC_SEQ_CST);
jlegg@10946
   283
#else
icculus@8088
   284
    void *value;
icculus@8088
   285
    do {
icculus@8088
   286
        value = *a;
icculus@8088
   287
    } while (!SDL_AtomicCASPtr(a, value, value));
icculus@8088
   288
    return value;
jlegg@10946
   289
#endif
icculus@8088
   290
}
icculus@8088
   291
slouken@12916
   292
#ifdef SDL_MEMORY_BARRIER_USES_FUNCTION
slouken@12912
   293
#error This file should be built in arm mode so the mcr instruction is available for memory barriers
slouken@12912
   294
#endif
slouken@12912
   295
slouken@10879
   296
void
slouken@10879
   297
SDL_MemoryBarrierReleaseFunction(void)
slouken@10879
   298
{
slouken@10879
   299
    SDL_MemoryBarrierRelease();
slouken@10879
   300
}
slouken@10879
   301
slouken@10879
   302
void
slouken@10879
   303
SDL_MemoryBarrierAcquireFunction(void)
slouken@10879
   304
{
slouken@10879
   305
    SDL_MemoryBarrierAcquire();
slouken@10879
   306
}
slouken@7394
   307
slouken@5003
   308
/* vi: set ts=4 sw=4 expandtab: */