4#include <SDL3/SDL_mutex.h>
5#include "SDL3pp_stdinc.h"
6#include "SDL3pp_thread.h"
57 constexpr explicit operator bool()
const {
return !!
value; }
93 constexpr explicit operator bool()
const {
return !!
value; }
129 constexpr explicit operator bool()
const {
return !!
value; }
165 constexpr explicit operator bool()
const {
return !!
value; }
207 : m_resource(resource)
245 : m_resource(SDL_CreateMutex())
250 ~Mutex() { SDL_DestroyMutex(m_resource); }
255 std::swap(m_resource, other.m_resource);
266 m_resource =
nullptr;
274 constexpr bool operator==(std::nullptr_t _)
const {
return !m_resource; }
277 constexpr explicit operator bool()
const {
return !!m_resource; }
280 constexpr operator MutexParam()
const {
return {m_resource}; }
371 :
Mutex(resource.value)
532 : m_resource(resource)
590 : m_resource(SDL_CreateRWLock())
600 std::swap(m_resource, other.m_resource);
611 m_resource =
nullptr;
619 constexpr bool operator==(std::nullptr_t _)
const {
return !m_resource; }
622 constexpr explicit operator bool()
const {
return !!m_resource; }
891 SDL_LockRWLockForReading(rwlock);
927 SDL_LockRWLockForWriting(rwlock);
957 CheckError(SDL_TryLockRWLockForReading(rwlock));
995 CheckError(SDL_TryLockRWLockForWriting(rwlock));
1081 : m_resource(resource)
1121 : m_resource(SDL_CreateSemaphore(initial_value))
1131 std::swap(m_resource, other.m_resource);
1141 auto r = m_resource;
1142 m_resource =
nullptr;
1150 constexpr bool operator==(std::nullptr_t _)
const {
return !m_resource; }
1153 constexpr explicit operator bool()
const {
return !!m_resource; }
1225 bool WaitTimeout(std::chrono::milliseconds timeout);
1358 return SDL_TryWaitSemaphore(sem);
1382 std::chrono::milliseconds timeout)
1384 return SDL_WaitSemaphoreTimeout(sem, timeout.count());
1417 return SDL_GetSemaphoreValue(sem);
1453 : m_resource(resource)
1485 : m_resource(SDL_CreateCondition())
1495 std::swap(m_resource, other.m_resource);
1505 auto r = m_resource;
1506 m_resource =
nullptr;
1514 constexpr bool operator==(std::nullptr_t _)
const {
return !m_resource; }
1517 constexpr explicit operator bool()
const {
return !!m_resource; }
1703 SDL_BroadcastCondition(cond);
1736 SDL_WaitCondition(cond, mutex);
1774 std::chrono::milliseconds timeout)
1776 return SDL_WaitConditionTimeout(cond, mutex, timeout.count());
1780 std::chrono::milliseconds timeout)
1793 SDL_INIT_STATUS_UNINITIALIZED;
1796 SDL_INIT_STATUS_INITIALIZING;
1799 SDL_INIT_STATUS_INITIALIZED;
1802 SDL_INIT_STATUS_UNINITIALIZING;
1997 SDL_SetInitialized(state, initialized);
A means to block multiple threads until a condition is satisfied.
Definition: SDL3pp_mutex.h:1441
constexpr ConditionRaw release()
Retrieves underlying ConditionRaw and clear this.
Definition: SDL3pp_mutex.h:1503
Condition()
Create a condition variable.
Definition: SDL3pp_mutex.h:1484
Condition & operator=(Condition other)
Assignment operator.
Definition: SDL3pp_mutex.h:1493
~Condition()
Destructor.
Definition: SDL3pp_mutex.h:1490
constexpr auto operator<=>(const Condition &other) const =default
Comparison.
constexpr Condition(const Condition &other)=delete
Copy constructor.
constexpr Condition(Condition &&other)
Move constructor.
Definition: SDL3pp_mutex.h:1461
constexpr ConditionRaw get() const
Retrieves underlying ConditionRaw.
Definition: SDL3pp_mutex.h:1500
constexpr Condition(const ConditionRaw resource)
Constructs from ConditionParam.
Definition: SDL3pp_mutex.h:1452
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_mutex.h:1514
A means to serialize access to a resource between threads.
Definition: SDL3pp_mutex.h:195
constexpr MutexRaw release()
Retrieves underlying MutexRaw and clear this.
Definition: SDL3pp_mutex.h:263
~Mutex()
Destructor.
Definition: SDL3pp_mutex.h:250
Mutex & operator=(Mutex other)
Assignment operator.
Definition: SDL3pp_mutex.h:253
Mutex()
Create a new mutex.
Definition: SDL3pp_mutex.h:244
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_mutex.h:274
constexpr auto operator<=>(const Mutex &other) const =default
Comparison.
constexpr Mutex(const Mutex &other)=delete
Copy constructor.
constexpr Mutex(Mutex &&other)
Move constructor.
Definition: SDL3pp_mutex.h:215
constexpr Mutex(const MutexRaw resource)
Constructs from MutexParam.
Definition: SDL3pp_mutex.h:206
constexpr MutexRaw get() const
Retrieves underlying MutexRaw.
Definition: SDL3pp_mutex.h:260
A mutex that allows read-only threads to run in parallel.
Definition: SDL3pp_mutex.h:520
constexpr RWLock(RWLock &&other)
Move constructor.
Definition: SDL3pp_mutex.h:540
constexpr RWLockRaw release()
Retrieves underlying RWLockRaw and clear this.
Definition: SDL3pp_mutex.h:608
~RWLock()
Destructor.
Definition: SDL3pp_mutex.h:595
constexpr RWLock(const RWLockRaw resource)
Constructs from RWLockParam.
Definition: SDL3pp_mutex.h:531
constexpr RWLockRaw get() const
Retrieves underlying RWLockRaw.
Definition: SDL3pp_mutex.h:605
RWLock()
Create a new read/write lock.
Definition: SDL3pp_mutex.h:589
constexpr auto operator<=>(const RWLock &other) const =default
Comparison.
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_mutex.h:619
constexpr RWLock(const RWLock &other)=delete
Copy constructor.
RWLock & operator=(RWLock other)
Assignment operator.
Definition: SDL3pp_mutex.h:598
A means to manage access to a resource, by count, between threads.
Definition: SDL3pp_mutex.h:1066
constexpr Semaphore()=default
Default ctor.
Semaphore & operator=(Semaphore other)
Assignment operator.
Definition: SDL3pp_mutex.h:1129
constexpr auto operator<=>(const Semaphore &other) const =default
Comparison.
~Semaphore()
Destructor.
Definition: SDL3pp_mutex.h:1126
constexpr Semaphore(Semaphore &&other)
Move constructor.
Definition: SDL3pp_mutex.h:1089
constexpr SemaphoreRaw release()
Retrieves underlying SemaphoreRaw and clear this.
Definition: SDL3pp_mutex.h:1139
Semaphore(Uint32 initial_value)
Create a semaphore.
Definition: SDL3pp_mutex.h:1120
constexpr Semaphore(const Semaphore &other)=delete
Copy constructor.
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_mutex.h:1150
constexpr SemaphoreRaw get() const
Retrieves underlying SemaphoreRaw.
Definition: SDL3pp_mutex.h:1136
constexpr Semaphore(const SemaphoreRaw resource)
Constructs from SemaphoreParam.
Definition: SDL3pp_mutex.h:1080
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:198
bool ShouldQuit(InitStateRaw *state)
Return whether cleanup should be done.
Definition: SDL3pp_mutex.h:1974
Uint32 GetSemaphoreValue(SemaphoreParam sem)
Get the current value of a semaphore.
Definition: SDL3pp_mutex.h:1415
constexpr InitStatus INIT_STATUS_UNINITIALIZING
INIT_STATUS_UNINITIALIZING.
Definition: SDL3pp_mutex.h:1801
void Unlock()
Unlock the read/write lock.
Definition: SDL3pp_mutex.h:1028
void LockMutex(MutexParam mutex)
Lock the mutex.
Definition: SDL3pp_mutex.h:429
bool TryWaitSemaphore(SemaphoreParam sem)
See if a semaphore has a positive value and decrement it if it does.
Definition: SDL3pp_mutex.h:1356
void TryLockForWriting()
Try to lock a read/write lock for writing without blocking.
Definition: SDL3pp_mutex.h:998
void Wait(MutexParam mutex)
Wait until a condition variable is signaled.
Definition: SDL3pp_mutex.h:1739
constexpr InitStatus INIT_STATUS_INITIALIZING
INIT_STATUS_INITIALIZING.
Definition: SDL3pp_mutex.h:1795
void Signal()
Restart one of the threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1686
Mutex CreateMutex()
Create a new mutex.
Definition: SDL3pp_mutex.h:405
constexpr InitStatus INIT_STATUS_UNINITIALIZED
INIT_STATUS_UNINITIALIZED.
Definition: SDL3pp_mutex.h:1792
void SignalSemaphore(SemaphoreParam sem)
Atomically increment a semaphore's value and wake waiting threads.
Definition: SDL3pp_mutex.h:1403
bool TryWait()
See if a semaphore has a positive value and decrement it if it does.
Definition: SDL3pp_mutex.h:1361
void TryLockRWLockForReading(RWLockParam rwlock)
Try to lock a read/write lock for reading without blocking.
Definition: SDL3pp_mutex.h:955
SDL_RWLock * RWLockRaw
Alias to raw representation for RWLock.
Definition: SDL3pp_mutex.h:70
bool WaitSemaphoreTimeout(SemaphoreParam sem, std::chrono::milliseconds timeout)
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1381
void Destroy()
Destroy a mutex created with Mutex.Mutex().
Definition: SDL3pp_mutex.h:497
SDL_InitState InitStateRaw
Alias to raw representation for InitState.
Definition: SDL3pp_mutex.h:175
void TryLock()
Try to lock a mutex without blocking.
Definition: SDL3pp_mutex.h:457
void Unlock()
Unlock the mutex.
Definition: SDL3pp_mutex.h:478
void UnlockMutex(MutexParam mutex)
Unlock the mutex.
Definition: SDL3pp_mutex.h:476
SDL_InitStatus InitStatus
The current status of an InitState structure.
Definition: SDL3pp_mutex.h:1790
Condition CreateCondition()
Create a condition variable.
Definition: SDL3pp_mutex.h:1656
void DestroyRWLock(RWLockRaw rwlock)
Destroy a read/write lock created with RWLock.RWLock().
Definition: SDL3pp_mutex.h:1045
bool ShouldInit(InitStateRaw *state)
Return whether initialization should be done.
Definition: SDL3pp_mutex.h:1951
void BroadcastCondition(ConditionParam cond)
Restart all threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1701
SDL_Condition * ConditionRaw
Alias to raw representation for Condition.
Definition: SDL3pp_mutex.h:142
bool ShouldQuit()
Return whether cleanup should be done.
Definition: SDL3pp_mutex.h:1976
void LockForReading()
Lock the read/write lock for read only operations.
Definition: SDL3pp_mutex.h:894
SDL_Semaphore * SemaphoreRaw
Alias to raw representation for Semaphore.
Definition: SDL3pp_mutex.h:106
Uint32 GetValue() const
Get the current value of a semaphore.
Definition: SDL3pp_mutex.h:1420
void UnlockRWLock(RWLockParam rwlock)
Unlock the read/write lock.
Definition: SDL3pp_mutex.h:1026
void TryLockMutex(MutexParam mutex)
Try to lock a mutex without blocking.
Definition: SDL3pp_mutex.h:452
void DestroyMutex(MutexRaw mutex)
Destroy a mutex created with Mutex.Mutex().
Definition: SDL3pp_mutex.h:495
bool ShouldInit()
Return whether initialization should be done.
Definition: SDL3pp_mutex.h:1953
bool WaitTimeout(std::chrono::milliseconds timeout)
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1387
void SignalCondition(ConditionParam cond)
Restart one of the threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1684
Semaphore CreateSemaphore(Uint32 initial_value)
Create a semaphore.
Definition: SDL3pp_mutex.h:1296
void LockRWLockForWriting(RWLockParam rwlock)
Lock the read/write lock for write operations.
Definition: SDL3pp_mutex.h:925
void Wait()
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1337
constexpr InitStatus INIT_STATUS_INITIALIZED
INIT_STATUS_INITIALIZED.
Definition: SDL3pp_mutex.h:1798
void Lock()
Lock the mutex.
Definition: SDL3pp_mutex.h:431
void Destroy()
Destroy a semaphore.
Definition: SDL3pp_mutex.h:1315
void Broadcast()
Restart all threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1706
bool WaitTimeout(MutexParam mutex, std::chrono::milliseconds timeout)
Wait until a condition variable is signaled or a certain time has passed.
Definition: SDL3pp_mutex.h:1779
void Signal()
Atomically increment a semaphore's value and wake waiting threads.
Definition: SDL3pp_mutex.h:1405
RWLock CreateRWLock()
Create a new read/write lock.
Definition: SDL3pp_mutex.h:852
SDL_Mutex * MutexRaw
Alias to raw representation for Mutex.
Definition: SDL3pp_mutex.h:34
void TryLockRWLockForWriting(RWLockParam rwlock)
Try to lock a read/write lock for writing without blocking.
Definition: SDL3pp_mutex.h:993
void DestroySemaphore(SemaphoreRaw sem)
Destroy a semaphore.
Definition: SDL3pp_mutex.h:1313
void LockForWriting()
Lock the read/write lock for write operations.
Definition: SDL3pp_mutex.h:930
void Destroy()
Destroy a read/write lock created with RWLock.RWLock().
Definition: SDL3pp_mutex.h:1047
bool WaitConditionTimeout(ConditionParam cond, MutexParam mutex, std::chrono::milliseconds timeout)
Wait until a condition variable is signaled or a certain time has passed.
Definition: SDL3pp_mutex.h:1772
void SetInitialized(InitStateRaw *state, bool initialized)
Finish an initialization state transition.
Definition: SDL3pp_mutex.h:1995
void TryLockForReading()
Try to lock a read/write lock for reading without blocking.
Definition: SDL3pp_mutex.h:960
void DestroyCondition(ConditionRaw cond)
Destroy a condition variable.
Definition: SDL3pp_mutex.h:1667
void Destroy()
Destroy a condition variable.
Definition: SDL3pp_mutex.h:1669
void LockRWLockForReading(RWLockParam rwlock)
Lock the read/write lock for read only operations.
Definition: SDL3pp_mutex.h:889
void WaitCondition(ConditionParam cond, MutexParam mutex)
Wait until a condition variable is signaled.
Definition: SDL3pp_mutex.h:1734
void SetInitialized(bool initialized)
Finish an initialization state transition.
Definition: SDL3pp_mutex.h:2000
void WaitSemaphore(SemaphoreParam sem)
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1335
Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:328
Main include header for the SDL3pp library.
Safely wrap Condition for non owning parameters.
Definition: SDL3pp_mutex.h:149
ConditionRaw value
parameter's ConditionRaw
Definition: SDL3pp_mutex.h:150
constexpr ConditionParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_mutex.h:159
constexpr ConditionParam(ConditionRaw value)
Constructs from ConditionRaw.
Definition: SDL3pp_mutex.h:153
constexpr auto operator<=>(const ConditionParam &other) const =default
Comparison.
Semi-safe reference for Condition.
Definition: SDL3pp_mutex.h:1619
ConditionRef(const ConditionRef &other)
Copy constructor.
Definition: SDL3pp_mutex.h:1633
ConditionRef(ConditionParam resource)
Constructs from ConditionParam.
Definition: SDL3pp_mutex.h:1627
~ConditionRef()
Destructor.
Definition: SDL3pp_mutex.h:1639
A structure used for thread-safe initialization and shutdown.
Definition: SDL3pp_mutex.h:1863
Safely wrap Mutex for non owning parameters.
Definition: SDL3pp_mutex.h:41
MutexRaw value
parameter's MutexRaw
Definition: SDL3pp_mutex.h:42
constexpr MutexParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_mutex.h:51
constexpr auto operator<=>(const MutexParam &other) const =default
Comparison.
constexpr MutexParam(MutexRaw value)
Constructs from MutexRaw.
Definition: SDL3pp_mutex.h:45
Semi-safe reference for Mutex.
Definition: SDL3pp_mutex.h:362
MutexRef(MutexParam resource)
Constructs from MutexParam.
Definition: SDL3pp_mutex.h:370
MutexRef(const MutexRef &other)
Copy constructor.
Definition: SDL3pp_mutex.h:376
~MutexRef()
Destructor.
Definition: SDL3pp_mutex.h:382
Safely wrap RWLock for non owning parameters.
Definition: SDL3pp_mutex.h:77
constexpr RWLockParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_mutex.h:87
RWLockRaw value
parameter's RWLockRaw
Definition: SDL3pp_mutex.h:78
constexpr auto operator<=>(const RWLockParam &other) const =default
Comparison.
constexpr RWLockParam(RWLockRaw value)
Constructs from RWLockRaw.
Definition: SDL3pp_mutex.h:81
Semi-safe reference for RWLock.
Definition: SDL3pp_mutex.h:789
RWLockRef(const RWLockRef &other)
Copy constructor.
Definition: SDL3pp_mutex.h:803
~RWLockRef()
Destructor.
Definition: SDL3pp_mutex.h:809
RWLockRef(RWLockParam resource)
Constructs from RWLockParam.
Definition: SDL3pp_mutex.h:797
Safely wrap Semaphore for non owning parameters.
Definition: SDL3pp_mutex.h:113
constexpr SemaphoreParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_mutex.h:123
SemaphoreRaw value
parameter's SemaphoreRaw
Definition: SDL3pp_mutex.h:114
constexpr SemaphoreParam(SemaphoreRaw value)
Constructs from SemaphoreRaw.
Definition: SDL3pp_mutex.h:117
constexpr auto operator<=>(const SemaphoreParam &other) const =default
Comparison.
Semi-safe reference for Semaphore.
Definition: SDL3pp_mutex.h:1251
SemaphoreRef(SemaphoreParam resource)
Constructs from SemaphoreParam.
Definition: SDL3pp_mutex.h:1259
~SemaphoreRef()
Destructor.
Definition: SDL3pp_mutex.h:1271
SemaphoreRef(const SemaphoreRef &other)
Copy constructor.
Definition: SDL3pp_mutex.h:1265