Skip to content

Commit 03dbf73

Browse files
committed
Add relaxed atomics
1 parent 5d9b170 commit 03dbf73

File tree

5 files changed

+456
-9
lines changed

5 files changed

+456
-9
lines changed

include/SDL3/SDL_atomic.h

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,27 @@ typedef struct SDL_AtomicInt { int value; } SDL_AtomicInt;
414414
*/
415415
extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapAtomicInt(SDL_AtomicInt *a, int oldval, int newval);
416416

417+
/**
418+
* Set an atomic variable to a new value if it is currently an old value,
419+
* using relaxed memory ordering.
420+
*
421+
* ***Note: If you don't know what this function is for, you shouldn't use
422+
* it!***
423+
*
424+
* \param a a pointer to an SDL_AtomicInt variable to be modified.
425+
* \param oldval the old value.
426+
* \param newval the new value.
427+
* \returns true if the atomic variable was set, false otherwise.
428+
*
429+
* \threadsafety It is safe to call this function from any thread.
430+
*
431+
* \since This function is available since SDL 3.4.0.
432+
*
433+
* \sa SDL_GetRelaxedAtomicInt
434+
* \sa SDL_SetRelaxedAtomicInt
435+
*/
436+
extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapRelaxedAtomicInt(SDL_AtomicInt *a, int oldval, int newval);
437+
417438
/**
418439
* Set an atomic variable to a value.
419440
*
@@ -434,6 +455,24 @@ extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapAtomicInt(SDL_AtomicInt *a, i
434455
*/
435456
extern SDL_DECLSPEC int SDLCALL SDL_SetAtomicInt(SDL_AtomicInt *a, int v);
436457

458+
/**
459+
* Set an atomic variable to a value using relaxed memory ordering.
460+
*
461+
* ***Note: If you don't know what this function is for, you shouldn't use
462+
* it!***
463+
*
464+
* \param a a pointer to an SDL_AtomicInt variable to be modified.
465+
* \param v the desired value.
466+
* \returns the previous value of the atomic variable.
467+
*
468+
* \threadsafety It is safe to call this function from any thread.
469+
*
470+
* \since This function is available since SDL 3.4.0.
471+
*
472+
* \sa SDL_GetRelaxedAtomicInt
473+
*/
474+
extern SDL_DECLSPEC int SDLCALL SDL_SetRelaxedAtomicInt(SDL_AtomicInt *a, int v);
475+
437476
/**
438477
* Get the value of an atomic variable.
439478
*
@@ -451,6 +490,23 @@ extern SDL_DECLSPEC int SDLCALL SDL_SetAtomicInt(SDL_AtomicInt *a, int v);
451490
*/
452491
extern SDL_DECLSPEC int SDLCALL SDL_GetAtomicInt(SDL_AtomicInt *a);
453492

493+
/**
494+
* Get the value of an atomic variable using relaxed memory ordering.
495+
*
496+
* ***Note: If you don't know what this function is for, you shouldn't use
497+
* it!***
498+
*
499+
* \param a a pointer to an SDL_AtomicInt variable.
500+
* \returns the current value of an atomic variable.
501+
*
502+
* \threadsafety It is safe to call this function from any thread.
503+
*
504+
* \since This function is available since SDL 3.4.0.
505+
*
506+
* \sa SDL_SetRelaxedAtomicInt
507+
*/
508+
extern SDL_DECLSPEC int SDLCALL SDL_GetRelaxedAtomicInt(SDL_AtomicInt *a);
509+
454510
/**
455511
* Add to an atomic variable.
456512
*
@@ -472,6 +528,25 @@ extern SDL_DECLSPEC int SDLCALL SDL_GetAtomicInt(SDL_AtomicInt *a);
472528
*/
473529
extern SDL_DECLSPEC int SDLCALL SDL_AddAtomicInt(SDL_AtomicInt *a, int v);
474530

531+
/**
532+
* Add to an atomic variable using relaxed memory ordering.
533+
*
534+
* ***Note: If you don't know what this function is for, you shouldn't use
535+
* it!***
536+
*
537+
* \param a a pointer to an SDL_AtomicInt variable to be modified.
538+
* \param v the desired value to add.
539+
* \returns the previous value of the atomic variable.
540+
*
541+
* \threadsafety It is safe to call this function from any thread.
542+
*
543+
* \since This function is available since SDL 3.4.0.
544+
*
545+
* \sa SDL_RelaxedAtomicDecRef
546+
* \sa SDL_RelaxedAtomicIncRef
547+
*/
548+
extern SDL_DECLSPEC int SDLCALL SDL_AddRelaxedAtomicInt(SDL_AtomicInt *a, int v);
549+
475550
#ifndef SDL_AtomicIncRef
476551

