SDL standard library

Dependents:   H261_encoder

Committer:
miruga27
Date:
Wed Sep 07 18:46:53 2016 +0000
Revision:
0:dda4f4550403
7/09/2016;

Who changed what in which revision?

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