include/SDL_atomic.h
author Sam Lantinga <slouken@libsdl.org>
Sat, 19 Aug 2017 03:07:44 -0700
changeset 11330 6ae35c5f7f7b
parent 11318 06fd8421e8f6
child 11706 d5d95b296cb0
permissions -rw-r--r--
Fixed building SDL applications with Visual Studio and the clang toolset
Also fixed building 64-bit SDL with clang. 32-bit doesn't build because of the inline assembly for C runtime support.
bob@3180
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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
slouken@10638
    59
#ifndef SDL_atomic_h_
slouken@10638
    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
bob@3180
    67
/* Set up for C function definitions, even when using C++ */
bob@3180
    68
#ifdef __cplusplus
bob@3180
    69
extern "C" {
bob@3180
    70
#endif
bob@3180
    71
bob@3261
    72
/**
slouken@5003
    73
 * \name SDL AtomicLock
slouken@7191
    74
 *
slouken@5003
    75
 * The atomic locks are efficient spinlocks using CPU instructions,
slouken@5003
    76
 * but are vulnerable to starvation and can spin forever if a thread
slouken@5003
    77
 * holding a lock has been terminated.  For this reason you should
slouken@5003
    78
 * minimize the code executed inside an atomic lock and never do
slouken@5003
    79
 * expensive things like API or system calls while holding them.
slouken@5003
    80
 *
slouken@5003
    81
 * The atomic locks are not safe to lock recursively.
slouken@5003
    82
 *
slouken@5003
    83
 * Porting Note:
slouken@5003
    84
 * The spin lock functions and type are required and can not be
slouken@5003
    85
 * emulated because they are used in the atomic emulation code.
bob@3261
    86
 */
gabomdq@7678
    87
/* @{ */
bob@3261
    88
slouken@5003
    89
typedef int SDL_SpinLock;
bob@3261
    90
bob@3261
    91
/**
slouken@5003
    92
 * \brief Try to lock a spin lock by setting it to a non-zero value.
slouken@7191
    93
 *
slouken@5003
    94
 * \param lock Points to the lock.
slouken@5003
    95
 *
slouken@5003
    96
 * \return SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.
slouken@5003
    97
 */
slouken@5003
    98
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock);
slouken@5003
    99
slouken@5003
   100
/**
slouken@5003
   101
 * \brief Lock a spin lock by setting it to a non-zero value.
slouken@7191
   102
 *
slouken@5003
   103
 * \param lock Points to the lock.
bob@3261
   104
 */
bob@3261
   105
extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
bob@3261
   106
bob@3261
   107
/**
slouken@5003
   108
 * \brief Unlock a spin lock by setting it to 0. Always returns immediately
bob@3261
   109
 *
slouken@5003
   110
 * \param lock Points to the lock.
bob@3261
   111
 */
bob@3261
   112
extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
bob@3261
   113
gabomdq@7678
   114
/* @} *//* SDL AtomicLock */
bob@3202
   115
slouken@5095
   116
slouken@5135
   117
/**
slouken@5135
   118
 * The compiler barrier prevents the compiler from reordering
slouken@5135
   119
 * reads and writes to globally visible variables across the call.
slouken@5135
   120
 */
slouken@11330
   121
#if defined(_MSC_VER) && (_MSC_VER > 1200) && !defined(__clang__)
slouken@5095
   122
void _ReadWriteBarrier(void);
slouken@5095
   123
#pragma intrinsic(_ReadWriteBarrier)
slouken@5095
   124
#define SDL_CompilerBarrier()   _ReadWriteBarrier()
icculus@9278
   125
#elif (defined(__GNUC__) && !defined(__EMSCRIPTEN__)) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x5120))
binarycrusader@8979
   126
/* This is correct for all CPUs when using GCC or Solaris Studio 12.1+. */
slouken@5095
   127
#define SDL_CompilerBarrier()   __asm__ __volatile__ ("" : : : "memory")
sezeroz@11318
   128
