1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
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 "SDL_stdinc.h"
54#include "SDL_platform.h"
55
56#include "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 * \name SDL AtomicLock
65 *
66 * The atomic locks are efficient spinlocks using CPU instructions,
67 * but are vulnerable to starvation and can spin forever if a thread
68 * holding a lock has been terminated. For this reason you should
69 * minimize the code executed inside an atomic lock and never do
70 * expensive things like API or system calls while holding them.
71 *
72 * The atomic locks are not safe to lock recursively.
73 *
74 * Porting Note:
75 * The spin lock functions and type are required and can not be
76 * emulated because they are used in the atomic emulation code.
77 */
78/* @{ */
79
80typedef int SDL_SpinLock;
81
82/**
83 * Try to lock a spin lock by setting it to a non-zero value.
84 *
85 * ***Please note that spinlocks are dangerous if you don't know what you're
86 * doing. Please be careful using any sort of spinlock!***
87 *
88 * \param lock a pointer to a lock variable.
89 * \returns SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already
90 * held.
91 *
92 * \since This function is available since SDL 2.0.0.
93 *
94 * \sa SDL_AtomicLock
95 * \sa SDL_AtomicUnlock
96 */
97extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock);
98
99/**
100 * Lock a spin lock by setting it to a non-zero value.
101 *
102 * ***Please note that spinlocks are dangerous if you don't know what you're
103 * doing. Please be careful using any sort of spinlock!***
104 *
105 * \param lock a pointer to a lock variable.
106 *
107 * \since This function is available since SDL 2.0.0.
108 *
109 * \sa SDL_AtomicTryLock
110 * \sa SDL_AtomicUnlock
111 */
112extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock);
113
114/**
115 * Unlock a spin lock by setting it to 0.
116 *
117 * Always returns immediately.
118 *
119 * ***Please note that spinlocks are dangerous if you don't know what you're
120 * doing. Please be careful using any sort of spinlock!***
121 *
122 * \param lock a pointer to a lock variable.
123 *
124 * \since This function is available since SDL 2.0.0.
125 *
126 * \sa SDL_AtomicLock
127 * \sa SDL_AtomicTryLock
128 */
129extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock);
130
131/* @} *//* SDL AtomicLock */
132
133
134/**
135 * The compiler barrier prevents the compiler from reordering
136 * reads and writes to globally visible variables across the call.
137 */
138#if _SDL_HAS_BUILTIN(__atomic_signal_fence) || (defined(__GNUC__) && (__GNUC__ >= 5))
139#define SDL_CompilerBarrier() __atomic_signal_fence(__ATOMIC_SEQ_CST)
140#elif defined(_MSC_VER) && (_MSC_VER > 1200) && !defined(__clang__)
141void _ReadWriteBarrier(void);
142#pragma intrinsic(_ReadWriteBarrier)
143#define SDL_CompilerBarrier() _ReadWriteBarrier()
144#elif (defined(__GNUC__) && !defined(__EMSCRIPTEN__)) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x5120))
145/* This is correct for all CPUs when using GCC or Solaris Studio 12.1+. */
146#define SDL_CompilerBarrier() __asm__ __volatile__ ("" : : : "memory")
147#elif defined(__WATCOMC__)
148extern __inline void SDL_CompilerBarrier(void);
149#pragma aux SDL_CompilerBarrier = "" parm [] modify exact [];
150#else
151/* We don't unlock here to avoid possible infinite recursion */
152#define SDL_CompilerBarrier() \
153{ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); }
154#endif
155
156/**
157 * Memory barriers are designed to prevent reads and writes from being
158 * reordered by the compiler and being seen out of order on multi-core CPUs.
159 *
160 * A typical pattern would be for thread A to write some data and a flag, and
161 * for thread B to read the flag and get the data. In this case you would
162 * insert a release barrier between writing the data and the flag,
163 * guaranteeing that the data write completes no later than the flag is
164 * written, and you would insert an acquire barrier between reading the flag
165 * and reading the data, to ensure that all the reads associated with the flag
166 * have completed.
167 *
168 * In this pattern you should always see a release barrier paired with an
169 * acquire barrier and you should gate the data reads/writes with a single
170 * flag variable.
171 *
172 * For more information on these semantics, take a look at the blog post:
173 * http://preshing.com/20120913/acquire-and-release-semantics
174 *
175 * \since This function is available since SDL 2.0.6.
176 */
177extern DECLSPEC void SDLCALL SDL_MemoryBarrierReleaseFunction(void);
178extern DECLSPEC void SDLCALL SDL_MemoryBarrierAcquireFunction(void);
179
180#if _SDL_HAS_BUILTIN(__atomic_thread_fence) || (defined(__GNUC__) && (__GNUC__ >= 5))
181#define SDL_MemoryBarrierRelease() __atomic_thread_fence(__ATOMIC_RELEASE)
182#define SDL_MemoryBarrierAcquire() __atomic_thread_fence(__ATOMIC_ACQUIRE)
183#elif defined(__GNUC__) && (defined(__powerpc__) || defined(__ppc__))
184#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("lwsync" : : : "memory")
185#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("lwsync" : : : "memory")
186#elif defined(__GNUC__) && defined(__aarch64__)
187#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("dmb ish" : : : "memory")
188#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("dmb ishld" : : : "memory")
189#elif defined(_MSC_VER) && (defined(_M_ARM64) || defined(_M_ARM64EC))
190#include <arm64intr.h>
191#define SDL_MemoryBarrierRelease() __dmb(_ARM64_BARRIER_ISH)
192#define SDL_MemoryBarrierAcquire() __dmb(_ARM64_BARRIER_ISHLD)
193#elif defined(_MSC_VER) && defined(_M_ARM)
194#include <armintr.h>
195#define SDL_MemoryBarrierRelease() __dmb(_ARM_BARRIER_ISH)
196#define SDL_MemoryBarrierAcquire() __dmb(_ARM_BARRIER_ISH)
197#elif defined(__GNUC__) && defined(__arm__)
198#if 0 /* defined(__LINUX__) || defined(__ANDROID__) */
199/* Information from:
200 https://chromium.googlesource.com/chromium/chromium/+/trunk/base/atomicops_internals_arm_gcc.h#19
201
202 The Linux kernel provides a helper function which provides the right code for a memory barrier,
203 hard-coded at address 0xffff0fa0
204*/
205typedef void (*SDL_KernelMemoryBarrierFunc)();
206#define SDL_MemoryBarrierRelease() ((SDL_KernelMemoryBarrierFunc)0xffff0fa0)()
207#define SDL_MemoryBarrierAcquire() ((SDL_KernelMemoryBarrierFunc)0xffff0fa0)()
208#elif 0 /* defined(__QNXNTO__) */
209#include <sys/cpuinline.h>
210
211#define SDL_MemoryBarrierRelease() __cpu_membarrier()
212#define SDL_MemoryBarrierAcquire() __cpu_membarrier()
213#else
214#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__)
215#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("dmb ish" : : : "memory")
216#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("dmb ish" : : : "memory")
217#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__)
218#ifdef __thumb__
219/* The mcr instruction isn't available in thumb mode, use real functions */
220#define SDL_MEMORY_BARRIER_USES_FUNCTION
221#define SDL_MemoryBarrierRelease() SDL_MemoryBarrierReleaseFunction()
222#define SDL_MemoryBarrierAcquire() SDL_MemoryBarrierAcquireFunction()
223#else
224#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
225#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 5" : : "r"(0) : "memory")
226#endif /* __thumb__ */
227#else
228#define SDL_MemoryBarrierRelease() __asm__ __volatile__ ("" : : : "memory")
229#define SDL_MemoryBarrierAcquire() __asm__ __volatile__ ("" : : : "memory")
230#endif /* __LINUX__ || __ANDROID__ */
231#endif /* __GNUC__ && __arm__ */
232#else
233#if (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x5120))
234/* This is correct for all CPUs on Solaris when using Solaris Studio 12.1+. */
235#include <mbarrier.h>
236#define SDL_MemoryBarrierRelease() __machine_rel_barrier()
237#define SDL_MemoryBarrierAcquire() __machine_acq_barrier()
238#else
239/* This is correct for the x86 and x64 CPUs, and we'll expand this over time. */
240#define SDL_MemoryBarrierRelease() SDL_CompilerBarrier()
241#define SDL_MemoryBarrierAcquire() SDL_CompilerBarrier()
242#endif
243#endif
244
245/* "REP NOP" is PAUSE, coded for tools that don't know it by that name. */
246#if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
247 #define SDL_CPUPauseInstruction() __asm__ __volatile__("pause\n") /* Some assemblers can't do REP NOP, so go with PAUSE. */
248#elif (defined(__arm__) && defined(__ARM_ARCH) && __ARM_ARCH >= 7) || defined(__aarch64__)
249 #define SDL_CPUPauseInstruction() __asm__ __volatile__("yield" ::: "memory")
250#elif (defined(__powerpc__) || defined(__powerpc64__))
251 #define SDL_CPUPauseInstruction() __asm__ __volatile__("or 27,27,27");
252#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
253 #define SDL_CPUPauseInstruction() _mm_pause() /* this is actually "rep nop" and not a SIMD instruction. No inline asm in MSVC x86-64! */
254#elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
255 #define SDL_CPUPauseInstruction() __yield()
256#elif defined(__WATCOMC__) && defined(__386__)
257 extern __inline void SDL_CPUPauseInstruction(void);
258 #pragma aux SDL_CPUPauseInstruction = ".686p" ".xmm2" "pause"
259#else
260 #define SDL_CPUPauseInstruction()
261#endif
262
263
264/**
265 * A type representing an atomic integer value.
266 *
267 * It is a struct so people don't accidentally use numeric operations on it.
268 */
269typedef struct SDL_atomic_t {
270 int value;
271} SDL_atomic_t;
272
273/**
274 * Set an atomic variable to a new value if it is currently an old value.
275 *
276 * ***Note: If you don't know what this function is for, you shouldn't use
277 * it!***
278 *
279 * \param a a pointer to an SDL_atomic_t variable to be modified.
280 * \param oldval the old value.
281 * \param newval the new value.
282 * \returns SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.
283 *
284 * \since This function is available since SDL 2.0.0.
285 *
286 * \sa SDL_AtomicCASPtr
287 * \sa SDL_AtomicGet
288 * \sa SDL_AtomicSet
289 */
290extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval);
291
292/**
293 * Set an atomic variable to a value.
294 *
295 * This function also acts as a full memory barrier.
296 *
297 * ***Note: If you don't know what this function is for, you shouldn't use
298 * it!***
299 *
300 * \param a a pointer to an SDL_atomic_t variable to be modified.
301 * \param v the desired value.
302 * \returns the previous value of the atomic variable.
303 *
304 * \since This function is available since SDL 2.0.2.
305 *
306 * \sa SDL_AtomicGet
307 */
308extern DECLSPEC int SDLCALL SDL_AtomicSet(SDL_atomic_t *a, int v);
309
310/**
311 * Get the value of an atomic variable.
312 *
313 * ***Note: If you don't know what this function is for, you shouldn't use
314 * it!***
315 *
316 * \param a a pointer to an SDL_atomic_t variable.
317 * \returns the current value of an atomic variable.
318 *
319 * \since This function is available since SDL 2.0.2.
320 *
321 * \sa SDL_AtomicSet
322 */
323extern DECLSPEC int SDLCALL SDL_AtomicGet(SDL_atomic_t *a);
324
325/**
326 * Add to an atomic variable.
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_atomic_t variable to be modified.
334 * \param v the desired value to add.
335 * \returns the previous value of the atomic variable.
336 *
337 * \since This function is available since SDL 2.0.2.
338 *
339 * \sa SDL_AtomicDecRef
340 * \sa SDL_AtomicIncRef
341 */
342extern DECLSPEC int SDLCALL SDL_AtomicAdd(SDL_atomic_t *a, int v);
343
344/**
345 * \brief Increment an atomic variable used as a reference count.
346 */
347#ifndef SDL_AtomicIncRef
348#define SDL_AtomicIncRef(a) SDL_AtomicAdd(a, 1)
349#endif
350
351/**
352 * \brief Decrement an atomic variable used as a reference count.
353 *
354 * \return SDL_TRUE if the variable reached zero after decrementing,
355 * SDL_FALSE otherwise
356 */
357#ifndef SDL_AtomicDecRef
358#define SDL_AtomicDecRef(a) (SDL_AtomicAdd(a, -1) == 1)
359#endif
360
361/**
362 * Set a pointer to a new value if it is currently an old value.
363 *
364 * ***Note: If you don't know what this function is for, you shouldn't use
365 * it!***
366 *
367 * \param a a pointer to a pointer.
368 * \param oldval the old pointer value.
369 * \param newval the new pointer value.
370 * \returns SDL_TRUE if the pointer was set, SDL_FALSE otherwise.
371 *
372 * \since This function is available since SDL 2.0.0.
373 *
374 * \sa SDL_AtomicCAS
375 * \sa SDL_AtomicGetPtr
376 * \sa SDL_AtomicSetPtr
377 */
378extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval);
379
380/**
381 * Set a pointer to a value atomically.
382 *
383 * ***Note: If you don't know what this function is for, you shouldn't use
384 * it!***
385 *
386 * \param a a pointer to a pointer.
387 * \param v the desired pointer value.
388 * \returns the previous value of the pointer.
389 *
390 * \since This function is available since SDL 2.0.2.
391 *
392 * \sa SDL_AtomicCASPtr
393 * \sa SDL_AtomicGetPtr
394 */
395extern DECLSPEC void* SDLCALL SDL_AtomicSetPtr(void **a, void* v);
396
397/**
398 * Get the value of a pointer atomically.
399 *
400 * ***Note: If you don't know what this function is for, you shouldn't use
401 * it!***
402 *
403 * \param a a pointer to a pointer.
404 * \returns the current value of a pointer.
405 *
406 * \since This function is available since SDL 2.0.2.
407 *
408 * \sa SDL_AtomicCASPtr
409 * \sa SDL_AtomicSetPtr
410 */
411extern DECLSPEC void* SDLCALL SDL_AtomicGetPtr(void **a);
412
413/* Ends C function definitions when using C++ */
414#ifdef __cplusplus
415}
416#endif
417
418#include "close_code.h"
419
420#endif /* SDL_atomic_h_ */
421
422/* vi: set ts=4 sw=4 expandtab: */
423