include/SDL_atomic.h
author Sam Lantinga <slouken@libsdl.org>
Wed, 10 Jul 2013 20:17:20 -0700
changeset 7394 38dc4961ab15
parent 7393 358696c354a8
child 7677 871d43c6968a
permissions -rw-r--r--
Added PowerPC and ARM versions of the memory barrier functions.
bob@3180
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
bob@3180
     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.
bob@3180
     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:
bob@3180
    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@5535
    20
*/
bob@3180
    21
bob@3180
    22
/**
slouken@5003
    23
 * \file SDL_atomic.h
slouken@7191
    24
 *
slouken@5003
    25
 * Atomic operations.
slouken@7191
    26
 *
slouken@5003
    27
 * IMPORTANT:
slouken@5003
    28
 * If you are not an expert in concurrent lockless programming, you should
slouken@5003
    29
 * only be using the atomic lock and reference counting functions in this
slouken@5003
    30
 * file.  In all other cases you should be protecting your data structures
slouken@5003
    31
 * with full mutexes.
slouken@7191
    32
 *
slouken@5003
    33
 * The list of "safe" functions to use are:
slouken@5003
    34
 *  SDL_AtomicLock()
slouken@5003
    35
 *  SDL_AtomicUnlock()
slouken@5003
    36
 *  SDL_AtomicIncRef()
slouken@5003
    37
 *  SDL_AtomicDecRef()
slouken@7191
    38
 *
slouken@5003
    39
 * Seriously, here be dragons!
slouken@5095
    40
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
slouken@5003
    41
 *
slouken@7191
    42
 * You can find out a little more about lockless programming and the
slouken@5095
    43
 * subtle issues that can arise here:
slouken@5095
    44
 * http://msdn.microsoft.com/en-us/library/ee418650%28v=vs.85%29.aspx
slouken@5095
    45
 *
slouken@5098
    46
 * There's also lots of good information here:
slouken@5098
    47
 * http://www.1024cores.net/home/lock-free-algorithms
slouken@7393
    48
 * http://preshing.com/
slouken@5098
    49
 *
slouken@5095
    50
 * These operations may or may not actually be implemented using
slouken@5003
    51
 * processor specific atomic operations. When possible they are
slouken@5003
    52
 * implemented as true processor specific atomic operations. When that
slouken@5003
    53
 * is not possible the are implemented using locks that *do* use the
slouken@5003
    54
 * available atomic operations.
slouken@5003
    55
 *
slouken@5003
    56
 * All of the atomic operations that modify memory are full memory barriers.
bob@3180
    57
 */
bob@3180
    58
bob@3180
    59
#ifndef _SDL_atomic_h_
bob@3180
    60
#define _SDL_atomic_h_
bob@3180
    61
bob@3180
    62
#include "SDL_stdinc.h"
bob@3180
    63
#include "SDL_platform.h"
bob@3180
    64
bob@3180
    65
#include "begin_code.h"
bob@3180
    66
slouken@5073
    67
/* Need to do this here because intrin.h has C++ code in it */
slouken@5073
    68
/* Visual Studio 2005 has a bug where intrin.h conflicts with winnt.h */
icculus@6430
    69
#if defined(_MSC_VER) && (_MSC_VER >= 1500)
slouken@5073
    70
#include <intrin.h>
slouken@5096
    71
#define HAVE_MSC_ATOMICS 1
slouken@5073
    72
#endif
slouken@5073
    73
bob@3180
    74
/* Set up for C function definitions, even when using C++ */
bob@3180
    75
#ifdef __cplusplus
bob@3180
    76
