include/SDL_atomic.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Feb 2011 22:37:15 -0800
changeset 5262 b530ef003506
parent 5225 5d01d426f2ea
child 5535 96594ac5fd1a
permissions -rw-r--r--
Happy 2011! :)
bob@3180
     1
/*
bob@3180
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
bob@3180
     4
bob@3180
     5
    This library is free software; you can redistribute it and/or
bob@3180
     6
    modify it under the terms of the GNU Lesser General Public
bob@3180
     7
    License as published by the Free Software Foundation; either
bob@3180
     8
    version 2.1 of the License, or (at your option) any later version.
bob@3180
     9
bob@3180
    10
    This library is distributed in the hope that it will be useful,
bob@3180
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
bob@3180
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
bob@3180
    13
    Lesser General Public License for more details.
bob@3180
    14
bob@3180
    15
    You should have received a copy of the GNU Lesser General Public
bob@3180
    16
    License along with this library; if not, write to the Free Software
bob@3180
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
bob@3180
    18
bob@3180
    19
    Sam Lantinga
bob@3180
    20
    slouken@libsdl.org
bob@3180
    21
 */
bob@3180
    22
bob@3180
    23
/**
slouken@5003
    24
 * \file SDL_atomic.h
slouken@5003
    25
 * 
slouken@5003
    26
 * Atomic operations.
slouken@5003
    27
 * 
slouken@5003
    28
 * IMPORTANT:
slouken@5003
    29
 * If you are not an expert in concurrent lockless programming, you should
slouken@5003
    30
 * only be using the atomic lock and reference counting functions in this
slouken@5003
    31
 * file.  In all other cases you should be protecting your data structures
slouken@5003
    32
 * with full mutexes.
slouken@5003
    33
 * 
slouken@5003
    34
 * The list of "safe" functions to use are:
slouken@5003
    35
 *  SDL_AtomicLock()
slouken@5003
    36
 *  SDL_AtomicUnlock()
slouken@5003
    37
 *  SDL_AtomicIncRef()
slouken@5003
    38
 *  SDL_AtomicDecRef()
slouken@5003
    39
 * 
slouken@5003
    40
 * Seriously, here be dragons!
slouken@5095
    41
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
slouken@5003
    42
 *
slouken@5095
    43
 * You can find out a little more about lockless programming and the 
slouken@5095
    44
 * subtle issues that can arise here:
slouken@5095
    45
 * http://msdn.microsoft.com/en-us/library/ee418650%28v=vs.85%29.aspx
slouken@5095
    46
 *
slouken@5098
    47
 * There's also lots of good information here:
slouken@5098
    48
 * http://www.1024cores.net/home/lock-free-algorithms
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 */
slouken@5086
    69
#if defined(_MSC_VER) && (_MSC_VER >= 1500) && !defined(_WIN32_WCE)
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
/* *INDENT-OFF* */
bob@3180
    77
extern "C" {
bob@3180
    78
/* *INDENT-ON* */
bob@3180
    79
#endif
bob@3180
    80
bob@3261
    81
/**
slouken@5003
    82
 * \name SDL AtomicLock
slouken@5003
    83
 * 
slouken@5003
    84
 * The atomic locks are efficient spinlocks using CPU instructions,
slouken@5003
    85
 * but are vulnerable to starvation and can spin forever if a thread
slouken@5003
    86
 * holding a lock has been terminated.  For this reason you should
slouken@5003
    87
 * minimize the code executed inside an atomic lock and never do
slouken@5003
    88
 * expensive things like API or system calls while holding them.
slouken@5003
    89
 *
slouken@5003
    90
 * The atomic locks are not safe to lock recursively.
slouken@5003
    91
 *
slouken@5003
    92
 * Porting Note:
slouken@5003
    93
 * The spin lock functions and type are required and can not be
slouken@5003
    94
 * emulated because they are used in the atomic emulation code.
bob@3261
    95
 */
slouken@3407
    96
/*@{*/
bob@3261
    97
slouken@5003
    98
typedef int SDL_SpinLock;
bob@3261
    99
bob@3261
   100
/**
slouken@5003
   101
 * \brief Try to lock a spin lock by setting it to a non-zero value.
slouken@5003
   102
 * 
slouken@5003
   103
 * \param lock Points to the lock.
slouken@5003
   104
 *
slouken@5003
   105
 * \return SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.
slouken@5003
   106
 */
slouken@5003
   107
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock);
slouken@5003
   108