477552
/**
@@ -491,6 +566,25 @@ extern SDL_DECLSPEC int SDLCALL SDL_AddAtomicInt(SDL_AtomicInt *a, int v);
491566
#define SDL_AtomicIncRef(a) SDL_AddAtomicInt(a, 1)
492567
#endif
493568

569+
#ifndef SDL_RelaxedAtomicIncRef
570+
571+
/**
572+
* Increment an atomic variable used as a reference count, using relaxed memory ordering.
573+
*
574+
* ***Note: If you don't know what this macro is for, you shouldn't use it!***
575+
*
576+
* \param a a pointer to an SDL_AtomicInt to increment.
577+
* \returns the previous value of the atomic variable.
578+
*
579+
* \threadsafety It is safe to call this macro from any thread.
580+
*
581+
* \since This macro is available since SDL 3.4.0.
582+
*
583+
* \sa SDL_RelaxedAtomicDecRef
584+
*/
585+
#define SDL_RelaxedAtomicIncRef(a) SDL_AddRelaxedAtomicInt(a, 1)
586+
#endif
587+
494588
#ifndef SDL_AtomicDecRef
495589

496590
/**
@@ -511,6 +605,26 @@ extern SDL_DECLSPEC int SDLCALL SDL_AddAtomicInt(SDL_AtomicInt *a, int v);
511605
#define SDL_AtomicDecRef(a) (SDL_AddAtomicInt(a, -1) == 1)
512606
#endif
513607

608+
#ifndef SDL_RelaxedAtomicDecRef
609+
610+
/**
611+
* Decrement an atomic variable used as a reference count, using relaxed memory ordering.
612+
*
613+
* ***Note: If you don't know what this macro is for, you shouldn't use it!***
614+
*
615+
* \param a a pointer to an SDL_AtomicInt to decrement.
616+
* \returns true if the variable reached zero after decrementing, false
617+
* otherwise.
618+
*
619+
* \threadsafety It is safe to call this macro from any thread.
620+
*
621+
* \since This macro is available since SDL 3.4.0.
622+
*
623+
* \sa SDL_AtomicIncRef
624+
*/
625+
#define SDL_RelaxedAtomicDecRef(a) (SDL_AddRelaxedAtomicInt(a, -1) == 1)
626+
#endif
627+
514628
/**
515629
* A type representing an atomic unsigned 32-bit value.
516630
*
@@ -559,6 +673,27 @@ typedef struct SDL_AtomicU32 { Uint32 value; } SDL_AtomicU32;
559673
*/
560674
extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapAtomicU32(SDL_AtomicU32 *a, Uint32 oldval, Uint32 newval);
561675

676+
/**
677+
* Set an atomic variable to a new value if it is currently an old value,
678+
* using relaxed memory ordering.
679+
*
680+
* ***Note: If you don't know what this function is for, you shouldn't use
681+
* it!***
682+
*
683+
* \param a a pointer to an SDL_AtomicU32 variable to be modified.
684+
* \param oldval the old value.
685+
* \param newval the new value.
686+
* \returns true if the atomic variable was set, false otherwise.
687+
*
688+
* \threadsafety It is safe to call this function from any thread.
689+
*
690+
* \since This function is available since SDL 3.4.0.
691+
*
692+
* \sa SDL_GetRelaxedAtomicU32
693+
* \sa SDL_SetRelaxedAtomicU32
694+
*/
695+
extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapRelaxedAtomicU32(SDL_AtomicU32 *a, Uint32 oldval, Uint32 newval);
696+
562697
/**
563698
* Set an atomic variable to a value.
564699
*
@@ -579,6 +714,24 @@ extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapAtomicU32(SDL_AtomicU32 *a, U
579714
*/
580715
extern SDL_DECLSPEC Uint32 SDLCALL SDL_SetAtomicU32(SDL_AtomicU32 *a, Uint32 v);
581716

717+
/**
718+
* Set an atomic variable to a value using relaxed memory ordering.
719+
*
720+
* ***Note: If you don't know what this function is for, you shouldn't use
721+
* it!***
722+
*
723+
* \param a a pointer to an SDL_AtomicU32 variable to be modified.
724+
* \param v the desired value.
725+
* \returns the previous value of the atomic variable.
726+
*
727+
* \threadsafety It is safe to call this function from any thread.
728+
*
729+
* \since This function is available since SDL 3.4.0.
730+
*
731+
* \sa SDL_GetRelaxedAtomicU32
732+
*/
733+
extern SDL_DECLSPEC Uint32 SDLCALL SDL_SetRelaxedAtomicU32(SDL_AtomicU32 *a, Uint32 v);
734+
582735
/**
583736
* Get the value of an atomic variable.
584737
*
@@ -596,6 +749,23 @@ extern SDL_DECLSPEC Uint32 SDLCALL SDL_SetAtomicU32(SDL_AtomicU32 *a, Uint32 v);
596749
*/
597750
extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetAtomicU32(SDL_AtomicU32 *a);
598751

