include/SDL_atomic.h
author Sam Lantinga <slouken@libsdl.org>
Tue, 25 Jan 2011 17:44:17 -0800
changeset 5096 e4301cde4de1
parent 5095 dceec93471e7
child 5097 b938ad843e52
permissions -rw-r--r--
Fixed compile error on Visual C++
bob@3180
     1
/*
bob@3180
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 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@5095
    47
 * These operations may or may not actually be implemented using
slouken@5003
    48
 * processor specific atomic operations. When possible they are
slouken@5003
    49
 * implemented as true processor specific atomic operations. When that
slouken@5003
    50
 * is not possible the are implemented using locks that *do* use the
slouken@5003
    51
 * available atomic operations.
slouken@5003
    52
 *
slouken@5003
    53
 * All of the atomic operations that modify memory are full memory barriers.
bob@3180
    54
 */
bob@3180
    55
bob@3180
    56
#ifndef _SDL_atomic_h_
bob@3180
    57
#define _SDL_atomic_h_
bob@3180
    58
bob@3180
    59
#include "SDL_stdinc.h"
bob@3180
    60
#include "SDL_platform.h"
bob@3180
    61
bob@3180
    62
#include "begin_code.h"
bob@3180
    63
slouken@5073
    64
/* Need to do this here because intrin.h has C++ code in it */
slouken@5073
    65
/* Visual Studio 2005 has a bug where intrin.h conflicts with winnt.h */
slouken@5086
    66
#if defined(_MSC_VER) && (_MSC_VER >= 1500) && !defined(_WIN32_WCE)
slouken@5073
    67
#include <intrin.h>
slouken@5096
    68
#define HAVE_MSC_ATOMICS 1
slouken@5073
    69
#endif
slouken@5073
    70
bob@3180
    71
/* Set up for C function definitions, even when using C++ */
bob@3180
    72
#ifdef __cplusplus
bob@3180
    73
/* *INDENT-OFF* */
bob@3180
    74
extern "C" {
bob@3180
    75
/* *INDENT-ON* */
bob@3180
    76
#endif
bob@3180
    77
bob@3261
    78
/**
slouken@5003
    79
 * \name SDL AtomicLock
slouken@5003
    80
 * 
slouken@5003
    81
 * The atomic locks are efficient spinlocks using CPU instructions,
slouken@5003
    82
 * but are vulnerable to starvation and can spin forever if a thread
slouken@5003
    83
 * holding a lock has been terminated.  For this reason you should
slouken@5003
    84
 * minimize the code executed inside an atomic lock and never do
slouken@5003
    85
 * expensive things like API or system calls while holding them.
slouken@5003
    86
 *
slouken@5003
    87
 * The atomic locks are not safe to lock recursively.
slouken@5003
    88
 *
slouken@5003
    89
 * Porting Note:
slouken@5003
    90
 * The spin lock functions and type are required and can not be
slouken@5003
    91
 * emulated because they are used in the atomic emulation code.
bob@3261
    92
 */
slouken@3407
    93
/*@{*/
bob@3261
    94
slouken@5003
    95
typedef int SDL_SpinLock;
bob@3261
    96
bob@3261
    97
/**
slouken@5003
    98
 * \brief Try to lock a spin lock by setting it to a non-zero value.
slouken@5003
    99
 * 
slouken@5003
   100
 * \param lock Points to the lock.
slouken@5003
   101
 *
slouken@5003
   102
 * \return SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.
slouken@5003
   103
 */
slouken@5003
   104
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock);
slouken@5003
   105
slouken@5003
   106
/**
slouken@5003
   107
 * \brief Lock a spin lock by setting it to a non-zero value.
slouken@5003
   108
 * 
slouken@5003
   109
 * \param lock Points to the lock.
bob@3261
   110
 */
bob@3261
   111
extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
bob@3261
   112
bob@3261
   113
/**
slouken@5003
   114
 * \brief Unlock a spin lock by setting it to 0. Always returns immediately
bob@3261
   115
 *
slouken@5003
   116
 * \param lock Points to the lock.
bob@3261
   117
 */
bob@3261
   118
extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
bob@3261
   119
slouken@3407
   120
/*@}*//*SDL AtomicLock*/
bob@3202
   121
slouken@5095
   122
slouken@5095
   123
/* The compiler barrier prevents the compiler from reordering
slouken@5095
   124
   reads and writes to globally visible variables across the call.
slouken@5095
   125
*/
slouken@5095
   126
#ifdef _MSC_VER
slouken@5095
   127
void _ReadWriteBarrier(void);
slouken@5095
   128
#pragma intrinsic(_ReadWriteBarrier)
slouken@5095
   129
#define SDL_CompilerBarrier()   _ReadWriteBarrier()
slouken@5095
   130
#elif __GNUC__
slouken@5095
   131
#define SDL_CompilerBarrier()   __asm__ __volatile__ ("" : : : "memory")
slouken@5095
   132
