SDL_atomic.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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 SDL_HAS_BUILTIN(__atomic_signal_fence) || (defined(__GNUC__) && (__GNUC__ >= 5))
  144. #define SDL_CompilerBarrier() __atomic_signal_fence(__ATOMIC_SEQ_CST)
  145. #elif defined(_MSC_VER) && (_MSC_VER > 1200) && !defined(__clang__)
  146. void _ReadWriteBarrier(void);
  147. #pragma intrinsic(_ReadWriteBarrier)
  148. #define SDL_CompilerBarrier() _ReadWriteBarrier()
  149. #elif (defined(__GNUC__) && !defined(SDL_PLATFORM_EMSCRIPTEN)) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x5120))
  150. /* This is correct for all CPUs when using GCC or Solaris Studio 12.1+. */
  151. #define SDL_CompilerBarrier() __asm__ __volatile__ ("" : : : "memory")
  152. #elif defined(__WATCOMC__)
  153. extern __inline void SDL_CompilerBarrier(void);
  154. #pragma aux SDL_CompilerBarrier = "" parm [] modify exact [];
  155. #else
  156. #define SDL_CompilerBarrier() \
  157. { SDL_SpinLock _tmp = 0; SDL_LockSpinlock(&_tmp); SDL_UnlockSpinlock(&_tmp); }
  158. #endif
  159. /**
  160. * Insert a memory release barrier (function version).
  161. *
  162. * Please refer to SDL_MemoryBarrierRelease for details. This is a function
  163. * version, which might be useful if you need to use this functionality from a
  164. * scripting language, etc. Also, some of the macro versions call this
  165. * function behind the scenes, where more heavy lifting can happen inside of
  166. * SDL. Generally, though, an app written in C/C++/etc should use the macro
  167. * version, as it will be more efficient.
  168. *
  169. * \threadsafety Obviously this function is safe to use from any thread at any
  170. * time, but if you find yourself needing this, you are probably
  171. * dealing with some very sensitive code; be careful!
  172. *
  173. * \since This function is available since SDL 3.2.0.
  174. *
  175. * \sa SDL_MemoryBarrierRelease
  176. */
  177. extern SDL_DECLSPEC void SDLCALL SDL_MemoryBarrierReleaseFunction(void);
  178. /**
  179. * Insert a memory acquire barrier (function version).
  180. *
  181. * Please refer to SDL_MemoryBarrierRelease for details. This is a function
  182. * version, which might be useful if you need to use this functionality from a
  183. * scripting language, etc. Also, some of the macro versions call this
  184. * function behind the scenes, where more heavy lifting can happen inside of
  185. * SDL. Generally, though, an app written in C/C++/etc should use the macro
  186. * version, as it will be more efficient.
  187. *
  188. * \threadsafety Obviously this function is safe to use from any thread at any
  189. * time, but if you find yourself needing this, you are probably
  190. * dealing with some very sensitive code; be careful!
  191. *
  192. * \since This function is available since SDL 3.2.0.
  193. *
  194. * \sa SDL_MemoryBarrierAcquire
  195. */
  196. extern SDL_DECLSPEC void SDLCALL SDL_MemoryBarrierAcquireFunction(void);
  197. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  198. /**
  199. * Insert a memory release barrier (macro version).
  200. *
  201. * Memory barriers are designed to prevent reads and writes from being
  202. * reordered by the compiler and being seen out of order on multi-core CPUs.
  203. *
  204. * A typical pattern would be for thread A to write some data and a flag, and
  205. * for thread B to read the flag and get the data. In this case you would
  206. * insert a release barrier between writing the data and the flag,
  207. * guaranteeing that the data write completes no later than the flag is
  208. * written, and you would insert an acquire barrier between reading the flag
  209. * and reading the data, to ensure that all the reads associated with the flag
  210. * have completed.
  211. *
  212. * In this pattern you should always see a release barrier paired with an
  213. * acquire barrier and you should gate the data reads/writes with a single
  214. * flag variable.
  215. *
  216. * For more information on these semantics, take a look at the blog post:
  217. * http://preshing.com/20120913/acquire-and-release-semantics
  218. *
  219. * This is the macro version of this functionality; if possible, SDL will use
  220. * compiler intrinsics or inline assembly, but some platforms might need to
  221. * call the function version of this, SDL_MemoryBarrierReleaseFunction to do
  222. * the heavy lifting. Apps that can use the macro should favor it over the
  223. * function.
  224. *
  225. * \threadsafety Obviously this macro is safe to use from any thread at any
  226. * time, but if you find yourself needing this, you are probably
  227. * dealing with some very sensitive code; be careful!
  228. *
  229. * \since This macro is available since SDL 3.2.0.
  230. *
  231. * \sa SDL_MemoryBarrierAcquire
  232. * \sa SDL_MemoryBarrierReleaseFunction
  233. */
  234. #define SDL_MemoryBarrierRelease() SDL_MemoryBarrierReleaseFunction()
  235. /**
  236. * Insert a memory acquire barrier (macro version).
  237. *
  238. * Please see SDL_MemoryBarrierRelease for the details on what memory barriers
  239. * are and when to use them.
  240. *
  241. * This is the macro version of this functionality; if possible, SDL will use
  242. * compiler intrinsics or inline assembly, but some platforms might need to
  243. * call the function version of this, SDL_MemoryBarrierAcquireFunction, to do
  244. * the heavy lifting. Apps that can use the macro should favor it over the
  245. * function.
  246. *
  247. * \threadsafety Obviously this macro is safe to use from any thread at any
  248. * time, but if you find yourself needing this, you are probably
  249. * dealing with some very sensitive code; be careful!
  250. *
  251. * \since This macro is available since SDL 3.2.0.
  252. *
  253. * \sa SDL_MemoryBarrierRelease
  254. * \sa SDL_MemoryBarrierAcquireFunction
  255. */
  256. #define SDL_MemoryBarrierAcquire() SDL_MemoryBarrierAcquireFunction()
  257. #elif SDL_HAS_BUILTIN(__atomic_thread_fence) || (defined(__GNUC__) && (__GNUC__ >= 5))
  258. #define SDL_MemoryBarrierRelease() __atomic_thread_fence(__ATOMIC_RELEASE)
  259. #define SDL_MemoryBarrierAcquire() __atomic_thread_fence(__ATOMIC_ACQUIRE)
  260. #elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
  261. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("lwsync" : : : "memory")
  262. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("lwsync" : : : "memory")
  263. #elif defined(__GNUC__) && defined(__aarch64__)
  264. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("dmb ish" : : : "memory")
  265. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("dmb ishld" : : : "memory")
  266. #elif defined(_MSC_VER) && (defined(_M_ARM64) || defined(_M_ARM64EC))
  267. #include <arm64intr.h>
  268. #define SDL_MemoryBarrierRelease() __dmb(_ARM64_BARRIER_ISH)
  269. #define SDL_MemoryBarrierAcquire() __dmb(_ARM64_BARRIER_ISHLD)
  270. #elif defined(__GNUC__) && defined(__arm__)
  271. #if 0 /* defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID) */
  272. /* Information from:
  273. https://chromium.googlesource.com/chromium/chromium/+/trunk/base/atomicops_internals_arm_gcc.h#19
  274. The Linux kernel provides a helper function which provides the right code for a memory barrier,
  275. hard-coded at address 0xffff0fa0
  276. */
  277. typedef void (*SDL_KernelMemoryBarrierFunc)();
  278. #define SDL_MemoryBarrierRelease() ((SDL_KernelMemoryBarrierFunc)0xffff0fa0)()
  279. #define SDL_MemoryBarrierAcquire() ((SDL_KernelMemoryBarrierFunc)0xffff0fa0)()
  280. #else
  281. #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__)
  282. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("dmb ish" : : : "memory")
  283. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("dmb ish" : : : "memory")
  284. #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__)
  285. #ifdef __thumb__
  286. /* The mcr instruction isn't available in thumb mode, use real functions */
  287. #define SDL_MEMORY_BARRIER_USES_FUNCTION
  288. #define SDL_MemoryBarrierRelease() SDL_MemoryBarrierReleaseFunction()
  289. #define SDL_MemoryBarrierAcquire() SDL_MemoryBarrierAcquireFunction()
  290. #else
  291. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
  292. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
  293. #endif /* __thumb__ */
  294. #else
  295. #define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("" : : : "memory")
  296. #define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("" : : : "memory")
  297. #endif /* SDL_PLATFORM_LINUX || SDL_PLATFORM_ANDROID */
  298. #endif /* __GNUC__ && __arm__ */
  299. #else
  300. #if (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x5120))
  301. /* This is correct for all CPUs on Solaris when using Solaris Studio 12.1+. */
  302. #include <mbarrier.h>
  303. #define SDL_MemoryBarrierRelease() __machine_rel_barrier()
  304. #define SDL_MemoryBarrierAcquire() __machine_acq_barrier()
  305. #else
  306. /* This is correct for the x86 and x64 CPUs, and we'll expand this over time. */
  307. #define SDL_MemoryBarrierRelease() SDL_CompilerBarrier()
  308. #define SDL_MemoryBarrierAcquire() SDL_CompilerBarrier()
  309. #endif
  310. #endif
  311. /* "REP NOP" is PAUSE, coded for tools that don't know it by that name. */
  312. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  313. /**
  314. * A macro to insert a CPU-specific "pause" instruction into the program.
  315. *
  316. * This can be useful in busy-wait loops, as it serves as a hint to the CPU as
  317. * to the program's intent; some CPUs can use this to do more efficient
  318. * processing. On some platforms, this doesn't do anything, so using this
  319. * macro might just be a harmless no-op.
  320. *
  321. * Note that if you are busy-waiting, there are often more-efficient
  322. * approaches with other synchronization primitives: mutexes, semaphores,
  323. * condition variables, etc.
  324. *
  325. * \threadsafety This macro is safe to use from any thread.
  326. *
  327. * \since This macro is available since SDL 3.2.0.
  328. */
  329. #define SDL_CPUPauseInstruction() DoACPUPauseInACompilerAndArchitectureSpecificWay
  330. #elif (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
  331. #define SDL_CPUPauseInstruction() __asm__ __volatile__("pause\n") /* Some assemblers can't do REP NOP, so go with PAUSE. */
  332. #elif (defined(__arm__) && defined(__ARM_ARCH) && __ARM_ARCH >= 7) || defined(__aarch64__)
  333. #define SDL_CPUPauseInstruction() __asm__ __volatile__("yield" ::: "memory")
  334. #elif (defined(__powerpc__) || defined(__powerpc64__))
  335. #define SDL_CPUPauseInstruction() __asm__ __volatile__("or 27,27,27");
  336. #elif (defined(__riscv) && __riscv_xlen == 64)
  337. #define SDL_CPUPauseInstruction() __asm__ __volatile__(".insn i 0x0F, 0, x0, x0, 0x010");
  338. #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
  339. #define SDL_CPUPauseInstruction() _mm_pause() /* this is actually "rep nop" and not a SIMD instruction. No inline asm in MSVC x86-64! */
  340. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  341. #define SDL_CPUPauseInstruction() __yield()
  342. #elif defined(__WATCOMC__) && defined(__386__)
  343. extern __inline void SDL_CPUPauseInstruction(void);
  344. #pragma aux SDL_CPUPauseInstruction = ".686p" ".xmm2" "pause"
  345. #else
  346. #define SDL_CPUPauseInstruction()
  347. #endif
  348. /**
  349. * A type representing an atomic integer value.
  350. *
  351. * This can be used to manage a value that is synchronized across multiple
  352. * CPUs without a race condition; when an app sets a value with
  353. * SDL_SetAtomicInt all other threads, regardless of the CPU it is running on,
  354. * will see that value when retrieved with SDL_GetAtomicInt, regardless of CPU
  355. * caches, etc.
  356. *
  357. * This is also useful for atomic compare-and-swap operations: a thread can
  358. * change the value as long as its current value matches expectations. When
  359. * done in a loop, one can guarantee data consistency across threads without a
  360. * lock (but the usual warnings apply: if you don't know what you're doing, or
  361. * you don't do it carefully, you can confidently cause any number of
  362. * disasters with this, so in most cases, you _should_ use a mutex instead of
  363. * this!).
  364. *
  365. * This is a struct so people don't accidentally use numeric operations on it
  366. * directly. You have to use SDL atomic functions.
  367. *
  368. * \since This struct is available since SDL 3.2.0.
  369. *
  370. * \sa SDL_CompareAndSwapAtomicInt
  371. * \sa SDL_GetAtomicInt
  372. * \sa SDL_SetAtomicInt
  373. * \sa SDL_AddAtomicInt
  374. */
  375. typedef struct SDL_AtomicInt { int value; } SDL_AtomicInt;
  376. /**
  377. * Set an atomic variable to a new value if it is currently an old value.
  378. *
  379. * ***Note: If you don't know what this function is for, you shouldn't use
  380. * it!***
  381. *
  382. * \param a a pointer to an SDL_AtomicInt variable to be modified.
  383. * \param oldval the old value.
  384. * \param newval the new value.
  385. * \returns true if the atomic variable was set, false otherwise.
  386. *
  387. * \threadsafety It is safe to call this function from any thread.
  388. *
  389. * \since This function is available since SDL 3.2.0.
  390. *
  391. * \sa SDL_GetAtomicInt
  392. * \sa SDL_SetAtomicInt
  393. */
  394. extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapAtomicInt(SDL_AtomicInt *a, int oldval, int newval);
  395. /**
  396. * Set an atomic variable to a value.
  397. *
  398. * This function also acts as a full memory barrier.
  399. *
  400. * ***Note: If you don't know what this function is for, you shouldn't use
  401. * it!***
  402. *
  403. * \param a a pointer to an SDL_AtomicInt variable to be modified.
  404. * \param v the desired value.
  405. * \returns the previous value of the atomic variable.
  406. *
  407. * \threadsafety It is safe to call this function from any thread.
  408. *
  409. * \since This function is available since SDL 3.2.0.
  410. *
  411. * \sa SDL_GetAtomicInt
  412. */
  413. extern SDL_DECLSPEC int SDLCALL SDL_SetAtomicInt(SDL_AtomicInt *a, int v);
  414. /**
  415. * Get the value of an atomic variable.
  416. *
  417. * ***Note: If you don't know what this function is for, you shouldn't use
  418. * it!***
  419. *
  420. * \param a a pointer to an SDL_AtomicInt variable.
  421. * \returns the current value of an atomic variable.
  422. *
  423. * \threadsafety It is safe to call this function from any thread.
  424. *
  425. * \since This function is available since SDL 3.2.0.
  426. *
  427. * \sa SDL_SetAtomicInt
  428. */
  429. extern SDL_DECLSPEC int SDLCALL SDL_GetAtomicInt(SDL_AtomicInt *a);
  430. /**
  431. * Add to an atomic variable.
  432. *
  433. * This function also acts as a full memory barrier.
  434. *
  435. * ***Note: If you don't know what this function is for, you shouldn't use
  436. * it!***
  437. *
  438. * \param a a pointer to an SDL_AtomicInt variable to be modified.
  439. * \param v the desired value to add.
  440. * \returns the previous value of the atomic variable.
  441. *
  442. * \threadsafety It is safe to call this function from any thread.
  443. *
  444. * \since This function is available since SDL 3.2.0.
  445. *
  446. * \sa SDL_AtomicDecRef
  447. * \sa SDL_AtomicIncRef
  448. */
  449. extern SDL_DECLSPEC int SDLCALL SDL_AddAtomicInt(SDL_AtomicInt *a, int v);
  450. #ifndef SDL_AtomicIncRef
  451. /**
  452. * Increment an atomic variable used as a reference count.
  453. *
  454. * ***Note: If you don't know what this macro is for, you shouldn't use it!***
  455. *
  456. * \param a a pointer to an SDL_AtomicInt to increment.
  457. * \returns the previous value of the atomic variable.
  458. *
  459. * \threadsafety It is safe to call this macro from any thread.
  460. *
  461. * \since This macro is available since SDL 3.2.0.
  462. *
  463. * \sa SDL_AtomicDecRef
  464. */
  465. #define SDL_AtomicIncRef(a) SDL_AddAtomicInt(a, 1)
  466. #endif
  467. #ifndef SDL_AtomicDecRef
  468. /**
  469. * Decrement an atomic variable used as a reference count.
  470. *
  471. * ***Note: If you don't know what this macro is for, you shouldn't use it!***
  472. *
  473. * \param a a pointer to an SDL_AtomicInt to decrement.
  474. * \returns true if the variable reached zero after decrementing, false
  475. * otherwise.
  476. *
  477. * \threadsafety It is safe to call this macro from any thread.
  478. *
  479. * \since This macro is available since SDL 3.2.0.
  480. *
  481. * \sa SDL_AtomicIncRef
  482. */
  483. #define SDL_AtomicDecRef(a) (SDL_AddAtomicInt(a, -1) == 1)
  484. #endif
  485. /**
  486. * A type representing an atomic unsigned 32-bit value.
  487. *
  488. * This can be used to manage a value that is synchronized across multiple
  489. * CPUs without a race condition; when an app sets a value with
  490. * SDL_SetAtomicU32 all other threads, regardless of the CPU it is running on,
  491. * will see that value when retrieved with SDL_GetAtomicU32, regardless of CPU
  492. * caches, etc.
  493. *
  494. * This is also useful for atomic compare-and-swap operations: a thread can
  495. * change the value as long as its current value matches expectations. When
  496. * done in a loop, one can guarantee data consistency across threads without a
  497. * lock (but the usual warnings apply: if you don't know what you're doing, or
  498. * you don't do it carefully, you can confidently cause any number of
  499. * disasters with this, so in most cases, you _should_ use a mutex instead of
  500. * this!).
  501. *
  502. * This is a struct so people don't accidentally use numeric operations on it
  503. * directly. You have to use SDL atomic functions.
  504. *
  505. * \since This struct is available since SDL 3.2.0.
  506. *
  507. * \sa SDL_CompareAndSwapAtomicU32
  508. * \sa SDL_GetAtomicU32
  509. * \sa SDL_SetAtomicU32
  510. */
  511. typedef struct SDL_AtomicU32 { Uint32 value; } SDL_AtomicU32;
  512. /**
  513. * Set an atomic variable to a new value if it is currently an old value.
  514. *
  515. * ***Note: If you don't know what this function is for, you shouldn't use
  516. * it!***
  517. *
  518. * \param a a pointer to an SDL_AtomicU32 variable to be modified.
  519. * \param oldval the old value.
  520. * \param newval the new value.
  521. * \returns true if the atomic variable was set, false otherwise.
  522. *
  523. * \threadsafety It is safe to call this function from any thread.
  524. *
  525. * \since This function is available since SDL 3.2.0.
  526. *
  527. * \sa SDL_GetAtomicU32
  528. * \sa SDL_SetAtomicU32
  529. */
  530. extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapAtomicU32(SDL_AtomicU32 *a, Uint32 oldval, Uint32 newval);
  531. /**
  532. * Set an atomic variable to a value.
  533. *
  534. * This function also acts as a full memory barrier.
  535. *
  536. * ***Note: If you don't know what this function is for, you shouldn't use
  537. * it!***
  538. *
  539. * \param a a pointer to an SDL_AtomicU32 variable to be modified.
  540. * \param v the desired value.
  541. * \returns the previous value of the atomic variable.
  542. *
  543. * \threadsafety It is safe to call this function from any thread.
  544. *
  545. * \since This function is available since SDL 3.2.0.
  546. *
  547. * \sa SDL_GetAtomicU32
  548. */
  549. extern SDL_DECLSPEC Uint32 SDLCALL SDL_SetAtomicU32(SDL_AtomicU32 *a, Uint32 v);
  550. /**
  551. * Get the value of an atomic variable.
  552. *
  553. * ***Note: If you don't know what this function is for, you shouldn't use
  554. * it!***
  555. *
  556. * \param a a pointer to an SDL_AtomicU32 variable.
  557. * \returns the current value of an atomic variable.
  558. *
  559. * \threadsafety It is safe to call this function from any thread.
  560. *
  561. * \since This function is available since SDL 3.2.0.
  562. *
  563. * \sa SDL_SetAtomicU32
  564. */
  565. extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetAtomicU32(SDL_AtomicU32 *a);
  566. /**
  567. * Add to an atomic variable.
  568. *
  569. * This function also acts as a full memory barrier.
  570. *
  571. * ***Note: If you don't know what this function is for, you shouldn't use
  572. * it!***
  573. *
  574. * \param a a pointer to an SDL_AtomicU32 variable to be modified.
  575. * \param v the desired value to add or subtract.
  576. * \returns the previous value of the atomic variable.
  577. *
  578. * \threadsafety It is safe to call this function from any thread.
  579. *
  580. * \since This function is available since SDL 3.4.0.
  581. */
  582. extern SDL_DECLSPEC Uint32 SDLCALL SDL_AddAtomicU32(SDL_AtomicU32 *a, int v);
  583. /**
  584. * Set a pointer to a new value if it is currently an old value.
  585. *
  586. * ***Note: If you don't know what this function is for, you shouldn't use
  587. * it!***
  588. *
  589. * \param a a pointer to a pointer.
  590. * \param oldval the old pointer value.
  591. * \param newval the new pointer value.
  592. * \returns true if the pointer was set, false otherwise.
  593. *
  594. * \threadsafety It is safe to call this function from any thread.
  595. *
  596. * \since This function is available since SDL 3.2.0.
  597. *
  598. * \sa SDL_CompareAndSwapAtomicInt
  599. * \sa SDL_GetAtomicPointer
  600. * \sa SDL_SetAtomicPointer
  601. */
  602. extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapAtomicPointer(void **a, void *oldval, void *newval);
  603. /**
  604. * Set a pointer to a value atomically.
  605. *
  606. * ***Note: If you don't know what this function is for, you shouldn't use
  607. * it!***
  608. *
  609. * \param a a pointer to a pointer.
  610. * \param v the desired pointer value.
  611. * \returns the previous value of the pointer.
  612. *
  613. * \threadsafety It is safe to call this function from any thread.
  614. *
  615. * \since This function is available since SDL 3.2.0.
  616. *
  617. * \sa SDL_CompareAndSwapAtomicPointer
  618. * \sa SDL_GetAtomicPointer
  619. */
  620. extern SDL_DECLSPEC void * SDLCALL SDL_SetAtomicPointer(void **a, void *v);
  621. /**
  622. * Get the value of a pointer atomically.
  623. *
  624. * ***Note: If you don't know what this function is for, you shouldn't use
  625. * it!***
  626. *
  627. * \param a a pointer to a pointer.
  628. * \returns the current value of a pointer.
  629. *
  630. * \threadsafety It is safe to call this function from any thread.
  631. *
  632. * \since This function is available since SDL 3.2.0.
  633. *
  634. * \sa SDL_CompareAndSwapAtomicPointer
  635. * \sa SDL_SetAtomicPointer
  636. */
  637. extern SDL_DECLSPEC void * SDLCALL SDL_GetAtomicPointer(void **a);
  638. /* Ends C function definitions when using C++ */
  639. #ifdef __cplusplus
  640. }
  641. #endif
  642. #include <SDL3/SDL_close_code.h>
  643. #endif /* SDL_atomic_h_ */