extern "C" {
bob@3180
    77
#endif
bob@3180
    78
bob@3261
    79
/**
slouken@5003
    80
 * \name SDL AtomicLock
slouken@7191
    81
 *
slouken@5003
    82
 * The atomic locks are efficient spinlocks using CPU instructions,
slouken@5003
    83
 * but are vulnerable to starvation and can spin forever if a thread
slouken@5003
    84
 * holding a lock has been terminated.  For this reason you should
slouken@5003
    85
 * minimize the code executed inside an atomic lock and never do
slouken@5003
    86
 * expensive things like API or system calls while holding them.
slouken@5003
    87
 *
slouken@5003
    88
 * The atomic locks are not safe to lock recursively.
slouken@5003
    89
 *
slouken@5003
    90
 * Porting Note:
slouken@5003
    91
 * The spin lock functions and type are required and can not be
slouken@5003
    92
 * emulated because they are used in the atomic emulation code.
bob@3261
    93
 */
slouken@3407
    94
/*@{*/
bob@3261
    95
slouken@5003
    96
typedef int SDL_SpinLock;
bob@3261
    97
bob@3261
    98
/**
slouken@5003
    99
 * \brief Try to lock a spin lock by setting it to a non-zero value.
slouken@7191
   100
 *
slouken@5003
   101
 * \param lock Points to the lock.
slouken@5003
   102
 *
slouken@5003
   103
 * \return SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.
slouken@5003
   104
 */
slouken@5003
   105
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock);
slouken@5003
   106
slouken@5003
   107
/**
slouken@5003
   108
 * \brief Lock a spin lock by setting it to a non-zero value.
slouken@7191
   109
 *
slouken@5003
   110
 * \param lock Points to the lock.
bob@3261
   111
 */
bob@3261
   112
extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
bob@3261
   113
bob@3261
   114
/**
slouken@5003
   115
 * \brief Unlock a spin lock by setting it to 0. Always returns immediately
bob@3261
   116
 *
slouken@5003
   117
 * \param lock Points to the lock.
bob@3261
   118
 */
bob@3261
   119
extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
bob@3261
   120
slouken@3407
   121
/*@}*//*SDL AtomicLock*/
bob@3202
   122
slouken@5095
   123
slouken@5135
   124
/**
slouken@5135
   125
 * The compiler barrier prevents the compiler from reordering
slouken@5135
   126
 * reads and writes to globally visible variables across the call.
slouken@5135
   127
 */
jorgen@6923
   128
#if defined(_MSC_VER) && (_MSC_VER > 1200)
slouken@5095
   129
void _ReadWriteBarrier(void);
slouken@5095
   130
#pragma intrinsic(_ReadWriteBarrier)
slouken@5095
   131
#define SDL_CompilerBarrier()   _ReadWriteBarrier()
slouken@5097
   132
#elif defined(__GNUC__)
slouken@5095
   133
#define SDL_CompilerBarrier()   __asm__ __volatile__ ("" : : : "memory")
slouken@5095
   134
#else
slouken@5095
   135
#define SDL_CompilerBarrier()   \
jorgen@6923
   136
{ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); }
slouken@5095
   137
#endif
slouken@5095
   138
slouken@7393
   139
/**
slouken@7393
   140
 * Memory barriers are designed to prevent reads and writes from being
slouken@7393
   141
 * reordered by the compiler and being seen out of order on multi-core CPUs.
slouken@7393
   142
 *
slouken@7393
   143
 * A typical pattern would be for thread A to write some data and a flag,
slouken@7393
   144
 * and for thread B to read the flag and get the data. In this case you
slouken@7393
   145
 * would insert a release barrier between writing the data and the flag,
slouken@7393
   146
 * guaranteeing that the data write completes no later than the flag is
slouken@7393
   147
 * written, and you would insert an acquire barrier between reading the
slouken@7393
   148
 * flag and reading the data, to ensure that all the reads associated
slouken@7393
   149
 * with the flag have completed.
slouken@7393
   150
 *
slouken@7393
   151
 * In this pattern you should always see a release barrier paired with
slouken@7393
   152
 * an acquire barrier and you should gate the data reads/writes with a
slouken@7393
   153
 * single flag variable.
slouken@7393
   154
 *
slouken@7393
   155
 * For more information on these semantics, take a look at the blog post:
slouken@7393
   156
 * http://preshing.com/20120913/acquire-and-release-semantics
slouken@7393
   157
 */