#else
slouken@5095
   133
#define SDL_CompilerBarrier()   \
slouken@5095
   134
({ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); })
slouken@5095
   135
#endif
slouken@5095
   136
slouken@5006
   137
/* Platform specific optimized versions of the atomic functions,
slouken@5006
   138
 * you can disable these by defining SDL_DISABLE_ATOMIC_INLINE
slouken@5006
   139
 */
slouken@5006
   140
#ifndef SDL_DISABLE_ATOMIC_INLINE
slouken@5006
   141
slouken@5095
   142
#if HAVE_MSC_ATOMICS
slouken@5004
   143
slouken@5016
   144
#define SDL_AtomicSet(a, v)     _InterlockedExchange((long*)&(a)->value, (v))
slouken@5016
   145
#define SDL_AtomicAdd(a, v)     _InterlockedExchangeAdd((long*)&(a)->value, (v))
slouken@5016
   146
#define SDL_AtomicCAS(a, oldval, newval) (_InterlockedCompareExchange((long*)&(a)->value, (newval), (oldval)) == (oldval))
slouken@5095
   147
#define SDL_AtomicSetPtr(a, v)  _InterlockedExchangePointer((a), (v))
slouken@5012
   148
#if _M_IX86
slouken@5012
   149
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchange((long*)(a), (long)(newval), (long)(oldval)) == (long)(oldval))
slouken@5012
   150
#else
slouken@5012
   151
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchangePointer((a), (newval), (oldval)) == (oldval))
slouken@5012
   152
#endif
slouken@5004
   153
slouken@5095
   154
#elif __MACOSX__
slouken@5004
   155
#include <libkern/OSAtomic.h>
slouken@5004
   156
slouken@5095
   157
#define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((oldval), (newval), &(a)->value)
slouken@5004
   158
#if SIZEOF_VOIDP == 4
slouken@5004
   159
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a))
slouken@5004
   160
#elif SIZEOF_VOIDP == 8
slouken@5004
   161
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
slouken@5004
   162
#endif
slouken@5004
   163
slouken@5095
   164
#elif HAVE_GCC_ATOMICS
slouken@5004
   165
slouken@5004
   166
#define SDL_AtomicSet(a, v)     __sync_lock_test_and_set(&(a)->value, v)
slouken@5004
   167
#define SDL_AtomicAdd(a, v)     __sync_fetch_and_add(&(a)->value, v)
slouken@5095
   168
#define SDL_AtomicSetPtr(a, v)  __sync_lock_test_and_set(a, v)
slouken@5004
   169
#define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval)
slouken@5004
   170
#define SDL_AtomicCASPtr(a, oldval, newval) __sync_bool_compare_and_swap(a, oldval, newval)
slouken@5004
   171
slouken@5004
   172
#endif
slouken@3407
   173
slouken@5006
   174
#endif /* !SDL_DISABLE_ATOMIC_INLINE */
slouken@5006
   175
slouken@5006
   176
slouken@3407
   177
/**
slouken@5003
   178
 * \brief A type representing an atomic integer value.  It is a struct
slouken@5003
   179
 *        so people don't accidentally use numeric operations on it.
bob@3199
   180
 */
slouken@5003
   181
#ifndef SDL_atomic_t_defined
slouken@5003
   182
typedef struct { int value; } SDL_atomic_t;
slouken@5003
   183
#endif
bob@3237
   184
bob@3199
   185
/**
slouken@5003
   186
 * \brief Set an atomic variable to a value.
slouken@5003
   187
 *
slouken@5003
   188
 * \return The previous value of the atomic variable.
bob@3199
   189
 */
slouken@5003
   190
#ifndef SDL_AtomicSet
slouken@5095
   191
#define SDL_AtomicSet(a, v) \
slouken@5095
   192
({                              \
slouken@5095
   193
    int _value;                 \
slouken@5095
   194
    do {                        \
slouken@5095
   195
        _value = (a)->value;    \
slouken@5095
   196
    } while (!SDL_AtomicCAS(a, _value, (v))); \
slouken@5095
   197
    _value;                     \
slouken@5095
   198
})
slouken@5003
   199
#endif
bob@3237
   200
bob@3199
   201
/**
slouken@5003
   202
 * \brief Get the value of an atomic variable
bob@3199
   203
 */
slouken@5003
   204
#ifndef SDL_AtomicGet
slouken@5095
   205
#define SDL_AtomicGet(a)        \
slouken@5095
   206
({                              \
slouken@5095
   207
    int _value = (a)->value;    \
slouken@5095
   208
    SDL_CompilerBarrier();      \
slouken@5095
   209
    _value;                     \
slouken@5095
   210
})
slouken@5003
   211
#endif
bob@3237
   212
bob@3199
   213