slouken@5003
   109
/**
slouken@5003
   110
 * \brief Lock a spin lock by setting it to a non-zero value.
slouken@5003
   111
 * 
slouken@5003
   112
 * \param lock Points to the lock.
bob@3261
   113
 */
bob@3261
   114
extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
bob@3261
   115
bob@3261
   116
/**
slouken@5003
   117
 * \brief Unlock a spin lock by setting it to 0. Always returns immediately
bob@3261
   118
 *
slouken@5003
   119
 * \param lock Points to the lock.
bob@3261
   120
 */
bob@3261
   121
extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
bob@3261
   122
slouken@3407
   123
/*@}*//*SDL AtomicLock*/
bob@3202
   124
slouken@5095
   125
slouken@5135
   126
/**
slouken@5135
   127
 * The compiler barrier prevents the compiler from reordering
slouken@5135
   128
 * reads and writes to globally visible variables across the call.
slouken@5135
   129
 */
slouken@5095
   130
#ifdef _MSC_VER
slouken@5095
   131
void _ReadWriteBarrier(void);
slouken@5095
   132
#pragma intrinsic(_ReadWriteBarrier)
slouken@5095
   133
#define SDL_CompilerBarrier()   _ReadWriteBarrier()
slouken@5097
   134
#elif defined(__GNUC__)
slouken@5095
   135
#define SDL_CompilerBarrier()   __asm__ __volatile__ ("" : : : "memory")
slouken@5095
   136
#else
slouken@5095
   137
#define SDL_CompilerBarrier()   \
slouken@5095
   138
({ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); })
slouken@5095
   139
#endif
slouken@5095
   140
slouken@5006
   141
/* Platform specific optimized versions of the atomic functions,
slouken@5006
   142
 * you can disable these by defining SDL_DISABLE_ATOMIC_INLINE
slouken@5006
   143
 */
slouken@5225
   144
#if SDL_ATOMIC_DISABLED
slouken@5225
   145
#define SDL_DISABLE_ATOMIC_INLINE
slouken@5225
   146
#endif
slouken@5006
   147
#ifndef SDL_DISABLE_ATOMIC_INLINE
slouken@5006
   148
slouken@5097
   149
#ifdef HAVE_MSC_ATOMICS
slouken@5004
   150
slouken@5016
   151
#define SDL_AtomicSet(a, v)     _InterlockedExchange((long*)&(a)->value, (v))
slouken@5016
   152
#define SDL_AtomicAdd(a, v)     _InterlockedExchangeAdd((long*)&(a)->value, (v))
slouken@5016
   153
#define SDL_AtomicCAS(a, oldval, newval) (_InterlockedCompareExchange((long*)&(a)->value, (newval), (oldval)) == (oldval))
slouken@5095
   154
#define SDL_AtomicSetPtr(a, v)  _InterlockedExchangePointer((a), (v))
slouken@5012
   155
#if _M_IX86
slouken@5012
   156
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchange((long*)(a), (long)(newval), (long)(oldval)) == (long)(oldval))
slouken@5012
   157
#else
slouken@5012
   158
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchangePointer((a), (newval), (oldval)) == (oldval))
slouken@5012
   159
#endif
slouken@5004
   160
slouken@5097
   161
#elif defined(__MACOSX__)
slouken@5004
   162
#include <libkern/OSAtomic.h>
slouken@5004
   163
slouken@5095
   164
#define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((oldval), (newval), &(a)->value)
slouken@5004
   165
