SDL_atomic.h 21 KB

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