include/SDL_atomic.h
author Sam Lantinga <slouken@libsdl.org>
Mon, 17 Jan 2011 13:53:31 -0800
changeset 5012 2e282002bac3
parent 5006 8e8876e4aec6
child 5016 cc879bc6c4cf
permissions -rw-r--r--
Use compiler intrinsics, where available
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
bob@3180
    59
/* Set up for C function definitions, even when using C++ */
bob@3180
    60
#ifdef __cplusplus
bob@3180
    61
/* *INDENT-OFF* */
bob@3180
    62
extern "C" {
bob@3180
    63
/* *INDENT-ON* */
bob@3180
    64
#endif
bob@3180
    65
bob@3261
    66
/**
slouken@5003
    67
 * \name SDL AtomicLock
slouken@5003
    68
 * 
slouken@5003
    69
 * The atomic locks are efficient spinlocks using CPU instructions,
slouken@5003
    70
 * but are vulnerable to starvation and can spin forever if a thread
slouken@5003
    71
 * holding a lock has been terminated.  For this reason you should
slouken@5003
    72
 * minimize the code executed inside an atomic lock and never do
slouken@5003
    73
 * expensive things like API or system calls while holding them.
slouken@5003
    74
 *
slouken@5003
    75
 * The atomic locks are not safe to lock recursively.
slouken@5003
    76
 *
slouken@5003
    77
 * Porting Note:
slouken@5003
    78
 * The spin lock functions and type are required and can not be
slouken@5003
    79
 * emulated because they are used in the atomic emulation code.
bob@3261
    80
 */
slouken@3407
    81
/*@{*/
bob@3261
    82
slouken@5003
    83
typedef int SDL_SpinLock;
bob@3261
    84
bob@3261
    85
/**
slouken@5003
    86
 * \brief Try to lock a spin lock by setting it to a non-zero value.
slouken@5003
    87
 * 
slouken@5003
    88
 * \param lock Points to the lock.
slouken@5003
    89
 *
slouken@5003
    90
 * \return SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.
slouken@5003
    91
 */
slouken@5003
    92
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock);
slouken@5003
    93
slouken@5003
    94
/**
slouken@5003
    95
 * \brief Lock a spin lock by setting it to a non-zero value.
slouken@5003
    96
 * 
slouken@5003
    97
 * \param lock Points to the lock.
bob@3261
    98
 */
bob@3261
    99
extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
bob@3261
   100
bob@3261
   101
/**
slouken@5003
   102
 * \brief Unlock a spin lock by setting it to 0. Always returns immediately
bob@3261
   103
 *
slouken@5003
   104
 * \param lock Points to the lock.
bob@3261
   105
 */
bob@3261
   106
extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
bob@3261
   107
slouken@3407
   108
/*@}*//*SDL AtomicLock*/
bob@3202
   109
slouken@5006
   110
/* Platform specific optimized versions of the atomic functions,
slouken@5006
   111
 * you can disable these by defining SDL_DISABLE_ATOMIC_INLINE
slouken@5006
   112
 */
slouken@5006
   113
#ifndef SDL_DISABLE_ATOMIC_INLINE
slouken@5006
   114
slouken@5006
   115
#if defined(__WIN32__)
slouken@5012
   116
#include <intrin.h>
slouken@5004
   117
slouken@5012
   118
#define SDL_AtomicSet(a, v)     _InterlockedExchange(&(a)->value, (v))
slouken@5004
   119
#define SDL_AtomicGet(a)        ((a)->value)
slouken@5012
   120
#define SDL_AtomicAdd(a, v)     _InterlockedExchangeAdd(&(a)->value, (v))
slouken@5012
   121
#define SDL_AtomicCAS(a, oldval, newval) (_InterlockedCompareExchange(&(a)->value, (newval), (oldval)) == (oldval))
slouken@5012
   122
#define SDL_AtomicSetPtr(a, v)  (void)_InterlockedExchangePointer((a), (v))
slouken@5012
   123
#define SDL_AtomicGetPtr(a)     (*(a))
slouken@5012
   124
#if _M_IX86
slouken@5012
   125
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchange((long*)(a), (long)(newval), (long)(oldval)) == (long)(oldval))
slouken@5012
   126