#if SIZEOF_VOIDP == 4
slouken@5004
   166
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a))
slouken@5004
   167
#elif SIZEOF_VOIDP == 8
slouken@5004
   168
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
slouken@5004
   169
#endif
slouken@5004
   170
slouken@5097
   171
#elif defined(HAVE_GCC_ATOMICS)
slouken@5004
   172
slouken@5004
   173
#define SDL_AtomicSet(a, v)     __sync_lock_test_and_set(&(a)->value, v)
slouken@5004
   174
#define SDL_AtomicAdd(a, v)     __sync_fetch_and_add(&(a)->value, v)
slouken@5095
   175
#define SDL_AtomicSetPtr(a, v)  __sync_lock_test_and_set(a, v)
slouken@5004
   176
#define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval)
slouken@5004
   177
#define SDL_AtomicCASPtr(a, oldval, newval) __sync_bool_compare_and_swap(a, oldval, newval)
slouken@5004
   178
slouken@5004
   179
#endif
slouken@3407
   180
slouken@5006
   181
#endif /* !SDL_DISABLE_ATOMIC_INLINE */
slouken@5006
   182
slouken@5006
   183
slouken@3407
   184
/**
slouken@5003
   185
 * \brief A type representing an atomic integer value.  It is a struct
slouken@5003
   186
 *        so people don't accidentally use numeric operations on it.
bob@3199
   187
 */
slouken@5003
   188
#ifndef SDL_atomic_t_defined
slouken@5003
   189
typedef struct { int value; } SDL_atomic_t;
slouken@5003
   190
#endif
bob@3237
   191
bob@3199
   192
/**
slouken@5097
   193
 * \brief Set an atomic variable to a new value if it is currently an old value.
slouken@5097
   194
 *
slouken@5097
   195
 * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
slouken@5097
   196
 *
slouken@5097
   197
 * \note If you don't know what this function is for, you shouldn't use it!
slouken@5097
   198
*/
slouken@5097
   199
#ifndef SDL_AtomicCAS
slouken@5097
   200
#define SDL_AtomicCAS SDL_AtomicCAS_
slouken@5097
   201
#endif
slouken@5097
   202
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval);
slouken@5097
   203
slouken@5097
   204
/**
slouken@5003
   205
 * \brief Set an atomic variable to a value.
slouken@5003
   206
 *
slouken@5003
   207
 * \return The previous value of the atomic variable.
bob@3199
   208
 */
slouken@5003
   209
#ifndef SDL_AtomicSet
slouken@5097
   210
static __inline__ int SDL_AtomicSet(SDL_atomic_t *a, int v)
slouken@5097
   211
{
slouken@5097
   212
    int value;
slouken@5097
   213
    do {
slouken@5097
   214
        value = a->value;
slouken@5097
   215
    } while (!SDL_AtomicCAS(a, value, v));
slouken@5097
   216
    return value;
slouken@5097
   217
}
slouken@5003
   218
#endif
bob@3237
   219
bob@3199
   220
/**
slouken@5003
   221
 * \brief Get the value of an atomic variable
bob@3199
   222
 */
slouken@5003
   223
#ifndef SDL_AtomicGet
slouken@5097
   224
static __inline__ int SDL_AtomicGet(SDL_atomic_t *a)
slouken@5097
   225
{
slouken@5097
   226
    int value = a->value;
slouken@5097
   227
    SDL_CompilerBarrier();
slouken@5097
   228
    return value;
slouken@5097
   229
}
slouken@5003
   230
#endif
bob@3237
   231
bob@3199
   232
/**
slouken@5095
   233
 * \brief Add to an atomic variable.
slouken@5003
   234
 *
slouken@5003
   235
 * \return The previous value of the atomic variable.
slouken@5095
   236
 *
slouken@5095
   237
 * \note This same style can be used for any number operation
bob@3199
   238
 */
slouken@5003
   239
#ifndef SDL_AtomicAdd
slouken@5097
   240
