SDL_atomic.h 22 KB

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