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
58inline void MemoryBarrierRelease() { SDL_MemoryBarrierReleaseFunction(); }
59
78inline void MemoryBarrierAcquire() { SDL_MemoryBarrierAcquireFunction(); }
79
80#ifdef SDL3PP_DOC
81
98#define SDL_CompilerBarrier() DoCompilerSpecificReadWriteBarrier()
99
136#define SDL_MemoryBarrierRelease() SDL_MemoryBarrierReleaseFunction()
137
159#define SDL_MemoryBarrierAcquire() SDL_MemoryBarrierAcquireFunction()
160
177#define SDL_CPUPauseInstruction() \
178 DoACPUPauseInACompilerAndArchitectureSpecificWay
179
180#endif
181
210{
211 SDL_AtomicInt m_value;
212
213public:
215 constexpr AtomicInt(int value = 0)
216 : m_value(value)
217 {
218 }
219
220 AtomicInt(const AtomicInt& value) = delete;
221
222 AtomicInt& operator=(const AtomicInt& value) = delete;
223
225 constexpr operator SDL_AtomicInt*() { return &m_value; }
226
244 bool CompareAndSwap(int oldval, int newval)
245 {
246 return SDL_CompareAndSwapAtomicInt(&m_value, oldval, newval);
247 }
248
266 int Set(int v) { return SDL_SetAtomicInt(&m_value, v); }
267
282 int Get() { return SDL_GetAtomicInt(&m_value); }
283
302 int Add(int v) { return SDL_AddAtomicInt(&m_value, v); }
303
317 bool AtomicIncRef() { return SDL_AtomicIncRef(&m_value); }
318
333 bool AtomicDecRef() { return SDL_AtomicDecRef(&m_value); }
334};
335
363{
364 SDL_AtomicU32 m_value;
365
366public:
368 constexpr AtomicU32(Uint32 value = 0)
369 : m_value(value)
370 {
371 }
372
373 AtomicU32(const AtomicU32& value) = delete;
374
375 AtomicU32& operator=(const AtomicU32& value) = delete;
376
378 constexpr operator SDL_AtomicU32*() { return &m_value; }
379
397 bool CompareAndSwap(Uint32 oldval, Uint32 newval)
398 {
399 return SDL_CompareAndSwapAtomicU32(&m_value, oldval, newval);
400 }
401
419 Uint32 Set(Uint32 v) { return SDL_SetAtomicU32(&m_value, v); }
420
435 Uint32 Get() { return SDL_GetAtomicU32(&m_value); }
436};
437
439template<class T>
441{
442 T* m_value;
443
444public:
446 constexpr AtomicPointer(T* value = nullptr)
447 : m_value(value)
448 {
449 }
450
451 AtomicPointer(const AtomicPointer& value) = delete;
452
453 AtomicPointer& operator=(const AtomicPointer& value) = delete;
454
473 bool CompareAndSwap(T* oldval, T* newval)
474 {
475 return SDL_CompareAndSwapAtomicPointer(&m_value, oldval, newval);
476 }
477
494 T* Set(T* v) { return SDL_SetAtomicPointer(&m_value, v); }
495
511 T* Get() { return SDL_GetAtomicPointer(&m_value); }
512};
513
515} // namespace SDL
516
517#endif /* SDL3PP_ATOMIC_H_ */
A type representing an atomic integer value.
Definition SDL3pp_atomic.h:210
constexpr AtomicInt(int value=0)
Constructor.
Definition SDL3pp_atomic.h:215
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:244
int Add(int v)
Add to an atomic variable.
Definition SDL3pp_atomic.h:302
bool AtomicDecRef()
Decrement an atomic variable used as a reference count.
Definition SDL3pp_atomic.h:333
int Get()
Get the value of an atomic variable.
Definition SDL3pp_atomic.h:282
bool AtomicIncRef()
Increment an atomic variable used as a reference count.
Definition SDL3pp_atomic.h:317
int Set(int v)
Set an atomic variable to a value.
Definition SDL3pp_atomic.h:266
Type representing an atomic pointer.
Definition SDL3pp_atomic.h:441
T * Get()
Get the value of a pointer atomically.
Definition SDL3pp_atomic.h:511
T * Set(T *v)
Set a pointer to a value atomically.
Definition SDL3pp_atomic.h:494
constexpr AtomicPointer(T *value=nullptr)
Constructor.
Definition SDL3pp_atomic.h:446
bool CompareAndSwap(T *oldval, T *newval)
Set a pointer to a new value if it is currently an old value.
Definition SDL3pp_atomic.h:473
A type representing an atomic unsigned 32-bit value.
Definition SDL3pp_atomic.h:363
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:397
constexpr AtomicU32(Uint32 value=0)
Constructor.
Definition SDL3pp_atomic.h:368
Uint32 Set(Uint32 v)
Set an atomic variable to a value.
Definition SDL3pp_atomic.h:419
Uint32 Get()
Get the value of an atomic variable.
Definition SDL3pp_atomic.h:435
void MemoryBarrierAcquire()
Insert a memory acquire barrier (function version).
Definition SDL3pp_atomic.h:78
void MemoryBarrierRelease()
Insert a memory release barrier (function version).
Definition SDL3pp_atomic.h:58
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7