SDL_atomic.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryAtomic
  20. *
  21. * Atomic operations.
  22. *
  23. * IMPORTANT: If you are not an expert in concurrent lockless programming, you
  24. * should not be using any functions in this file. You should be protecting
  25. * your data structures with full mutexes instead.
  26. *
  27. * ***Seriously, here be dragons!***
  28. *
  29. * You can find out a little more about lockless programming and the subtle
  30. * issues that can arise here:
  31. * https://learn.microsoft.com/en-us/windows/win32/dxtecharts/lockless-programming
  32. *
  33. * There's also lots of good information here:
  34. *
  35. * - https://www.1024cores.net/home/lock-free-algorithms
  36. * - https://preshing.com/
  37. *
  38. * These operations may or may not actually be implemented using processor
  39. * specific atomic operations. When possible they are implemented as true
  40. * processor specific atomic operations. When that is not possible the are
  41. * implemented using locks that *do* use the available atomic operations.
  42. *
  43. * All of the atomic operations that modify memory are full memory barriers.
  44. */
  45. #ifndef SDL_atomic_h_
  46. #define SDL_atomic_h_
  47. #include <SDL3/SDL_stdinc.h>
  48. #include <SDL3/SDL_platform_defines.h>
  49. #include <SDL3/SDL_begin_code.h>
  50. /* Set up for C function definitions, even when using C++ */
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /**
  55. * An atomic spinlock.
  56. *
  57. * The atomic locks are efficient spinlocks using CPU instructions, but are
  58. * vulnerable to starvation and can spin forever if a thread holding a lock
  59. * has been terminated. For this reason you should minimize the code executed
  60. * inside an atomic lock and never do expensive things like API or system
  61. * calls while holding them.
  62. *
  63. * They are also vulnerable to starvation if the thread holding the lock is
  64. * lower priority than other threads and doesn't get scheduled. In general you
  65. * should use mutexes instead, since they have better performance and
  66. * contention behavior.
  67. *
  68. * The atomic locks are not safe to lock recursively.
  69. *
  70. * Porting Note: The spin lock functions and type are required and can not be
  71. * emulated because they are used in the atomic emulation code.
  72. */
  73. typedef int SDL_SpinLock;
  74. /**
  75. * Try to lock a spin lock by setting it to a non-zero value.
  76. *
  77. * ***Please note that spinlocks are dangerous if you don't know what you're
  78. * doing. Please be careful using any sort of spinlock!***
  79. *
  80. * \param lock a pointer to a lock variable.
  81. * \returns true if the lock succeeded, false if the lock is already held.
  82. *
  83. * \threadsafety It is safe to call this function from any thread.
  84. *
  85. * \since This function is available since SDL 3.2.0.
  86. *
  87. * \sa SDL_LockSpinlock
  88. * \sa SDL_UnlockSpinlock
  89. */
  90. extern SDL_DECLSPEC bool SDLCALL SDL_TryLockSpinlock(SDL_SpinLock *lock);
  91. /**
  92. * Lock a spin lock by setting it to a non-zero value.
  93. *
  94. * ***Please note that spinlocks are dangerous if you don't know what you're
  95. * doing. Please be careful using any sort of spinlock!***
  96. *
  97. * \param lock a pointer to a lock variable.
  98. *
  99. * \threadsafety It is safe to call this function from any thread.
  100. *
  101. * \since This function is available since SDL 3.2.0.
  102. *
  103. * \sa SDL_TryLockSpinlock
  104. * \sa SDL_UnlockSpinlock
  105. */
  106. extern SDL_DECLSPEC void SDLCALL SDL_LockSpinlock(SDL_SpinLock *lock);
  107. /**
  108. * Unlock a spin lock by setting it to 0.
  109. *
  110. * Always returns immediately.
  111. *
  112. * ***Please note that spinlocks are dangerous if you don't know what you're
  113. * doing. Please be careful using any sort of spinlock!***
  114. *
  115. * \param lock a pointer to a lock variable.
  116. *
  117. * \threadsafety It is safe to call this function from any thread.
  118. *
  119. * \since This function is available since SDL 3.2.0.
  120. *
  121. * \sa SDL_LockSpinlock
  122. * \sa SDL_TryLockSpinlock
  123. */
  124. extern SDL_DECLSPEC void SDLCALL SDL_UnlockSpinlock(SDL_SpinLock *lock);
  125. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  126. /**
  127. * Mark a compiler barrier.
  128. *
  129. * A compiler barrier prevents the compiler from reordering reads and writes
  130. * to globally visible variables across the call.
  131. *
  132. * This macro only prevents the compiler from reordering reads and writes, it
  133. * does not prevent the CPU from reordering reads and writes. However, all of
  134. * the atomic operations that modify memory are full memory barriers.
  135. *
  136. * \threadsafety Obviously this macro is safe to use from any thread at any
  137. * time, but if you find yourself needing this, you are probably
  138. * dealing with some very sensitive code; be careful!
  139. *
  140. * \since This macro is available since SDL 3.2.0.
  141. */
  142. #define SDL_CompilerBarrier() DoCompilerSpecificReadWriteBarrier()
  143. #elif defined(_MSC_VER) && (_MSC_VER > 1200) && !defined(__clang__)
  144. void _ReadWriteBarrier(void);
  145. #pragma intrinsic(_ReadWriteBarrier)
  146. #define SDL_CompilerBarrier() _ReadWriteBarrier()
  147. #elif (defined(__GNUC__) && !defined(SDL_PLATFORM_EMSCRIPTEN)) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x5120))
  148. /* This is correct for all CPUs when using GCC or Solaris Studio 12.1+. */
  149. #define SDL_CompilerBarrier() __asm__ __volatile__ ("" : : : "memory")
  150. #elif defined(__WATCOMC__)
  151. extern __inline void SDL_CompilerBarrier(void);
  152. #pragma aux SDL_CompilerBarrier = "" parm [] modify exact [];
  153. #else
  154. #define SDL_CompilerBarrier() \
  155. { SDL_SpinLock _tmp = 0; SDL_LockSpinlock(&_tmp); SDL_UnlockSpinlock(&_tmp); }
  156. #endif
  157. /**
  158. * Insert a memory release barrier (function version).
  159. *
  160. * Please refer to SDL_MemoryBarrierRelease for details. This is a function
  161. * version, which might be useful if you need to use this functionality from a
  162. * scripting language, etc. Also, some of the macro versions call this
  163. * function behind the scenes, where more heavy lifting can happen inside of
  164. * SDL. Generally, though, an app written in C/C++/etc should use the macro
  165. * version, as it will be more efficient.
  166. *
  167. * \threadsafety Obviously this function is safe to use from any thread at any
  168. * time, but if you find yourself needing this, you are probably
  169. * dealing with some very sensitive code; be careful!
  170. *
  171. * \since This function is available since SDL 3.2.0.
  172. *
  173. * \sa SDL_MemoryBarrierRelease
  174. */
  175. extern SDL_DECLSPEC void SDLCALL SDL_MemoryBarrierReleaseFunction(void);
  176. /**
  177. * Insert a memory acquire barrier (function version).
  178. *
  179. * Please refer to SDL_MemoryBarrierRelease for details. This is a function
  180. * version, which might be useful if you need to use this functionality from a
  181. * scripting language, etc. Also, some of the macro versions call this
  182. * function behind the scenes, where more heavy lifting can happen inside of
  183. * SDL. Generally, though, an app written in C/C++/etc should use the macro
  184. * version, as it will be more efficient.
  185. *
  186. * \threadsafety Obviously this function is safe to use from any thread at any
  187. * time, but if you find yourself needing this, you are probably
  188. * dealing with some very sensitive code; be careful!
  189. *
  190. * \since This function is available since SDL 3.2.0.
  191. *
  192. * \sa SDL_MemoryBarrierAcquire
  193. */
  194. extern SDL_DECLSPEC void SDLCALL SDL_MemoryBarrierAcquireFunction(void);
  195. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  196. /**
  197. * Insert a memory release barrier (macro version).
  198. *
  199. * Memory barriers are designed to prevent reads and writes from being
  200. * reordered by the compiler and being seen out of order on multi-core CPUs.
  201. *
  202. * A typical pattern would be for thread A to write some data and a flag, and
  203. * for thread B to read the flag and get the data. In this case you would
  204. * insert a release barrier between writing the data and the flag,
  205. * guaranteeing that the data write completes no later than the flag is
  206. * written, and you would insert an acquire barrier between reading the flag
  207. * and reading the data, to ensure that all the reads associated with the flag
  208. * have completed.
  209. *
  210. * In this pattern you should always see a release barrier paired with an
  211. * acquire barrier and you should gate the data reads/writes with a single
  212. * flag variable.
  213. *
  214. * For more information on these semantics, take a look at the blog post:
  215. * http://preshing.com/20120913/acquire-and-release-semantics
  216. *
  217. * This is the macro version of this functionality; if possible, SDL will use
  218. * compiler intrinsics or inline assembly, but some platforms might need to
  219. * call the function version of this, SDL_MemoryBarrierReleaseFunction to do
  220. * the heavy lifting. Apps that can use the macro should favor it over the
  221. * function.
  222. *
  223. * \threadsafety Obviously this macro is safe to use from any thread at any
  224. * time, but if you find yourself needing this, you are probably
  225. * dealing with some very sensitive code; be careful!
  226. *
  227. * \since This macro is available since SDL 3.2.0.
  228. *
  229. * \sa SDL_MemoryBarrierAcquire
  230. * \sa SDL_MemoryBarrierReleaseFunction
  231. */
  232. #define SDL_MemoryBarrierRelease() SDL_MemoryBarrierReleaseFunction()
  233. /**
  234. * Insert a memory acquire barrier (macro version).
  235. *
  236. * Please see SDL_MemoryBarrierRelease for the details on what memory barriers
  237. * are and when to use them.
  238. *
  239. * This is the macro version of this functionality; if possible, SDL will use
  240. * compiler intrinsics or inline assembly, but some platforms might need to
  241. * call the function version of this, SDL_MemoryBarrierAcquireFunction, to do
  242. * the heavy lifting. Apps that can use the macro should favor it over the
  243. * function.
  244. *
  245. * \threadsafety Obviously this macro is safe to use from any thread at any
  246. * time, but if you find yourself needing this, you are probably
  247. * dealing with some very sensitive code; be careful!
  248. *
  249. * \since This macro is available since SDL 3.2.0.
  250. *
  251. * \sa SDL_MemoryBarrierRelease
  252. * \sa SDL_MemoryBarrierAcquireFunction
  253. */
  254. #define SDL_MemoryBarrierAcquire() SDL_MemoryBarrierAcquireFunction()
  255. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  256. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("lwsync" : : : "memory")
  257. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("lwsync" : : : "memory")
  258. #elif defined(__GNUC__) && defined(__aarch64__)
  259. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("dmb ish" : : : "memory")
  260. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("dmb ish" : : : "memory")
  261. #elif defined(__GNUC__) && defined(__arm__)
  262. #if 0 /* defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID) */
  263. /* Information from:
  264. https://chromium.googlesource.com/chromium/chromium/+/trunk/base/atomicops_internals_arm_gcc.h#19
  265. The Linux kernel provides a helper function which provides the right code for a memory barrier,
  266. hard-coded at address 0xffff0fa0
  267. */
  268. typedef void (*SDL_KernelMemoryBarrierFunc)();
  269. #define SDL_MemoryBarrierRelease() ((SDL_KernelMemoryBarrierFunc)0xffff0fa0)()
  270. #define SDL_MemoryBarrierAcquire() ((SDL_KernelMemoryBarrierFunc)0xffff0fa0)()
  271. #else
  272. #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__) || defined(__ARM_ARCH_8A__)
  273. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("dmb ish" : : : "memory")
  274. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("dmb ish" : : : "memory")
  275. #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__)
  276. #ifdef __thumb__
  277. /* The mcr instruction isn't available in thumb mode, use real functions */
  278. #define SDL_MEMORY_BARRIER_USES_FUNCTION
  279. #define SDL_MemoryBarrierRelease() SDL_MemoryBarrierReleaseFunction()
  280. #define SDL_MemoryBarrierAcquire() SDL_MemoryBarrierAcquireFunction()
  281. #else
  282. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
  283. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
  284. #endif /* __thumb__ */
  285. #else
  286. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("" : : : "memory")
  287. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("" : : : "memory")
  288. #endif /* SDL_PLATFORM_LINUX || SDL_PLATFORM_ANDROID */
  289. #endif /* __GNUC__ && __arm__ */
  290. #else
  291. #if (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x5120))
  292. /* This is correct for all CPUs on Solaris when using Solaris Studio 12.1+. */
  293. #include <mbarrier.h>
  294. #define SDL_MemoryBarrierRelease() __machine_rel_barrier()
  295. #define SDL_MemoryBarrierAcquire() __machine_acq_barrier()
  296. #else
  297. /* This is correct for the x86 and x64 CPUs, and we'll expand this over time. */
  298. #define SDL_MemoryBarrierRelease() SDL_CompilerBarrier()
  299. #define SDL_MemoryBarrierAcquire() SDL_CompilerBarrier()
  300. #endif
  301. #endif
  302. /* "REP NOP" is PAUSE, coded for tools that don't know it by that name. */
  303. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  304. /**
  305. * A macro to insert a CPU-specific "pause" instruction into the program.
  306. *
  307. * This can be useful in busy-wait loops, as it serves as a hint to the CPU as
  308. * to the program's intent; some CPUs can use this to do more efficient
  309. * processing. On some platforms, this doesn't do anything, so using this
  310. * macro might just be a harmless no-op.
  311. *
  312. * Note that if you are busy-waiting, there are often more-efficient
  313. * approaches with other synchronization primitives: mutexes, semaphores,
  314. * condition variables, etc.
  315. *
  316. * \threadsafety This macro is safe to use from any thread.
  317. *
  318. * \since This macro is available since SDL 3.2.0.
  319. */
  320. #define SDL_CPUPauseInstruction() DoACPUPauseInACompilerAndArchitectureSpecificWay
  321. #elif (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
  322. #define SDL_CPUPauseInstruction() __asm__ __volatile__("pause\n") /* Some assemblers can't do REP NOP, so go with PAUSE. */
  323. #elif (defined(__arm__) && defined(__ARM_ARCH) && __ARM_ARCH >= 7) || defined(__aarch64__)
  324. #define SDL_CPUPauseInstruction() __asm__ __volatile__("yield" ::: "memory")
  325. #elif (defined(__powerpc__) || defined(__powerpc64__))
  326. #define SDL_CPUPauseInstruction() __asm__ __volatile__("or 27,27,27");
  327. #elif (defined(__riscv) && __riscv_xlen == 64)
  328. #define SDL_CPUPauseInstruction() __asm__ __volatile__(".insn i 0x0F, 0, x0, x0, 0x010");
  329. #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
  330. #define SDL_CPUPauseInstruction() _mm_pause() /* this is actually "rep nop" and not a SIMD instruction. No inline asm in MSVC x86-64! */
  331. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  332. #define SDL_CPUPauseInstruction() __yield()
  333. #elif defined(__WATCOMC__) && defined(__386__)
  334. extern __inline void SDL_CPUPauseInstruction(void);
  335. #pragma aux SDL_CPUPauseInstruction = ".686p" ".xmm2" "pause"
  336. #else
  337. #define SDL_CPUPauseInstruction()
  338. #endif
  339. /**
  340. * A type representing an atomic integer value.
  341. *
  342. * This can be used to manage a value that is synchronized across multiple
  343. * CPUs without a race condition; when an app sets a value with
  344. * SDL_SetAtomicInt all other threads, regardless of the CPU it is running on,
  345. * will see that value when retrieved with SDL_GetAtomicInt, regardless of CPU
  346. * caches, etc.
  347. *
  348. * This is also useful for atomic compare-and-swap operations: a thread can
  349. * change the value as long as its current value matches expectations. When
  350. * done in a loop, one can guarantee data consistency across threads without a
  351. * lock (but the usual warnings apply: if you don't know what you're doing, or
  352. * you don't do it carefully, you can confidently cause any number of
  353. * disasters with this, so in most cases, you _should_ use a mutex instead of
  354. * this!).
  355. *
  356. * This is a struct so people don't accidentally use numeric operations on it
  357. * directly. You have to use SDL atomic functions.
  358. *
  359. * \since This struct is available since SDL 3.2.0.
  360. *
  361. * \sa SDL_CompareAndSwapAtomicInt
  362. * \sa SDL_GetAtomicInt
  363. * \sa SDL_SetAtomicInt
  364. * \sa SDL_AddAtomicInt
  365. */
  366. typedef struct SDL_AtomicInt { int value; } SDL_AtomicInt;
  367. /**
  368. * Set an atomic variable to a new value if it is currently an old value.
  369. *
  370. * ***Note: If you don't know what this function is for, you shouldn't use
  371. * it!***
  372. *
  373. * \param a a pointer to an SDL_AtomicInt variable to be modified.
  374. * \param oldval the old value.
  375. * \param newval the new value.
  376. * \returns true if the atomic variable was set, false otherwise.
  377. *
  378. * \threadsafety It is safe to call this function from any thread.
  379. *
  380. * \since This function is available since SDL 3.2.0.
  381. *
  382. * \sa SDL_GetAtomicInt
  383. * \sa SDL_SetAtomicInt
  384. */
  385. extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapAtomicInt(SDL_AtomicInt *a, int oldval, int newval);
  386. /**
  387. * Set an atomic variable to a value.
  388. *
  389. * This function also acts as a full memory barrier.
  390. *
  391. * ***Note: If you don't know what this function is for, you shouldn't use
  392. * it!***
  393. *
  394. * \param a a pointer to an SDL_AtomicInt variable to be modified.
  395. * \param v the desired value.
  396. * \returns the previous value of the atomic variable.
  397. *
  398. * \threadsafety It is safe to call this function from any thread.
  399. *
  400. * \since This function is available since SDL 3.2.0.
  401. *
  402. * \sa SDL_GetAtomicInt
  403. */
  404. extern SDL_DECLSPEC int SDLCALL SDL_SetAtomicInt(SDL_AtomicInt *a, int v);
  405. /**
  406. * Get the value of an atomic variable.
  407. *
  408. * ***Note: If you don't know what this function is for, you shouldn't use
  409. * it!***
  410. *
  411. * \param a a pointer to an SDL_AtomicInt variable.
  412. * \returns the current value of an atomic variable.
  413. *
  414. * \threadsafety It is safe to call this function from any thread.
  415. *
  416. * \since This function is available since SDL 3.2.0.
  417. *
  418. * \sa SDL_SetAtomicInt
  419. */
  420. extern SDL_DECLSPEC int SDLCALL SDL_GetAtomicInt(SDL_AtomicInt *a);
  421. /**
  422. * Add to an atomic variable.
  423. *
  424. * This function also acts as a full memory barrier.
  425. *
  426. * ***Note: If you don't know what this function is for, you shouldn't use
  427. * it!***
  428. *
  429. * \param a a pointer to an SDL_AtomicInt variable to be modified.
  430. * \param v the desired value to add.
  431. * \returns the previous value of the atomic variable.
  432. *
  433. * \threadsafety It is safe to call this function from any thread.
  434. *
  435. * \since This function is available since SDL 3.2.0.
  436. *
  437. * \sa SDL_AtomicDecRef
  438. * \sa SDL_AtomicIncRef
  439. */
  440. extern SDL_DECLSPEC int SDLCALL SDL_AddAtomicInt(SDL_AtomicInt *a, int v);
  441. #ifndef SDL_AtomicIncRef
  442. /**
  443. * Increment an atomic variable used as a reference count.
  444. *
  445. * ***Note: If you don't know what this macro is for, you shouldn't use it!***
  446. *
  447. * \param a a pointer to an SDL_AtomicInt to increment.
  448. * \returns the previous value of the atomic variable.
  449. *
  450. * \threadsafety It is safe to call this macro from any thread.
  451. *
  452. * \since This macro is available since SDL 3.2.0.
  453. *
  454. * \sa SDL_AtomicDecRef
  455. */
  456. #define SDL_AtomicIncRef(a) SDL_AddAtomicInt(a, 1)
  457. #endif
  458. #ifndef SDL_AtomicDecRef
  459. /**
  460. * Decrement an atomic variable used as a reference count.
  461. *
  462. * ***Note: If you don't know what this macro is for, you shouldn't use it!***
  463. *
  464. * \param a a pointer to an SDL_AtomicInt to decrement.
  465. * \returns true if the variable reached zero after decrementing, false
  466. * otherwise.
  467. *
  468. * \threadsafety It is safe to call this macro from any thread.
  469. *
  470. * \since This macro is available since SDL 3.2.0.
  471. *
  472. * \sa SDL_AtomicIncRef
  473. */
  474. #define SDL_AtomicDecRef(a) (SDL_AddAtomicInt(a, -1) == 1)
  475. #endif
  476. /**
  477. * A type representing an atomic unsigned 32-bit value.
  478. *
  479. * This can be used to manage a value that is synchronized across multiple
  480. * CPUs without a race condition; when an app sets a value with
  481. * SDL_SetAtomicU32 all other threads, regardless of the CPU it is running on,
  482. * will see that value when retrieved with SDL_GetAtomicU32, regardless of CPU
  483. * caches, etc.
  484. *
  485. * This is also useful for atomic compare-and-swap operations: a thread can
  486. * change the value as long as its current value matches expectations. When
  487. * done in a loop, one can guarantee data consistency across threads without a
  488. * lock (but the usual warnings apply: if you don't know what you're doing, or
  489. * you don't do it carefully, you can confidently cause any number of
  490. * disasters with this, so in most cases, you _should_ use a mutex instead of
  491. * this!).
  492. *
  493. * This is a struct so people don't accidentally use numeric operations on it
  494. * directly. You have to use SDL atomic functions.
  495. *
  496. * \since This struct is available since SDL 3.2.0.
  497. *
  498. * \sa SDL_CompareAndSwapAtomicU32
  499. * \sa SDL_GetAtomicU32
  500. * \sa SDL_SetAtomicU32
  501. */
  502. typedef struct SDL_AtomicU32 { Uint32 value; } SDL_AtomicU32;
  503. /**
  504. * Set an atomic variable to a new value if it is currently an old value.
  505. *
  506. * ***Note: If you don't know what this function is for, you shouldn't use
  507. * it!***
  508. *
  509. * \param a a pointer to an SDL_AtomicU32 variable to be modified.
  510. * \param oldval the old value.
  511. * \param newval the new value.
  512. * \returns true if the atomic variable was set, false otherwise.
  513. *
  514. * \threadsafety It is safe to call this function from any thread.
  515. *
  516. * \since This function is available since SDL 3.2.0.
  517. *
  518. * \sa SDL_GetAtomicU32
  519. * \sa SDL_SetAtomicU32
  520. */
  521. extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapAtomicU32(SDL_AtomicU32 *a, Uint32 oldval, Uint32 newval);
  522. /**
  523. * Set an atomic variable to a value.
  524. *
  525. * This function also acts as a full memory barrier.
  526. *
  527. * ***Note: If you don't know what this function is for, you shouldn't use
  528. * it!***
  529. *
  530. * \param a a pointer to an SDL_AtomicU32 variable to be modified.
  531. * \param v the desired value.
  532. * \returns the previous value of the atomic variable.
  533. *
  534. * \threadsafety It is safe to call this function from any thread.
  535. *
  536. * \since This function is available since SDL 3.2.0.
  537. *
  538. * \sa SDL_GetAtomicU32
  539. */
  540. extern SDL_DECLSPEC Uint32 SDLCALL SDL_SetAtomicU32(SDL_AtomicU32 *a, Uint32 v);
  541. /**
  542. * Get the value of an atomic variable.
  543. *
  544. * ***Note: If you don't know what this function is for, you shouldn't use
  545. * it!***
  546. *
  547. * \param a a pointer to an SDL_AtomicU32 variable.
  548. * \returns the current value of an atomic variable.
  549. *
  550. * \threadsafety It is safe to call this function from any thread.
  551. *
  552. * \since This function is available since SDL 3.2.0.
  553. *
  554. * \sa SDL_SetAtomicU32
  555. */
  556. extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetAtomicU32(SDL_AtomicU32 *a);
  557. /**
  558. * Set a pointer to a new value if it is currently an old value.
  559. *
  560. * ***Note: If you don't know what this function is for, you shouldn't use
  561. * it!***
  562. *
  563. * \param a a pointer to a pointer.
  564. * \param oldval the old pointer value.
  565. * \param newval the new pointer value.
  566. * \returns true if the pointer was set, false otherwise.
  567. *
  568. * \threadsafety It is safe to call this function from any thread.
  569. *
  570. * \since This function is available since SDL 3.2.0.
  571. *
  572. * \sa SDL_CompareAndSwapAtomicInt
  573. * \sa SDL_GetAtomicPointer
  574. * \sa SDL_SetAtomicPointer
  575. */
  576. extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapAtomicPointer(void **a, void *oldval, void *newval);
  577. /**
  578. * Set a pointer to a value atomically.
  579. *
  580. * ***Note: If you don't know what this function is for, you shouldn't use
  581. * it!***
  582. *
  583. * \param a a pointer to a pointer.
  584. * \param v the desired pointer value.
  585. * \returns the previous value of the pointer.
  586. *
  587. * \threadsafety It is safe to call this function from any thread.
  588. *
  589. * \since This function is available since SDL 3.2.0.
  590. *
  591. * \sa SDL_CompareAndSwapAtomicPointer
  592. * \sa SDL_GetAtomicPointer
  593. */
  594. extern SDL_DECLSPEC void * SDLCALL SDL_SetAtomicPointer(void **a, void *v);
  595. /**
  596. * Get the value of a pointer atomically.
  597. *
  598. * ***Note: If you don't know what this function is for, you shouldn't use
  599. * it!***
  600. *
  601. * \param a a pointer to a pointer.
  602. * \returns the current value of a pointer.
  603. *
  604. * \threadsafety It is safe to call this function from any thread.
  605. *
  606. * \since This function is available since SDL 3.2.0.
  607. *
  608. * \sa SDL_CompareAndSwapAtomicPointer
  609. * \sa SDL_SetAtomicPointer
  610. */
  611. extern SDL_DECLSPEC void * SDLCALL SDL_GetAtomicPointer(void **a);
  612. /* Ends C function definitions when using C++ */
  613. #ifdef __cplusplus
  614. }
  615. #endif
  616. #include <SDL3/SDL_close_code.h>
  617. #endif /* SDL_atomic_h_ */