slouken@7394
   158
#if defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
slouken@7394
   159
#define SDL_MemoryBarrierRelease()   __asm__ __volatile__ ("lwsync" : : : "memory")
slouken@7394
   160
#define SDL_MemoryBarrierAcquire()   __asm__ __volatile__ ("lwsync" : : : "memory")
slouken@7394
   161
#elif defined(__GNUC__) && defined(__arm__)
slouken@7394
   162
#if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7EM__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
slouken@7394
   163
#define SDL_MemoryBarrierRelease()   __asm__ __volatile__ ("dmb ish" : : : "memory")
slouken@7394
   164
#define SDL_MemoryBarrierAcquire()   __asm__ __volatile__ ("dmb ish" : : : "memory")
slouken@7394
   165
#elif 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
   166
#ifdef __thumb__
slouken@7394
   167
/* The mcr instruction isn't available in thumb mode, use real functions */
slouken@7394
   168
extern DECLSPEC void SDLCALL SDL_MemoryBarrierRelease();
slouken@7394
   169
extern DECLSPEC void SDLCALL SDL_MemoryBarrierAcquire();
slouken@7394
   170
#else
slouken@7394
   171
#define SDL_MemoryBarrierRelease()   __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
slouken@7394
   172
#define SDL_MemoryBarrierAcquire()   __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
slouken@7394
   173
#endif /* __thumb__ */
slouken@7394
   174
#else
slouken@7394
   175
#define SDL_MemoryBarrierRelease()   __asm__ __volatile__ ("" : : : "memory")
slouken@7394
   176
#define SDL_MemoryBarrierAcquire()   __asm__ __volatile__ ("" : : : "memory")
slouken@7394
   177
#endif /* __GNUC__ && __arm__ */
slouken@7394
   178
#else
slouken@7394
   179
/* This is correct for the x86 and x64 CPUs, and we'll expand this over time. */
slouken@7393
   180
#define SDL_MemoryBarrierRelease()  SDL_CompilerBarrier()
slouken@7393
   181
#define SDL_MemoryBarrierAcquire()  SDL_CompilerBarrier()
slouken@7394
   182
#endif
slouken@7393
   183
slouken@7393
   184
slouken@5006
   185
/* Platform specific optimized versions of the atomic functions,
slouken@5006
   186
 * you can disable these by defining SDL_DISABLE_ATOMIC_INLINE
slouken@5006
   187
 */
icculus@5552
   188
#if defined(SDL_ATOMIC_DISABLED) && SDL_ATOMIC_DISABLED
slouken@5225
   189
#define SDL_DISABLE_ATOMIC_INLINE
slouken@5225
   190
#endif
slouken@5006
   191
#ifndef SDL_DISABLE_ATOMIC_INLINE
slouken@5006
   192
slouken@5097
   193
#ifdef HAVE_MSC_ATOMICS
slouken@5004
   194
slouken@5016
   195
#define SDL_AtomicSet(a, v)     _InterlockedExchange((long*)&(a)->value, (v))
slouken@5016
   196
#define SDL_AtomicAdd(a, v)     _InterlockedExchangeAdd((long*)&(a)->value, (v))
slouken@5016
   197
#define SDL_AtomicCAS(a, oldval, newval) (_InterlockedCompareExchange((long*)&(a)->value, (newval), (oldval)) == (oldval))
slouken@5095
   198
#define SDL_AtomicSetPtr(a, v)  _InterlockedExchangePointer((a), (v))
slouken@5012
   199
#if _M_IX86
slouken@5012
   200
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchange((long*)(a), (long)(newval), (long)(oldval)) == (long)(oldval))
slouken@5012
   201
#else
slouken@5012
   202
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchangePointer((a), (newval), (oldval)) == (oldval))
slouken@5012
   203
#endif
slouken@5004
   204
slouken@5097
   205
