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
7namespace SDL {
8
40using AtomicIntRaw = SDL_AtomicInt;
41
42// Forward decl
43struct AtomicInt;
44
46using AtomicU32Raw = SDL_AtomicU32;
47
48// Forward decl
49struct AtomicU32;
50
69inline void MemoryBarrierRelease() { SDL_MemoryBarrierReleaseFunction(); }
70
89inline void MemoryBarrierAcquire() { SDL_MemoryBarrierAcquireFunction(); }
90
91#ifdef SDL3PP_DOC
92
109#define SDL_CompilerBarrier() DoCompilerSpecificReadWriteBarrier()
110
145#define SDL_MemoryBarrierRelease() SDL_MemoryBarrierReleaseFunction()
146
167#define SDL_MemoryBarrierAcquire() SDL_MemoryBarrierAcquireFunction()
168
185#define SDL_CPUPauseInstruction() \
186 DoACPUPauseInACompilerAndArchitectureSpecificWay
187
188#endif // SDL3PP_DOC
189
216{
222 constexpr AtomicInt(int value)
223 : AtomicIntRaw(value)
224 {
225 }
226
227 AtomicInt(const AtomicInt& value) = delete;
228
229 AtomicInt& operator=(const AtomicInt& value) = delete;
230
248 bool CompareAndSwap(int oldval, int newval);
249
267 int Set(int v);
268
283 int Get();
284
303 int Add(int v);
304
318 bool AtomicIncRef();
319
334 bool AtomicDecRef();
335};
336
355inline bool CompareAndSwapAtomicInt(AtomicIntRaw* a, int oldval, int newval)
356{
357 return SDL_CompareAndSwapAtomicInt(a, oldval, newval);
358}
359
360inline bool AtomicInt::CompareAndSwap(int oldval, int newval)
361{
362 return SDL::CompareAndSwapAtomicInt(this, oldval, newval);
363}
364
383inline int SetAtomicInt(AtomicIntRaw* a, int v)
384{
385 return SDL_SetAtomicInt(a, v);
386}
387
388inline int AtomicInt::Set(int v) { return SDL::SetAtomicInt(this, v); }
389
405inline int GetAtomicInt(AtomicIntRaw* a) { return SDL_GetAtomicInt(a); }
406
407inline int AtomicInt::Get() { return SDL::GetAtomicInt(this); }
408
428inline int AddAtomicInt(AtomicIntRaw* a, int v)
429{
430 return SDL_AddAtomicInt(a, v);
431}
432
433inline int AtomicInt::Add(int v) { return SDL::AddAtomicInt(this, v); }
434
449inline bool AtomicIncRef(AtomicIntRaw* a) { return SDL_AtomicIncRef(a); }
450
451inline bool AtomicInt::AtomicIncRef() { return SDL::AtomicIncRef(this); }
452
468inline bool AtomicDecRef(AtomicIntRaw* a) { return SDL_AtomicDecRef(a); }
469
470inline bool AtomicInt::AtomicDecRef() { return SDL::AtomicDecRef(this); }
471
497{
503 constexpr AtomicU32(Uint32 value)
504 : AtomicU32Raw(value)
505 {
506 }
507
508 AtomicU32(const AtomicU32& value) = delete;
509
510 AtomicU32& operator=(const AtomicU32& value) = delete;
511
529 bool CompareAndSwap(Uint32 oldval, Uint32 newval);
530
548 Uint32 Set(Uint32 v);
549
564 Uint32 Get();
565};
566
568template<class T>
570{
571 T* m_value;
572
573public:
575 constexpr AtomicPointer(T* value)
576 : m_value(value)
577 {
578 }
579
580 AtomicPointer(const AtomicPointer& value) = delete;
581
582 AtomicPointer& operator=(const AtomicPointer& value) = delete;
583
602 bool CompareAndSwap(T* oldval, T* newval);
603
620 T* Set(T* v);
621
637 T* Get();
638};
639
659 Uint32 oldval,
660 Uint32 newval)
661{
662 return SDL_CompareAndSwapAtomicU32(a, oldval, newval);
663}
664
665inline bool AtomicU32::CompareAndSwap(Uint32 oldval, Uint32 newval)
666{
667 return SDL::CompareAndSwapAtomicU32(this, oldval, newval);
668}
669
689{
690 return SDL_SetAtomicU32(a, v);
691}
692
693inline Uint32 AtomicU32::Set(Uint32 v) { return SDL::SetAtomicU32(this, v); }
694
710inline Uint32 GetAtomicU32(AtomicU32Raw* a) { return SDL_GetAtomicU32(a); }
711
712inline Uint32 AtomicU32::Get() { return SDL::GetAtomicU32(this); }
713
714template<class T>
715inline bool AtomicPointer<T>::CompareAndSwap(T* oldval, T* newval)
716{
717 return SDL_CompareAndSwapAtomicPointer(&m_value, oldval, newval);
718}
719
720template<class T>
722{
723 return SDL_SetAtomicPointer(&m_value, v);
724}
725
726template<class T>
728{
729 return SDL_GetAtomicPointer(&m_value);
730}
731
733
734} // namespace SDL
735
736#endif /* SDL3PP_ATOMIC_H_ */
A type representing an atomic of an arbitrary pointer.
Definition: SDL3pp_atomic.h:570
constexpr AtomicPointer(T *value)
Construcst from T.
Definition: SDL3pp_atomic.h:575
SDL_AtomicU32 AtomicU32Raw
Alias to raw representation for AtomicU32.
Definition: SDL3pp_atomic.h:46
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:665
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:360
T * Get()
Get the value of a pointer atomically.
Definition: SDL3pp_atomic.h:727
int Add(int v)
Add to an atomic variable.
Definition: SDL3pp_atomic.h:433
bool AtomicIncRef(AtomicIntRaw *a)
Increment an atomic variable used as a reference count.
Definition: SDL3pp_atomic.h:449
T * Set(T *v)
Set a pointer to a value atomically.
Definition: SDL3pp_atomic.h:721
bool AtomicDecRef()
Decrement an atomic variable used as a reference count.
Definition: SDL3pp_atomic.h:470
int GetAtomicInt(AtomicIntRaw *a)
Get the value of an atomic variable.
Definition: SDL3pp_atomic.h:405
bool AtomicDecRef(AtomicIntRaw *a)
Decrement an atomic variable used as a reference count.
Definition: SDL3pp_atomic.h:468
SDL_AtomicInt AtomicIntRaw
Alias to raw representation for AtomicInt.
Definition: SDL3pp_atomic.h:40
Uint32 Set(Uint32 v)
Set an atomic variable to a value.
Definition: SDL3pp_atomic.h:693
int Get()
Get the value of an atomic variable.
Definition: SDL3pp_atomic.h:407
Uint32 GetAtomicU32(AtomicU32Raw *a)
Get the value of an atomic variable.
Definition: SDL3pp_atomic.h:710
Uint32 SetAtomicU32(AtomicU32Raw *a, Uint32 v)
Set an atomic variable to a value.
Definition: SDL3pp_atomic.h:688
bool CompareAndSwap(T *oldval, T *newval)
Set a pointer to a new value if it is currently an old value.
Definition: SDL3pp_atomic.h:715
bool AtomicIncRef()
Increment an atomic variable used as a reference count.
Definition: SDL3pp_atomic.h:451
int SetAtomicInt(AtomicIntRaw *a, int v)
Set an atomic variable to a value.
Definition: SDL3pp_atomic.h:383
int Set(int v)
Set an atomic variable to a value.
Definition: SDL3pp_atomic.h:388
void MemoryBarrierAcquire()
Insert a memory acquire barrier (function version).
Definition: SDL3pp_atomic.h:89
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:355
void MemoryBarrierRelease()
Insert a memory release barrier (function version).
Definition: SDL3pp_atomic.h:69
Uint32 Get()
Get the value of an atomic variable.
Definition: SDL3pp_atomic.h:712
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:658
int AddAtomicInt(AtomicIntRaw *a, int v)
Add to an atomic variable.
Definition: SDL3pp_atomic.h:428
::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:216
constexpr AtomicInt(int value)
Wraps AtomicInt.
Definition: SDL3pp_atomic.h:222
A type representing an atomic unsigned 32-bit value.
Definition: SDL3pp_atomic.h:497
constexpr AtomicU32(Uint32 value)
Wraps value.
Definition: SDL3pp_atomic.h:503