Skip to content

Commit 42f634f

Browse files
committed
Added SDL_AddAtomicU32()
Fixes #13496
1 parent b795762 commit 42f634f

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

include/SDL3/SDL_atomic.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,24 @@ extern SDL_DECLSPEC Uint32 SDLCALL SDL_SetAtomicU32(SDL_AtomicU32 *a, Uint32 v);
596596
*/
597597
extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetAtomicU32(SDL_AtomicU32 *a);
598598

599+
/**
600+
* Add to an atomic variable.
601+
*
602+
* This function also acts as a full memory barrier.
603+
*
604+
* ***Note: If you don't know what this function is for, you shouldn't use
605+
* it!***
606+
*
607+
* \param a a pointer to an SDL_AtomicU32 variable to be modified.
608+
* \param v the desired value to add or subtract.
609+
* \returns the previous value of the atomic variable.
610+
*
611+
* \threadsafety It is safe to call this function from any thread.
612+
*
613+
* \since This function is available since SDL 3.2.0.
614+
*/
615+
extern SDL_DECLSPEC Uint32 SDLCALL SDL_AddAtomicU32(SDL_AtomicU32 *a, int v);
616+
599617
/**
600618
* Set a pointer to a new value if it is currently an old value.
601619
*

src/atomic/SDL_atomic.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,30 @@ int SDL_AddAtomicInt(SDL_AtomicInt *a, int v)
298298
#endif
299299
}
300300

301+
Uint32 SDL_AddAtomicU32(SDL_AtomicU32 *a, int v)
302+
{
303+
#ifdef HAVE_MSC_ATOMICS
304+
SDL_COMPILE_TIME_ASSERT(atomic_add, sizeof(long) == sizeof(a->value));
305+
return (Uint32)_InterlockedExchangeAdd((long *)&a->value, v);
306+
#elif defined(HAVE_WATCOM_ATOMICS)
307+
SDL_COMPILE_TIME_ASSERT(atomic_add, sizeof(int) == sizeof(a->value));
308+
return (Uint32)_SDL_xadd_watcom((volatile int *)&a->value, v);
309+
#elif defined(HAVE_GCC_ATOMICS)
310+
return __sync_fetch_and_add(&a->value, v);
311+
#elif defined(SDL_PLATFORM_SOLARIS)
312+
Uint32 pv = a->value;
313+
membar_consumer();
314+
atomic_add_int((volatile uint_t *)&a->value, v);
315+
return pv;
316+
#else
317+
Uint32 value;
318+
do {
319+
value = a->value;
320+
} while (!SDL_CompareAndSwapAtomicU32(a, value, (value + v)));
321+
return value;
322+
#endif
323+
}
324+
301325
int SDL_GetAtomicInt(SDL_AtomicInt *a)
302326
{
303327
#ifdef HAVE_ATOMIC_LOAD_N

src/dynapi/SDL_dynapi.sym

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,7 @@ SDL3_0.0.0 {
12531253
SDL_PutAudioStreamPlanarData;
12541254
SDL_GetEventDescription;
12551255
SDL_PutAudioStreamDataNoCopy;
1256+
SDL_AddAtomicU32;
12561257
# extra symbols go here (don't modify this line)
12571258
local: *;
12581259
};

src/dynapi/SDL_dynapi_overrides.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,3 +1278,4 @@
12781278
#define SDL_PutAudioStreamPlanarData SDL_PutAudioStreamPlanarData_REAL
12791279
#define SDL_GetEventDescription SDL_GetEventDescription_REAL
12801280
#define SDL_PutAudioStreamDataNoCopy SDL_PutAudioStreamDataNoCopy_REAL
1281+
#define SDL_AddAtomicU32 SDL_AddAtomicU32_REAL

src/dynapi/SDL_dynapi_procs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,3 +1286,4 @@ SDL_DYNAPI_PROC(SDL_Renderer*,SDL_CreateGPURenderer,(SDL_Window *a,SDL_GPUShader
12861286
SDL_DYNAPI_PROC(bool,SDL_PutAudioStreamPlanarData,(SDL_AudioStream *a,const void * const*b,int c,int d),(a,b,c,d),return)
12871287
SDL_DYNAPI_PROC(int,SDL_GetEventDescription,(const SDL_Event *a,char *b,int c),(a,b,c),return)
12881288
SDL_DYNAPI_PROC(bool,SDL_PutAudioStreamDataNoCopy,(SDL_AudioStream *a,const void *b,int c,SDL_AudioStreamDataCompleteCallback d,void *e),(a,b,c,d,e),return)
1289+
SDL_DYNAPI_PROC(Uint32,SDL_AddAtomicU32,(SDL_AtomicU32 *a,int b),(a,b),return)

0 commit comments

Comments
 (0)