#elif defined(__MACOSX__)
slouken@5004
   206
#include <libkern/OSAtomic.h>
slouken@5004
   207
slouken@5095
   208
#define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((oldval), (newval), &(a)->value)
gabomdq@6463
   209
#ifdef __LP64__
gabomdq@6463
   210
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
gabomdq@6463
   211
#else
slouken@5004
   212
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a))
slouken@5004
   213
#endif
slouken@5004
   214
slouken@5097
   215
#elif defined(HAVE_GCC_ATOMICS)
slouken@5004
   216
slouken@5004
   217
#define SDL_AtomicSet(a, v)     __sync_lock_test_and_set(&(a)->value, v)
slouken@5004
   218
#define SDL_AtomicAdd(a, v)     __sync_fetch_and_add(&(a)->value, v)
slouken@5095
   219
#define SDL_AtomicSetPtr(a, v)  __sync_lock_test_and_set(a, v)
slouken@5004
   220
#define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval)
slouken@5004
   221
#define SDL_AtomicCASPtr(a, oldval, newval) __sync_bool_compare_and_swap(a, oldval, newval)
slouken@5004
   222
slouken@5004
   223
#endif
slouken@3407
   224
slouken@5006
   225
#endif /* !SDL_DISABLE_ATOMIC_INLINE */
slouken@5006
   226
slouken@5006
   227
slouken@3407
   228
/**
slouken@5003
   229
 * \brief A type representing an atomic integer value.  It is a struct
slouken@5003
   230
 *        so people don't accidentally use numeric operations on it.
bob@3199
   231
 */
slouken@5003
   232
#ifndef SDL_atomic_t_defined
slouken@5003
   233
typedef struct { int value; } SDL_atomic_t;
slouken@5003
   234
#endif
bob@3237
   235
bob@3199
   236
/**
slouken@5097
   237
 * \brief Set an atomic variable to a new value if it is currently an old value.
slouken@5097
   238
 *
slouken@5097
   239
 * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
slouken@5097
   240
 *
slouken@5097
   241
 * \note If you don't know what this function is for, you shouldn't use it!
slouken@5097
   242
*/
slouken@5097
   243
#ifndef SDL_AtomicCAS
slouken@6978
   244
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
slouken@5097
   245
#endif
slouken@5097
   246
slouken@5097
   247
/**
slouken@5003
   248
 * \brief Set an atomic variable to a value.
slouken@5003
   249
 *
slouken@5003
   250
 * \return The previous value of the atomic variable.
bob@3199
   251
 */
slouken@5003
   252
#ifndef SDL_AtomicSet
icculus@7004
   253
SDL_FORCE_INLINE int SDL_AtomicSet(SDL_atomic_t *a, int v)
slouken@5097
   254
{
slouken@5097
   255
    int value;
slouken@5097
   256
    do {
slouken@5097
   257
        value = a->value;
slouken@5097
   258
    } while (!SDL_AtomicCAS(a, value, v));
slouken@5097
   259
    return value;
slouken@5097
   260
}
slouken@5003
   261
#endif
bob@3237
   262
bob@3199
   263
/**
slouken@5003
   264
 * \brief Get the value of an atomic variable
bob@3199
   265
 */
slouken@5003
   266
#ifndef SDL_AtomicGet
icculus@7004
   267
SDL_FORCE_INLINE int SDL_AtomicGet(SDL_atomic_t *a)
slouken@5097
   268
{
slouken@5097
   269
    int value = a->value;
slouken@5097
   270
    SDL_CompilerBarrier();
slouken@5097
   271
    return value;
slouken@5097
   272
}
slouken@5003
   273
#endif
bob@3237
   274
bob@3199
   275
/**
slouken@5095
   276
 * \brief Add to an atomic variable.
slouken@5003
   277
 *
slouken@5003
   278
 * \return The previous value of the atomic variable.
slouken@5095
   279
 *
slouken@5095
   280
 * \note This same style can be used for any number operation
bob@3199
   281
 */