static __inline__ int SDL_AtomicAdd(SDL_atomic_t *a, int v)
slouken@5097
   241
{
slouken@5097
   242
    int value;
slouken@5097
   243
    do {
slouken@5097
   244
        value = a->value;
slouken@5097
   245
    } while (!SDL_AtomicCAS(a, value, (value + v)));
slouken@5097
   246
    return value;
slouken@5097
   247
}
slouken@5003
   248
#endif
bob@3237
   249
bob@3199
   250
/**
slouken@5003
   251
 * \brief Increment an atomic variable used as a reference count.
bob@3199
   252
 */
slouken@5003
   253
#ifndef SDL_AtomicIncRef
slouken@5095
   254
#define SDL_AtomicIncRef(a)    SDL_AtomicAdd(a, 1)
slouken@5003
   255
#endif
bob@3237
   256
bob@3199
   257
/**
slouken@5003
   258
 * \brief Decrement an atomic variable used as a reference count.
slouken@5003
   259
 *
slouken@5095
   260
 * \return SDL_TRUE if the variable reached zero after decrementing,
slouken@5003
   261
 *         SDL_FALSE otherwise
bob@3199
   262
 */
slouken@5003
   263
#ifndef SDL_AtomicDecRef
slouken@5095
   264
#define SDL_AtomicDecRef(a)    (SDL_AtomicAdd(a, -1) == 1)
slouken@5003
   265
#endif
bob@3237
   266
bob@3199
   267
/**
slouken@5003
   268
 * \brief Set a pointer to a new value if it is currently an old value.
slouken@5003
   269
 *
slouken@5004
   270
 * \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
slouken@5003
   271
 *
slouken@5003
   272
 * \note If you don't know what this function is for, you shouldn't use it!
slouken@5003
   273
*/
slouken@5003
   274
#ifndef SDL_AtomicCASPtr
slouken@5095
   275
#define SDL_AtomicCASPtr SDL_AtomicCASPtr_
slouken@5003
   276
#endif
slouken@5136
   277
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr_(void* *a, void *oldval, void *newval);
slouken@3407
   278
slouken@5097
   279
/**
slouken@5097
   280
 * \brief Set a pointer to a value atomically.
slouken@5097
   281
 *
slouken@5097
   282
 * \return The previous value of the pointer.
slouken@5097
   283
 */
slouken@5097
   284
#ifndef SDL_AtomicSetPtr
slouken@5097
   285
static __inline__ void* SDL_AtomicSetPtr(void* *a, void* v)
slouken@5097
   286
{
slouken@5097
   287
    void* value;
slouken@5097
   288
    do {
slouken@5097
   289
        value = *a;
slouken@5097
   290
    } while (!SDL_AtomicCASPtr(a, value, v));
slouken@5097
   291
    return value;
slouken@5097
   292
}
slouken@5097
   293
#endif
slouken@5097
   294
slouken@5097
   295
/**
slouken@5097
   296
 * \brief Get the value of a pointer atomically.
slouken@5097
   297
 */
slouken@5097
   298
#ifndef SDL_AtomicGetPtr
slouken@5097
   299
static __inline__ void* SDL_AtomicGetPtr(void* *a)
slouken@5097
   300
{
slouken@5097
   301
    void* value = *a;
slouken@5097
   302
    SDL_CompilerBarrier();
slouken@5097
   303
    return value;
slouken@5097
   304
}
slouken@5097
   305
#endif
slouken@5097
   306
slouken@5097
   307
bob@3180
   308
/* Ends C function definitions when using C++ */
bob@3180
   309
#ifdef __cplusplus
bob@3180
   310
/* *INDENT-OFF* */
bob@3180
   311
}
bob@3180
   312
/* *INDENT-ON* */
bob@3180
   313
#endif
bob@3180
   314
bob@3180
   315
#include "close_code.h"
bob@3180
   316
bob@3180
   317
#endif /* _SDL_atomic_h_ */
bob@3180
   318
bob@3180
   319
/* vi: set ts=4 sw=4 expandtab: */