#else
slouken@5012
   127
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchangePointer((a), (newval), (oldval)) == (oldval))
slouken@5012
   128
#endif
slouken@5004
   129
slouken@5004
   130
#elif defined(__MACOSX__)
slouken@5004
   131
#include <libkern/OSAtomic.h>
slouken@5004
   132
slouken@5004
   133
#define SDL_AtomicSet(a, v) \
slouken@5004
   134
({                          \
slouken@5004
   135
    int oldvalue;           \
slouken@5004
   136
                            \
slouken@5004
   137
    do {                    \
slouken@5004
   138
        oldvalue = (a)->value; \
slouken@5004
   139
    } while (!OSAtomicCompareAndSwap32Barrier(oldvalue, v, &(a)->value)); \
slouken@5004
   140
                            \
slouken@5004
   141
    oldvalue;               \
slouken@5004
   142
})
slouken@5004
   143
#define SDL_AtomicGet(a)        ((a)->value)
slouken@5004
   144
#define SDL_AtomicAdd(a, v) \
slouken@5004
   145
({                          \
slouken@5004
   146
    int oldvalue;           \
slouken@5004
   147
                            \
slouken@5004
   148
    do {                    \
slouken@5004
   149
        oldvalue = (a)->value; \
slouken@5004
   150
    } while (!OSAtomicCompareAndSwap32Barrier(oldvalue, oldvalue+v, &(a)->value)); \
slouken@5004
   151
                            \
slouken@5004
   152
    oldvalue;               \
slouken@5004
   153
})
slouken@5004
   154
#define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier(oldval, newval, &(a)->value)
slouken@5004
   155
#define SDL_AtomicSetPtr(a, v)  (*(a) = v, OSMemoryBarrier())
slouken@5004
   156
#define SDL_AtomicGetPtr(a)     (*(a))
slouken@5004
   157
#if SIZEOF_VOIDP == 4
slouken@5004
   158
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a))
slouken@5004
   159
#elif SIZEOF_VOIDP == 8
slouken@5004
   160
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
slouken@5004
   161
#endif
slouken@5004
   162
slouken@5004
   163
#elif defined(HAVE_GCC_ATOMICS)
slouken@5004
   164
slouken@5004
   165
#define SDL_AtomicSet(a, v)     __sync_lock_test_and_set(&(a)->value, v)
slouken@5004
   166
#define SDL_AtomicGet(a)        ((a)->value)
slouken@5004
   167
#define SDL_AtomicAdd(a, v)     __sync_fetch_and_add(&(a)->value, v)
slouken@5004
   168
#define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval)
slouken@5004
   169
#define SDL_AtomicSetPtr(a, v)  (*(a) = v, __sync_synchronize())
slouken@5004
   170
#define SDL_AtomicGetPtr(a)     (*(a))
slouken@5004
   171
#define SDL_AtomicCASPtr(a, oldval, newval) __sync_bool_compare_and_swap(a, oldval, newval)
slouken@5004
   172
slouken@5004
   173
#endif
slouken@3407
   174
slouken@5006
   175
#endif /* !SDL_DISABLE_ATOMIC_INLINE */
slouken@5006
   176
slouken@5006
   177
slouken@3407
   178
/**
slouken@5003
   179
 * \brief A type representing an atomic integer value.  It is a struct
slouken@5003
   180
 *        so people don't accidentally use numeric operations on it.
bob@3199
   181
 */
slouken@5003
   182
#ifndef SDL_atomic_t_defined
slouken@5003
   183
typedef struct { int value; } SDL_atomic_t;
slouken@5003
   184
#endif
bob@3237
   185
bob@3199
   186
/**
slouken@5003
   187
 * \brief Set an atomic variable to a value.
slouken@5003
   188
 *
slouken@5003
   189
 * \return The previous value of the atomic variable.
bob@3199
   190
 */
slouken@5003
   191
#ifndef SDL_AtomicSet
slouken@5003
   192
extern DECLSPEC int SDLCALL SDL_AtomicSet(SDL_atomic_t *a, int value);
slouken@5003
   193
#endif
bob@3237
   194