slouken@5003
   282
#ifndef SDL_AtomicAdd
icculus@7004
   283
SDL_FORCE_INLINE int SDL_AtomicAdd(SDL_atomic_t *a, int v)
slouken@5097
   284
{
slouken@5097
   285
    int value;
slouken@5097
   286
    do {
slouken@5097
   287
        value = a->value;
slouken@5097
   288
    } while (!SDL_AtomicCAS(a, value, (value + v)));
slouken@5097
   289
    return value;
slouken@5097
   290
}
slouken@5003
   291
#endif
bob@3237
   292
bob@3199
   293
/**
slouken@5003
   294
 * \brief Increment an atomic variable used as a reference count.
bob@3199
   295
 */
slouken@5003
   296
#ifndef SDL_AtomicIncRef
slouken@5095
   297
#define SDL_AtomicIncRef(a)    SDL_AtomicAdd(a, 1)
slouken@5003
   298
#endif
bob@3237
   299
bob@3199
   300
/**
slouken@5003
   301
 * \brief Decrement an atomic variable used as a reference count.
slouken@5003
   302
 *
slouken@5095
   303
 * \return SDL_TRUE if the variable reached zero after decrementing,
slouken@5003
   304
 *         SDL_FALSE otherwise
bob@3199
   305
 */
slouken@5003
   306
#ifndef SDL_AtomicDecRef
slouken@5095
   307
#define SDL_AtomicDecRef(a)    (SDL_AtomicAdd(a, -1) == 1)
slouken@5003
   308
#endif
bob@3237
   309
bob@3199
   310
/**
slouken@5003
   311
 * \brief Set a pointer to a new value if it is currently an old value.
slouken@5003
   312
 *
slouken@5004
   313
 * \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
slouken@5003
   314
 *
slouken@5003
   315
 * \note If you don't know what this function is for, you shouldn't use it!
slouken@5003
   316
*/
slouken@5003
   317
#ifndef SDL_AtomicCASPtr
slouken@6978
   318
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void* *a, void *oldval, void *newval);
slouken@5003
   319
#endif
slouken@3407
   320
slouken@5097
   321
/**
slouken@5097
   322
 * \brief Set a pointer to a value atomically.
slouken@5097
   323
 *
slouken@5097
   324
 * \return The previous value of the pointer.
slouken@5097
   325
 */
slouken@5097
   326
#ifndef SDL_AtomicSetPtr
icculus@7004
   327
SDL_FORCE_INLINE void* SDL_AtomicSetPtr(void* *a, void* v)
slouken@5097
   328
{
slouken@5097
   329
    void* value;
slouken@5097
   330
    do {
slouken@5097
   331
        value = *a;
slouken@5097
   332
    } while (!SDL_AtomicCASPtr(a, value, v));
slouken@5097
   333
    return value;
slouken@5097
   334
}
slouken@5097
   335
#endif
slouken@5097
   336
slouken@5097
   337
/**
slouken@5097
   338
 * \brief Get the value of a pointer atomically.
slouken@5097
   339
 */
slouken@5097
   340
#ifndef SDL_AtomicGetPtr
icculus@7004
   341
SDL_FORCE_INLINE void* SDL_AtomicGetPtr(void* *a)
slouken@5097
   342
{
slouken@5097
   343
    void* value = *a;
slouken@5097
   344
    SDL_CompilerBarrier();
slouken@5097
   345
    return value;
slouken@5097
   346
}
slouken@5097
   347
#endif
slouken@5097
   348
slouken@5097
   349
bob@3180
   350
/* Ends C function definitions when using C++ */
bob@3180
   351
#ifdef __cplusplus
bob@3180
   352
}
bob@3180
   353
#endif
bob@3180
   354
bob@3180
   355
#include "close_code.h"
bob@3180
   356
bob@3180
   357
#endif /* _SDL_atomic_h_ */
bob@3180
   358
bob@3180
   359
/* vi: set ts=4 sw=4 expandtab: */