#elif defined(__WATCOMC__)
sezeroz@11318
   129
extern _inline void SDL_CompilerBarrier (void);
sezeroz@11318
   130
#pragma aux SDL_CompilerBarrier = "" parm [] modify exact [];
slouken@5095
   131
#else
slouken@5095
   132
#define SDL_CompilerBarrier()   \
jorgen@6923
   133
{ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); }
slouken@5095
   134
#endif
slouken@5095
   135
slouken@7393
   136
/**
slouken@7393
   137
 * Memory barriers are designed to prevent reads and writes from being
slouken@7393
   138
 * reordered by the compiler and being seen out of order on multi-core CPUs.
slouken@7393
   139
 *
slouken@7393
   140
 * A typical pattern would be for thread A to write some data and a flag,
slouken@7393
   141
 * and for thread B to read the flag and get the data. In this case you
slouken@7393
   142
 * would insert a release barrier between writing the data and the flag,
slouken@7393
   143
 * guaranteeing that the data write completes no later than the flag is
slouken@7393
   144
 * written, and you would insert an acquire barrier between reading the
slouken@7393
   145
 * flag and reading the data, to ensure that all the reads associated
slouken@7393
   146
 * with the flag have completed.
slouken@7393
   147
 *
slouken@7393
   148
 * In this pattern you should always see a release barrier paired with
slouken@7393
   149
 * an acquire barrier and you should gate the data reads/writes with a
slouken@7393
   150
 * single flag variable.
slouken@7393
   151
 *
slouken@7393
   152
 * For more information on these semantics, take a look at the blog post:
slouken@7393
   153
 * http://preshing.com/20120913/acquire-and-release-semantics
slouken@7393
   154
 */
slouken@10879
   155
extern DECLSPEC void SDLCALL SDL_MemoryBarrierReleaseFunction(void);
slouken@10879
   156
extern DECLSPEC void SDLCALL SDL_MemoryBarrierAcquireFunction(void);
slouken@10879
   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@10879
   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__) || defined(__ARM_ARCH_5TE__)
slouken@7394
   166
#ifdef __thumb__
slouken@7394
   167
/* The mcr instruction isn't available in thumb mode, use real functions */
slouken@10879
   168
#define SDL_MemoryBarrierRelease()   SDL_MemoryBarrierReleaseFunction()
slouken@10879
   169
#define SDL_MemoryBarrierAcquire()   SDL_MemoryBarrierAcquireFunction()
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
binarycrusader@8979
   179
#if (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x5120))
binarycrusader@8979
   180
/* This is correct for all CPUs on Solaris when using Solaris Studio 12.1+. */
binarycrusader@8979
   181
#include <mbarrier.h>
binarycrusader@8979
   182
#define SDL_MemoryBarrierRelease()  __machine_rel_barrier()
binarycrusader@8979
   183
#define SDL_MemoryBarrierAcquire()  __machine_acq_barrier()
binarycrusader@8979
   184
#else
slouken@7394
   185
/* This is correct for the x86 and x64 CPUs, and we'll expand this over time. */
slouken@7393
   186
#define SDL_MemoryBarrierRelease()  SDL_CompilerBarrier()
slouken@7393
   187
#define SDL_MemoryBarrierAcquire()  SDL_CompilerBarrier()
slouken@7394
   188
#endif
binarycrusader@8979
   189
#endif
slouken@7393
   190
slouken@3407
   191
/**
slouken@5003
   192
 * \brief A type representing an atomic integer value.  It is a struct
slouken@5003
   193
 *        so people don't accidentally use numeric operations on it.
bob@3199
   194
 */
slouken@5003
   195
typedef struct { int value; } SDL_atomic_t;
bob@3237
   196
bob@3199
   197