bob@3199
   195
/**
slouken@5003
   196
 * \brief Get the value of an atomic variable
bob@3199
   197
 */
slouken@5003
   198
#ifndef SDL_AtomicGet
slouken@5003
   199
extern DECLSPEC int SDLCALL SDL_AtomicGet(SDL_atomic_t *a);
slouken@5003
   200
#endif
bob@3237
   201
bob@3199
   202
/**
slouken@5003
   203
 * \brief  Add to an atomic variable.
slouken@5003
   204
 *
slouken@5003
   205
 * \return The previous value of the atomic variable.
bob@3199
   206
 */
slouken@5003
   207
#ifndef SDL_AtomicAdd
slouken@5003
   208
extern DECLSPEC int SDLCALL SDL_AtomicAdd(SDL_atomic_t *a, int value);
slouken@5003
   209
#endif
bob@3237
   210
bob@3199
   211
/**
slouken@5003
   212
 * \brief Increment an atomic variable used as a reference count.
bob@3199
   213
 */
slouken@5003
   214
#ifndef SDL_AtomicIncRef
slouken@5003
   215
extern DECLSPEC void SDLCALL SDL_AtomicIncRef(SDL_atomic_t *a);
slouken@5003
   216
#endif
bob@3237
   217
bob@3199
   218
/**
slouken@5003
   219
 * \brief Decrement an atomic variable used as a reference count.
slouken@5003
   220
 *
slouken@5003
   221
 * \return SDL_TRUE if the variable has reached zero after decrementing,
slouken@5003
   222
 *         SDL_FALSE otherwise
bob@3199
   223
 */
slouken@5003
   224
#ifndef SDL_AtomicDecRef
slouken@5003
   225
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicDecRef(SDL_atomic_t *a);
slouken@5003
   226
#endif
bob@3237
   227
bob@3199
   228
/**
slouken@5003
   229
 * \brief Set an atomic variable to a new value if it is currently an old value.
slouken@5003
   230
 *
slouken@5004
   231
 * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
slouken@5003
   232
 *
slouken@5003
   233
 * \note If you don't know what this function is for, you shouldn't use it!
slouken@5003
   234
*/
slouken@5003
   235
#ifndef SDL_AtomicCAS
slouken@5004
   236
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
slouken@5003
   237
#endif
bob@3237
   238
bob@3199
   239
/**
slouken@5003
   240
 * \brief Set a pointer to a value atomically.
bob@3199
   241
 */
slouken@5003
   242
#ifndef SDL_AtomicSetPtr
slouken@5003
   243
extern DECLSPEC void SDLCALL SDL_AtomicSetPtr(void** a, void* value);
slouken@5003
   244
#endif
bob@3237
   245
bob@3199
   246
/**
slouken@5003
   247
 * \brief Get the value of a pointer atomically.
bob@3199
   248
 */
slouken@5003
   249
#ifndef SDL_AtomicGetPtr
slouken@5003
   250
extern DECLSPEC void* SDLCALL SDL_AtomicGetPtr(void** a);
slouken@5003
   251
#endif
bob@3237
   252
bob@3199
   253
/**
slouken@5003
   254
 * \brief Set a pointer to a new value if it is currently an old value.
slouken@5003
   255
 *
slouken@5004
   256
 * \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
slouken@5003
   257
 *
slouken@5003
   258
 * \note If you don't know what this function is for, you shouldn't use it!
slouken@5003
   259
*/
slouken@5003
   260
#ifndef SDL_AtomicCASPtr
slouken@5004
   261
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval);
slouken@5003
   262
#endif
slouken@3407
   263
bob@3180
   264
/* Ends C function definitions when using C++ */
bob@3180
   265
#ifdef __cplusplus
bob@3180
   266
/* *INDENT-OFF* */
bob@3180
   267
}
bob@3180
   268
/* *INDENT-ON* */
bob@3180
   269
#endif
bob@3180
   270
bob@3180
   271
#include "close_code.h"
bob@3180
   272
bob@3180
   273
#endif /* _SDL_atomic_h_ */
bob@3180
   274
bob@3180
   275
/* vi: set ts=4 sw=4 expandtab: */