SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_atomic.h
1#ifndef SDL3PP_ATOMIC_H_
2#define SDL3PP_ATOMIC_H_
3
4#include <SDL3/SDL_atomic.h>
5#include "SDL3pp_stdinc.h"
6#include "SDL3pp_version.h"
7
8namespace SDL {
9
41using AtomicIntRaw = SDL_AtomicInt;
42
43// Forward decl
44struct AtomicInt;
45
47using AtomicU32Raw = SDL_AtomicU32;
48
49// Forward decl
50struct AtomicU32;
51
68SDL_FORCE_INLINE void CompilerBarrier() { SDL_CompilerBarrier(); }
69
89{
90 SDL_MemoryBarrierReleaseFunction();
91}
92
112{
113 SDL_MemoryBarrierAcquireFunction();
114}
115
150SDL_FORCE_INLINE void MemoryBarrierRelease() { SDL_MemoryBarrierRelease(); }
151
172SDL_FORCE_INLINE void MemoryBarrierAcquire() { SDL_MemoryBarrierAcquire(); }
173
190SDL_FORCE_INLINE void CPUPauseInstruction() { SDL_CPUPauseInstruction(); }
191
218{
224 constexpr AtomicInt(int value)
225 : AtomicIntRaw(value)
226 {
227 }
228
229 AtomicInt(const AtomicInt& value) = delete;
230
231 AtomicInt& operator=(const AtomicInt& value) = delete;
232
250 bool CompareAndSwap(int oldval, int newval);
251
269 int Set(int v);
270
285 int Get();
286
305 int Add(int v);
306
321 bool AtomicIncRef();
322
338 bool AtomicDecRef();
339};
340
359inline bool CompareAndSwapAtomicInt(AtomicIntRaw* a, int oldval, int newval)
360{
361 return SDL_CompareAndSwapAtomicInt(a, oldval, newval);
362}
363
364inline bool AtomicInt::CompareAndSwap(int oldval, int newval)
365{
366 return SDL::CompareAndSwapAtomicInt(this, oldval, newval);
367}
368
387inline int SetAtomicInt(AtomicIntRaw* a, int v)
388{
389 return SDL_SetAtomicInt(a, v);
390}
391
392inline int AtomicInt::Set(int v) { return SDL::SetAtomicInt(this, v); }
393
409inline int GetAtomicInt(AtomicIntRaw* a) { return SDL_GetAtomicInt(a); }
410
411inline int AtomicInt::Get() { return SDL::GetAtomicInt(this); }
412
432inline int AddAtomicInt(AtomicIntRaw* a, int v)
433{
434 return SDL_AddAtomicInt(a, v);
435}
436
437inline int AtomicInt::Add(int v) { return SDL::AddAtomicInt(this, v); }
438
454inline bool AtomicIncRef(AtomicIntRaw* a) { return SDL_AtomicIncRef(a); }
455
456inline bool AtomicInt::AtomicIncRef() { return SDL::AtomicIncRef(this); }
457
474inline bool AtomicDecRef(AtomicIntRaw* a) { return SDL_AtomicDecRef(a); }
475
476inline bool AtomicInt::AtomicDecRef() { return SDL::AtomicDecRef(this); }
477
503{
509 constexpr AtomicU32(Uint32 value)
510 : AtomicU32Raw(value)
511 {
512 }
513
514 AtomicU32(const AtomicU32& value) = delete;
515
516 AtomicU32& operator=(const AtomicU32& value) = delete;
517
535 bool CompareAndSwap(Uint32 oldval, Uint32 newval);
536
554 Uint32 Set(Uint32 v);
555
570 Uint32 Get();
571
572#if SDL_VERSION_ATLEAST(3, 4, 0)
573
589 Uint32 Add(int v);
590
591#endif // SDL_VERSION_ATLEAST(3, 4, 0)
592};
593
595template<class T>
597{
598 T* m_value;
599
600public:
602 constexpr AtomicPointer(T* value)
603 : m_value(value)
604 {
605 }
606
607 AtomicPointer(const AtomicPointer& value) = delete;
608
609 AtomicPointer& operator=(const AtomicPointer& value) = delete;
610
629 bool CompareAndSwap(T* oldval, T* newval);
630
647 T* Set(T* v);
648
664 T* Get();
665};
666
686 Uint32 oldval,
687 Uint32 newval)
688{
689 return SDL_CompareAndSwapAtomicU32(a, oldval, newval);
690}
691
692inline bool AtomicU32::CompareAndSwap(Uint32 oldval, Uint32 newval)
693{
694 return SDL::CompareAndSwapAtomicU32(this, oldval, newval);
695}
696
716{
717 return SDL_SetAtomicU32(a, v);
718}
719
720inline Uint32 AtomicU32::Set(Uint32 v) { return SDL::SetAtomicU32(this, v); }
721
737inline Uint32 GetAtomicU32(AtomicU32Raw* a) { return SDL_GetAtomicU32(a); }
738
739inline Uint32 AtomicU32::Get() { return SDL::GetAtomicU32(this); }
740
741#if SDL_VERSION_ATLEAST(3, 4, 0)
742
760{
761 return SDL_AddAtomicU32(a, v);
762}
763
764inline Uint32 AtomicU32::Add(int v) { return SDL::AddAtomicU32(this, v); }
765
766#endif // SDL_VERSION_ATLEAST(3, 4, 0)
767
768template<class T>
769inline bool AtomicPointer<T>::CompareAndSwap(T* oldval, T* newval)
770{
771 return SDL_CompareAndSwapAtomicPointer(&m_value, oldval, newval);
772}
773
774template<class T>
776{
777 return SDL_SetAtomicPointer(&m_value, v);
778}
779
780template<class T>
782{
783 return SDL_GetAtomicPointer(&m_value);
784}
785
787
788} // namespace SDL
789
790#endif /* SDL3PP_ATOMIC_H_ */
A type representing an atomic of an arbitrary pointer.
Definition: SDL3pp_atomic.h:597
constexpr AtomicPointer(T *value)
Construcst from T.
Definition: SDL3pp_atomic.h:602
SDL_AtomicU32 AtomicU32Raw
Alias to raw representation for AtomicU32.
Definition: SDL3pp_atomic.h:47
bool CompareAndSwap(Uint32 oldval, Uint32 newval)
Set an atomic variable to a new value if it is currently an old value.
Definition: SDL3pp_atomic.h:692
bool CompareAndSwap(int oldval, int newval)
Set an atomic variable to a new value if it is currently an old value.
Definition: SDL3pp_atomic.h:364
T * Get()
Get the value of a pointer atomically.
Definition: SDL3pp_atomic.h:781
int Add(int v)
Add to an atomic variable.
Definition: SDL3pp_atomic.h:437
bool AtomicIncRef(AtomicIntRaw *a)
Increment an atomic variable used as a reference count.
Definition: SDL3pp_atomic.h:454
T * Set(T *v)
Set a pointer to a value atomically.
Definition: SDL3pp_atomic.h:775
bool AtomicDecRef()
Decrement an atomic variable used as a reference count.
Definition: SDL3pp_atomic.h:476
void MemoryBarrierReleaseFunction()
Insert a memory release barrier (function version).
Definition: SDL3pp_atomic.h:88
int GetAtomicInt(AtomicIntRaw *a)
Get the value of an atomic variable.
Definition: SDL3pp_atomic.h:409
bool AtomicDecRef(AtomicIntRaw *a)
Decrement an atomic variable used as a reference count.
Definition: SDL3pp_atomic.h:474
Uint32 Add(int v)
Add to an atomic variable.
Definition: SDL3pp_atomic.h:764
SDL_AtomicInt AtomicIntRaw
Alias to raw representation for AtomicInt.
Definition: SDL3pp_atomic.h:41
Uint32 Set(Uint32 v)
Set an atomic variable to a value.
Definition: SDL3pp_atomic.h:720
int Get()
Get the value of an atomic variable.
Definition: SDL3pp_atomic.h:411
SDL_FORCE_INLINE void MemoryBarrierAcquire()
Insert a memory acquire barrier.
Definition: SDL3pp_atomic.h:172
Uint32 GetAtomicU32(AtomicU32Raw *a)
Get the value of an atomic variable.
Definition: SDL3pp_atomic.h:737
void MemoryBarrierAcquireFunction()
Insert a memory acquire barrier (function version).
Definition: SDL3pp_atomic.h:111
SDL_FORCE_INLINE void MemoryBarrierRelease()
Insert a memory release barrier.
Definition: SDL3pp_atomic.h:150
Uint32 SetAtomicU32(AtomicU32Raw *a, Uint32 v)
Set an atomic variable to a value.
Definition: SDL3pp_atomic.h:715
bool CompareAndSwap(T *oldval, T *newval)
Set a pointer to a new value if it is currently an old value.
Definition: SDL3pp_atomic.h:769
bool AtomicIncRef()
Increment an atomic variable used as a reference count.
Definition: SDL3pp_atomic.h:456
int SetAtomicInt(AtomicIntRaw *a, int v)
Set an atomic variable to a value.
Definition: SDL3pp_atomic.h:387
Uint32 AddAtomicU32(AtomicU32Raw *a, int v)
Add to an atomic variable.
Definition: SDL3pp_atomic.h:759
SDL_FORCE_INLINE void CompilerBarrier()
Mark a compiler barrier.
Definition: SDL3pp_atomic.h:68
int Set(int v)
Set an atomic variable to a value.
Definition: SDL3pp_atomic.h:392
SDL_FORCE_INLINE void CPUPauseInstruction()
A function to insert a CPU-specific "pause" instruction into the program.
Definition: SDL3pp_atomic.h:190
bool CompareAndSwapAtomicInt(AtomicIntRaw *a, int oldval, int newval)
Set an atomic variable to a new value if it is currently an old value.
Definition: SDL3pp_atomic.h:359
Uint32 Get()
Get the value of an atomic variable.
Definition: SDL3pp_atomic.h:739
bool CompareAndSwapAtomicU32(AtomicU32Raw *a, Uint32 oldval, Uint32 newval)
Set an atomic variable to a new value if it is currently an old value.
Definition: SDL3pp_atomic.h:685
int AddAtomicInt(AtomicIntRaw *a, int v)
Add to an atomic variable.
Definition: SDL3pp_atomic.h:432
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:341
Main include header for the SDL3pp library.
A type representing an atomic integer value.
Definition: SDL3pp_atomic.h:218
constexpr AtomicInt(int value)
Wraps AtomicInt.
Definition: SDL3pp_atomic.h:224
A type representing an atomic unsigned 32-bit value.
Definition: SDL3pp_atomic.h:503
constexpr AtomicU32(Uint32 value)
Wraps value.
Definition: SDL3pp_atomic.h:509