SDL_atomic.h 21 KB

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