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