From 8d223b3037e16f5c7b044d7f19c4e7677db8c211 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 16 Sep 2024 23:21:31 -0700
Subject: [PATCH] Renamed atomic functions to match SDL 3.0 naming convention
This will also allow us to cleanly add atomic operations for other types in the future.
---
build-scripts/SDL_migration.cocci | 39 +++--
docs/README-migration.md | 11 +-
include/SDL3/SDL_atomic.h | 50 +++----
include/SDL3/SDL_oldnames.h | 22 +--
src/SDL_log.c | 2 +-
src/SDL_utils.c | 10 +-
src/atomic/SDL_atomic.c | 24 ++--
src/audio/SDL_audio.c | 58 ++++----
src/audio/aaudio/SDL_aaudio.c | 20 +--
src/audio/alsa/SDL_alsa_audio.c | 12 +-
src/audio/coreaudio/SDL_coreaudio.m | 6 +-
src/audio/directsound/SDL_directsound.c | 4 +-
src/audio/dsp/SDL_dspaudio.c | 2 +-
src/audio/n3ds/SDL_n3dsaudio.c | 2 +-
src/audio/netbsd/SDL_netbsdaudio.c | 2 +-
src/audio/openslES/SDL_openslES.c | 10 +-
src/audio/pulseaudio/SDL_pulseaudio.c | 14 +-
src/audio/qnx/SDL_qsa_audio.c | 4 +-
src/audio/sndio/SDL_sndioaudio.c | 4 +-
src/audio/vita/SDL_vitaaudio.c | 4 +-
src/audio/wasapi/SDL_wasapi.c | 28 ++--
src/camera/SDL_camera.c | 26 ++--
.../SDL_camera_mediafoundation.c | 2 +-
src/camera/v4l2/SDL_camera_v4l2.c | 2 +-
src/core/android/SDL_android.c | 16 +--
src/core/linux/SDL_evdev_kbd.c | 8 +-
src/core/windows/SDL_immdevice.c | 4 +-
src/dynapi/SDL_dynapi.sym | 14 +-
src/dynapi/SDL_dynapi_overrides.h | 14 +-
src/dynapi/SDL_dynapi_procs.h | 14 +-
src/events/SDL_events.c | 22 +--
src/events/SDL_pen.c | 10 +-
src/gpu/d3d11/SDL_gpu_d3d11.c | 28 ++--
src/gpu/d3d12/SDL_gpu_d3d12.c | 36 ++---
src/gpu/metal/SDL_gpu_metal.m | 28 ++--
src/gpu/vulkan/SDL_gpu_vulkan.c | 42 +++---
src/hidapi/android/hid.cpp | 8 +-
src/joystick/SDL_joystick.c | 2 +-
src/joystick/hidapi/SDL_hidapi_rumble.c | 12 +-
src/joystick/hidapi/SDL_hidapi_switch.c | 2 +-
src/joystick/hidapi/SDL_hidapi_wii.c | 2 +-
src/joystick/hidapi/SDL_hidapijoystick.c | 8 +-
src/joystick/windows/SDL_rawinputjoystick.c | 4 +-
.../windows/SDL_windows_gaming_input.c | 4 +-
src/main/SDL_main_callbacks.c | 20 +--
src/sensor/SDL_sensor.c | 2 +-
src/sensor/android/SDL_androidsensor.c | 6 +-
src/stdlib/SDL_malloc.c | 2 +-
src/test/SDL_test_memory.c | 4 +-
src/thread/SDL_thread.c | 20 +--
src/thread/generic/SDL_sysrwlock.c | 28 ++--
src/thread/n3ds/SDL_systhread.c | 2 +-
src/timer/SDL_timer.c | 16 +--
src/video/SDL_video.c | 2 +-
src/video/cocoa/SDL_cocoaopengl.m | 30 ++--
src/video/wayland/SDL_waylandopengles.c | 4 +-
src/video/wayland/SDL_waylandwindow.c | 2 +-
test/testatomic.c | 136 +++++++++---------
test/testaudio.c | 8 +-
test/testgles2.c | 10 +-
test/testlock.c | 10 +-
test/testrwlock.c | 10 +-
test/testthread.c | 10 +-
test/torturethread.c | 12 +-
64 files changed, 497 insertions(+), 473 deletions(-)
diff --git a/build-scripts/SDL_migration.cocci b/build-scripts/SDL_migration.cocci
index 8b4e12d2017f2..41d178c8ae896 100644
--- a/build-scripts/SDL_migration.cocci
+++ b/build-scripts/SDL_migration.cocci
@@ -2907,12 +2907,37 @@ expression e1, e2, e3, e4;
@@
@@
- SDL_AtomicCAS
-+ SDL_AtomicCompareAndSwap
++ SDL_CompareAndSwapAtomicInt
+ (...)
+@@
+@@
+- SDL_AtomicSet
++ SDL_SetAtomicInt
+ (...)
+@@
+@@
+- SDL_AtomicGet
++ SDL_GetAtomicInt
+ (...)
+@@
+@@
+- SDL_AtomicAdd
++ SDL_AddAtomicInt
(...)
@@
@@
- SDL_AtomicCASPtr
-+ SDL_AtomicCompareAndSwapPointer
++ SDL_CompareAndSwapAtomicPointer
+ (...)
+@@
+@@
+- SDL_AtomicSetPtr
++ SDL_SetAtomicPointer
+ (...)
+@@
+@@
+- SDL_AtomicGetPtr
++ SDL_GetAtomicPointer
(...)
@@
@@
@@ -3586,16 +3611,6 @@ typedef SDL_JoystickGUID, SDL_GUID;
+ SDL_HINT_MOUSE_EMULATE_WARP_WITH_RELATIVE
@@
@@
-- SDL_AtomicSetPtr
-+ SDL_AtomicSetPointer
- (...)
-@@
-@@
-- SDL_AtomicGetPtr
-+ SDL_AtomicGetPointer
- (...)
-@@
-@@
- SDL_DelEventWatch
+ SDL_RemoveEventWatch
(...)
diff --git a/docs/README-migration.md b/docs/README-migration.md
index b479011c35b45..ed52ec01c3eb9 100644
--- a/docs/README-migration.md
+++ b/docs/README-migration.md
@@ -86,11 +86,14 @@ The following structures have been renamed:
- SDL_atomic_t => SDL_AtomicInt
The following functions have been renamed:
-* SDL_AtomicCAS() => SDL_AtomicCompareAndSwap()
-* SDL_AtomicCASPtr() => SDL_AtomicCompareAndSwapPointer()
-* SDL_AtomicGetPtr() => SDL_AtomicGetPointer()
+* SDL_AtomicAdd() => SDL_AddAtomicInt()
+* SDL_AtomicCAS() => SDL_CompareAndSwapAtomicInt()
+* SDL_AtomicCASPtr() => SDL_CompareAndSwapAtomicPointer()
+* SDL_AtomicGet() => SDL_GetAtomicInt()
+* SDL_AtomicGetPtr() => SDL_GetAtomicPointer()
* SDL_AtomicLock() => SDL_LockSpinlock()
-* SDL_AtomicSetPtr() => SDL_AtomicSetPointer()
+* SDL_AtomicSet() => SDL_SetAtomicInt()
+* SDL_AtomicSetPtr() => SDL_SetAtomicPointer()
* SDL_AtomicTryLock() => SDL_TryLockSpinlock()
* SDL_AtomicUnlock() => SDL_UnlockSpinlock()
diff --git a/include/SDL3/SDL_atomic.h b/include/SDL3/SDL_atomic.h
index f159bcd27506b..f76bcbb43e7cf 100644
--- a/include/SDL3/SDL_atomic.h
+++ b/include/SDL3/SDL_atomic.h
@@ -303,9 +303,9 @@ typedef void (*SDL_KernelMemoryBarrierFunc)();
* A type representing an atomic integer value.
*
* This can be used to manage a value that is synchronized across multiple
- * CPUs without a race condition; when an app sets a value with SDL_AtomicSet
+ * CPUs without a race condition; when an app sets a value with SDL_SetAtomicInt
* all other threads, regardless of the CPU it is running on, will see that
- * value when retrieved with SDL_AtomicGet, regardless of CPU caches, etc.
+ * value when retrieved with SDL_GetAtomicInt, regardless of CPU caches, etc.
*
* This is also useful for atomic compare-and-swap operations: a thread can
* change the value as long as its current value matches expectations. When
@@ -320,10 +320,10 @@ typedef void (*SDL_KernelMemoryBarrierFunc)();
*
* \since This struct is available since SDL 3.0.0.
*
- * \sa SDL_AtomicCompareAndSwap
- * \sa SDL_AtomicGet
- * \sa SDL_AtomicSet
- * \sa SDL_AtomicAdd
+ * \sa SDL_CompareAndSwapAtomicInt
+ * \sa SDL_GetAtomicInt
+ * \sa SDL_SetAtomicInt
+ * \sa SDL_AddAtomicInt
*/
typedef struct SDL_AtomicInt { int value; } SDL_AtomicInt;
@@ -342,9 +342,9 @@ typedef struct SDL_AtomicInt { int value; } SDL_AtomicInt;
*
* \since This function is available since SDL 3.0.0.
*
- * \sa SDL_AtomicCompareAndSwapPointer
+ * \sa SDL_CompareAndSwapAtomicPointer
*/
-extern SDL_DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareAndSwap(SDL_AtomicInt *a, int oldval, int newval);
+extern SDL_DECLSPEC SDL_bool SDLCALL SDL_CompareAndSwapAtomicInt(SDL_AtomicInt *a, int oldval, int newval);
/**
* Set an atomic variable to a value.
@@ -362,9 +362,9 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareAndSwap(SDL_AtomicInt *a,
*
* \since This function is available since SDL 3.0.0.
*
- * \sa SDL_AtomicGet
+ * \sa SDL_GetAtomicInt
*/
-extern SDL_DECLSPEC int SDLCALL SDL_AtomicSet(SDL_AtomicInt *a, int v);
+extern SDL_DECLSPEC int SDLCALL SDL_SetAtomicInt(SDL_AtomicInt *a, int v);
/**
* Get the value of an atomic variable.
@@ -379,9 +379,9 @@ extern SDL_DECLSPEC int SDLCALL SDL_AtomicSet(SDL_AtomicInt *a, int v);
*
* \since This function is available since SDL 3.0.0.
*
- * \sa SDL_AtomicSet
+ * \sa SDL_SetAtomicInt
*/
-extern SDL_DECLSPEC int SDLCALL SDL_AtomicGet(SDL_AtomicInt *a);
+extern SDL_DECLSPEC int SDLCALL SDL_GetAtomicInt(SDL_AtomicInt *a);
/**
* Add to an atomic variable.
@@ -402,7 +402,7 @@ extern SDL_DECLSPEC int SDLCALL SDL_AtomicGet(SDL_AtomicInt *a);
* \sa SDL_AtomicDecRef
* \sa SDL_AtomicIncRef
*/
-extern SDL_DECLSPEC int SDLCALL SDL_AtomicAdd(SDL_AtomicInt *a, int v);
+extern SDL_DECLSPEC int SDLCALL SDL_AddAtomicInt(SDL_AtomicInt *a, int v);
#ifndef SDL_AtomicIncRef
@@ -418,7 +418,7 @@ extern SDL_DECLSPEC int SDLCALL SDL_AtomicAdd(SDL_AtomicInt *a, int v);
*
* \sa SDL_AtomicDecRef
*/
-#define SDL_AtomicIncRef(a) SDL_AtomicAdd(a, 1)
+#define SDL_AtomicIncRef(a) SDL_AddAtomicInt(a, 1)
#endif
#ifndef SDL_AtomicDecRef
@@ -436,7 +436,7 @@ extern SDL_DECLSPEC int SDLCALL SDL_AtomicAdd(SDL_AtomicInt *a, int v);
*
* \sa SDL_AtomicIncRef
*/
-#define SDL_AtomicDecRef(a) (SDL_AtomicAdd(a, -1) == 1)
+#define SDL_AtomicDecRef(a) (SDL_AddAtomicInt(a, -1) == 1)
#endif
/**
@@ -454,11 +454,11 @@ extern SDL_DECLSPEC int SDLCALL SDL_AtomicAdd(SDL_AtomicInt *a, int v);
*
* \since This function is available since SDL 3.0.0.
*
- * \sa SDL_AtomicCompareAndSwap
- * \sa SDL_AtomicGetPointer
- * \sa SDL_AtomicSetPointer
+ * \sa SDL_CompareAndSwapAtomicInt
+ * \sa SDL_GetAtomicPointer
+ * \sa SDL_SetAtomicPointer
*/
-extern SDL_DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareAndSwapPointer(void **a, void *oldval, void *newval);
+extern SDL_DECLSPEC SDL_bool SDLCALL SDL_CompareAndSwapAtomicPointer(void **a, void *oldval, void *newval);
/**
* Set a pointer to a value atomically.
@@ -474,10 +474,10 @@ extern SDL_DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareAndSwapPointer(void **a, v
*
* \since This function is available since SDL 3.0.0.
*
- * \sa SDL_AtomicCompareAndSwapPointer
- * \sa SDL_AtomicGetPointer
+ * \sa SDL_CompareAndSwapAtomicPointer
+ * \sa SDL_GetAtomicPointer
*/
-extern SDL_DECLSPEC void * SDLCALL SDL_AtomicSetPointer(void **a, void *v);
+extern SDL_DECLSPEC void * SDLCALL SDL_SetAtomicPointer(void **a, void *v);
/**
* Get the value of a pointer atomically.
@@ -492,10 +492,10 @@ extern SDL_DECLSPEC void * SDLCALL SDL_AtomicSetPointer(void **a, void *v);
*
* \since This function is available since SDL 3.0.0.
*
- * \sa SDL_AtomicCompareAndSwapPointer
- * \sa SDL_AtomicSetPointer
+ * \sa SDL_CompareAndSwapAtomicPointer
+ * \sa SDL_SetAtomicPointer
*/
-extern SDL_DECLSPEC void * SDLCALL SDL_AtomicGetPointer(void **a);
+extern SDL_DECLSPEC void * SDLCALL SDL_GetAtomicPointer(void **a);
/* Ends C function definitions when using C++ */
#ifdef __cplusplus
diff --git a/include/SDL3/SDL_oldnames.h b/include/SDL3/SDL_oldnames.h
index 7638922f5733d..3af24cc3ee7db 100644
--- a/include/SDL3/SDL_oldnames.h
+++ b/include/SDL3/SDL_oldnames.h
@@ -38,11 +38,14 @@
#ifdef SDL_ENABLE_OLD_NAMES
/* ##SDL_atomic.h */
-#define SDL_AtomicCAS SDL_AtomicCompareAndSwap
-#define SDL_AtomicCASPtr SDL_AtomicCompareAndSwapPointer
-#define SDL_AtomicGetPtr SDL_AtomicGetPointer
+#define SDL_AtomicAdd SDL_AddAtomicInt
+#define SDL_AtomicCAS SDL_CompareAndSwapAtomicInt
+#define SDL_AtomicCASPtr SDL_CompareAndSwapAtomicPointer
+#define SDL_AtomicGet SDL_GetAtomicInt
+#define SDL_AtomicGetPtr SDL_GetAtomicPointer
#define SDL_AtomicLock SDL_LockSpinlock
-#define SDL_AtomicSetPtr SDL_AtomicSetPointer
+#define SDL_AtomicSet SDL_SetAtomicInt
+#define SDL_AtomicSetPtr SDL_SetAtomicPointer
#define SDL_AtomicTryLock SDL_TryLockSpinlock
#define SDL_AtomicUnlock SDL_UnlockSpinlock
#define SDL_atomic_t SDL_AtomicInt
@@ -668,11 +671,14 @@
#elif !defined(SDL_DISABLE_OLD_NAMES)
/* ##SDL_atomic.h */
-#define SDL_AtomicCAS SDL_AtomicCAS_renamed_SDL_AtomicCompareAndSwap
-#define SDL_AtomicCASPtr SDL_AtomicCASPtr_renamed_SDL_AtomicCompareAndSwapPointer
-#define SDL_AtomicGetPtr SDL_AtomicGetPtr_renamed_SDL_AtomicGetPointer
+#define SDL_AtomicAdd SDL_AtomicAdd_renamed_SDL_AddAtomicInt
+#define SDL_AtomicCAS SDL_AtomicCAS_renamed_SDL_CompareAndSwapAtomicInt
+#define SDL_AtomicCASPtr SDL_AtomicCASPtr_renamed_SDL_CompareAndSwapAtomicPointer
+#define SDL_AtomicGet SDL_AtomicGet_renamed_SDL_GetAtomicInt
+#define SDL_AtomicGetPtr SDL_AtomicGetPtr_renamed_SDL_GetAtomicPointer
#define SDL_AtomicLock SDL_AtomicLock_renamed_SDL_LockSpinlock
-#define SDL_AtomicSetPtr SDL_AtomicSetPtr_renamed_SDL_AtomicSetPointer
+#define SDL_AtomicSet SDL_AtomicSet_renamed_SDL_SetAtomicInt
+#define SDL_AtomicSetPtr SDL_AtomicSetPtr_renamed_SDL_SetAtomicPointer
#define SDL_AtomicTryLock SDL_AtomicTryLock_renamed_SDL_TryLockSpinlock
#define SDL_AtomicUnlock SDL_AtomicUnlock_renamed_SDL_UnlockSpinlock
#define SDL_atomic_t SDL_atomic_t_renamed_SDL_AtomicInt
diff --git a/src/SDL_log.c b/src/SDL_log.c
index aec4d6f00ec1b..5abaf86c7fcf0 100644
--- a/src/SDL_log.c
+++ b/src/SDL_log.c
@@ -165,7 +165,7 @@ void SDL_QuitLog(void)
static void SDL_CheckInitLog(void)
{
- int status = SDL_AtomicGet(&SDL_log_init.status);
+ int status = SDL_GetAtomicInt(&SDL_log_init.status);
if (status == SDL_INIT_STATUS_INITIALIZED ||
(status == SDL_INIT_STATUS_INITIALIZING && SDL_log_init.thread == SDL_GetCurrentThreadID())) {
return;
diff --git a/src/SDL_utils.c b/src/SDL_utils.c
index c120a38eda899..1263fea3b017d 100644
--- a/src/SDL_utils.c
+++ b/src/SDL_utils.c
@@ -123,8 +123,8 @@ bool SDL_endswith(const char *string, const char *suffix)
bool SDL_ShouldInit(SDL_InitState *state)
{
- while (SDL_AtomicGet(&state->status) != SDL_INIT_STATUS_INITIALIZED) {
- if (SDL_AtomicCompareAndSwap(&state->status, SDL_INIT_STATUS_UNINITIALIZED, SDL_INIT_STATUS_INITIALIZING)) {
+ while (SDL_GetAtomicInt(&state->status) != SDL_INIT_STATUS_INITIALIZED) {
+ if (SDL_CompareAndSwapAtomicInt(&state->status, SDL_INIT_STATUS_UNINITIALIZED, SDL_INIT_STATUS_INITIALIZING)) {
state->thread = SDL_GetCurrentThreadID();
return true;
}
@@ -137,7 +137,7 @@ bool SDL_ShouldInit(SDL_InitState *state)
bool SDL_ShouldQuit(SDL_InitState *state)
{
- if (SDL_AtomicCompareAndSwap(&state->status, SDL_INIT_STATUS_INITIALIZED, SDL_INIT_STATUS_UNINITIALIZING)) {
+ if (SDL_CompareAndSwapAtomicInt(&state->status, SDL_INIT_STATUS_INITIALIZED, SDL_INIT_STATUS_UNINITIALIZING)) {
state->thread = SDL_GetCurrentThreadID();
return true;
}
@@ -149,9 +149,9 @@ void SDL_SetInitialized(SDL_InitState *state, bool initialized)
SDL_assert(state->thread == SDL_GetCurrentThreadID());
if (initialized) {
- SDL_AtomicSet(&state->status, SDL_INIT_STATUS_INITIALIZED);
+ SDL_SetAtomicInt(&state->status, SDL_INIT_STATUS_INITIALIZED);
} else {
- SDL_AtomicSet(&state->status, SDL_INIT_STATUS_UNINITIALIZED);
+ SDL_SetAtomicInt(&state->status, SDL_INIT_STATUS_UNINITIALIZED);
}
}
diff --git a/src/atomic/SDL_atomic.c b/src/atomic/SDL_atomic.c
index 2816d6d4ecb58..1eb1a90ca5b50 100644
--- a/src/atomic/SDL_atomic.c
+++ b/src/atomic/SDL_atomic.c
@@ -122,7 +122,7 @@ static SDL_INLINE void leaveLock(void *a)
}
#endif
-SDL_bool SDL_AtomicCompareAndSwap(SDL_AtomicInt *a, int oldval, int newval)
+SDL_bool SDL_CompareAndSwapAtomicInt(SDL_AtomicInt *a, int oldval, int newval)
{
#ifdef HAVE_MSC_ATOMICS
SDL_COMPILE_TIME_ASSERT(atomic_cas, sizeof(long) == sizeof(a->value));
@@ -151,7 +151,7 @@ SDL_bool SDL_AtomicCompareAndSwap(SDL_AtomicInt *a, int oldval, int newval)
#endif
}
-SDL_bool SDL_AtomicCompareAndSwapPointer(void **a, void *oldval, void *newval)
+SDL_bool SDL_CompareAndSwapAtomicPointer(void **a, void *oldval, void *newval)
{
#ifdef HAVE_MSC_ATOMICS
return _InterlockedCompareExchangePointer(a, newval, oldval) == oldval;
@@ -181,7 +181,7 @@ SDL_bool SDL_AtomicCompareAndSwapPointer(void **a, void *oldval, void *newval)
#endif
}
-int SDL_AtomicSet(SDL_AtomicInt *a, int v)
+int SDL_SetAtomicInt(SDL_AtomicInt *a, int v)
{
#ifdef HAVE_MSC_ATOMICS
SDL_COMPILE_TIME_ASSERT(atomic_set, sizeof(long) == sizeof(a->value));
@@ -196,12 +196,12 @@ int SDL_AtomicSet(SDL_AtomicInt *a, int v)
int value;
do {
value = a->value;
- } while (!SDL_AtomicCompareAndSwap(a, value, v));
+ } while (!SDL_CompareAndSwapAtomicInt(a, value, v));
return value;
#endif
}
-void *SDL_AtomicSetPointer(void **a, void *v)
+void *SDL_SetAtomicPointer(void **a, void *v)
{
#ifdef HAVE_MSC_ATOMICS
return _InterlockedExchangePointer(a, v);
@@ -215,12 +215,12 @@ void *SDL_AtomicSetPointer(void **a, void *v)
void *value;
do {
value = *a;
- } while (!SDL_AtomicCompareAndSwapPointer(a, value, v));
+ } while (!SDL_CompareAndSwapAtomicPointer(a, value, v));
return value;
#endif
}
-int SDL_AtomicAdd(SDL_AtomicInt *a, int v)
+int SDL_AddAtomicInt(SDL_AtomicInt *a, int v)
{
#ifdef HAVE_MSC_ATOMICS
SDL_COMPILE_TIME_ASSERT(atomic_add, sizeof(long) == sizeof(a->value));
@@ -238,12 +238,12 @@ int SDL_AtomicAdd(SDL_AtomicInt *a, int v)
int value;
do {
value = a->value;
- } while (!SDL_AtomicCompareAndSwap(a, value, (value + v)));
+ } while (!SDL_CompareAndSwapAtomicInt(a, value, (value + v)));
return value;
#endif
}
-int SDL_AtomicGet(SDL_AtomicInt *a)
+int SDL_GetAtomicInt(SDL_AtomicInt *a)
{
#ifdef HAVE_ATOMIC_LOAD_N
return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
@@ -262,12 +262,12 @@ int SDL_AtomicGet(SDL_AtomicInt *a)
int value;
do {
value = a->value;
- } while (!SDL_AtomicCompareAndSwap(a, value, value));
+ } while (!SDL_CompareAndSwapAtomicInt(a, value, value));
return value;
#endif
}
-void *SDL_AtomicGetPointer(void **a)
+void *SDL_GetAtomicPointer(void **a)
{
#ifdef HAVE_ATOMIC_LOAD_N
return __atomic_load_n(a, __ATOMIC_SEQ_CST);
@@ -281,7 +281,7 @@ void *SDL_AtomicGetPointer(void **a)
void *value;
do {
value = *a;
- } while (!SDL_AtomicCompareAndSwapPointer(a, value, value));
+ } while (!SDL_CompareAndSwapAtomicPointer(a, value, value));
return value;
#endif
}
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 409e74fca0a7d..ba37fcb17b4ef 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -280,7 +280,7 @@ bool SDL_AudioSpecsEqual(const SDL_AudioSpec *a, const SDL_AudioSpec *b, const i
// consumed and apps relying on audio callbacks don't stop making progress.
static bool ZombieWaitDevice(SDL_AudioDevice *device)
{
- if (!SDL_AtomicGet(&device->shutdown)) {
+ if (!SDL_GetAtomicInt(&device->shutdown)) {
const int frames = device->buffer_size / SDL_AUDIO_FRAMESIZE(device->spec);
SDL_Delay((frames * 1000) / device->spec.freq);
}
@@ -390,7 +390,7 @@ static SDL_LogicalAudioDevice *ObtainLogicalAudioDevice(SDL_AudioDeviceID devid,
// to make sure the correct physical device gets locked, in case we're in a race with the default changing.
while (true) {
SDL_LockMutex(device->lock);
- SDL_AudioDevice *recheck_device = (SDL_AudioDevice *) SDL_AtomicGetPointer((void **) &logdev->physical_device);
+ SDL_AudioDevice *recheck_device = (SDL_AudioDevice *) SDL_GetAtomicPointer((void **) &logdev->physical_device);
if (device == recheck_device) {
break;
}
@@ -559,7 +559,7 @@ void UnrefPhysicalAudioDevice(SDL_AudioDevice *device)
// take it out of the device list.
SDL_LockRWLockForWriting(current_audio.device_hash_lock);
if (SDL_RemoveFromHashTable(current_audio.device_hash, (const void *) (uintptr_t) device->instance_id)) {
- SDL_AtomicAdd(device->recording ? ¤t_audio.recording_device_count : ¤t_audio.playback_device_count, -1);
+ SDL_AddAtomicInt(device->recording ? ¤t_audio.recording_device_count : ¤t_audio.playback_device_count, -1);
}
SDL_UnlockRWLock(current_audio.device_hash_lock);
DestroyPhysicalAudioDevice(device); // ...and nuke it.
@@ -576,7 +576,7 @@ static SDL_AudioDevice *CreatePhysicalAudioDevice(const char *name, bool recordi
SDL_assert(name != NULL);
SDL_LockRWLockForReading(current_audio.device_hash_lock);
- const int shutting_down = SDL_AtomicGet(¤t_audio.shutting_down);
+ const int shutting_down = SDL_GetAtomicInt(¤t_audio.shutting_down);
SDL_UnlockRWLock(current_audio.device_hash_lock);
if (shutting_down) {
return NULL; // we're shutting down, don't add any devices that are hotplugged at the last possible moment.
@@ -608,8 +608,8 @@ static SDL_AudioDevice *CreatePhysicalAudioDevice(const char *name, bool recordi
return NULL;
}
- SDL_AtomicSet(&device->shutdown, 0);
- SDL_AtomicSet(&device->zombie, 0);
+ SDL_SetAtomicInt(&device->shutdown, 0);
+ SDL_SetAtomicInt(&device->zombie, 0);
device->recording = recording;
SDL_copyp(&device->spec, spec);
SDL_copyp(&device->default_spec, spec);
@@ -621,7 +621,7 @@ static SDL_AudioDevice *CreatePhysicalAudioDevice(const char *name, bool recordi
SDL_LockRWLockForWriting(current_audio.device_hash_lock);
if (SDL_InsertIntoHashTable(current_audio.device_hash, (const void *) (uintptr_t) device->instance_id, device)) {
- SDL_AtomicAdd(device_count, 1);
+ SDL_AddAtomicInt(device_count, 1);
} else {
SDL_DestroyCondition(device->close_cond);
SDL_DestroyMutex(device->lock);
@@ -711,7 +711,7 @@ void SDL_AudioDeviceDisconnected(SDL_AudioDevice *device)
const bool is_default_device = ((devid == current_audio.default_playback_device_id) || (devid == current_audio.default_recording_device_id));
SDL_UnlockRWLock(current_audio.device_hash_lock);
- const bool first_disconnect = SDL_AtomicCompareAndSwap(&device->zombie, 0, 1);
+ const bool first_disconnect = SDL_CompareAndSwapAtomicInt(&device->zombie, 0, 1);
if (first_disconnect) { // if already disconnected this device, don't do it twice.
// Swap in "Zombie" versions of the usual platform interfaces, so the device will keep
// making progress until the app closes it. Otherwise, streams might continue to
@@ -886,7 +886,7 @@ bool SDL_InitAudio(const char *driver_name)
}
// make sure device IDs start at 2 (because of SDL2 legacy interface), but don't reset the counter on each init, in case the app is holding an old device ID somewhere.
- SDL_AtomicCompareAndSwap(&last_device_instance_id, 0, 2);
+ SDL_CompareAndSwapAtomicInt(&last_device_instance_id, 0, 2);
SDL_ChooseAudioConverters();
SDL_SetupAudioResampler();
@@ -1031,13 +1031,13 @@ void SDL_QuitAudio(void)
}
SDL_LockRWLockForWriting(current_audio.device_hash_lock);
- SDL_AtomicSet(¤t_audio.shutting_down, 1);
+ SDL_SetAtomicInt(¤t_audio.shutting_down, 1);
SDL_HashTable *device_hash = current_audio.device_hash;
current_audio.device_hash = NULL;
SDL_PendingAudioDeviceEvent *pending_events = current_audio.pending_events.next;
current_audio.pending_events.next = NULL;
- SDL_AtomicSet(¤t_audio.playback_device_count, 0);
- SDL_AtomicSet(¤t_audio.recording_device_count, 0);
+ SDL_SetAtomicInt(¤t_audio.playback_device_count, 0);
+ SDL_SetAtomicInt(¤t_audio.recording_device_count, 0);
SDL_UnlockRWLock(current_audio.device_hash_lock);
SDL_PendingAudioDeviceEvent *pending_next = NULL;
@@ -1094,7 +1094,7 @@ bool SDL_PlaybackAudioThreadIterate(SDL_AudioDevice *device)
SDL_LockMutex(device->lock);
- if (SDL_AtomicGet(&device->shutdown)) {
+ if (SDL_GetAtomicInt(&device->shutdown)) {
SDL_UnlockMutex(device->lock);
return false; // we're done, shut it down.
}
@@ -1118,7 +1118,7 @@ bool SDL_PlaybackAudioThreadIterate(SDL_AudioDevice *device)
// We should have updated this elsewhere if the format changed!
SDL_assert(SDL_AudioSpecsEqual(&stream->dst_spec, &device->spec, stream->dst_chmap, device->chmap));
- const int br = SDL_AtomicGet(&logdev->paused) ? 0 : SDL_GetAudioStreamDataAdjustGain(stream, device_buffer, buffer_size, logdev->gain);
+ const int br = SDL_GetAtomicInt(&logdev->paused) ? 0 : SDL_GetAudioStreamDataAdjustGain(stream, device_buffer, buffer_size, logdev->gain);
if (br < 0) { // Probably OOM. Kill the audio device; the whole thing is likely dying soon anyhow.
failed = true;
SDL_memset(device_buffer, device->silence_value, buffer_size); // just supply silence to the device before we die.
@@ -1139,7 +1139,7 @@ bool SDL_PlaybackAudioThreadIterate(SDL_AudioDevice *device)
SDL_memset(final_mix_buffer, '\0', work_buffer_size); // start with silence.
for (SDL_LogicalAudioDevice *logdev = device->logical_devices; logdev; logdev = logdev->next) {
- if (SDL_AtomicGet(&logdev->paused)) {
+ if (SDL_GetAtomicInt(&logdev->paused)) {
continue; // paused? Skip this logical device.
}
@@ -1202,7 +1202,7 @@ void SDL_PlaybackAudioThreadShutdown(SDL_AudioDevice *device)
SDL_assert(!device->recording);
const int frames = device->buffer_size / SDL_AUDIO_FRAMESIZE(device->spec);
// Wait for the audio to drain if device didn't die.
- if (!SDL_AtomicGet(&device->zombie)) {
+ if (!SDL_GetAtomicInt(&device->zombie)) {
SDL_Delay(((frames * 1000) / device->spec.freq) * 2);
}
current_audio.impl.ThreadDeinit(device);
@@ -1242,7 +1242,7 @@ bool SDL_RecordingAudioThreadIterate(SDL_AudioDevice *device)
SDL_LockMutex(device->lock);
- if (SDL_AtomicGet(&device->shutdown)) {
+ if (SDL_GetAtomicInt(&device->shutdown)) {
SDL_UnlockMutex(device->lock);
return false; // we're done, shut it down.
}
@@ -1258,7 +1258,7 @@ bool SDL_RecordingAudioThreadIterate(SDL_AudioDevice *device)
failed = true;
} else if (br > 0) { // queue the new data to each bound stream.
for (SDL_LogicalAudioDevice *logdev = device->logical_devices; logdev; logdev = logdev->next) {
- if (SDL_AtomicGet(&logdev->paused)) {
+ if (SDL_GetAtomicInt(&logdev->paused)) {
continue; // paused? Skip this logical device.
}
@@ -1342,7 +1342,7 @@ static SDL_AudioDeviceID *GetAudioDevices(int *count, bool recording)
if (SDL_GetCurrentAudioDriver()) {
SDL_LockRWLockForReading(current_audio.device_hash_lock);
{
- num_devices = SDL_AtomicGet(recording ? ¤t_audio.recording_device_count : ¤t_audio.playback_device_count);
+ num_devices = SDL_GetAtomicInt(recording ? ¤t_audio.recording_device_count : ¤t_audio.playback_device_count);
result = (SDL_AudioDeviceID *) SDL_malloc((num_devices + 1) * sizeof (SDL_AudioDeviceID));
if (result) {
int devs_seen = 0;
@@ -1490,7 +1490,7 @@ int *SDL_GetAudioDeviceChannelMap(SDL_AudioDeviceID devid, int *count)
// BE CAREFUL WITH THIS.
static void SerializePhysicalDeviceClose(SDL_AudioDevice *device)
{
- while (SDL_AtomicGet(&device->shutdown)) {
+ while (SDL_GetAtomicInt(&device->shutdown)) {
SDL_WaitCondition(device->close_cond, device->lock);
}
}
@@ -1500,7 +1500,7 @@ static void ClosePhysicalAudioDevice(SDL_AudioDevice *device)
{
SerializePhysicalDeviceClose(device);
- SDL_AtomicSet(&device->shutdown, 1);
+ SDL_SetAtomicInt(&device->shutdown, 1);
// YOU MUST PROTECT KEY POINTS WITH SerializePhysicalDeviceClose() WHILE THE THREAD JOINS
SDL_UnlockMutex(device->lock);
@@ -1517,7 +1517,7 @@ static void ClosePhysicalAudioDevice(SDL_AudioDevice *device)
}
SDL_LockMutex(device->lock);
- SDL_AtomicSet(&device->shutdown, 0); // ready to go again.
+ SDL_SetAtomicInt(&device->shutdown, 0); // ready to go again.
SDL_BroadcastCondition(device->close_cond); // release anyone waiting in SerializePhysicalDeviceClose; they'll still block until we release device->lock, though.
SDL_aligned_free(device->work_buffer);
@@ -1630,7 +1630,7 @@ static bool OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec
}
// Just pretend to open a zombie device. It can still collect logical devices on a default device under the assumption they will all migrate when the default device is officially changed.
- if (SDL_AtomicGet(&device->zombie)) {
+ if (SDL_GetAtomicInt(&device->zombie)) {
return true; // Braaaaaaaaains.
}
@@ -1719,7 +1719,7 @@ SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSp
if (device) {
SDL_LogicalAudioDevice *logdev = NULL;
- if (!wants_default && SDL_AtomicGet(&device->zombie)) {
+ if (!wants_default && SDL_GetAtomicInt(&device->zombie)) {
// uhoh, this device is undead, and just waiting to be cleaned up. Refuse explicit opens.
SDL_SetError("Device was already lost and can't accept new opens");
} else if ((logdev = (SDL_LogicalAudioDevice *) SDL_calloc(1, sizeof (SDL_LogicalAudioDevice))) == NULL) {
@@ -1728,7 +1728,7 @@ SDL_AudioDeviceID SDL_OpenAudioDevice(SDL_AudioDeviceID devid, const SDL_AudioSp
SDL_free(logdev);
} else {
RefPhysicalAudioDevice(device); // unref'd on successful SDL_CloseAudioDevice
- SDL_AtomicSet(&logdev->paused, 0);
+ SDL_SetAtomicInt(&logdev->paused, 0);
result = logdev->instance_id = AssignAudioDeviceInstanceId(device->recording, /*islogical=*/true);
logdev->physical_device = device;
logdev->gain = 1.0f;
@@ -1761,7 +1761,7 @@ static bool SetLogicalAudioDevicePauseState(SDL_AudioDeviceID devid, int value)
SDL_AudioDevice *device = NULL;
SDL_LogicalAudioDevice *logdev = ObtainLogicalAudioDevice(devid, &device);
if (logdev) {
- SDL_AtomicSet(&logdev->paused, value);
+ SDL_SetAtomicInt(&logdev->paused, value);
}
ReleaseAudioDevice(device);
return logdev ? true : false; // ObtainLogicalAudioDevice will have set an error.
@@ -1782,7 +1782,7 @@ SDL_bool SDL_AudioDevicePaused(SDL_AudioDeviceID devid)
SDL_AudioDevice *device = NULL;
SDL_LogicalAudioDevice *logdev = ObtainLogicalAudioDevice(devid, &device);
bool result = false;
- if (logdev && SDL_AtomicGet(&logdev->paused)) {
+ if (logdev && SDL_GetAtomicInt(&logdev->paused)) {
result = true;
}
ReleaseAudioDevice(device);
@@ -2067,7 +2067,7 @@ SDL_AudioStream *SDL_OpenAudioDeviceStream(SDL_AudioDeviceID devid, const SDL_Au
if (!logdev) { // this shouldn't happen, but just in case.
failed = true;
} else {
- SDL_AtomicSet(&logdev->paused, 1); // start the device paused, to match SDL2.
+ SDL_SetAtomicInt(&logdev->paused, 1); // start the device paused, to match SDL2.
SDL_assert(device != NULL);
const bool recording = device->recording;
@@ -2302,7 +2302,7 @@ void SDL_DefaultAudioDeviceCh
(Patch may be truncated, please check the link at the top of this post.)