include/SDL_atomic.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 21 Jan 2011 23:46:51 -0800
changeset 5073 1e94e68525d5
parent 5024 38638d228a22
child 5086 c2539ff054c8
permissions -rw-r--r--
Fixed bug #1097

There are problems with including intrin.h in C linkage in C++ compilation,
and further conflicts between intrin.h and winnt.h on Visual Studio 2005.
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@5003
    41
 *
slouken@5003
    42
 * These operations may, or may not, actually be implemented using
slouken@5003
    43
 * processor specific atomic operations. When possible they are
slouken@5003
    44
 * implemented as true processor specific atomic operations. When that
slouken@5003
    45
 * is not possible the are implemented using locks that *do* use the
slouken@5003
    46
 * available atomic operations.
slouken@5003
    47
 *
slouken@5003
    48
 * All of the atomic operations that modify memory are full memory barriers.
bob@3180
    49
 */
bob@3180
    50
bob@3180
    51
#ifndef _SDL_atomic_h_
bob@3180
    52
#define _SDL_atomic_h_
bob@3180
    53
bob@3180
    54
#include "SDL_stdinc.h"
bob@3180
    55
#include "SDL_platform.h"
bob@3180
    56
bob@3180
    57
#include "begin_code.h"
bob@3180
    58
slouken@5073
    59
/* Need to do this here because intrin.h has C++ code in it */
slouken@5073
    60
/* Visual Studio 2005 has a bug where intrin.h conflicts with winnt.h */
slouken@5073
    61
#if defined(_MSC_VER) && (_MSC_VER >= 1500)
slouken@5073
    62
#include <intrin.h>
slouken@5073
    63
#define HAVE_MSC_ATOMICS
slouken@5073
    64
#endif
slouken@5073
    65
bob@3180
    66
/* Set up for C function definitions, even when using C++ */
bob@3180
    67
#ifdef __cplusplus
bob@3180
    68
/* *INDENT-OFF* */
bob@3180
    69
extern "C" {
bob@3180
    70
/* *INDENT-ON* */
bob@3180
    71
#endif
bob@3180
    72
bob@3261
    73
/**
slouken@5003
    74
 * \name SDL AtomicLock
slouken@5003
    75
 * 
slouken@5003
    76
 * The atomic locks are efficient spinlocks using CPU instructions,
slouken@5003
    77
 * but are vulnerable to starvation and can spin forever if a thread
slouken@5003
    78
 * holding a lock has been terminated.  For this reason you should
slouken@5003
    79
 * minimize the code executed inside an atomic lock and never do
slouken@5003
    80
 * expensive things like API or system calls while holding them.
slouken@5003
    81
 *
slouken@5003
    82
 * The atomic locks are not safe to lock recursively.
slouken@5003
    83
 *
slouken@5003
    84
 * Porting Note:
slouken@5003
    85
 * The spin lock functions and type are required and can not be
slouken@5003
    86
 * emulated because they are used in the atomic emulation code.
bob@3261
    87
 */
slouken@3407
    88
/*@{*/
bob@3261
    89
slouken@5003
    90
typedef int SDL_SpinLock;
bob@3261
    91
bob@3261
    92
/**
slouken@5003
    93
 * \brief Try to lock a spin lock by setting it to a non-zero value.
slouken@5003
    94
 * 
slouken@5003
    95
 * \param lock Points to the lock.
slouken@5003
    96
 *
slouken@5003
    97
 * \return SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.
slouken@5003
    98
 */
slouken@5003
    99
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock);
slouken@5003
   100
slouken@5003
   101
/**
slouken@5003
   102
 * \brief Lock a spin lock by setting it to a non-zero value.
slouken@5003
   103
 * 
slouken@5003
   104
 * \param lock Points to the lock.
bob@3261
   105
 */
bob@3261
   106
extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
bob@3261
   107
bob@3261
   108
/**
slouken@5003
   109
 * \brief Unlock a spin lock by setting it to 0. Always returns immediately
bob@3261
   110
 *
slouken@5003
   111
 * \param lock Points to the lock.
bob@3261
   112
 */
bob@3261
   113
extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
bob@3261
   114
slouken@3407
   115
/*@}*//*SDL AtomicLock*/
bob@3202
   116
slouken@5006
   117
