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
147#define SDL_MemoryBarrierRelease() SDL_MemoryBarrierReleaseFunction()
148
170#define SDL_MemoryBarrierAcquire() SDL_MemoryBarrierAcquireFunction()
171
188#define SDL_CPUPauseInstruction() \
189 DoACPUPauseInACompilerAndArchitectureSpecificWay
190
191#endif // SDL3PP_DOC
192
221{
227 constexpr AtomicInt(int value)
228 : AtomicIntRaw(value)
229 {
230 }
231
232 AtomicInt(const AtomicInt& value) = delete;
233
234 AtomicInt& operator=(const AtomicInt& value) = delete;
235
253 bool CompareAndSwap(int oldval, int newval);
254
272 int Set(int v);
273
288 int Get();
289
308 int Add(int v);
309
323 bool AtomicIncRef();
324
339 bool AtomicDecRef();
340};
341
360inline bool CompareAndSwapAtomicInt(AtomicIntRaw* a, int oldval, int newval)
361{
362 return SDL_CompareAndSwapAtomicInt(a, oldval, newval);
363}
364
365inline bool AtomicInt::CompareAndSwap(int oldval, int newval)
366{
367 return SDL::CompareAndSwapAtomicInt(this, oldval, newval);
368}
369
388inline int SetAtomicInt(AtomicIntRaw* a, int v)
389{
390 return SDL_SetAtomicInt(a, v);
391}
392
393inline int AtomicInt::Set(int v) { return SDL::SetAtomicInt(this, v); }
394
410inline int GetAtomicInt(AtomicIntRaw* a) { return SDL_GetAtomicInt(a); }
411
412inline int AtomicInt::Get() { return SDL::GetAtomicInt(this); }
413
433inline int AddAtomicInt(AtomicIntRaw* a, int v)
434{
435 return SDL_AddAtomicInt(a, v);
436}
437
438inline int AtomicInt::Add(int v) { return SDL::AddAtomicInt(this, v); }
439
454inline bool AtomicIncRef(AtomicIntRaw* a) { return SDL_AtomicIncRef(a); }
455
456inline bool AtomicInt::AtomicIncRef() { return SDL::AtomicIncRef(this); }
457
473inline bool AtomicDecRef(AtomicIntRaw* a) { return SDL_AtomicDecRef(a); }
474
475inline bool AtomicInt::AtomicDecRef() { return SDL::AtomicDecRef(this); }
476
504{
510 constexpr AtomicU32(Uint32 value)
511 : AtomicU32Raw(value)
512 {
513 }
514
515 AtomicU32(const AtomicU32& value) = delete;
516
517 AtomicU32& operator=(const AtomicU32& value) = delete;
518
536 bool CompareAndSwap(Uint32 oldval, Uint32 newval);
537
555 Uint32 Set(Uint32 v);
556
571 Uint32 Get();
572};
573
574template<class T>
576{
577 T* m_value;
578
579public:
581 constexpr AtomicPointer(T* value)
582 : m_value(value)
583 {
584 }
585
586 AtomicPointer(const AtomicPointer& value) = delete;
587
588 AtomicPointer& operator=(const AtomicPointer& value) = delete;
589
608 bool CompareAndSwap(T* oldval, T* newval);
609
626 T* Set(T* v);
627
643 T* Get();
644};
645
665 Uint32 oldval,
666 Uint32 newval)
667{
668 return SDL_CompareAndSwapAtomicU32(a, oldval, newval);
669}
670
671inline bool AtomicU32::CompareAndSwap(Uint32 oldval, Uint32 newval)
672{
673 return SDL::CompareAndSwapAtomicU32(this, oldval, newval);
674}
675
695{
696 return SDL_SetAtomicU32(a, v);
697}
698
699inline Uint32 AtomicU32::Set(Uint32 v) { return SDL::SetAtomicU32(this, v); }
700
716inline Uint32 GetAtomicU32(AtomicU32Raw* a) { return SDL_GetAtomicU32(a); }
717
718inline Uint32 AtomicU32::Get() { return SDL::GetAtomicU32(this); }
719
720template<class T>
721inline bool AtomicPointer<T>::CompareAndSwap(T* oldval, T* newval)
722{
723 return SDL_CompareAndSwapAtomicPointer(&m_value, oldval, newval);
724}
725
726template<class T>
728{
729 return SDL_SetAtomicPointer(&m_value, v);
730}
731
732template<class T>
734{
735 return SDL_GetAtomicPointer(&m_value);
736}
737
739
740} // namespace SDL
741
742#endif /* SDL3PP_ATOMIC_H_ */
Definition: SDL3pp_atomic.h:576
constexpr AtomicPointer(T *value)
Construcst from T.
Definition: SDL3pp_atomic.h:581
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:671
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:365
T * Get()
Get the value of a pointer atomically.
Definition: SDL3pp_atomic.h:733
int Add(int v)
Add to an atomic variable.
Definition: SDL3pp_atomic.h:438
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:727
bool AtomicDecRef()
Decrement an atomic variable used as a reference count.
Definition: SDL3pp_atomic.h:475
int GetAtomicInt(AtomicIntRaw *a)
Get the value of an atomic variable.
Definition: SDL3pp_atomic.h:410
bool AtomicDecRef(AtomicIntRaw *a)
Decrement an atomic variable used as a reference count.
Definition: SDL3pp_atomic.h:473
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:699
int Get()
Get the value of an atomic variable.
Definition: SDL3pp_atomic.h:412
Uint32 GetAtomicU32(AtomicU32Raw *a)
Get the value of an atomic variable.
Definition: SDL3pp_atomic.h:716
Uint32 SetAtomicU32(AtomicU32Raw *a, Uint32 v)
Set an atomic variable to a value.
Definition: SDL3pp_atomic.h:694
bool CompareAndSwap(T *oldval, T *newval)
Set a pointer to a new value if it is currently an old value.
Definition: SDL3pp_atomic.h:721
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:388
int Set(int v)
Set an atomic variable to a value.
Definition: SDL3pp_atomic.h:393
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:360
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:718
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:664
int AddAtomicInt(AtomicIntRaw *a, int v)
Add to an atomic variable.
Definition: SDL3pp_atomic.h:433
Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:328
Main include header for the SDL3pp library.
A type representing an atomic integer value.
Definition: SDL3pp_atomic.h:221
constexpr AtomicInt(int value)
Wraps AtomicInt.
Definition: SDL3pp_atomic.h:227
A type representing an atomic unsigned 32-bit value.
Definition: SDL3pp_atomic.h:504
constexpr AtomicU32(Uint32 value)
Wraps value.
Definition: SDL3pp_atomic.h:510