From a2665f54c86c08b866f8315d33252f0952aa3315 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Thu, 18 Jan 2024 03:39:55 -0800 Subject: [PATCH] Updated the atomic API for SDL 3.0 naming conventions Fixes https://github.com/libsdl-org/SDL/issues/7388 --- build-scripts/SDL_migration.cocci | 25 ++++++++++++++++ docs/README-migration.md | 7 +++++ include/SDL3/SDL_atomic.h | 36 ++++++++++++------------ include/SDL3/SDL_oldnames.h | 12 ++++++++ src/SDL_assert.c | 6 ++-- src/atomic/SDL_atomic.c | 18 ++++++------ src/atomic/SDL_spinlock.c | 8 +++--- src/audio/SDL_audio.c | 4 +-- src/core/linux/SDL_dbus.c | 4 +-- src/core/linux/SDL_evdev_kbd.c | 2 +- src/dynapi/SDL_dynapi.c | 4 +-- src/dynapi/SDL_dynapi.sym | 10 +++---- src/dynapi/SDL_dynapi_overrides.h | 10 +++---- src/dynapi/SDL_dynapi_procs.h | 10 +++---- src/joystick/hidapi/SDL_hidapi_rumble.c | 2 +- src/joystick/hidapi/SDL_hidapijoystick.c | 16 +++++------ src/main/SDL_main_callbacks.c | 6 ++-- src/thread/SDL_thread.c | 16 +++++------ src/thread/pthread/SDL_systls.c | 4 +-- src/thread/windows/SDL_systls.c | 4 +-- src/timer/SDL_timer.c | 16 +++++------ src/video/SDL_pixels.c | 16 +++++------ test/testatomic.c | 28 +++++++++--------- test/testgles2.c | 4 +-- 24 files changed, 156 insertions(+), 112 deletions(-) diff --git a/build-scripts/SDL_migration.cocci b/build-scripts/SDL_migration.cocci index bb39f23ea..2e7f1e519 100644 --- a/build-scripts/SDL_migration.cocci +++ b/build-scripts/SDL_migration.cocci @@ -2884,3 +2884,28 @@ expression e1, e2, e3, e4; - SDL_HapticRumbleStop + SDL_StopHapticRumble (...) +@@ +@@ +- SDL_AtomicTryLock ++ SDL_TryLockSpinlock + (...) +@@ +@@ +- SDL_AtomicLock ++ SDL_LockSpinlock + (...) +@@ +@@ +- SDL_AtomicUnlock ++ SDL_UnlockSpinlock + (...) +@@ +@@ +- SDL_AtomicCAS ++ SDL_AtomicCompareAndSwap + (...) +@@ +@@ +- SDL_AtomicCASPtr ++ SDL_AtomicCompareAndSwapPointer + (...) diff --git a/docs/README-migration.md b/docs/README-migration.md index daad9924d..d9f33ed53 100644 --- a/docs/README-migration.md +++ b/docs/README-migration.md @@ -47,6 +47,13 @@ The vi format comments have been removed from source code. Vim users can use the 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_AtomicLock() => SDL_LockSpinlock() +* SDL_AtomicTryLock() => SDL_TryLockSpinlock() +* SDL_AtomicUnlock() => SDL_UnlockSpinlock() + ## SDL_audio.h The audio subsystem in SDL3 is dramatically different than SDL2. The primary way to play audio is no longer an audio callback; instead you bind SDL_AudioStreams to devices; however, there is still a callback method available if needed. diff --git a/include/SDL3/SDL_atomic.h b/include/SDL3/SDL_atomic.h index 255417238..82e54b32e 100644 --- a/include/SDL3/SDL_atomic.h +++ b/include/SDL3/SDL_atomic.h @@ -31,8 +31,8 @@ * with full mutexes. * * The list of "safe" functions to use are: - * SDL_AtomicLock() - * SDL_AtomicUnlock() + * SDL_LockSpinlock() + * SDL_UnlockSpinlock() * SDL_AtomicIncRef() * SDL_AtomicDecRef() * @@ -105,10 +105,10 @@ typedef int SDL_SpinLock; * * \since This function is available since SDL 3.0.0. * - * \sa SDL_AtomicLock - * \sa SDL_AtomicUnlock + * \sa SDL_LockSpinlock + * \sa SDL_UnlockSpinlock */ -extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock); +extern DECLSPEC SDL_bool SDLCALL SDL_TryLockSpinlock(SDL_SpinLock *lock); /** * Lock a spin lock by setting it to a non-zero value. @@ -120,10 +120,10 @@ extern DECLSPEC SDL_bool SDLCALL SDL_AtomicTryLock(SDL_SpinLock *lock); * * \since This function is available since SDL 3.0.0. * - * \sa SDL_AtomicTryLock - * \sa SDL_AtomicUnlock + * \sa SDL_TryLockSpinlock + * \sa SDL_UnlockSpinlock */ -extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock); +extern DECLSPEC void SDLCALL SDL_LockSpinlock(SDL_SpinLock *lock); /** * Unlock a spin lock by setting it to 0. @@ -137,10 +137,10 @@ extern DECLSPEC void SDLCALL SDL_AtomicLock(SDL_SpinLock *lock); * * \since This function is available since SDL 3.0.0. * - * \sa SDL_AtomicLock - * \sa SDL_AtomicTryLock + * \sa SDL_LockSpinlock + * \sa SDL_TryLockSpinlock */ -extern DECLSPEC void SDLCALL SDL_AtomicUnlock(SDL_SpinLock *lock); +extern DECLSPEC void SDLCALL SDL_UnlockSpinlock(SDL_SpinLock *lock); /* @} *//* SDL AtomicLock */ @@ -161,7 +161,7 @@ extern __inline void SDL_CompilerBarrier(void); #pragma aux SDL_CompilerBarrier = "" parm [] modify exact []; #else #define SDL_CompilerBarrier() \ -{ SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); } +{ SDL_SpinLock _tmp = 0; SDL_LockSpinlock(&_tmp); SDL_UnlockSpinlock(&_tmp); } #endif /** @@ -282,11 +282,11 @@ typedef struct { int value; } SDL_AtomicInt; * * \since This function is available since SDL 3.0.0. * - * \sa SDL_AtomicCASPtr + * \sa SDL_AtomicCompareAndSwapPointer * \sa SDL_AtomicGet * \sa SDL_AtomicSet */ -extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCAS(SDL_AtomicInt *a, int oldval, int newval); +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareAndSwap(SDL_AtomicInt *a, int oldval, int newval); /** * Set an atomic variable to a value. @@ -370,11 +370,11 @@ extern DECLSPEC int SDLCALL SDL_AtomicAdd(SDL_AtomicInt *a, int v); * * \since This function is available since SDL 3.0.0. * - * \sa SDL_AtomicCAS + * \sa SDL_AtomicCompareAndSwap * \sa SDL_AtomicGetPtr * \sa SDL_AtomicSetPtr */ -extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void *newval); +extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCompareAndSwapPointer(void **a, void *oldval, void *newval); /** * Set a pointer to a value atomically. @@ -388,7 +388,7 @@ extern DECLSPEC SDL_bool SDLCALL SDL_AtomicCASPtr(void **a, void *oldval, void * * * \since This function is available since SDL 3.0.0. * - * \sa SDL_AtomicCASPtr + * \sa SDL_AtomicCompareAndSwapPointer * \sa SDL_AtomicGetPtr */ extern DECLSPEC void* SDLCALL SDL_AtomicSetPtr(void **a, void* v); @@ -404,7 +404,7 @@ extern DECLSPEC void* SDLCALL SDL_AtomicSetPtr(void **a, void* v); * * \since This function is available since SDL 3.0.0. * - * \sa SDL_AtomicCASPtr + * \sa SDL_AtomicCompareAndSwapPointer * \sa SDL_AtomicSetPtr */ extern DECLSPEC void* SDLCALL SDL_AtomicGetPtr(void **a); diff --git a/include/SDL3/SDL_oldnames.h b/include/SDL3/SDL_oldnames.h index 2b32b816f..ac779d036 100644 --- a/include/SDL3/SDL_oldnames.h +++ b/include/SDL3/SDL_oldnames.h @@ -40,6 +40,11 @@ #ifdef SDL_ENABLE_OLD_NAMES /* ##SDL_atomic.h */ +#define SDL_AtomicCAS SDL_AtomicCompareAndSwap +#define SDL_AtomicCASPtr SDL_AtomicCompareAndSwapPointer +#define SDL_AtomicLock SDL_LockSpinlock +#define SDL_AtomicTryLock SDL_TryLockSpinlock +#define SDL_AtomicUnlock SDL_UnlockSpinlock #define SDL_atomic_t SDL_AtomicInt /* ##SDL_audio.h */ @@ -512,6 +517,13 @@ #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_AtomicLock SDL_AtomicLock_renamed_SDL_LockSpinlock +#define SDL_AtomicTryLock SDL_AtomicTryLock_renamed_SDL_TryLockSpinlock +#define SDL_AtomicUnlock SDL_AtomicUnlock_renamed_SDL_UnlockSpinlock + /* ##SDL_audio.h */ #define AUDIO_F32 AUDIO_F32_renamed_SDL_AUDIO_F32LE #define AUDIO_F32LSB AUDIO_F32LSB_renamed_SDL_AUDIO_F32LE diff --git a/src/SDL_assert.c b/src/SDL_assert.c index fd167430f..cfc5f9a05 100644 --- a/src/SDL_assert.c +++ b/src/SDL_assert.c @@ -333,15 +333,15 @@ SDL_AssertState SDL_ReportAssertion(SDL_AssertData *data, const char *func, cons #ifndef SDL_THREADS_DISABLED static SDL_SpinLock spinlock = 0; - SDL_AtomicLock(&spinlock); + SDL_LockSpinlock(&spinlock); if (!assertion_mutex) { /* never called SDL_Init()? */ assertion_mutex = SDL_CreateMutex(); if (!assertion_mutex) { - SDL_AtomicUnlock(&spinlock); + SDL_UnlockSpinlock(&spinlock); return SDL_ASSERTION_IGNORE; /* oh well, I guess. */ } } - SDL_AtomicUnlock(&spinlock); + SDL_UnlockSpinlock(&spinlock); SDL_LockMutex(assertion_mutex); #endif /* !SDL_THREADS_DISABLED */ diff --git a/src/atomic/SDL_atomic.c b/src/atomic/SDL_atomic.c index 2919e8000..f2e457917 100644 --- a/src/atomic/SDL_atomic.c +++ b/src/atomic/SDL_atomic.c @@ -111,18 +111,18 @@ static SDL_INLINE void enterLock(void *a) { uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f); - SDL_AtomicLock(&locks[index]); + SDL_LockSpinlock(&locks[index]); } static SDL_INLINE void leaveLock(void *a) { uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f); - SDL_AtomicUnlock(&locks[index]); + SDL_UnlockSpinlock(&locks[index]); } #endif -SDL_bool SDL_AtomicCAS(SDL_AtomicInt *a, int oldval, int newval) +SDL_bool SDL_AtomicCompareAndSwap(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_AtomicCAS(SDL_AtomicInt *a, int oldval, int newval) #endif } -SDL_bool SDL_AtomicCASPtr(void **a, void *oldval, void *newval) +SDL_bool SDL_AtomicCompareAndSwapPointer(void **a, void *oldval, void *newval) { #ifdef HAVE_MSC_ATOMICS return _InterlockedCompareExchangePointer(a, newval, oldval) == oldval; @@ -196,7 +196,7 @@ int SDL_AtomicSet(SDL_AtomicInt *a, int v) int value; do { value = a->value; - } while (!SDL_AtomicCAS(a, value, v)); + } while (!SDL_AtomicCompareAndSwap(a, value, v)); return value; #endif } @@ -215,7 +215,7 @@ void *SDL_AtomicSetPtr(void **a, void *v) void *value; do { value = *a; - } while (!SDL_AtomicCASPtr(a, value, v)); + } while (!SDL_AtomicCompareAndSwapPointer(a, value, v)); return value; #endif } @@ -238,7 +238,7 @@ int SDL_AtomicAdd(SDL_AtomicInt *a, int v) int value; do { value = a->value; - } while (!SDL_AtomicCAS(a, value, (value + v))); + } while (!SDL_AtomicCompareAndSwap(a, value, (value + v))); return value; #endif } @@ -262,7 +262,7 @@ int SDL_AtomicGet(SDL_AtomicInt *a) int value; do { value = a->value; - } while (!SDL_AtomicCAS(a, value, value)); + } while (!SDL_AtomicCompareAndSwap(a, value, value)); return value; #endif } @@ -281,7 +281,7 @@ void *SDL_AtomicGetPtr(void **a) void *value; do { value = *a; - } while (!SDL_AtomicCASPtr(a, value, value)); + } while (!SDL_AtomicCompareAndSwapPointer(a, value, value)); return value; #endif } diff --git a/src/atomic/SDL_spinlock.c b/src/atomic/SDL_spinlock.c index b19ded7e2..1abed587c 100644 --- a/src/atomic/SDL_spinlock.c +++ b/src/atomic/SDL_spinlock.c @@ -57,7 +57,7 @@ extern __inline int _SDL_xchg_watcom(volatile int *a, int v); /* *INDENT-ON* */ /* clang-format on */ /* This function is where all the magic happens... */ -SDL_bool SDL_AtomicTryLock(SDL_SpinLock *lock) +SDL_bool SDL_TryLockSpinlock(SDL_SpinLock *lock) { #if defined(HAVE_GCC_ATOMICS) || defined(HAVE_GCC_SYNC_LOCK_TEST_AND_SET) return __sync_lock_test_and_set(lock, 1) == 0; @@ -161,11 +161,11 @@ SDL_bool SDL_AtomicTryLock(SDL_SpinLock *lock) #endif } -void SDL_AtomicLock(SDL_SpinLock *lock) +void SDL_LockSpinlock(SDL_SpinLock *lock) { int iterations = 0; /* FIXME: Should we have an eventual timeout? */ - while (!SDL_AtomicTryLock(lock)) { + while (!SDL_TryLockSpinlock(lock)) { if (iterations < 32) { iterations++; SDL_CPUPauseInstruction(); @@ -176,7 +176,7 @@ void SDL_AtomicLock(SDL_SpinLock *lock) } } -void SDL_AtomicUnlock(SDL_SpinLock *lock) +void SDL_UnlockSpinlock(SDL_SpinLock *lock) { #if defined(HAVE_GCC_ATOMICS) || defined(HAVE_GCC_SYNC_LOCK_TEST_AND_SET) __sync_lock_release(lock); diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c index b8f3d17b2..6a1c4aa0e 100644 --- a/src/audio/SDL_audio.c +++ b/src/audio/SDL_audio.c @@ -635,7 +635,7 @@ void SDL_AudioDeviceDisconnected(SDL_AudioDevice *device) const SDL_bool is_default_device = ((devid == current_audio.default_output_device_id) || (devid == current_audio.default_capture_device_id)); SDL_UnlockRWLock(current_audio.device_hash_lock); - const SDL_bool first_disconnect = SDL_AtomicCAS(&device->zombie, 0, 1); + const SDL_bool first_disconnect = SDL_AtomicCompareAndSwap(&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 @@ -809,7 +809,7 @@ int 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_AtomicCAS(&last_device_instance_id, 0, 2); + SDL_AtomicCompareAndSwap(&last_device_instance_id, 0, 2); SDL_ChooseAudioConverters(); SDL_SetupAudioResampler(); diff --git a/src/core/linux/SDL_dbus.c b/src/core/linux/SDL_dbus.c index f7f5b6d62..60be16dc4 100644 --- a/src/core/linux/SDL_dbus.c +++ b/src/core/linux/SDL_dbus.c @@ -168,9 +168,9 @@ static void SDL_DBus_Init_Spinlocked(void) void SDL_DBus_Init(void) { - SDL_AtomicLock(&spinlock_dbus_init); /* make sure two threads can't init at same time, since this can happen before SDL_Init. */ + SDL_LockSpinlock(&spinlock_dbus_init); /* make sure two threads can't init at same time, since this can happen before SDL_Init. */ SDL_DBus_Init_Spinlocked(); - SDL_AtomicUnlock(&spinlock_dbus_init); + SDL_UnlockSpinlock(&spinlock_dbus_init); } void SDL_DBus_Quit(void) diff --git a/src/core/linux/SDL_evdev_kbd.c b/src/core/linux/SDL_evdev_kbd.c index 7c81b5444..bf4832983 100644 --- a/src/core/linux/SDL_evdev_kbd.c +++ b/src/core/linux/SDL_evdev_kbd.c @@ -416,7 +416,7 @@ static void kbd_vt_update(SDL_EVDEV_keyboard_state *state) } ioctl(state->console_fd, VT_RELDISP, VT_ACKACQ); } - SDL_AtomicCAS(&vt_signal_pending, signal_pending, VT_SIGNAL_NONE); + SDL_AtomicCompareAndSwap(&vt_signal_pending, signal_pending, VT_SIGNAL_NONE); } } diff --git a/src/dynapi/SDL_dynapi.c b/src/dynapi/SDL_dynapi.c index 84c23cb23..08c18b910 100644 --- a/src/dynapi/SDL_dynapi.c +++ b/src/dynapi/SDL_dynapi.c @@ -536,14 +536,14 @@ static void SDL_InitDynamicAPI(void) static SDL_bool already_initialized = SDL_FALSE; static SDL_SpinLock lock = 0; - SDL_AtomicLock_REAL(&lock); + SDL_LockSpinlock_REAL(&lock); if (!already_initialized) { SDL_InitDynamicAPILocked(); already_initialized = SDL_TRUE; } - SDL_AtomicUnlock_REAL(&lock); + SDL_UnlockSpinlock_REAL(&lock); } #else /* SDL_DYNAMIC_API */ diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym index 23f3cbbde..037207aa2 100644 --- a/src/dynapi/SDL_dynapi.sym +++ b/src/dynapi/SDL_dynapi.sym @@ -17,15 +17,15 @@ SDL3_0.0.0 { SDL_AndroidSendMessage; SDL_AndroidShowToast; SDL_AtomicAdd; - SDL_AtomicCAS; - SDL_AtomicCASPtr; + SDL_AtomicCompareAndSwap; + SDL_AtomicCompareAndSwapPointer; SDL_AtomicGet; SDL_AtomicGetPtr; - SDL_AtomicLock; + SDL_LockSpinlock; SDL_AtomicSet; SDL_AtomicSetPtr; - SDL_AtomicTryLock; - SDL_AtomicUnlock; + SDL_TryLockSpinlock; + SDL_UnlockSpinlock; SDL_AttachVirtualJoystick; SDL_AttachVirtualJoystickEx; SDL_BlitSurface; diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index 83da3e39e..3e939ae2b 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -41,15 +41,15 @@ #define SDL_AndroidSendMessage SDL_AndroidSendMessage_REAL #define SDL_AndroidShowToast SDL_AndroidShowToast_REAL #define SDL_AtomicAdd SDL_AtomicAdd_REAL -#define SDL_AtomicCAS SDL_AtomicCAS_REAL -#define SDL_AtomicCASPtr SDL_AtomicCASPtr_REAL +#define SDL_AtomicCompareAndSwap SDL_AtomicCompareAndSwap_REAL +#define SDL_AtomicCompareAndSwapPointer SDL_AtomicCompareAndSwapPointer_REAL #define SDL_AtomicGet SDL_AtomicGet_REAL #define SDL_AtomicGetPtr SDL_AtomicGetPtr_REAL -#define SDL_AtomicLock SDL_AtomicLock_REAL +#define SDL_LockSpinlock SDL_LockSpinlock_REAL #define SDL_AtomicSet SDL_AtomicSet_REAL #define SDL_AtomicSetPtr SDL_AtomicSetPtr_REAL -#define SDL_AtomicTryLock SDL_AtomicTryLock_REAL -#define SDL_AtomicUnlock SDL_AtomicUnlock_REAL +#define SDL_TryLockSpinlock SDL_TryLockSpinlock_REAL +#define SDL_UnlockSpinlock SDL_UnlockSpinlock_REAL #define SDL_AttachVirtualJoystick SDL_AttachVirtualJoystick_REAL #define SDL_AttachVirtualJoystickEx SDL_AttachVirtualJoystickEx_REAL #define SDL_BlitSurface SDL_BlitSurface_REAL diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 8314eb918..54f05b9f5 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -107,15 +107,15 @@ SDL_DYNAPI_PROC(int,SDL_AddGamepadMappingsFromRW,(SDL_RWops *a, SDL_bool b),(a,b SDL_DYNAPI_PROC(int,SDL_AddHintCallback,(const char *a, SDL_HintCallback b, void *c),(a,b,c),return) SDL_DYNAPI_PROC(SDL_TimerID,SDL_AddTimer,(Uint32 a, SDL_TimerCallback b, void *c),(a,b,c),return) SDL_DYNAPI_PROC(int,SDL_AtomicAdd,(SDL_AtomicInt *a, int b),(a,b),return) -SDL_DYNAPI_PROC(SDL_bool,SDL_AtomicCAS,(SDL_AtomicInt *a, int b, int c),(a,b,c),return) -SDL_DYNAPI_PROC(SDL_bool,SDL_AtomicCASPtr,(void **a, void *b, void *c),(a,b,c),return) +SDL_DYNAPI_PROC(SDL_bool,SDL_AtomicCompareAndSwap,(SDL_AtomicInt *a, int b, int c),(a,b,c),return) +SDL_DYNAPI_PROC(SDL_bool,SDL_AtomicCompareAndSwapPointer,(void **a, void *b, void *c),(a,b,c),return) SDL_DYNAPI_PROC(int,SDL_AtomicGet,(SDL_AtomicInt *a),(a),return) SDL_DYNAPI_PROC(void*,SDL_AtomicGetPtr,(void **a),(a),return) -SDL_DYNAPI_PROC(void,SDL_AtomicLock,(SDL_SpinLock *a),(a),) +SDL_DYNAPI_PROC(void,SDL_LockSpinlock,(SDL_SpinLock *a),(a),) SDL_DYNAPI_PROC(int,SDL_AtomicSet,(SDL_AtomicInt *a, int b),(a,b),return) SDL_DYNAPI_PROC(void*,SDL_AtomicSetPtr,(void **a, void *b),(a,b),return) -SDL_DYNAPI_PROC(SDL_bool,SDL_AtomicTryLock,(SDL_SpinLock *a),(a),return) -SDL_DYNAPI_PROC(void,SDL_AtomicUnlock,(SDL_SpinLock *a),(a),) +SDL_DYNAPI_PROC(SDL_bool,SDL_TryLockSpinlock,(SDL_SpinLock *a),(a),return) +SDL_DYNAPI_PROC(void,SDL_UnlockSpinlock,(SDL_SpinLock *a),(a),) SDL_DYNAPI_PROC(SDL_JoystickID,SDL_AttachVirtualJoystick,(SDL_JoystickType a, int b, int c, int d),(a,b,c,d),return) SDL_DYNAPI_PROC(SDL_JoystickID,SDL_AttachVirtualJoystickEx,(const SDL_VirtualJoystickDesc *a),(a),return) SDL_DYNAPI_PROC(int,SDL_BlitSurface,(SDL_Surface *a, const SDL_Rect *b, SDL_Surface *c, SDL_Rect *d),(a,b,c,d),return) diff --git a/src/joystick/hidapi/SDL_hidapi_rumble.c b/src/joystick/hidapi/SDL_hidapi_rumble.c index 5d270fc19..8dd7316c1 100644 --- a/src/joystick/hidapi/SDL_hidapi_rumble.c +++ b/src/joystick/hidapi/SDL_hidapi_rumble.c @@ -168,7 +168,7 @@ int SDL_HIDAPI_LockRumble(void) { SDL_HIDAPI_RumbleContext *ctx = &rumble_context; - if (SDL_AtomicCAS(&ctx->initialized, SDL_FALSE, SDL_TRUE)) { + if (SDL_AtomicCompareAndSwap(&ctx->initialized, SDL_FALSE, SDL_TRUE)) { if (SDL_HIDAPI_StartRumbleThread(ctx) < 0) { return -1; } diff --git a/src/joystick/hidapi/SDL_hidapijoystick.c b/src/joystick/hidapi/SDL_hidapijoystick.c index bb1f9376f..79dfe5a94 100644 --- a/src/joystick/hidapi/SDL_hidapijoystick.c +++ b/src/joystick/hidapi/SDL_hidapijoystick.c @@ -1206,9 +1206,9 @@ SDL_bool HIDAPI_IsDeviceTypePresent(SDL_GamepadType type) return SDL_FALSE; } - if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) { + if (SDL_TryLockSpinlock(&SDL_HIDAPI_spinlock)) { HIDAPI_UpdateDeviceList(); - SDL_AtomicUnlock(&SDL_HIDAPI_spinlock); + SDL_UnlockSpinlock(&SDL_HIDAPI_spinlock); } SDL_LockJoysticks(); @@ -1251,9 +1251,9 @@ SDL_bool HIDAPI_IsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 vers } #endif /* SDL_JOYSTICK_HIDAPI_XBOX360 || SDL_JOYSTICK_HIDAPI_XBOXONE */ if (supported) { - if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) { + if (SDL_TryLockSpinlock(&SDL_HIDAPI_spinlock)) { HIDAPI_UpdateDeviceList(); - SDL_AtomicUnlock(&SDL_HIDAPI_spinlock); + SDL_UnlockSpinlock(&SDL_HIDAPI_spinlock); } } @@ -1313,13 +1313,13 @@ SDL_GamepadType HIDAPI_GetGamepadTypeFromGUID(SDL_JoystickGUID guid) static void HIDAPI_JoystickDetect(void) { - if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) { + if (SDL_TryLockSpinlock(&SDL_HIDAPI_spinlock)) { Uint32 count = SDL_hid_device_change_count(); if (SDL_HIDAPI_change_count != count) { SDL_HIDAPI_change_count = count; HIDAPI_UpdateDeviceList(); } - SDL_AtomicUnlock(&SDL_HIDAPI_spinlock); + SDL_UnlockSpinlock(&SDL_HIDAPI_spinlock); } } @@ -1332,7 +1332,7 @@ void HIDAPI_UpdateDevices(void) /* Update the devices, which may change connected joysticks and send events */ /* Prepare the existing device list */ - if (SDL_AtomicTryLock(&SDL_HIDAPI_spinlock)) { + if (SDL_TryLockSpinlock(&SDL_HIDAPI_spinlock)) { for (device = SDL_HIDAPI_devices; device; device = device->next) { if (device->parent) { continue; @@ -1346,7 +1346,7 @@ void HIDAPI_UpdateDevices(void) } } } - SDL_AtomicUnlock(&SDL_HIDAPI_spinlock); + SDL_UnlockSpinlock(&SDL_HIDAPI_spinlock); } } diff --git a/src/main/SDL_main_callbacks.c b/src/main/SDL_main_callbacks.c index a89ffb915..37657a866 100644 --- a/src/main/SDL_main_callbacks.c +++ b/src/main/SDL_main_callbacks.c @@ -46,7 +46,7 @@ static SDL_bool ShouldDispatchImmediately(SDL_Event *event) static void SDL_DispatchMainCallbackEvent(SDL_Event *event) { if (SDL_AtomicGet(&apprc) == 0) { // if already quitting, don't send the event to the app. - SDL_AtomicCAS(&apprc, 0, SDL_main_event_callback(event)); + SDL_AtomicCompareAndSwap(&apprc, 0, SDL_main_event_callback(event)); } } @@ -96,7 +96,7 @@ int SDL_InitMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_ SDL_AtomicSet(&apprc, 0); const int rc = appinit(argc, argv); - if (SDL_AtomicCAS(&apprc, 0, rc) && (rc == 0)) { // bounce if SDL_AppInit already said abort, otherwise... + if (SDL_AtomicCompareAndSwap(&apprc, 0, rc) && (rc == 0)) { // bounce if SDL_AppInit already said abort, otherwise... // make sure we definitely have events initialized, even if the app didn't do it. if (SDL_InitSubSystem(SDL_INIT_EVENTS) == -1) { SDL_AtomicSet(&apprc, -1); @@ -122,7 +122,7 @@ int SDL_IterateMainCallbacks(SDL_bool pump_events) int rc = SDL_AtomicGet(&apprc); if (rc == 0) { rc = SDL_main_iteration_callback(); - if (!SDL_AtomicCAS(&apprc, 0, rc)) { + if (!SDL_AtomicCompareAndSwap(&apprc, 0, rc)) { rc = SDL_AtomicGet(&apprc); // something else already set a quit result, keep that. } } diff --git a/src/thread/SDL_thread.c b/src/thread/SDL_thread.c index ae9a1fda7..9b28f6b8e 100644 --- a/src/thread/SDL_thread.c +++ b/src/thread/SDL_thread.c @@ -122,17 +122,17 @@ SDL_TLSData *SDL_Generic_GetTLSData(void) #ifndef SDL_THREADS_DISABLED if (!SDL_generic_TLS_mutex) { static SDL_SpinLock tls_lock; - SDL_AtomicLock(&tls_lock); + SDL_LockSpinlock(&tls_lock); if (!SDL_generic_TLS_mutex) { SDL_Mutex *mutex = SDL_CreateMutex(); SDL_MemoryBarrierRelease(); SDL_generic_TLS_mutex = mutex; if (!SDL_generic_TLS_mutex) { - SDL_AtomicUnlock(&tls_lock); + SDL_UnlockSpinlock(&tls_lock); return NULL; } } - SDL_AtomicUnlock(&tls_lock); + SDL_UnlockSpinlock(&tls_lock); } SDL_MemoryBarrierAcquire(); SDL_LockMutex(SDL_generic_TLS_mutex); @@ -232,7 +232,7 @@ SDL_error *SDL_GetErrBuf(SDL_bool create) but that's very unlikely and hopefully won't cause issues. */ if (!tls_errbuf && !tls_being_created) { - SDL_AtomicLock(&tls_lock); + SDL_LockSpinlock(&tls_lock); if (!tls_errbuf) { SDL_TLSID slot; tls_being_created = SDL_TRUE; @@ -241,7 +241,7 @@ SDL_error *SDL_GetErrBuf(SDL_bool create) SDL_MemoryBarrierRelease(); tls_errbuf = slot; } - SDL_AtomicUnlock(&tls_lock); + SDL_UnlockSpinlock(&tls_lock); } if (!tls_errbuf) { return SDL_GetStaticErrBuf(); @@ -296,9 +296,9 @@ void SDL_RunThread(SDL_Thread *thread) SDL_CleanupTLS(); /* Mark us as ready to be joined (or detached) */ - if (!SDL_AtomicCAS(&thread->state, SDL_THREAD_STATE_ALIVE, SDL_THREAD_STATE_ZOMBIE)) { + if (!SDL_AtomicCompareAndSwap(&thread->state, SDL_THREAD_STATE_ALIVE, SDL_THREAD_STATE_ZOMBIE)) { /* Clean up if something already detached us. */ - if (SDL_AtomicCAS(&thread->state, SDL_THREAD_STATE_DETACHED, SDL_THREAD_STATE_CLEANED)) { + if (SDL_AtomicCompareAndSwap(&thread->state, SDL_THREAD_STATE_DETACHED, SDL_THREAD_STATE_CLEANED)) { if (thread->name) { SDL_free(thread->name); } @@ -456,7 +456,7 @@ void SDL_DetachThread(SDL_Thread *thread) } /* Grab dibs if the state is alive+joinable. */ - if (SDL_AtomicCAS(&thread->state, SDL_THREAD_STATE_ALIVE, SDL_THREAD_STATE_DETACHED)) { + if (SDL_AtomicCompareAndSwap(&thread->state, SDL_THREAD_STATE_ALIVE, SDL_THREAD_STATE_DETACHED)) { SDL_SYS_DetachThread(thread); } else { /* all other states are pretty final, see where we landed. */ diff --git a/src/thread/pthread/SDL_systls.c b/src/thread/pthread/SDL_systls.c index 9c44d575e..15a7dd077 100644 --- a/src/thread/pthread/SDL_systls.c +++ b/src/thread/pthread/SDL_systls.c @@ -33,7 +33,7 @@ SDL_TLSData *SDL_SYS_GetTLSData(void) { if (thread_local_storage == INVALID_PTHREAD_KEY && !generic_local_storage) { static SDL_SpinLock lock; - SDL_AtomicLock(&lock); + SDL_LockSpinlock(&lock); if (thread_local_storage == INVALID_PTHREAD_KEY && !generic_local_storage) { pthread_key_t storage; if (pthread_key_create(&storage, NULL) == 0) { @@ -43,7 +43,7 @@ SDL_TLSData *SDL_SYS_GetTLSData(void) generic_local_storage = SDL_TRUE; } } - SDL_AtomicUnlock(&lock); + SDL_UnlockSpinlock(&lock); } if (generic_local_storage) { return SDL_Generic_GetTLSData(); diff --git a/src/thread/windows/SDL_systls.c b/src/thread/windows/SDL_systls.c index 4eb2d53da..16393afde 100644 --- a/src/thread/windows/SDL_systls.c +++ b/src/thread/windows/SDL_systls.c @@ -46,7 +46,7 @@ SDL_TLSData *SDL_SYS_GetTLSData(void) { if (thread_local_storage == TLS_OUT_OF_INDEXES && !generic_local_storage) { static SDL_SpinLock lock; - SDL_AtomicLock(&lock); + SDL_LockSpinlock(&lock); if (thread_local_storage == TLS_OUT_OF_INDEXES && !generic_local_storage) { DWORD storage = TlsAlloc(); if (storage != TLS_OUT_OF_INDEXES) { @@ -56,7 +56,7 @@ SDL_TLSData *SDL_SYS_GetTLSData(void) generic_local_storage = SDL_TRUE; } } - SDL_AtomicUnlock(&lock); + SDL_UnlockSpinlock(&lock); } if (generic_local_storage) { return SDL_Generic_GetTLSData(); diff --git a/src/timer/SDL_timer.c b/src/timer/SDL_timer.c index d3501155a..30babaac2 100644 --- a/src/timer/SDL_timer.c +++ b/src/timer/SDL_timer.c @@ -112,7 +112,7 @@ static int SDLCALL SDL_TimerThread(void *_data) */ for (;;) { /* Pending and freelist maintenance */ - SDL_AtomicLock(&data->lock); + SDL_LockSpinlock(&data->lock); { /* Get any timers ready to be queued */ pending = data->pending; @@ -124,7 +124,7 @@ static int SDLCALL SDL_TimerThread(void *_data) data->freelist = freelist_head; } } - SDL_AtomicUnlock(&data->lock); + SDL_UnlockSpinlock(&data->lock); /* Sort the pending timers into our list */ while (pending) { @@ -239,7 +239,7 @@ void SDL_QuitTimers(void) SDL_Timer *timer; SDL_TimerMap *entry; - if (SDL_AtomicCAS(&data->active, 1, 0)) { /* active? Move to inactive. */ + if (SDL_AtomicCompareAndSwap(&data->active, 1, 0)) { /* active? Move to inactive. */ /* Shutdown the timer thread */ if (data->thread) { SDL_PostSemaphore(data->sem); @@ -278,10 +278,10 @@ SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *para SDL_Timer *timer; SDL_TimerMap *entry; - SDL_AtomicLock(&data->lock); + SDL_LockSpinlock(&data->lock); if (!SDL_AtomicGet(&data->active)) { if (SDL_InitTimers() < 0) { - SDL_AtomicUnlock(&data->lock); + SDL_UnlockSpinlock(&data->lock); return 0; } } @@ -290,7 +290,7 @@ SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *para if (timer) { data->freelist = timer->next; } - SDL_AtomicUnlock(&data->lock); + SDL_UnlockSpinlock(&data->lock); if (timer) { SDL_RemoveTimer(timer->timerID); @@ -321,10 +321,10 @@ SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *para SDL_UnlockMutex(data->timermap_lock); /* Add the timer to the pending list for the timer thread */ - SDL_AtomicLock(&data->lock); + SDL_LockSpinlock(&data->lock); timer->next = data->pending; data->pending = timer; - SDL_AtomicUnlock(&data->lock); + SDL_UnlockSpinlock(&data->lock); /* Wake up the timer thread if necessary */ SDL_PostSemaphore(data->sem); diff --git a/src/video/SDL_pixels.c b/src/video/SDL_pixels.c index 72def3a19..5779212e7 100644 --- a/src/video/SDL_pixels.c +++ b/src/video/SDL_pixels.c @@ -550,13 +550,13 @@ SDL_PixelFormat *SDL_CreatePixelFormat(Uint32 pixel_format) { SDL_PixelFormat *format; - SDL_AtomicLock(&formats_lock); + SDL_LockSpinlock(&formats_lock); /* Look it up in our list of previously allocated formats */ for (format = formats; format; format = format->next) { if (pixel_format == format->format) { ++format->refcount; - SDL_AtomicUnlock(&formats_lock); + SDL_UnlockSpinlock(&formats_lock); return format; } } @@ -564,11 +564,11 @@ SDL_PixelFormat *SDL_CreatePixelFormat(Uint32 pixel_format) /* Allocate an empty pixel format structure, and initialize it */ format = SDL_malloc(sizeof(*format)); if (!format) { - SDL_AtomicUnlock(&formats_lock); + SDL_UnlockSpinlock(&formats_lock); return NULL; } if (SDL_InitFormat(format, pixel_format) < 0) { - SDL_AtomicUnlock(&formats_lock); + SDL_UnlockSpinlock(&formats_lock); SDL_free(format); return NULL; } @@ -579,7 +579,7 @@ SDL_PixelFormat *SDL_CreatePixelFormat(Uint32 pixel_format) formats = format; } - SDL_AtomicUnlock(&formats_lock); + SDL_UnlockSpinlock(&formats_lock); return format; } @@ -664,10 +664,10 @@ void SDL_DestroyPixelFormat(SDL_PixelFormat *format) return; } - SDL_AtomicLock(&formats_lock); + SDL_LockSpinlock(&formats_lock); if (--format->refcount > 0) { - SDL_AtomicUnlock(&formats_lock); + SDL_UnlockSpinlock(&formats_lock); return; } @@ -683,7 +683,7 @@ void SDL_DestroyPixelFormat(SDL_PixelFormat *format) } } - SDL_AtomicUnlock(&formats_lock); + SDL_UnlockSpinlock(&formats_lock); if (format->palette) { SDL_DestroyPalette(format->palette); diff --git a/test/testatomic.c b/test/testatomic.c index 84b41bf68..c2cca7428 100644 --- a/test/testatomic.c +++ b/test/testatomic.c @@ -42,9 +42,9 @@ static void RunBasicTest(void) SDL_Log("\nspin lock---------------------------------------\n\n"); - SDL_AtomicLock(&lock); + SDL_LockSpinlock(&lock); SDL_Log("AtomicLock lock=%d\n", lock); - SDL_AtomicUnlock(&lock); + SDL_UnlockSpinlock(&lock); SDL_Log("AtomicUnlock lock=%d\n", lock); SDL_Log("\natomic -----------------------------------------\n\n"); @@ -68,10 +68,10 @@ static void RunBasicTest(void) SDL_Log("AtomicDecRef() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v)); SDL_AtomicSet(&v, 10); - tfret = (SDL_AtomicCAS(&v, 0, 20) == SDL_FALSE); + tfret = (SDL_AtomicCompareAndSwap(&v, 0, 20) == SDL_FALSE); SDL_Log("AtomicCAS() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v)); value = SDL_AtomicGet(&v); - tfret = (SDL_AtomicCAS(&v, value, 20) == SDL_TRUE); + tfret = (SDL_AtomicCompareAndSwap(&v, value, 20) == SDL_TRUE); SDL_Log("AtomicCAS() tfret=%s val=%d\n", tf(tfret), SDL_AtomicGet(&v)); } @@ -179,12 +179,12 @@ static void RunEpicTest(void) SDL_Log("Test compare and exchange\n"); - b = SDL_AtomicCAS(&good, 500, 43); + b = SDL_AtomicCompareAndSwap(&good, 500, 43); SDL_assert(!b); /* no swap since CountTo!=500 */ v = SDL_AtomicGet(&good); SDL_assert(v == CountTo); /* ensure no swap */ - b = SDL_AtomicCAS(&good, CountTo, 44); + b = SDL_AtomicCompareAndSwap(&good, CountTo, 44); SDL_assert(!!b); /* will swap */ v = SDL_AtomicGet(&good); SDL_assert(v == 44); @@ -317,10 +317,10 @@ static SDL_bool EnqueueEvent_LockFree(SDL_EventQueue *queue, const SDL_Event *ev #ifdef TEST_SPINLOCK_FIFO /* This is a gate so an external thread can lock the queue */ - SDL_AtomicLock(&queue->lock); + SDL_LockSpinlock(&queue->lock); SDL_assert(SDL_AtomicGet(&queue->watcher) == 0); SDL_AtomicIncRef(&queue->rwcount); - SDL_AtomicUnlock(&queue->lock); + SDL_UnlockSpinlock(&queue->lock); #endif queue_pos = (unsigned)SDL_AtomicGet(&queue->enqueue_pos); @@ -331,7 +331,7 @@ static SDL_bool EnqueueEvent_LockFree(SDL_EventQueue *queue, const SDL_Event *ev delta = (int)(entry_seq - queue_pos); if (delta == 0) { /* The entry and the queue position match, try to increment the queue position */ - if (SDL_AtomicCAS(&queue->enqueue_pos, (int)queue_pos, (int)(queue_pos + 1))) { + if (SDL_AtomicCompareAndSwap(&queue->enqueue_pos, (int)queue_pos, (int)(queue_pos + 1))) { /* We own the object, fill it! */ entry->event = *event; SDL_AtomicSet(&entry->sequence, (int)(queue_pos + 1)); @@ -364,10 +364,10 @@ static SDL_bool DequeueEvent_LockFree(SDL_EventQueue *queue, SDL_Event *event) #ifdef TEST_SPINLOCK_FIFO /* This is a gate so an external thread can lock the queue */ - SDL_AtomicLock(&queue->lock); + SDL_LockSpinlock(&queue->lock); SDL_assert(SDL_AtomicGet(&queue->watcher) == 0); SDL_AtomicIncRef(&queue->rwcount); - SDL_AtomicUnlock(&queue->lock); + SDL_UnlockSpinlock(&queue->lock); #endif queue_pos = (unsigned)SDL_AtomicGet(&queue->dequeue_pos); @@ -378,7 +378,7 @@ static SDL_bool DequeueEvent_LockFree(SDL_EventQueue *queue, SDL_Event *event) delta = (int)(entry_seq - (queue_pos + 1)); if (delta == 0) { /* The entry and the queue position match, try to increment the queue position */ - if (SDL_AtomicCAS(&queue->dequeue_pos, (int)queue_pos, (int)(queue_pos + 1))) { + if (SDL_AtomicCompareAndSwap(&queue->dequeue_pos, (int)queue_pos, (int)(queue_pos + 1))) { /* We own the object, fill it! */ *event = entry->event; SDL_AtomicSet(&entry->sequence, (int)(queue_pos + MAX_ENTRIES)); @@ -564,14 +564,14 @@ static int SDLCALL FIFO_Watcher(void *_data) SDL_EventQueue *queue = (SDL_EventQueue *)_data; while (SDL_AtomicGet(&queue->active)) { - SDL_AtomicLock(&queue->lock); + SDL_LockSpinlock(&queue->lock); SDL_AtomicIncRef(&queue->watcher); while (SDL_AtomicGet(&queue->rwcount) > 0) { SDL_Delay(0); } /* Do queue manipulation here... */ (void)SDL_AtomicDecRef(&queue->watcher); - SDL_AtomicUnlock(&queue->lock); + SDL_UnlockSpinlock(&queue->lock); /* Wait a bit... */ SDL_Delay(1); diff --git a/test/testgles2.c b/test/testgles2.c index af408eca8..289aaf3dd 100644 --- a/test/testgles2.c +++ b/test/testgles2.c @@ -567,7 +567,7 @@ render_thread_fn(void *render_ctx) thread_data *thread = render_ctx; while (!done && !thread->done && state->windows[thread->index]) { - if (SDL_AtomicCAS(&thread->suspended, WAIT_STATE_ENTER_SEM, WAIT_STATE_WAITING_ON_SEM)) { + if (SDL_AtomicCompareAndSwap(&thread->suspended, WAIT_STATE_ENTER_SEM, WAIT_STATE_WAITING_ON_SEM)) { SDL_WaitSemaphore(thread->suspend_sem); } render_window(thread->index); @@ -602,7 +602,7 @@ loop_threaded(void) if (suspend_when_occluded && event.type == SDL_EVENT_WINDOW_OCCLUDED) { tdata = GetThreadDataForWindow(event.window.windowID); if (tdata) { - SDL_AtomicCAS(&tdata->suspended, WAIT_STATE_GO, WAIT_STATE_ENTER_SEM); + SDL_AtomicCompareAndSwap(&tdata->suspended, WAIT_STATE_GO, WAIT_STATE_ENTER_SEM); } } else if (suspend_when_occluded && event.type == SDL_EVENT_WINDOW_EXPOSED) { tdata = GetThreadDataForWindow(event.window.windowID);