/* Platform specific optimized versions of the atomic functions,
slouken@5006
   118
 * you can disable these by defining SDL_DISABLE_ATOMIC_INLINE
slouken@5006
   119
 */
slouken@5006
   120
#ifndef SDL_DISABLE_ATOMIC_INLINE
slouken@5006
   121
slouken@5073
   122
#if defined(HAVE_MSC_ATOMICS)
slouken@5004
   123
slouken@5016
   124
#define SDL_AtomicSet(a, v)     _InterlockedExchange((long*)&(a)->value, (v))
slouken@5004
   125
#define SDL_AtomicGet(a)        ((a)->value)
slouken@5016
   126
#define SDL_AtomicAdd(a, v)     _InterlockedExchangeAdd((long*)&(a)->value, (v))
slouken@5016
   127
#define SDL_AtomicCAS(a, oldval, newval) (_InterlockedCompareExchange((long*)&(a)->value, (newval), (oldval)) == (oldval))
slouken@5012
   128
#define SDL_AtomicSetPtr(a, v)  (void)_InterlockedExchangePointer((a), (v))
slouken@5016
   129
#define SDL_AtomicGetPtr(a)     (*(a))
slouken@5012
   130
#if _M_IX86
slouken@5012
   131
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchange((long*)(a), (long)(newval), (long)(oldval)) == (long)(oldval))
slouken@5012
   132
#else
slouken@5012
   133
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchangePointer((a), (newval), (oldval)) == (oldval))
slouken@5012
   134
#endif
slouken@5004
   135
slouken@5004
   136
#elif defined(__MACOSX__)
slouken@5004
   137
#include <libkern/OSAtomic.h>
slouken@5004
   138
slouken@5004
   139
#define SDL_AtomicSet(a, v) \
slouken@5004
   140
({                          \
slouken@5004
   141
    int oldvalue;           \
slouken@5004
   142
                            \
slouken@5004
   143
    do {                    \
slouken@5004
   144
        oldvalue = (a)->value; \
slouken@5004
   145
    } while (!OSAtomicCompareAndSwap32Barrier(oldvalue, v, &(a)->value)); \
slouken@5004
   146
                            \
slouken@5004
   147
    oldvalue;               \
slouken@5004
   148
})
slouken@5004
   149
#define SDL_AtomicGet(a)        ((a)->value)
slouken@5004
   150
#define SDL_AtomicAdd(a, v) \
slouken@5004
   151
({                          \
slouken@5004
   152
    int oldvalue;           \
slouken@5004
   153
                            \
slouken@5004
   154
    do {                    \
slouken@5004
   155
        oldvalue = (a)->value; \
slouken@5004
   156
    } while (!OSAtomicCompareAndSwap32Barrier(oldvalue, oldvalue+v, &(a)->value)); \
slouken@5004
   157
                            \
slouken@5004
   158
    oldvalue;               \
slouken@5004
   159
})
slouken@5004
   160
#define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier(oldval, newval, &(a)->value)
slouken@5004
   161
#define SDL_AtomicSetPtr(a, v)  (*(a) = v, OSMemoryBarrier())
slouken@5004
   162
#define SDL_AtomicGetPtr(a)     (*(a))
slouken@5004
   163
#if SIZEOF_VOIDP == 4
slouken@5004
   164
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a))
slouken@5004
   165
#elif SIZEOF_VOIDP == 8
slouken@5004
   166
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
slouken@5004
   167
#endif
slouken@5004
   168
slouken@5004
   169
#elif defined(HAVE_GCC_ATOMICS)
slouken@5004
   170
slouken@5004
   171
#define SDL_AtomicSet(a, v)     __sync_lock_test_and_set(&(a)->value, v)
slouken@5004
   172
#define SDL_AtomicGet(a)        ((a)->value)
slouken@5004
   173
#define SDL_AtomicAdd(a, v)     __sync_fetch_and_add(&(a)->value, v)
slouken@5004
   174
#define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval)
slouken@5004
   175
#define SDL_AtomicSetPtr(a, v)  (*(a) = v, __sync_synchronize())
slouken@5004
   176
#define SDL_AtomicGetPtr(a)     (*(a))
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@5003
   193
 * \brief Set an atomic variable to a value.
slouken@5003
   194
 *
