SDL Library

Dependents:   H261_decoder

Committer:
miruga27
Date:
Thu Sep 22 00:03:09 2016 +0000
Revision:
0:7fb6877b5d7c
SDL

Who changed what in which revision?

UserRevisionLine numberNew contents of line
miruga27 0:7fb6877b5d7c 1 /*
miruga27 0:7fb6877b5d7c 2 Simple DirectMedia Layer
miruga27 0:7fb6877b5d7c 3 Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
miruga27 0:7fb6877b5d7c 4
miruga27 0:7fb6877b5d7c 5 This software is provided 'as-is', without any express or implied
miruga27 0:7fb6877b5d7c 6 warranty. In no event will the authors be held liable for any damages
miruga27 0:7fb6877b5d7c 7 arising from the use of this software.
miruga27 0:7fb6877b5d7c 8
miruga27 0:7fb6877b5d7c 9 Permission is granted to anyone to use this software for any purpose,
miruga27 0:7fb6877b5d7c 10 including commercial applications, and to alter it and redistribute it
miruga27 0:7fb6877b5d7c 11 freely, subject to the following restrictions:
miruga27 0:7fb6877b5d7c 12
miruga27 0:7fb6877b5d7c 13 1. The origin of this software must not be misrepresented; you must not
miruga27 0:7fb6877b5d7c 14 claim that you wrote the original software. If you use this software
miruga27 0:7fb6877b5d7c 15 in a product, an acknowledgment in the product documentation would be
miruga27 0:7fb6877b5d7c 16 appreciated but is not required.
miruga27 0:7fb6877b5d7c 17 2. Altered source versions must be plainly marked as such, and must not be
miruga27 0:7fb6877b5d7c 18 misrepresented as being the original software.
miruga27 0:7fb6877b5d7c 19 3. This notice may not be removed or altered from any source distribution.
miruga27 0:7fb6877b5d7c 20 */
miruga27 0:7fb6877b5d7c 21
miruga27 0:7fb6877b5d7c 22 /**
miruga27 0:7fb6877b5d7c 23 * \file SDL_atomic.h
miruga27 0:7fb6877b5d7c 24 *
miruga27 0:7fb6877b5d7c 25 * Atomic operations.
miruga27 0:7fb6877b5d7c 26 *
miruga27 0:7fb6877b5d7c 27 * IMPORTANT:
miruga27 0:7fb6877b5d7c 28 * If you are not an expert in concurrent lockless programming, you should
miruga27 0:7fb6877b5d7c 29 * only be using the atomic lock and reference counting functions in this
miruga27 0:7fb6877b5d7c 30 * file. In all other cases you should be protecting your data structures
miruga27 0:7fb6877b5d7c 31 * with full mutexes.
miruga27 0:7fb6877b5d7c 32 *
miruga27 0:7fb6877b5d7c 33 * The list of "safe" functions to use are:
miruga27 0:7fb6877b5d7c 34 * SDL_AtomicLock()
miruga27 0:7fb6877b5d7c 35 * SDL_AtomicUnlock()
miruga27 0:7fb6877b5d7c 36 * SDL_AtomicIncRef()
miruga27 0:7fb6877b5d7c 37 * SDL_AtomicDecRef()
miruga27 0:7fb6877b5d7c 38 *
miruga27 0:7fb6877b5d7c 39 * Seriously, here be dragons!
miruga27 0:7fb6877b5d7c 40 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
miruga27 0:7fb6877b5d7c 41 *
miruga27 0:7fb6877b5d7c 42 * You can find out a little more about lockless programming and the
miruga27 0:7fb6877b5d7c 43 * subtle issues that can arise here:
miruga27 0:7fb6877b5d7c 44 * http://msdn.microsoft.com/en-us/library/ee418650%28v=vs.85%29.aspx
miruga27 0:7fb6877b5d7c 45 *
miruga27 0:7fb6877b5d7c 46 * There's also lots of good information here:
miruga27 0:7fb6877b5d7c 47 * http://www.1024cores.net/home/lock-free-algorithms
miruga27 0:7fb6877b5d7c 48 * http://preshing.com/
miruga27 0:7fb6877b5d7c 49 *
miruga27 0:7fb6877b5d7c 50 * These operations may or may not actually be implemented using
miruga27 0:7fb6877b5d7c 51 * processor specific atomic operations. When possible they are
miruga27 0:7fb6877b5d7c 52 * implemented as true processor specific atomic operations. When that
miruga27 0:7fb6877b5d7c 53 * is not possible the are implemented using locks that *do* use the
miruga27 0:7fb6877b5d7c 54 * available atomic operations.
miruga27 0:7fb6877b5d7c 55 *
miruga27 0:7fb6877b5d7c 56 * All of the atomic operations that modify memory are full memory barriers.
miruga27 0:7fb6877b5d7c 57 */
miruga27 0:7fb6877b5d7c 58
miruga27 0:7fb6877b5d7c 59 #ifndef _SDL_atomic_h_
miruga27 0:7fb6877b5d7c 60 #define _SDL_atomic_h_
miruga27 0:7fb6877b5d7c 61
miruga27 0:7fb6877b5d7c 62 #include "SDL_stdinc.h"
miruga27 0:7fb6877b5d7c 63 #include "SDL_platform.h"
miruga27 0:7fb6877b5d7c 64
miruga27 0:7fb6877b5d7c 65 #include "begin_code.h"
miruga27 0:7fb6877b5d7c 66
miruga27 0:7fb6877b5d7c 67 /* Set up for C function definitions, even when using C++ */
miruga27 0:7fb6877b5d7c 68 #ifdef __cplusplus
miruga27 0:7fb6877b5d7c 69 extern "C" {
miruga27 0:7fb6877b5d7c 70 #endif
miruga27 0:7fb6877b5d7c 71
miruga27 0:7fb6877b5d7c 72 /**
miruga27 0:7fb6877b5d7c 73 * \name SDL AtomicLock
miruga27 0:7fb6877b5d7c 74 *
miruga27 0:7fb6877b5d7c 75 * The atomic locks are efficient spinlocks using CPU instructions,
miruga27 0:7fb6877b5d7c 76 * but are vulnerable to starvation and can spin forever if a thread
miruga27 0:7fb6877b5d7c 77 * holding a lock has been terminated. For this reason you should
miruga27 0:7fb6877b5d7c 78 * minimize the code executed inside an atomic lock and never do
miruga27 0:7fb6877b5d7c 79 * expensive things like API or system calls while holding them.
miruga27 0:7fb6877b5d7c 80 *
miruga27 0:7fb6877b5d7c 81 * The atomic locks are not safe to lock recursively.
miruga27 0:7fb6877b5d7c 82 *
miruga27 0:7fb6877b5d7c 83 * Porting Note:
miruga27 0:7fb6877b5d7c 84 * The spin lock functions and type are required and can not be
miruga27 0:7fb6877b5d7c 85 * emulated because they are used in the atomic emulation code.
miruga27 0:7fb6877b5d7c 86 */
miruga27 0:7fb6877b5d7c 87 /* @{ */
miruga27 0:7fb6877b5d7c 88
miruga27 0:7fb6877b5d7c 89 typedef int SDL_SpinLock;
miruga27 0:7fb6877b5d7c 90
miruga27 0:7fb6877b5d7c 91 /**
miruga27 0:7fb6877b5d7c 92 * \brief Try to lock a spin lock by setting it to a non-zero value.
miruga27 0:7fb6877b5d7c 93 *
miruga27 0:7fb6877b5d7c 94 * \param lock Points to the lock.
miruga27 0:7fb6877b5d7c 95 *
miruga27 0:7fb6877b5d7c 96 * \return SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.
miruga27 0:7fb6877b5d7c 97 */
miruga27 0:7fb6877b5d7c 98 extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock);
miruga27 0:7fb6877b5d7c 99
miruga27 0:7fb6877b5d7c 100 /**
miruga27 0:7fb6877b5d7c 101 * \brief Lock a spin lock by setting it to a non-zero value.
miruga27 0:7fb6877b5d7c 102 *
miruga27 0:7fb6877b5d7c 103 * \param lock Points to the lock.
miruga27 0:7fb6877b5d7c 104 */
miruga27 0:7fb6877b5d7c 105 extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
miruga27 0:7fb6877b5d7c 106
miruga27 0:7fb6877b5d7c 107 /**
miruga27 0:7fb6877b5d7c 108 * \brief Unlock a spin lock by setting it to 0. Always returns immediately
miruga27 0:7fb6877b5d7c 109 *
miruga27 0:7fb6877b5d7c 110 * \param lock Points to the lock.
miruga27 0:7fb6877b5d7c 111 */
miruga27 0:7fb6877b5d7c 112 extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
miruga27 0:7fb6877b5d7c 113
miruga27 0:7fb6877b5d7c 114 /* @} *//* SDL AtomicLock */
miruga27 0:7fb6877b5d7c 115
miruga27 0:7fb6877b5d7c 116
miruga27 0:7fb6877b5d7c 117 /**
miruga27 0:7fb6877b5d7c 118 * The compiler barrier prevents the compiler from reordering
miruga27 0:7fb6877b5d7c 119 * reads and writes to globally visible variables across the call.
miruga27 0:7fb6877b5d7c 120 */
miruga27 0:7fb6877b5d7c 121 #if defined(_MSC_VER) && (_MSC_VER > 1200)
miruga27 0:7fb6877b5d7c 122 void _ReadWriteBarrier(void);
miruga27 0:7fb6877b5d7c 123 #pragma intrinsic(_ReadWriteBarrier)
miruga27 0:7fb6877b5d7c 124 #define SDL_CompilerBarrier() _ReadWriteBarrier()
miruga27 0:7fb6877b5d7c 125 #elif defined(__GNUC__)
miruga27 0:7fb6877b5d7c 126 #define SDL_CompilerBarrier() __asm__ __volatile__ ("" : : : "memory")
miruga27 0:7fb6877b5d7c 127 #else
miruga27 0:7fb6877b5d7c 128 #define SDL_CompilerBarrier() \
miruga27 0:7fb6877b5d7c 129 { SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); }
miruga27 0:7fb6877b5d7c 130 #endif
miruga27 0:7fb6877b5d7c 131
miruga27 0:7fb6877b5d7c 132 /**
miruga27 0:7fb6877b5d7c 133 * Memory barriers are designed to prevent reads and writes from being
miruga27 0:7fb6877b5d7c 134 * reordered by the compiler and being seen out of order on multi-core CPUs.
miruga27 0:7fb6877b5d7c 135 *
miruga27 0:7fb6877b5d7c 136 * A typical pattern would be for thread A to write some data and a flag,
miruga27 0:7fb6877b5d7c 137 * and for thread B to read the flag and get the data. In this case you
miruga27 0:7fb6877b5d7c 138 * would insert a release barrier between writing the data and the flag,
miruga27 0:7fb6877b5d7c 139 * guaranteeing that the data write completes no later than the flag is
miruga27 0:7fb6877b5d7c 140 * written, and you would insert an acquire barrier between reading the
miruga27 0:7fb6877b5d7c 141 * flag and reading the data, to ensure that all the reads associated
miruga27 0:7fb6877b5d7c 142 * with the flag have completed.
miruga27 0:7fb6877b5d7c 143 *
miruga27 0:7fb6877b5d7c 144 * In this pattern you should always see a release barrier paired with
miruga27 0:7fb6877b5d7c 145 * an acquire barrier and you should gate the data reads/writes with a
miruga27 0:7fb6877b5d7c 146 * single flag variable.
miruga27 0:7fb6877b5d7c 147 *
miruga27 0:7fb6877b5d7c 148 * For more information on these semantics, take a look at the blog post:
miruga27 0:7fb6877b5d7c 149 * http://preshing.com/20120913/acquire-and-release-semantics
miruga27 0:7fb6877b5d7c 150 */
miruga27 0:7fb6877b5d7c 151 #if defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
miruga27 0:7fb6877b5d7c 152 #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("lwsync" : : : "memory")
miruga27 0:7fb6877b5d7c 153 #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("lwsync" : : : "memory")
miruga27 0:7fb6877b5d7c 154 #elif defined(__GNUC__) && defined(__arm__)
miruga27 0:7fb6877b5d7c 155 #if defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7EM__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
miruga27 0:7fb6877b5d7c 156 #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("dmb ish" : : : "memory")
miruga27 0:7fb6877b5d7c 157 #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("dmb ish" : : : "memory")
miruga27 0:7fb6877b5d7c 158 #elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__)
miruga27 0:7fb6877b5d7c 159 #ifdef __thumb__
miruga27 0:7fb6877b5d7c 160 /* The mcr instruction isn't available in thumb mode, use real functions */
miruga27 0:7fb6877b5d7c 161 extern DECLSPEC void SDLCALL SDL_MemoryBarrierRelease();
miruga27 0:7fb6877b5d7c 162 extern DECLSPEC void SDLCALL SDL_MemoryBarrierAcquire();
miruga27 0:7fb6877b5d7c 163 #else
miruga27 0:7fb6877b5d7c 164 #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
miruga27 0:7fb6877b5d7c 165 #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
miruga27 0:7fb6877b5d7c 166 #endif /* __thumb__ */
miruga27 0:7fb6877b5d7c 167 #else
miruga27 0:7fb6877b5d7c 168 #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("" : : : "memory")
miruga27 0:7fb6877b5d7c 169 #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("" : : : "memory")
miruga27 0:7fb6877b5d7c 170 #endif /* __GNUC__ && __arm__ */
miruga27 0:7fb6877b5d7c 171 #else
miruga27 0:7fb6877b5d7c 172 /* This is correct for the x86 and x64 CPUs, and we'll expand this over time. */
miruga27 0:7fb6877b5d7c 173 #define SDL_MemoryBarrierRelease() SDL_CompilerBarrier()
miruga27 0:7fb6877b5d7c 174 #define SDL_MemoryBarrierAcquire() SDL_CompilerBarrier()
miruga27 0:7fb6877b5d7c 175 #endif
miruga27 0:7fb6877b5d7c 176
miruga27 0:7fb6877b5d7c 177 /**
miruga27 0:7fb6877b5d7c 178 * \brief A type representing an atomic integer value. It is a struct
miruga27 0:7fb6877b5d7c 179 * so people don't accidentally use numeric operations on it.
miruga27 0:7fb6877b5d7c 180 */
miruga27 0:7fb6877b5d7c 181 typedef struct { int value; } SDL_atomic_t;
miruga27 0:7fb6877b5d7c 182
miruga27 0:7fb6877b5d7c 183 /**
miruga27 0:7fb6877b5d7c 184 * \brief Set an atomic variable to a new value if it is currently an old value.
miruga27 0:7fb6877b5d7c 185 *
miruga27 0:7fb6877b5d7c 186 * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
miruga27 0:7fb6877b5d7c 187 *
miruga27 0:7fb6877b5d7c 188 * \note If you don't know what this function is for, you shouldn't use it!
miruga27 0:7fb6877b5d7c 189 */
miruga27 0:7fb6877b5d7c 190 extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
miruga27 0:7fb6877b5d7c 191
miruga27 0:7fb6877b5d7c 192 /**
miruga27 0:7fb6877b5d7c 193 * \brief Set an atomic variable to a value.
miruga27 0:7fb6877b5d7c 194 *
miruga27 0:7fb6877b5d7c 195 * \return The previous value of the atomic variable.
miruga27 0:7fb6877b5d7c 196 */
miruga27 0:7fb6877b5d7c 197 extern DECLSPEC int SDLCALL SDL_AtomicSet(SDL_atomic_t *a, int v);
miruga27 0:7fb6877b5d7c 198
miruga27 0:7fb6877b5d7c 199 /**
miruga27 0:7fb6877b5d7c 200 * \brief Get the value of an atomic variable
miruga27 0:7fb6877b5d7c 201 */
miruga27 0:7fb6877b5d7c 202 extern DECLSPEC int SDLCALL SDL_AtomicGet(SDL_atomic_t *a);
miruga27 0:7fb6877b5d7c 203
miruga27 0:7fb6877b5d7c 204 /**
miruga27 0:7fb6877b5d7c 205 * \brief Add to an atomic variable.
miruga27 0:7fb6877b5d7c 206 *
miruga27 0:7fb6877b5d7c 207 * \return The previous value of the atomic variable.
miruga27 0:7fb6877b5d7c 208 *
miruga27 0:7fb6877b5d7c 209 * \note This same style can be used for any number operation
miruga27 0:7fb6877b5d7c 210 */
miruga27 0:7fb6877b5d7c 211 extern DECLSPEC int SDLCALL SDL_AtomicAdd(SDL_atomic_t *a, int v);
miruga27 0:7fb6877b5d7c 212
miruga27 0:7fb6877b5d7c 213 /**
miruga27 0:7fb6877b5d7c 214 * \brief Increment an atomic variable used as a reference count.
miruga27 0:7fb6877b5d7c 215 */
miruga27 0:7fb6877b5d7c 216 #ifndef SDL_AtomicIncRef
miruga27 0:7fb6877b5d7c 217 #define SDL_AtomicIncRef(a) SDL_AtomicAdd(a, 1)
miruga27 0:7fb6877b5d7c 218 #endif
miruga27 0:7fb6877b5d7c 219
miruga27 0:7fb6877b5d7c 220 /**
miruga27 0:7fb6877b5d7c 221 * \brief Decrement an atomic variable used as a reference count.
miruga27 0:7fb6877b5d7c 222 *
miruga27 0:7fb6877b5d7c 223 * \return SDL_TRUE if the variable reached zero after decrementing,
miruga27 0:7fb6877b5d7c 224 * SDL_FALSE otherwise
miruga27 0:7fb6877b5d7c 225 */
miruga27 0:7fb6877b5d7c 226 #ifndef SDL_AtomicDecRef
miruga27 0:7fb6877b5d7c 227 #define SDL_AtomicDecRef(a) (SDL_AtomicAdd(a, -1) == 1)
miruga27 0:7fb6877b5d7c 228 #endif
miruga27 0:7fb6877b5d7c 229
miruga27 0:7fb6877b5d7c 230 /**
miruga27 0:7fb6877b5d7c 231 * \brief Set a pointer to a new value if it is currently an old value.
miruga27 0:7fb6877b5d7c 232 *
miruga27 0:7fb6877b5d7c 233 * \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
miruga27 0:7fb6877b5d7c 234 *
miruga27 0:7fb6877b5d7c 235 * \note If you don't know what this function is for, you shouldn't use it!
miruga27 0:7fb6877b5d7c 236 */
miruga27 0:7fb6877b5d7c 237 extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval);
miruga27 0:7fb6877b5d7c 238
miruga27 0:7fb6877b5d7c 239 /**
miruga27 0:7fb6877b5d7c 240 * \brief Set a pointer to a value atomically.
miruga27 0:7fb6877b5d7c 241 *
miruga27 0:7fb6877b5d7c 242 * \return The previous value of the pointer.
miruga27 0:7fb6877b5d7c 243 */
miruga27 0:7fb6877b5d7c 244 extern DECLSPEC void* SDLCALL SDL_AtomicSetPtr(void **a, void* v);
miruga27 0:7fb6877b5d7c 245
miruga27 0:7fb6877b5d7c 246 /**
miruga27 0:7fb6877b5d7c 247 * \brief Get the value of a pointer atomically.
miruga27 0:7fb6877b5d7c 248 */
miruga27 0:7fb6877b5d7c 249 extern DECLSPEC void* SDLCALL SDL_AtomicGetPtr(void **a);
miruga27 0:7fb6877b5d7c 250
miruga27 0:7fb6877b5d7c 251 /* Ends C function definitions when using C++ */
miruga27 0:7fb6877b5d7c 252 #ifdef __cplusplus
miruga27 0:7fb6877b5d7c 253 }
miruga27 0:7fb6877b5d7c 254 #endif
miruga27 0:7fb6877b5d7c 255
miruga27 0:7fb6877b5d7c 256 #include "close_code.h"
miruga27 0:7fb6877b5d7c 257
miruga27 0:7fb6877b5d7c 258 #endif /* _SDL_atomic_h_ */
miruga27 0:7fb6877b5d7c 259
miruga27 0:7fb6877b5d7c 260 /* vi: set ts=4 sw=4 expandtab: */