SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_mutex.h
1#ifndef SDL3PP_MUTEX_H_
2#define SDL3PP_MUTEX_H_
3
4#include <SDL3/SDL_mutex.h>
5#include "SDL3pp_stdinc.h"
6#include "SDL3pp_thread.h"
7
8namespace SDL {
9
29
30// Forward decl
31struct MutexBase;
32
33// Forward decl
34struct Mutex;
35
37using MutexRaw = SDL_Mutex*;
38
45
46// Forward decl
47struct RWLockBase;
48
49// Forward decl
50struct RWLock;
51
53using RWLockRaw = SDL_RWLock*;
54
61
62// Forward decl
63struct SemaphoreBase;
64
65// Forward decl
66struct Semaphore;
67
69using SemaphoreRaw = SDL_Semaphore*;
70
77
78// Forward decl
79struct ConditionBase;
80
81// Forward decl
82struct Condition;
83
85using ConditionRaw = SDL_Condition*;
86
93
95using InitStateRaw = SDL_InitState;
96
97// Forward decl
98struct InitState;
99
105struct MutexBase : ResourceBaseT<MutexRaw>
106{
108
124 void Destroy();
125
148 void Lock();
149
170 bool TryLock();
171
190 void Unlock();
191};
192
208{
209 using MutexBase::MutexBase;
210
218 constexpr explicit Mutex(MutexRaw resource) noexcept
219 : MutexBase(resource)
220 {
221 }
222
224 constexpr Mutex(Mutex&& other) noexcept
225 : Mutex(other.release())
226 {
227 }
228
251 Mutex();
252
254 ~Mutex() { SDL_DestroyMutex(get()); }
255
257 constexpr Mutex& operator=(Mutex&& other) noexcept
258 {
259 swap(*this, other);
260 return *this;
261 }
262};
263
286inline Mutex CreateMutex() { return Mutex(); }
287
289 : Mutex(SDL_CreateMutex())
290{
291}
292
317inline void LockMutex(MutexRef mutex) { SDL_LockMutex(mutex); }
318
319inline void MutexBase::Lock() { SDL::LockMutex(get()); }
320
342inline bool TryLockMutex(MutexRef mutex) { return SDL_TryLockMutex(mutex); }
343
344inline bool MutexBase::TryLock() { return SDL::TryLockMutex(get()); }
345
366inline void UnlockMutex(MutexRef mutex) { SDL_UnlockMutex(mutex); }
367
369
387inline void DestroyMutex(MutexRaw mutex) { SDL_DestroyMutex(mutex); }
388
390
396struct RWLockBase : ResourceBaseT<RWLockRaw>
397{
399
415 void Destroy();
416
452 void LockForReading();
453
483 void LockForWriting();
484
509 bool TryLockForReading();
510
540 bool TryLockForWriting();
541
566 void Unlock();
567};
568
590{
591 using RWLockBase::RWLockBase;
592
600 constexpr explicit RWLock(RWLockRaw resource) noexcept
601 : RWLockBase(resource)
602 {
603 }
604
606 constexpr RWLock(RWLock&& other) noexcept
607 : RWLock(other.release())
608 {
609 }
610
653 RWLock();
654
656 ~RWLock() { SDL_DestroyRWLock(get()); }
657
659 constexpr RWLock& operator=(RWLock&& other) noexcept
660 {
661 swap(*this, other);
662 return *this;
663 }
664};
665
708inline RWLock CreateRWLock() { return RWLock(); }
709
711 : RWLock(SDL_CreateRWLock())
712{
713}
714
753{
754 SDL_LockRWLockForReading(rwlock);
755}
756
758
791{
792 SDL_LockRWLockForWriting(rwlock);
793}
794
796
823{
824 return SDL_TryLockRWLockForReading(rwlock);
825}
826
831
863{
864 return SDL_TryLockRWLockForWriting(rwlock);
865}
866
871
898inline void UnlockRWLock(RWLockRef rwlock) { SDL_UnlockRWLock(rwlock); }
899
901
919inline void DestroyRWLock(RWLockRaw rwlock) { SDL_DestroyRWLock(rwlock); }
920
922
928struct SemaphoreBase : ResourceBaseT<SemaphoreRaw>
929{
931
944 void Destroy();
945
964 void Wait();
965
984 bool TryWait();
985
1005 bool WaitTimeout(Milliseconds timeout);
1006
1018 void Signal();
1019
1029 Uint32 GetValue() const;
1030};
1031
1049{
1050 using SemaphoreBase::SemaphoreBase;
1051
1059 constexpr explicit Semaphore(SemaphoreRaw resource) noexcept
1060 : SemaphoreBase(resource)
1061 {
1062 }
1063
1065 constexpr Semaphore(Semaphore&& other) noexcept
1066 : Semaphore(other.release())
1067 {
1068 }
1069
1094 Semaphore(Uint32 initial_value);
1095
1097 ~Semaphore() { SDL_DestroySemaphore(get()); }
1098
1100 constexpr Semaphore& operator=(Semaphore&& other) noexcept
1101 {
1102 swap(*this, other);
1103 return *this;
1104 }
1105};
1106
1131inline Semaphore CreateSemaphore(Uint32 initial_value)
1132{
1133 return Semaphore(initial_value);
1134}
1135
1136inline Semaphore::Semaphore(Uint32 initial_value)
1137 : Semaphore(SDL_CreateSemaphore(initial_value))
1138{
1139}
1140
1155inline void DestroySemaphore(SemaphoreRaw sem) { SDL_DestroySemaphore(sem); }
1156
1158
1179inline void WaitSemaphore(SemaphoreRef sem) { SDL_WaitSemaphore(sem); }
1180
1182
1203{
1204 return SDL_TryWaitSemaphore(sem);
1205}
1206
1208
1230{
1231 return SDL_WaitSemaphoreTimeout(sem, narrowS32(timeout.count()));
1232}
1233
1235{
1236 return SDL::WaitSemaphoreTimeout(get(), timeout);
1237}
1238
1252inline void SignalSemaphore(SemaphoreRef sem) { SDL_SignalSemaphore(sem); }
1253
1255
1267{
1268 return SDL_GetSemaphoreValue(sem);
1269}
1270
1272{
1273 return SDL::GetSemaphoreValue(get());
1274}
1275
1281struct ConditionBase : ResourceBaseT<ConditionRaw>
1282{
1284
1294 void Destroy();
1295
1307 void Signal();
1308
1320 void Broadcast();
1321
1347 void Wait(MutexRef mutex);
1348
1376 bool WaitTimeout(MutexRef mutex, Milliseconds timeout);
1377};
1378
1395{
1396 using ConditionBase::ConditionBase;
1397
1405 constexpr explicit Condition(ConditionRaw resource) noexcept
1406 : ConditionBase(resource)
1407 {
1408 }
1409
1411 constexpr Condition(Condition&& other) noexcept
1412 : Condition(other.release())
1413 {
1414 }
1415
1432 Condition();
1433
1435 ~Condition() { SDL_DestroyCondition(get()); }
1436
1438 constexpr Condition& operator=(Condition&& other) noexcept
1439 {
1440 swap(*this, other);
1441 return *this;
1442 }
1443};
1444
1462
1464 : Condition(SDL_CreateCondition())
1465{
1466}
1467
1479inline void DestroyCondition(ConditionRaw cond) { SDL_DestroyCondition(cond); }
1480
1482
1496inline void SignalCondition(ConditionRef cond) { SDL_SignalCondition(cond); }
1497
1499
1514{
1515 SDL_BroadcastCondition(cond);
1516}
1517
1519
1546inline void WaitCondition(ConditionRef cond, MutexRef mutex)
1547{
1548 SDL_WaitCondition(cond, mutex);
1549}
1550
1552{
1553 SDL::WaitCondition(get(), mutex);
1554}
1555
1585 MutexRef mutex,
1586 Milliseconds timeout)
1587{
1588 return SDL_WaitConditionTimeout(cond, mutex, narrowS32(timeout.count()));
1589}
1590
1592{
1593 return SDL::WaitConditionTimeout(get(), mutex, timeout);
1594}
1595
1601using InitStatus = SDL_InitStatus;
1602
1604 SDL_INIT_STATUS_UNINITIALIZED;
1605
1607 SDL_INIT_STATUS_INITIALIZING;
1608
1610 SDL_INIT_STATUS_INITIALIZED;
1611
1613 SDL_INIT_STATUS_UNINITIALIZING;
1614
1672struct InitState : InitStateRaw
1673{
1674 constexpr InitState()
1675 : SDL_InitState{}
1676 {
1677 }
1678
1699 bool ShouldInit();
1700
1719 bool ShouldQuit();
1720
1737 void SetInitialized(bool initialized);
1738};
1739
1761inline bool ShouldInit(InitStateRaw* state) { return SDL_ShouldInit(state); }
1762
1763inline bool InitState::ShouldInit() { return SDL::ShouldInit(this); }
1764
1784inline bool ShouldQuit(InitStateRaw* state) { return SDL_ShouldQuit(state); }
1785
1786inline bool InitState::ShouldQuit() { return SDL::ShouldQuit(this); }
1787
1805inline void SetInitialized(InitStateRaw* state, bool initialized)
1806{
1807 SDL_SetInitialized(state, initialized);
1808}
1809
1810inline void InitState::SetInitialized(bool initialized)
1811{
1812 SDL::SetInitialized(this, initialized);
1813}
1814
1816
1817} // namespace SDL
1818
1819#endif /* SDL3PP_MUTEX_H_ */
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:57
friend constexpr void swap(ResourceBaseT &lhs, ResourceBaseT &rhs) noexcept
Definition SDL3pp_resource.h:65
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:54
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
void Destroy()
Destroy a read/write lock created with CreateRWLock().
Definition SDL3pp_mutex.h:921
bool ShouldQuit(InitStateRaw *state)
Return whether cleanup should be done.
Definition SDL3pp_mutex.h:1784
void BroadcastCondition(ConditionRef cond)
Restart all threads that are waiting on the condition variable.
Definition SDL3pp_mutex.h:1513
constexpr InitStatus INIT_STATUS_UNINITIALIZING
INIT_STATUS_UNINITIALIZING.
Definition SDL3pp_mutex.h:1612
void Destroy()
Destroy a condition variable.
Definition SDL3pp_mutex.h:1481
Uint32 GetValue() const
Get the current value of a semaphore.
Definition SDL3pp_mutex.h:1271
Condition()
Create a condition variable.
Definition SDL3pp_mutex.h:1463
void Unlock()
Unlock the mutex.
Definition SDL3pp_mutex.h:368
constexpr InitStatus INIT_STATUS_INITIALIZING
INIT_STATUS_INITIALIZING.
Definition SDL3pp_mutex.h:1606
Mutex CreateMutex()
Create a new mutex.
Definition SDL3pp_mutex.h:286
bool TryLockForWriting()
Try to lock a read/write lock for writing without blocking.
Definition SDL3pp_mutex.h:867
constexpr InitStatus INIT_STATUS_UNINITIALIZED
INIT_STATUS_UNINITIALIZED.
Definition SDL3pp_mutex.h:1603
bool TryWait()
See if a semaphore has a positive value and decrement it if it does.
Definition SDL3pp_mutex.h:1207
void LockForWriting()
Lock the read/write lock for write operations.
Definition SDL3pp_mutex.h:795
Uint32 GetSemaphoreValue(SemaphoreRef sem)
Get the current value of a semaphore.
Definition SDL3pp_mutex.h:1266
bool TryLockRWLockForReading(RWLockRef rwlock)
Try to lock a read/write lock for reading without blocking.
Definition SDL3pp_mutex.h:822
void Destroy()
Destroy a mutex created with CreateMutex().
Definition SDL3pp_mutex.h:389
SDL_Semaphore * SemaphoreRaw
Alias to raw representation for Semaphore.
Definition SDL3pp_mutex.h:69
void UnlockRWLock(RWLockRef rwlock)
Unlock the read/write lock.
Definition SDL3pp_mutex.h:898
Condition CreateCondition()
Create a condition variable.
Definition SDL3pp_mutex.h:1461
void DestroyRWLock(RWLockRaw rwlock)
Destroy a read/write lock created with CreateRWLock().
Definition SDL3pp_mutex.h:919
bool ShouldInit(InitStateRaw *state)
Return whether initialization should be done.
Definition SDL3pp_mutex.h:1761
ResourceRefT< RWLockBase > RWLockRef
Reference for RWLock.
Definition SDL3pp_mutex.h:60
Mutex()
Create a new mutex.
Definition SDL3pp_mutex.h:288
void WaitSemaphore(SemaphoreRef sem)
Wait until a semaphore has a positive value and then decrements it.
Definition SDL3pp_mutex.h:1179
bool ShouldQuit()
Return whether cleanup should be done.
Definition SDL3pp_mutex.h:1786
void Wait()
Wait until a semaphore has a positive value and then decrements it.
Definition SDL3pp_mutex.h:1181
bool TryWaitSemaphore(SemaphoreRef sem)
See if a semaphore has a positive value and decrement it if it does.
Definition SDL3pp_mutex.h:1202
bool WaitTimeout(MutexRef mutex, Milliseconds timeout)
Wait until a condition variable is signaled or a certain time has passed.
Definition SDL3pp_mutex.h:1591
void Destroy()
Destroy a semaphore.
Definition SDL3pp_mutex.h:1157
void Signal()
Restart one of the threads that are waiting on the condition variable.
Definition SDL3pp_mutex.h:1498
bool TryLockForReading()
Try to lock a read/write lock for reading without blocking.
Definition SDL3pp_mutex.h:827
void DestroyMutex(MutexRaw mutex)
Destroy a mutex created with CreateMutex().
Definition SDL3pp_mutex.h:387
bool ShouldInit()
Return whether initialization should be done.
Definition SDL3pp_mutex.h:1763
ResourceRefT< ConditionBase > ConditionRef
Reference for Condition.
Definition SDL3pp_mutex.h:92
void Signal()
Atomically increment a semaphore's value and wake waiting threads.
Definition SDL3pp_mutex.h:1254
void LockRWLockForWriting(RWLockRef rwlock)
Lock the read/write lock for write operations.
Definition SDL3pp_mutex.h:790
bool TryLock()
Try to lock a mutex without blocking.
Definition SDL3pp_mutex.h:344
RWLock()
Create a new read/write lock.
Definition SDL3pp_mutex.h:710
bool TryLockRWLockForWriting(RWLockRef rwlock)
Try to lock a read/write lock for writing without blocking.
Definition SDL3pp_mutex.h:862
Semaphore CreateSemaphore(Uint32 initial_value)
Create a semaphore.
Definition SDL3pp_mutex.h:1131
ResourceRefT< MutexBase > MutexRef
Reference for Mutex.
Definition SDL3pp_mutex.h:44
void UnlockMutex(MutexRef mutex)
Unlock the mutex.
Definition SDL3pp_mutex.h:366
void LockMutex(MutexRef mutex)
Lock the mutex.
Definition SDL3pp_mutex.h:317
void SignalSemaphore(SemaphoreRef sem)
Atomically increment a semaphore's value and wake waiting threads.
Definition SDL3pp_mutex.h:1252
constexpr InitStatus INIT_STATUS_INITIALIZED
INIT_STATUS_INITIALIZED.
Definition SDL3pp_mutex.h:1609
SDL_Mutex * MutexRaw
Alias to raw representation for Mutex.
Definition SDL3pp_mutex.h:37
void Lock()
Lock the mutex.
Definition SDL3pp_mutex.h:319
bool WaitConditionTimeout(ConditionRef cond, MutexRef mutex, Milliseconds timeout)
Wait until a condition variable is signaled or a certain time has passed.
Definition SDL3pp_mutex.h:1584
SDL_Condition * ConditionRaw
Alias to raw representation for Condition.
Definition SDL3pp_mutex.h:85
RWLock CreateRWLock()
Create a new read/write lock.
Definition SDL3pp_mutex.h:708
void LockForReading()
Lock the read/write lock for read only operations.
Definition SDL3pp_mutex.h:757
bool TryLockMutex(MutexRef mutex)
Try to lock a mutex without blocking.
Definition SDL3pp_mutex.h:342
ResourceRefT< SemaphoreBase > SemaphoreRef
Reference for Semaphore.
Definition SDL3pp_mutex.h:76
SDL_InitState InitStateRaw
Alias to raw representation for InitState.
Definition SDL3pp_mutex.h:95
void DestroySemaphore(SemaphoreRaw sem)
Destroy a semaphore.
Definition SDL3pp_mutex.h:1155
void Broadcast()
Restart all threads that are waiting on the condition variable.
Definition SDL3pp_mutex.h:1518
void LockRWLockForReading(RWLockRef rwlock)
Lock the read/write lock for read only operations.
Definition SDL3pp_mutex.h:752
void SetInitialized(InitStateRaw *state, bool initialized)
Finish an initialization state transition.
Definition SDL3pp_mutex.h:1805
void DestroyCondition(ConditionRaw cond)
Destroy a condition variable.
Definition SDL3pp_mutex.h:1479
bool WaitSemaphoreTimeout(SemaphoreRef sem, Milliseconds timeout)
Wait until a semaphore has a positive value and then decrements it.
Definition SDL3pp_mutex.h:1229
SDL_InitStatus InitStatus
The current status of an InitState structure.
Definition SDL3pp_mutex.h:1601
void Wait(MutexRef mutex)
Wait until a condition variable is signaled.
Definition SDL3pp_mutex.h:1551
void Unlock()
Unlock the read/write lock.
Definition SDL3pp_mutex.h:900
bool WaitTimeout(Milliseconds timeout)
Wait until a semaphore has a positive value and then decrements it.
Definition SDL3pp_mutex.h:1234
void SetInitialized(bool initialized)
Finish an initialization state transition.
Definition SDL3pp_mutex.h:1810
SDL_RWLock * RWLockRaw
Alias to raw representation for RWLock.
Definition SDL3pp_mutex.h:53
void WaitCondition(ConditionRef cond, MutexRef mutex)
Wait until a condition variable is signaled.
Definition SDL3pp_mutex.h:1546
void SignalCondition(ConditionRef cond)
Restart one of the threads that are waiting on the condition variable.
Definition SDL3pp_mutex.h:1496
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition SDL3pp_stdinc.h:296
std::chrono::milliseconds Milliseconds
Duration in Miliseconds (Uint32).
Definition SDL3pp_stdinc.h:341
Main include header for the SDL3pp library.
Sint32 narrowS32(T value)
Narrows to Sint32.
Definition SDL3pp_stdinc.h:6263
Base class to Condition.
Definition SDL3pp_mutex.h:1282
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
A means to block multiple threads until a condition is satisfied.
Definition SDL3pp_mutex.h:1395
constexpr Condition(Condition &&other) noexcept
Move constructor.
Definition SDL3pp_mutex.h:1411
~Condition()
Destructor.
Definition SDL3pp_mutex.h:1435
constexpr Condition(ConditionRaw resource) noexcept
Constructs from raw Condition.
Definition SDL3pp_mutex.h:1405
constexpr Condition & operator=(Condition &&other) noexcept
Assignment operator.
Definition SDL3pp_mutex.h:1438
A structure used for thread-safe initialization and shutdown.
Definition SDL3pp_mutex.h:1673
Base class to Mutex.
Definition SDL3pp_mutex.h:106
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
A means to serialize access to a resource between threads.
Definition SDL3pp_mutex.h:208
constexpr Mutex & operator=(Mutex &&other) noexcept
Assignment operator.
Definition SDL3pp_mutex.h:257
~Mutex()
Destructor.
Definition SDL3pp_mutex.h:254
constexpr Mutex(MutexRaw resource) noexcept
Constructs from raw Mutex.
Definition SDL3pp_mutex.h:218
constexpr Mutex(Mutex &&other) noexcept
Move constructor.
Definition SDL3pp_mutex.h:224
Base class to RWLock.
Definition SDL3pp_mutex.h:397
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
A mutex that allows read-only threads to run in parallel.
Definition SDL3pp_mutex.h:590
~RWLock()
Destructor.
Definition SDL3pp_mutex.h:656
constexpr RWLock(RWLock &&other) noexcept
Move constructor.
Definition SDL3pp_mutex.h:606
constexpr RWLock & operator=(RWLock &&other) noexcept
Assignment operator.
Definition SDL3pp_mutex.h:659
constexpr RWLock(RWLockRaw resource) noexcept
Constructs from raw RWLock.
Definition SDL3pp_mutex.h:600
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:93
Base class to Semaphore.
Definition SDL3pp_mutex.h:929
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
A means to manage access to a resource, by count, between threads.
Definition SDL3pp_mutex.h:1049
constexpr Semaphore(SemaphoreRaw resource) noexcept
Constructs from raw Semaphore.
Definition SDL3pp_mutex.h:1059
constexpr Semaphore & operator=(Semaphore &&other) noexcept
Assignment operator.
Definition SDL3pp_mutex.h:1100
~Semaphore()
Destructor.
Definition SDL3pp_mutex.h:1097
constexpr Semaphore(Semaphore &&other) noexcept
Move constructor.
Definition SDL3pp_mutex.h:1065