slouken@5003
   195
 * \return The previous value of the atomic variable.
bob@3199
   196
 */
slouken@5003
   197
#ifndef SDL_AtomicSet
slouken@5003
   198
extern DECLSPEC int SDLCALL SDL_AtomicSet(SDL_atomic_t *a, int value);
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@5003
   205
extern DECLSPEC int SDLCALL SDL_AtomicGet(SDL_atomic_t *a);
slouken@5003
   206
#endif
bob@3237
   207
bob@3199
   208
/**
slouken@5003
   209
 * \brief  Add to an atomic variable.
slouken@5003
   210
 *
slouken@5003
   211
 * \return The previous value of the atomic variable.
bob@3199
   212
 */
slouken@5003
   213
#ifndef SDL_AtomicAdd
slouken@5003
   214
extern DECLSPEC int SDLCALL SDL_AtomicAdd(SDL_atomic_t *a, int value);
slouken@5003
   215
#endif
bob@3237
   216
bob@3199
   217
/**
slouken@5003
   218
 * \brief Increment an atomic variable used as a reference count.
bob@3199
   219
 */
slouken@5003
   220
#ifndef SDL_AtomicIncRef
slouken@5003
   221
extern DECLSPEC void SDLCALL SDL_AtomicIncRef(SDL_atomic_t *a);
slouken@5003
   222
#endif
bob@3237
   223
bob@3199
   224
/**
slouken@5003
   225
 * \brief Decrement an atomic variable used as a reference count.
slouken@5003
   226
 *
slouken@5003
   227
 * \return SDL_TRUE if the variable has reached zero after decrementing,
slouken@5003
   228
 *         SDL_FALSE otherwise
bob@3199
   229
 */
slouken@5003
   230
#ifndef SDL_AtomicDecRef
slouken@5003
   231
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicDecRef(SDL_atomic_t *a);
slouken@5003
   232
#endif
bob@3237
   233
bob@3199
   234
/**
slouken@5003
   235
 * \brief Set an atomic variable to a new value if it is currently an old value.
slouken@5003
   236
 *
slouken@5004
   237
 * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
slouken@5003
   238
 *
slouken@5003
   239
 * \note If you don't know what this function is for, you shouldn't use it!
slouken@5003
   240
*/
slouken@5003
   241
#ifndef SDL_AtomicCAS
slouken@5004
   242
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
slouken@5003
   243
#endif
bob@3237
   244
bob@3199
   245
/**
slouken@5003
   246
 * \brief Set a pointer to a value atomically.
bob@3199
   247
 */
slouken@5003
   248
#ifndef SDL_AtomicSetPtr
slouken@5003
   249
extern DECLSPEC void SDLCALL SDL_AtomicSetPtr(void** a, void* value);
slouken@5003
   250
#endif
bob@3237
   251
bob@3199
   252
/**
slouken@5003
   253
 * \brief Get the value of a pointer atomically.
bob@3199
   254
 */
slouken@5003
   255
#ifndef SDL_AtomicGetPtr
slouken@5003
   256
extern DECLSPEC void* SDLCALL SDL_AtomicGetPtr(void** a);
slouken@5003
   257
#endif
bob@3237
   258
bob@3199
   259
/**
slouken@5003
   260
 * \brief Set a pointer to a new value if it is currently an old value.
slouken@5003
   261
 *
slouken@5004
   262
 * \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
slouken@5003
   263
 *
slouken@5003
   264
 * \note If you don't know what this function is for, you shouldn't use it!
slouken@5003
   265
*/
slouken@5003
   266
#ifndef SDL_AtomicCASPtr
slouken@5004
   267
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval);
slouken@5003
   268
#endif
slouken@3407
   269
bob@3180
   270
/* Ends C function definitions when using C++ */
bob@3180
   271
#ifdef __cplusplus
bob@3180
   272
/* *INDENT-OFF* */
bob@3180
   273
}
bob@3180
   274
/* *INDENT-ON* */
bob@3180
   275
#endif
bob@3180
   276
bob@3180
   277
#include "close_code.h"
bob@3180
   278
bob@3180
   279
#endif /* _SDL_atomic_h_ */
bob@3180
   280
bob@3180
   281
/* vi: set ts=4 sw=4 expandtab: */