/**
slouken@5097
   198
 * \brief Set an atomic variable to a new value if it is currently an old value.
slouken@5097
   199
 *
slouken@5097
   200
 * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
slouken@5097
   201
 *
slouken@5097
   202
 * \note If you don't know what this function is for, you shouldn't use it!
slouken@5097
   203
*/
slouken@6978
   204
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
slouken@5097
   205
slouken@5097
   206
/**
slouken@5003
   207
 * \brief Set an atomic variable to a value.
slouken@5003
   208
 *
slouken@5003
   209
 * \return The previous value of the atomic variable.
bob@3199
   210
 */
icculus@8088
   211
extern DECLSPEC int SDLCALL SDL_AtomicSet(SDL_atomic_t *a, int v);
bob@3237
   212
bob@3199
   213
/**
slouken@5003
   214
 * \brief Get the value of an atomic variable
bob@3199
   215
 */
icculus@8088
   216
extern DECLSPEC int SDLCALL SDL_AtomicGet(SDL_atomic_t *a);
bob@3237
   217
bob@3199
   218
/**
slouken@5095
   219
 * \brief Add to an atomic variable.
slouken@5003
   220
 *
slouken@5003
   221
 * \return The previous value of the atomic variable.
slouken@5095
   222
 *
slouken@5095
   223
 * \note This same style can be used for any number operation
bob@3199
   224
 */
icculus@8088
   225
extern DECLSPEC int SDLCALL SDL_AtomicAdd(SDL_atomic_t *a, int v);
bob@3237
   226
bob@3199
   227
/**
slouken@5003
   228
 * \brief Increment an atomic variable used as a reference count.
bob@3199
   229
 */
slouken@5003
   230
#ifndef SDL_AtomicIncRef
slouken@5095
   231
#define SDL_AtomicIncRef(a)    SDL_AtomicAdd(a, 1)
slouken@5003
   232
#endif
bob@3237
   233
bob@3199
   234
/**
slouken@5003
   235
 * \brief Decrement an atomic variable used as a reference count.
slouken@5003
   236
 *
slouken@5095
   237
 * \return SDL_TRUE if the variable reached zero after decrementing,
slouken@5003
   238
 *         SDL_FALSE otherwise
bob@3199
   239
 */
slouken@5003
   240
#ifndef SDL_AtomicDecRef
slouken@5095
   241
#define SDL_AtomicDecRef(a)    (SDL_AtomicAdd(a, -1) == 1)
slouken@5003
   242
#endif
bob@3237
   243
bob@3199
   244
/**
slouken@5003
   245
 * \brief Set a pointer to a new value if it is currently an old value.
slouken@5003
   246
 *
slouken@5004
   247
 * \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
slouken@5003
   248
 *
slouken@5003
   249
 * \note If you don't know what this function is for, you shouldn't use it!
slouken@5003
   250
*/
icculus@8088
   251
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval);
slouken@3407
   252
slouken@5097
   253
/**
slouken@5097
   254
 * \brief Set a pointer to a value atomically.
slouken@5097
   255
 *
slouken@5097
   256
 * \return The previous value of the pointer.
slouken@5097
   257
 */
icculus@8088
   258
extern DECLSPEC void* SDLCALL SDL_AtomicSetPtr(void **a, void* v);
slouken@5097
   259
slouken@5097
   260
/**
slouken@5097
   261
 * \brief Get the value of a pointer atomically.
slouken@5097
   262
 */
icculus@8088
   263
extern DECLSPEC void* SDLCALL SDL_AtomicGetPtr(void **a);
slouken@5097
   264
bob@3180
   265
/* Ends C function definitions when using C++ */
bob@3180
   266
#ifdef __cplusplus
bob@3180
   267
}
bob@3180
   268
#endif
bob@3180
   269
bob@3180
   270
#include "close_code.h"
bob@3180
   271
slouken@10638
   272
#endif /* SDL_atomic_h_ */
bob@3180
   273
bob@3180
   274
/* vi: set ts=4 sw=4 expandtab: */