2
Simple DirectMedia Layer
3
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
5
This software is provided 'as-is', without any express or implied
6
warranty. In no event will the authors be held liable for any damages
7
arising from the use of this software.
9
Permission is granted to anyone to use this software for any purpose,
10
including commercial applications, and to alter it and redistribute it
11
freely, subject to the following restrictions:
13
1. The origin of this software must not be misrepresented; you must not
14
claim that you wrote the original software. If you use this software
15
in a product, an acknowledgment in the product documentation would be
16
appreciated but is not required.
17
2. Altered source versions must be plainly marked as such, and must not be
18
misrepresented as being the original software.
19
3. This notice may not be removed or altered from any source distribution.
28
* If you are not an expert in concurrent lockless programming, you should
29
* only be using the atomic lock and reference counting functions in this
30
* file. In all other cases you should be protecting your data structures
33
* The list of "safe" functions to use are:
39
* Seriously, here be dragons!
40
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^
42
* You can find out a little more about lockless programming and the
43
* subtle issues that can arise here:
44
* http://msdn.microsoft.com/en-us/library/ee418650%28v=vs.85%29.aspx
46
* There's also lots of good information here:
47
* http://www.1024cores.net/home/lock-free-algorithms
49
* These operations may or may not actually be implemented using
50
* processor specific atomic operations. When possible they are
51
* implemented as true processor specific atomic operations. When that
52
* is not possible the are implemented using locks that *do* use the
53
* available atomic operations.
55
* All of the atomic operations that modify memory are full memory barriers.
58
#ifndef _SDL_atomic_h_
59
#define _SDL_atomic_h_
61
#include "SDL_stdinc.h"
62
#include "SDL_platform.h"
64
#include "begin_code.h"
66
/* Need to do this here because intrin.h has C++ code in it */
67
/* Visual Studio 2005 has a bug where intrin.h conflicts with winnt.h */
68
#if defined(_MSC_VER) && (_MSC_VER >= 1500) && !defined(_WIN32_WCE)
70
#define HAVE_MSC_ATOMICS 1
73
/* Set up for C function definitions, even when using C++ */
81
* \name SDL AtomicLock
83
* The atomic locks are efficient spinlocks using CPU instructions,
84
* but are vulnerable to starvation and can spin forever if a thread
85
* holding a lock has been terminated. For this reason you should
86
* minimize the code executed inside an atomic lock and never do
87
* expensive things like API or system calls while holding them.
89
* The atomic locks are not safe to lock recursively.
92
* The spin lock functions and type are required and can not be
93
* emulated because they are used in the atomic emulation code.
97
typedef int SDL_SpinLock;
100
* \brief Try to lock a spin lock by setting it to a non-zero value.
102
* \param lock Points to the lock.
104
* \return SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.
106
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock);
109
* \brief Lock a spin lock by setting it to a non-zero value.
111
* \param lock Points to the lock.
113
extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
116
* \brief Unlock a spin lock by setting it to 0. Always returns immediately
118
* \param lock Points to the lock.
120
extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
122
/*@}*//*SDL AtomicLock*/
126
* The compiler barrier prevents the compiler from reordering
127
* reads and writes to globally visible variables across the call.
130
void _ReadWriteBarrier(void);
131
#pragma intrinsic(_ReadWriteBarrier)
132
#define SDL_CompilerBarrier() _ReadWriteBarrier()
133
#elif defined(__GNUC__)
134
#define SDL_CompilerBarrier() __asm__ __volatile__ ("" : : : "memory")
136
#define SDL_CompilerBarrier() \
137
({ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); })
140
/* Platform specific optimized versions of the atomic functions,
141
* you can disable these by defining SDL_DISABLE_ATOMIC_INLINE
143
#if defined(SDL_ATOMIC_DISABLED) && SDL_ATOMIC_DISABLED
144
#define SDL_DISABLE_ATOMIC_INLINE
146
#ifndef SDL_DISABLE_ATOMIC_INLINE
148
#ifdef HAVE_MSC_ATOMICS
150
#define SDL_AtomicSet(a, v) _InterlockedExchange((long*)&(a)->value, (v))
151
#define SDL_AtomicAdd(a, v) _InterlockedExchangeAdd((long*)&(a)->value, (v))
152
#define SDL_AtomicCAS(a, oldval, newval) (_InterlockedCompareExchange((long*)&(a)->value, (newval), (oldval)) == (oldval))
153
#define SDL_AtomicSetPtr(a, v) _InterlockedExchangePointer((a), (v))
155
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchange((long*)(a), (long)(newval), (long)(oldval)) == (long)(oldval))
157
#define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchangePointer((a), (newval), (oldval)) == (oldval))
160
#elif defined(__MACOSX__)
161
#include <libkern/OSAtomic.h>
163
#define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((oldval), (newval), &(a)->value)
164
#if SIZEOF_VOIDP == 4
165
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a))
166
#elif SIZEOF_VOIDP == 8
167
#define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
170
#elif defined(HAVE_GCC_ATOMICS)
172
#define SDL_AtomicSet(a, v) __sync_lock_test_and_set(&(a)->value, v)
173
#define SDL_AtomicAdd(a, v) __sync_fetch_and_add(&(a)->value, v)
174
#define SDL_AtomicSetPtr(a, v) __sync_lock_test_and_set(a, v)
175
#define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval)
176
#define SDL_AtomicCASPtr(a, oldval, newval) __sync_bool_compare_and_swap(a, oldval, newval)
180
#endif /* !SDL_DISABLE_ATOMIC_INLINE */
184
* \brief A type representing an atomic integer value. It is a struct
185
* so people don't accidentally use numeric operations on it.
187
#ifndef SDL_atomic_t_defined
188
typedef struct { int value; } SDL_atomic_t;
192
* \brief Set an atomic variable to a new value if it is currently an old value.
194
* \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
196
* \note If you don't know what this function is for, you shouldn't use it!
198
#ifndef SDL_AtomicCAS
199
#define SDL_AtomicCAS SDL_AtomicCAS_
201
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval);
204
* \brief Set an atomic variable to a value.
206
* \return The previous value of the atomic variable.
208
#ifndef SDL_AtomicSet
209
static __inline__ int SDL_AtomicSet(SDL_atomic_t *a, int v)
214
} while (!SDL_AtomicCAS(a, value, v));
220
* \brief Get the value of an atomic variable
222
#ifndef SDL_AtomicGet
223
static __inline__ int SDL_AtomicGet(SDL_atomic_t *a)
225
int value = a->value;
226
SDL_CompilerBarrier();
232
* \brief Add to an atomic variable.
234
* \return The previous value of the atomic variable.
236
* \note This same style can be used for any number operation
238
#ifndef SDL_AtomicAdd
239
static __inline__ int SDL_AtomicAdd(SDL_atomic_t *a, int v)
244
} while (!SDL_AtomicCAS(a, value, (value + v)));
250
* \brief Increment an atomic variable used as a reference count.
252
#ifndef SDL_AtomicIncRef
253
#define SDL_AtomicIncRef(a) SDL_AtomicAdd(a, 1)
257
* \brief Decrement an atomic variable used as a reference count.
259
* \return SDL_TRUE if the variable reached zero after decrementing,
260
* SDL_FALSE otherwise
262
#ifndef SDL_AtomicDecRef
263
#define SDL_AtomicDecRef(a) (SDL_AtomicAdd(a, -1) == 1)
267
* \brief Set a pointer to a new value if it is currently an old value.
269
* \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
271
* \note If you don't know what this function is for, you shouldn't use it!
273
#ifndef SDL_AtomicCASPtr
274
#define SDL_AtomicCASPtr SDL_AtomicCASPtr_
276
extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr_(void* *a, void *oldval, void *newval);
279
* \brief Set a pointer to a value atomically.
281
* \return The previous value of the pointer.
283
#ifndef SDL_AtomicSetPtr
284
static __inline__ void* SDL_AtomicSetPtr(void* *a, void* v)
289
} while (!SDL_AtomicCASPtr(a, value, v));
295
* \brief Get the value of a pointer atomically.
297
#ifndef SDL_AtomicGetPtr
298
static __inline__ void* SDL_AtomicGetPtr(void* *a)
301
SDL_CompilerBarrier();
307
/* Ends C function definitions when using C++ */
314
#include "close_code.h"
316
#endif /* _SDL_atomic_h_ */
318
/* vi: set ts=4 sw=4 expandtab: */