/**
slouken@5095
   214
 * \brief Add to an atomic variable.
slouken@5003
   215
 *
slouken@5003
   216
 * \return The previous value of the atomic variable.
slouken@5095
   217
 *
slouken@5095
   218
 * \note This same style can be used for any number operation
bob@3199
   219
 */
slouken@5003
   220
#ifndef SDL_AtomicAdd
slouken@5095
   221
#define SDL_AtomicAdd(a, v)     \
slouken@5095
   222
({                              \
slouken@5095
   223
    int _value;                 \
slouken@5095
   224
    do {                        \
slouken@5095
   225
        _value = (a)->value;    \
slouken@5095
   226
    } while (!SDL_AtomicCAS(a, _value, (_value + (v)))); \
slouken@5095
   227
    _value;                     \
slouken@5095
   228
})
slouken@5003
   229
#endif
bob@3237
   230
bob@3199
   231
/**
slouken@5003
   232
 * \brief Increment an atomic variable used as a reference count.
bob@3199
   233
 */
slouken@5003
   234
#ifndef SDL_AtomicIncRef
slouken@5095
   235
#define SDL_AtomicIncRef(a)    SDL_AtomicAdd(a, 1)
slouken@5003
   236
#endif
bob@3237
   237
bob@3199
   238
/**
slouken@5003
   239
 * \brief Decrement an atomic variable used as a reference count.
slouken@5003
   240
 *
slouken@5095
   241
 * \return SDL_TRUE if the variable reached zero after decrementing,
slouken@5003
   242
 *         SDL_FALSE otherwise
bob@3199
   243
 */
slouken@5003
   244
#ifndef SDL_AtomicDecRef
slouken@5095
   245
#define SDL_AtomicDecRef(a)    (SDL_AtomicAdd(a, -1) == 1)
slouken@5003
   246
#endif
bob@3237
   247
bob@3199
   248
/**
slouken@5003
   249
 * \brief Set an atomic variable to a new value if it is currently an old value.
slouken@5003
   250
 *
slouken@5004
   251
 * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
slouken@5003
   252
 *
slouken@5003
   253
 * \note If you don't know what this function is for, you shouldn't use it!
slouken@5003
   254
*/
slouken@5003
   255
#ifndef SDL_AtomicCAS
slouken@5095
   256
#define SDL_AtomicCAS SDL_AtomicCAS_
slouken@5003
   257
#endif
slouken@5095
   258
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval);
bob@3237
   259
bob@3199
   260
/**
slouken@5003
   261
 * \brief Set a pointer to a value atomically.
slouken@5095
   262
 *
slouken@5095
   263
 * \return The previous value of the pointer.
bob@3199
   264
 */
slouken@5003
   265
#ifndef SDL_AtomicSetPtr
slouken@5095
   266
#define SDL_AtomicSetPtr(a, v)  \
slouken@5095
   267
({                              \
slouken@5095
   268
    void* _value;               \
slouken@5095
   269
    do {                        \
slouken@5095
   270
        _value = *(a);          \
slouken@5095
   271
    } while (!SDL_AtomicCASPtr(a, _value, (v))); \
slouken@5095
   272
    _value;                     \
slouken@5095
   273
})
slouken@5003
   274
#endif
bob@3237
   275
bob@3199
   276
/**
slouken@5003
   277
 * \brief Get the value of a pointer atomically.
bob@3199
   278
 */
slouken@5003
   279
#ifndef SDL_AtomicGetPtr
slouken@5095
   280
#define SDL_AtomicGetPtr(a)     \
slouken@5095
   281
({                              \
slouken@5095
   282
    void* _value = *(a);        \
slouken@5095
   283
    SDL_CompilerBarrier();      \
slouken@5095
   284
    _value;                     \
slouken@5095
   285
})
slouken@5003
   286
#endif
bob@3237
   287
bob@3199
   288
/**
slouken@5003
   289
 * \brief Set a pointer to a new value if it is currently an old value.
slouken@5003
   290
 *
slouken@5004
   291
 * \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
slouken@5003
   292
 *
slouken@5003
   293
 * \note If you don't know what this function is for, you shouldn't use it!
slouken@5003
   294
*/
slouken@5003
   295
#ifndef SDL_AtomicCASPtr
slouken@5095
   296
#define SDL_AtomicCASPtr SDL_AtomicCASPtr_
slouken@5003
   297
#endif
slouken@5095
   298
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr_(void **a, void *oldval, void *newval);
slouken@3407
   299
bob@3180
   300
/* Ends C function definitions when using C++ */
bob@3180
   301
#ifdef __cplusplus
bob@3180
   302
/* *INDENT-OFF* */
bob@3180
   303
}
bob@3180
   304
/* *INDENT-ON* */
bob@3180
   305
#endif
bob@3180
   306
bob@3180
   307
#include "close_code.h"
bob@3180
   308
bob@3180
   309
#endif /* _SDL_atomic_h_ */
bob@3180
   310
bob@3180
   311
/* vi: set ts=4 sw=4 expandtab: */