752+
/**
753+
* Get the value of an atomic variable using relaxed memory ordering.
754+
*
755+
* ***Note: If you don't know what this function is for, you shouldn't use
756+
* it!***
757+
*
758+
* \param a a pointer to an SDL_AtomicU32 variable.
759+
* \returns the current value of an atomic variable.
760+
*
761+
* \threadsafety It is safe to call this function from any thread.
762+
*
763+
* \since This function is available since SDL 3.4.0.
764+
*
765+
* \sa SDL_SetRelaxedAtomicU32
766+
*/
767+
extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetRelaxedAtomicU32(SDL_AtomicU32 *a);
768+
599769
/**
600770
* Add to an atomic variable.
601771
*
@@ -614,6 +784,22 @@ extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetAtomicU32(SDL_AtomicU32 *a);
614784
*/
615785
extern SDL_DECLSPEC Uint32 SDLCALL SDL_AddAtomicU32(SDL_AtomicU32 *a, int v);
616786

787+
/**
788+
* Add to an atomic variable using relaxed memory ordering.
789+
*
790+
* ***Note: If you don't know what this function is for, you shouldn't use
791+
* it!***
792+
*
793+
* \param a a pointer to an SDL_AtomicU32 variable to be modified.
794+
* \param v the desired value to add or subtract.
795+
* \returns the previous value of the atomic variable.
796+
*
797+
* \threadsafety It is safe to call this function from any thread.
798+
*
799+
* \since This function is available since SDL 3.4.0.
800+
*/
801+
extern SDL_DECLSPEC Uint32 SDLCALL SDL_AddRelaxedAtomicU32(SDL_AtomicU32 *a, int v);
802+
617803
/**
618804
* Set a pointer to a new value if it is currently an old value.
619805
*
@@ -635,6 +821,28 @@ extern SDL_DECLSPEC Uint32 SDLCALL SDL_AddAtomicU32(SDL_AtomicU32 *a, int v);
635821
*/
636822
extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapAtomicPointer(void **a, void *oldval, void *newval);
637823

824+
/**
825+
* Set a pointer to a new value if it is currently an old value,
826+
* using relaxed memory ordering.
827+
*
828+
* ***Note: If you don't know what this function is for, you shouldn't use
829+
* it!***
830+
*
831+
* \param a a pointer to a pointer.
832+
* \param oldval the old pointer value.
833+
* \param newval the new pointer value.
834+
* \returns true if the pointer was set, false otherwise.
835+
*
836+
* \threadsafety It is safe to call this function from any thread.
837+
*
838+
* \since This function is available since SDL 3.4.0.
839+
*
840+
* \sa SDL_CompareAndSwapRelaxedAtomicInt
841+
* \sa SDL_GetRelaxedAtomicPointer
842+
* \sa SDL_SetRelaxedAtomicPointer
843+
*/
844+
extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapRelaxedAtomicPointer(void **a, void *oldval, void *newval);
845+
638846
/**
639847
* Set a pointer to a value atomically.
640848
*
@@ -654,6 +862,25 @@ extern SDL_DECLSPEC bool SDLCALL SDL_CompareAndSwapAtomicPointer(void **a, void
654862
*/
655863
extern SDL_DECLSPEC void * SDLCALL SDL_SetAtomicPointer(void **a, void *v);
656864

865+
/**
866+
* Set a pointer to a value atomically using relaxed memory ordering.
867+
*
868+
* ***Note: If you don't know what this function is for, you shouldn't use
869+
* it!***
870+
*
871+
* \param a a pointer to a pointer.
872+
* \param v the desired pointer value.
873+
* \returns the previous value of the pointer.
874+
*
875+
* \threadsafety It is safe to call this function from any thread.
876+
*
877+
* \since This function is available since SDL 3.4.0.
878+
*
879+
* \sa SDL_CompareAndSwapRelaxedAtomicPointer
880+
* \sa SDL_GetRelaxedAtomicPointer
881+
*/
882+
extern SDL_DECLSPEC void * SDLCALL SDL_SetRelaxedAtomicPointer(void **a, void *v);
883+
657884
/**
658885
* Get the value of a pointer atomically.
659886
*
@@ -672,6 +899,24 @@ extern SDL_DECLSPEC void * SDLCALL SDL_SetAtomicPointer(void **a, void *v);
672899
*/
673900
extern SDL_DECLSPEC void * SDLCALL SDL_GetAtomicPointer(void **a);
674901

902+
/**
903+
* Get the value of a pointer atomically using relaxed memory ordering.
904+
*
905+
* ***Note: If you don't know what this function is for, you shouldn't use
906+
* it!***
907+
*
908+
* \param a a pointer to a pointer.
909+
* \returns the current value of a pointer.
910+
*
911+
* \threadsafety It is safe to call this function from any thread.
912+
*
913+
* \since This function is available since SDL 3.4.0.
914+
*
915+
* \sa SDL_CompareAndSwapRelaxedAtomicPointer
916+
* \sa SDL_SetRelaxedAtomicPointer
917+
*/
918+
extern SDL_DECLSPEC void * SDLCALL SDL_GetRelaxedAtomicPointer(void **a);
919+
675920
/* Ends C function definitions when using C++ */
676921
#ifdef __cplusplus
677922
}

0 commit comments

Comments
 (0)