Skip to content

Commit

Permalink
Updated the atomic API for SDL 3.0 naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Jan 18, 2024
1 parent 4e60fc9 commit a2665f5
Show file tree
Hide file tree
Showing 24 changed files with 156 additions and 112 deletions.
25 changes: 25 additions & 0 deletions build-scripts/SDL_migration.cocci
Original file line number Diff line number Diff line change
Expand Up @@ -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
(...)
7 changes: 7 additions & 0 deletions docs/README-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 18 additions & 18 deletions include/SDL3/SDL_atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
*
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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 */

Expand All @@ -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

/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions include/SDL3/SDL_oldnames.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/SDL_assert.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
18 changes: 9 additions & 9 deletions src/atomic/SDL_atomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions src/atomic/SDL_spinlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/audio/SDL_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/core/linux/SDL_dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/core/linux/SDL_evdev_kbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/dynapi/SDL_dynapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
10 changes: 5 additions & 5 deletions src/dynapi/SDL_dynapi.sym
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit a2665f5

Please sign in to comment.