SDL_atomic.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. SDL - Simple DirectMedia Layer
  3. Copyright (C) 1997-2011 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Sam Lantinga
  16. [email protected]
  17. */
  18. /**
  19. * \file SDL_atomic.h
  20. *
  21. * Atomic operations.
  22. *
  23. * IMPORTANT:
  24. * If you are not an expert in concurrent lockless programming, you should
  25. * only be using the atomic lock and reference counting functions in this
  26. * file. In all other cases you should be protecting your data structures
  27. * with full mutexes.
  28. *
  29. * The list of "safe" functions to use are:
  30. * SDL_AtomicLock()
  31. * SDL_AtomicUnlock()
  32. * SDL_AtomicIncRef()
  33. * SDL_AtomicDecRef()
  34. *
  35. * Seriously, here be dragons!
  36. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  37. *
  38. * You can find out a little more about lockless programming and the
  39. * subtle issues that can arise here:
  40. * http://msdn.microsoft.com/en-us/library/ee418650%28v=vs.85%29.aspx
  41. *
  42. * There's also lots of good information here:
  43. * http://www.1024cores.net/home/lock-free-algorithms
  44. *
  45. * These operations may or may not actually be implemented using
  46. * processor specific atomic operations. When possible they are
  47. * implemented as true processor specific atomic operations. When that
  48. * is not possible the are implemented using locks that *do* use the
  49. * available atomic operations.
  50. *
  51. * All of the atomic operations that modify memory are full memory barriers.
  52. */
  53. #ifndef _SDL_atomic_h_
  54. #define _SDL_atomic_h_
  55. #include "SDL_stdinc.h"
  56. #include "SDL_platform.h"
  57. #include "begin_code.h"
  58. /* Need to do this here because intrin.h has C++ code in it */
  59. /* Visual Studio 2005 has a bug where intrin.h conflicts with winnt.h */
  60. #if defined(_MSC_VER) && (_MSC_VER >= 1500) && !defined(_WIN32_WCE)
  61. #include <intrin.h>
  62. #define HAVE_MSC_ATOMICS 1
  63. #endif
  64. /* Set up for C function definitions, even when using C++ */
  65. #ifdef __cplusplus
  66. /* *INDENT-OFF* */
  67. extern "C" {
  68. /* *INDENT-ON* */
  69. #endif
  70. /**
  71. * \name SDL AtomicLock
  72. *
  73. * The atomic locks are efficient spinlocks using CPU instructions,
  74. * but are vulnerable to starvation and can spin forever if a thread
  75. * holding a lock has been terminated. For this reason you should
  76. * minimize the code executed inside an atomic lock and never do
  77. * expensive things like API or system calls while holding them.
  78. *
  79. * The atomic locks are not safe to lock recursively.
  80. *
  81. * Porting Note:
  82. * The spin lock functions and type are required and can not be
  83. * emulated because they are used in the atomic emulation code.
  84. */
  85. /*@{*/
  86. typedef int SDL_SpinLock;
  87. /**
  88. * \brief Try to lock a spin lock by setting it to a non-zero value.
  89. *
  90. * \param lock Points to the lock.
  91. *
  92. * \return SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.
  93. */
  94. extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock);
  95. /**
  96. * \brief Lock a spin lock by setting it to a non-zero value.
  97. *
  98. * \param lock Points to the lock.
  99. */
  100. extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
  101. /**
  102. * \brief Unlock a spin lock by setting it to 0. Always returns immediately
  103. *
  104. * \param lock Points to the lock.
  105. */
  106. extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
  107. /*@}*//*SDL AtomicLock*/
  108. /**
  109. * The compiler barrier prevents the compiler from reordering
  110. * reads and writes to globally visible variables across the call.
  111. */
  112. #ifdef _MSC_VER
  113. void _ReadWriteBarrier(void);
  114. #pragma intrinsic(_ReadWriteBarrier)
  115. #define SDL_CompilerBarrier() _ReadWriteBarrier()
  116. #elif defined(__GNUC__)
  117. #define SDL_CompilerBarrier() __asm__ __volatile__ ("" : : : "memory")
  118. #else
  119. #define SDL_CompilerBarrier() \
  120. ({ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); })
  121. #endif
  122. /* Platform specific optimized versions of the atomic functions,
  123. * you can disable these by defining SDL_DISABLE_ATOMIC_INLINE
  124. */
  125. #if SDL_ATOMIC_DISABLED
  126. #define SDL_DISABLE_ATOMIC_INLINE
  127. #endif
  128. #ifndef SDL_DISABLE_ATOMIC_INLINE
  129. #ifdef HAVE_MSC_ATOMICS
  130. #define SDL_AtomicSet(a, v) _InterlockedExchange((long*)&(a)->value, (v))
  131. #define SDL_AtomicAdd(a, v) _InterlockedExchangeAdd((long*)&(a)->value, (v))
  132. #define SDL_AtomicCAS(a, oldval, newval) (_InterlockedCompareExchange((long*)&(a)->value, (newval), (oldval)) == (oldval))
  133. #define SDL_AtomicSetPtr(a, v) _InterlockedExchangePointer((a), (v))
  134. #if _M_IX86
  135. #define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchange((long*)(a), (long)(newval), (long)(oldval)) == (long)(oldval))
  136. #else
  137. #define SDL_AtomicCASPtr(a, oldval, newval) (_InterlockedCompareExchangePointer((a), (newval), (oldval)) == (oldval))
  138. #endif
  139. #elif defined(__MACOSX__)
  140. #include <libkern/OSAtomic.h>
  141. #define SDL_AtomicCAS(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((oldval), (newval), &(a)->value)
  142. #if SIZEOF_VOIDP == 4
  143. #define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap32Barrier((int32_t)(oldval), (int32_t)(newval), (int32_t*)(a))
  144. #elif SIZEOF_VOIDP == 8
  145. #define SDL_AtomicCASPtr(a, oldval, newval) OSAtomicCompareAndSwap64Barrier((int64_t)(oldval), (int64_t)(newval), (int64_t*)(a))
  146. #endif
  147. #elif defined(HAVE_GCC_ATOMICS)
  148. #define SDL_AtomicSet(a, v) __sync_lock_test_and_set(&(a)->value, v)
  149. #define SDL_AtomicAdd(a, v) __sync_fetch_and_add(&(a)->value, v)
  150. #define SDL_AtomicSetPtr(a, v) __sync_lock_test_and_set(a, v)
  151. #define SDL_AtomicCAS(a, oldval, newval) __sync_bool_compare_and_swap(&(a)->value, oldval, newval)
  152. #define SDL_AtomicCASPtr(a, oldval, newval) __sync_bool_compare_and_swap(a, oldval, newval)
  153. #endif
  154. #endif /* !SDL_DISABLE_ATOMIC_INLINE */
  155. /**
  156. * \brief A type representing an atomic integer value. It is a struct
  157. * so people don't accidentally use numeric operations on it.
  158. */
  159. #ifndef SDL_atomic_t_defined
  160. typedef struct { int value; } SDL_atomic_t;
  161. #endif
  162. /**
  163. * \brief Set an atomic variable to a new value if it is currently an old value.
  164. *
  165. * \return SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
  166. *
  167. * \note If you don't know what this function is for, you shouldn't use it!
  168. */
  169. #ifndef SDL_AtomicCAS
  170. #define SDL_AtomicCAS SDL_AtomicCAS_
  171. #endif
  172. extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS_(SDL_atomic_t *a, int oldval, int newval);
  173. /**
  174. * \brief Set an atomic variable to a value.
  175. *
  176. * \return The previous value of the atomic variable.
  177. */
  178. #ifndef SDL_AtomicSet
  179. static __inline__ int SDL_AtomicSet(SDL_atomic_t *a, int v)
  180. {
  181. int value;
  182. do {
  183. value = a->value;
  184. } while (!SDL_AtomicCAS(a, value, v));
  185. return value;
  186. }
  187. #endif
  188. /**
  189. * \brief Get the value of an atomic variable
  190. */
  191. #ifndef SDL_AtomicGet
  192. static __inline__ int SDL_AtomicGet(SDL_atomic_t *a)
  193. {
  194. int value = a->value;
  195. SDL_CompilerBarrier();
  196. return value;
  197. }
  198. #endif
  199. /**
  200. * \brief Add to an atomic variable.
  201. *
  202. * \return The previous value of the atomic variable.
  203. *
  204. * \note This same style can be used for any number operation
  205. */
  206. #ifndef SDL_AtomicAdd
  207. static __inline__ int SDL_AtomicAdd(SDL_atomic_t *a, int v)
  208. {
  209. int value;
  210. do {
  211. value = a->value;
  212. } while (!SDL_AtomicCAS(a, value, (value + v)));
  213. return value;
  214. }
  215. #endif
  216. /**
  217. * \brief Increment an atomic variable used as a reference count.
  218. */
  219. #ifndef SDL_AtomicIncRef
  220. #define SDL_AtomicIncRef(a) SDL_AtomicAdd(a, 1)
  221. #endif
  222. /**
  223. * \brief Decrement an atomic variable used as a reference count.
  224. *
  225. * \return SDL_TRUE if the variable reached zero after decrementing,
  226. * SDL_FALSE otherwise
  227. */
  228. #ifndef SDL_AtomicDecRef
  229. #define SDL_AtomicDecRef(a) (SDL_AtomicAdd(a, -1) == 1)
  230. #endif
  231. /**
  232. * \brief Set a pointer to a new value if it is currently an old value.
  233. *
  234. * \return SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
  235. *
  236. * \note If you don't know what this function is for, you shouldn't use it!
  237. */
  238. #ifndef SDL_AtomicCASPtr
  239. #define SDL_AtomicCASPtr SDL_AtomicCASPtr_
  240. #endif
  241. extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr_(void* *a, void *oldval, void *newval);
  242. /**
  243. * \brief Set a pointer to a value atomically.
  244. *
  245. * \return The previous value of the pointer.
  246. */
  247. #ifndef SDL_AtomicSetPtr
  248. static __inline__ void* SDL_AtomicSetPtr(void* *a, void* v)
  249. {
  250. void* value;
  251. do {
  252. value = *a;
  253. } while (!SDL_AtomicCASPtr(a, value, v));
  254. return value;
  255. }
  256. #endif
  257. /**
  258. * \brief Get the value of a pointer atomically.
  259. */
  260. #ifndef SDL_AtomicGetPtr
  261. static __inline__ void* SDL_AtomicGetPtr(void* *a)
  262. {
  263. void* value = *a;
  264. SDL_CompilerBarrier();
  265. return value;
  266. }
  267. #endif
  268. /* Ends C function definitions when using C++ */
  269. #ifdef __cplusplus
  270. /* *INDENT-OFF* */
  271. }
  272. /* *INDENT-ON* */
  273. #endif
  274. #include "close_code.h"
  275. #endif /* _SDL_atomic_h_ */
  276. /* vi: set ts=4 sw=4 expandtab: */