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; }
200 constexpr Mutex(std::nullptr_t) noexcept
213 : m_resource(resource)
222 :
Mutex(other.release())
251 : m_resource(SDL_CreateMutex())
256 ~Mutex() { SDL_DestroyMutex(m_resource); }
261 std::swap(m_resource, other.m_resource);
277 m_resource =
nullptr;
285 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
288 constexpr operator MutexParam() const noexcept {
return {m_resource}; }
378 :
Mutex(resource.value)
544 constexpr RWLock(std::nullptr_t) noexcept
557 : m_resource(resource)
615 : m_resource(SDL_CreateRWLock())
625 std::swap(m_resource, other.m_resource);
641 m_resource =
nullptr;
649 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
652 constexpr operator RWLockParam() const noexcept {
return {m_resource}; }
928 SDL_LockRWLockForReading(rwlock);
964 SDL_LockRWLockForWriting(rwlock);
994 CheckError(SDL_TryLockRWLockForReading(rwlock));
1032 CheckError(SDL_TryLockRWLockForWriting(rwlock));
1121 : m_resource(resource)
1161 : m_resource(SDL_CreateSemaphore(initial_value))
1171 std::swap(m_resource, other.m_resource);
1186 auto r = m_resource;
1187 m_resource =
nullptr;
1195 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
1265 bool WaitTimeout(std::chrono::milliseconds timeout);
1411 return SDL_TryWaitSemaphore(sem);
1435 std::chrono::milliseconds timeout)
1437 return SDL_WaitSemaphoreTimeout(sem, timeout.count());
1470 return SDL_GetSemaphoreValue(sem);
1512 : m_resource(resource)
1544 : m_resource(SDL_CreateCondition())
1554 std::swap(m_resource, other.m_resource);
1569 auto r = m_resource;
1570 m_resource =
nullptr;
1578 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
1775 SDL_BroadcastCondition(cond);
1808 SDL_WaitCondition(cond, mutex);
1846 std::chrono::milliseconds timeout)
1848 return SDL_WaitConditionTimeout(cond, mutex, timeout.count());
1852 std::chrono::milliseconds timeout)
1865 SDL_INIT_STATUS_UNINITIALIZED;
1868 SDL_INIT_STATUS_INITIALIZING;
1871 SDL_INIT_STATUS_INITIALIZED;
1874 SDL_INIT_STATUS_UNINITIALIZING;
2068 SDL_SetInitialized(state, initialized);
A means to block multiple threads until a condition is satisfied.
Definition: SDL3pp_mutex.h:1494
constexpr Condition(const ConditionRaw resource) noexcept
Constructs from ConditionParam.
Definition: SDL3pp_mutex.h:1511
constexpr ConditionRaw get() const noexcept
Retrieves underlying ConditionRaw.
Definition: SDL3pp_mutex.h:1564
Condition()
Create a condition variable.
Definition: SDL3pp_mutex.h:1543
constexpr Condition(Condition &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:1520
constexpr Condition(std::nullptr_t) noexcept
Default ctor.
Definition: SDL3pp_mutex.h:1499
constexpr ConditionRaw release() noexcept
Retrieves underlying ConditionRaw and clear this.
Definition: SDL3pp_mutex.h:1567
constexpr Condition & operator=(const Condition &other) noexcept=default
Assignment operator.
~Condition()
Destructor.
Definition: SDL3pp_mutex.h:1549
constexpr Condition(const Condition &other)=delete
Copy constructor.
constexpr auto operator<=>(const Condition &other) const noexcept=default
Comparison.
constexpr Condition & operator=(Condition &&other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:1552
A means to serialize access to a resource between threads.
Definition: SDL3pp_mutex.h:195
constexpr Mutex & operator=(Mutex &&other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:259
~Mutex()
Destructor.
Definition: SDL3pp_mutex.h:256
constexpr MutexRaw get() const noexcept
Retrieves underlying MutexRaw.
Definition: SDL3pp_mutex.h:271
Mutex()
Create a new mutex.
Definition: SDL3pp_mutex.h:250
constexpr auto operator<=>(const Mutex &other) const noexcept=default
Comparison.
constexpr Mutex & operator=(const Mutex &other) noexcept=default
Assignment operator.
constexpr Mutex(const Mutex &other)=delete
Copy constructor.
constexpr MutexRaw release() noexcept
Retrieves underlying MutexRaw and clear this.
Definition: SDL3pp_mutex.h:274
constexpr Mutex(const MutexRaw resource) noexcept
Constructs from MutexParam.
Definition: SDL3pp_mutex.h:212
constexpr Mutex(std::nullptr_t) noexcept
Default ctor.
Definition: SDL3pp_mutex.h:200
constexpr Mutex(Mutex &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:221
A mutex that allows read-only threads to run in parallel.
Definition: SDL3pp_mutex.h:539
constexpr auto operator<=>(const RWLock &other) const noexcept=default
Comparison.
~RWLock()
Destructor.
Definition: SDL3pp_mutex.h:620
constexpr RWLock(const RWLockRaw resource) noexcept
Constructs from RWLockParam.
Definition: SDL3pp_mutex.h:556
constexpr RWLock(RWLock &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:565
constexpr RWLock & operator=(const RWLock &other) noexcept=default
Assignment operator.
RWLock()
Create a new read/write lock.
Definition: SDL3pp_mutex.h:614
constexpr RWLock(const RWLock &other)=delete
Copy constructor.
constexpr RWLock(std::nullptr_t) noexcept
Default ctor.
Definition: SDL3pp_mutex.h:544
constexpr RWLockRaw get() const noexcept
Retrieves underlying RWLockRaw.
Definition: SDL3pp_mutex.h:635
constexpr RWLock & operator=(RWLock &&other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:623
constexpr RWLockRaw release() noexcept
Retrieves underlying RWLockRaw and clear this.
Definition: SDL3pp_mutex.h:638
A means to manage access to a resource, by count, between threads.
Definition: SDL3pp_mutex.h:1103
constexpr SemaphoreRaw release() noexcept
Retrieves underlying SemaphoreRaw and clear this.
Definition: SDL3pp_mutex.h:1184
constexpr Semaphore & operator=(Semaphore &&other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:1169
~Semaphore()
Destructor.
Definition: SDL3pp_mutex.h:1166
constexpr auto operator<=>(const Semaphore &other) const noexcept=default
Comparison.
Semaphore(Uint32 initial_value)
Create a semaphore.
Definition: SDL3pp_mutex.h:1160
constexpr Semaphore(const Semaphore &other)=delete
Copy constructor.
constexpr Semaphore(const SemaphoreRaw resource) noexcept
Constructs from SemaphoreParam.
Definition: SDL3pp_mutex.h:1120
constexpr SemaphoreRaw get() const noexcept
Retrieves underlying SemaphoreRaw.
Definition: SDL3pp_mutex.h:1181
constexpr Semaphore(Semaphore &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:1129
constexpr Semaphore(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_mutex.h:1108
constexpr Semaphore & operator=(const Semaphore &other) noexcept=default
Assignment operator.
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
bool ShouldQuit(InitStateRaw *state)
Return whether cleanup should be done.
Definition: SDL3pp_mutex.h:2045
Uint32 GetSemaphoreValue(SemaphoreParam sem)
Get the current value of a semaphore.
Definition: SDL3pp_mutex.h:1468
constexpr InitStatus INIT_STATUS_UNINITIALIZING
INIT_STATUS_UNINITIALIZING.
Definition: SDL3pp_mutex.h:1873
void Unlock()
Unlock the read/write lock.
Definition: SDL3pp_mutex.h:1065
void LockMutex(MutexParam mutex)
Lock the mutex.
Definition: SDL3pp_mutex.h:448
bool TryWaitSemaphore(SemaphoreParam sem)
See if a semaphore has a positive value and decrement it if it does.
Definition: SDL3pp_mutex.h:1409
void TryLockForWriting()
Try to lock a read/write lock for writing without blocking.
Definition: SDL3pp_mutex.h:1035
void Wait(MutexParam mutex)
Wait until a condition variable is signaled.
Definition: SDL3pp_mutex.h:1811
constexpr InitStatus INIT_STATUS_INITIALIZING
INIT_STATUS_INITIALIZING.
Definition: SDL3pp_mutex.h:1867
void Signal()
Restart one of the threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1758
Mutex CreateMutex()
Create a new mutex.
Definition: SDL3pp_mutex.h:424
constexpr InitStatus INIT_STATUS_UNINITIALIZED
INIT_STATUS_UNINITIALIZED.
Definition: SDL3pp_mutex.h:1864
void SignalSemaphore(SemaphoreParam sem)
Atomically increment a semaphore's value and wake waiting threads.
Definition: SDL3pp_mutex.h:1456
bool TryWait()
See if a semaphore has a positive value and decrement it if it does.
Definition: SDL3pp_mutex.h:1414
void TryLockRWLockForReading(RWLockParam rwlock)
Try to lock a read/write lock for reading without blocking.
Definition: SDL3pp_mutex.h:992
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:1434
void Destroy()
Destroy a mutex created with Mutex.Mutex().
Definition: SDL3pp_mutex.h:516
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:476
void Unlock()
Unlock the mutex.
Definition: SDL3pp_mutex.h:497
void UnlockMutex(MutexParam mutex)
Unlock the mutex.
Definition: SDL3pp_mutex.h:495
SDL_InitStatus InitStatus
The current status of an InitState structure.
Definition: SDL3pp_mutex.h:1862
Condition CreateCondition()
Create a condition variable.
Definition: SDL3pp_mutex.h:1728
void DestroyRWLock(RWLockRaw rwlock)
Destroy a read/write lock created with RWLock.RWLock().
Definition: SDL3pp_mutex.h:1082
bool ShouldInit(InitStateRaw *state)
Return whether initialization should be done.
Definition: SDL3pp_mutex.h:2022
void BroadcastCondition(ConditionParam cond)
Restart all threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1773
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:2047
void LockForReading()
Lock the read/write lock for read only operations.
Definition: SDL3pp_mutex.h:931
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:1473
void UnlockRWLock(RWLockParam rwlock)
Unlock the read/write lock.
Definition: SDL3pp_mutex.h:1063
void TryLockMutex(MutexParam mutex)
Try to lock a mutex without blocking.
Definition: SDL3pp_mutex.h:471
void DestroyMutex(MutexRaw mutex)
Destroy a mutex created with Mutex.Mutex().
Definition: SDL3pp_mutex.h:514
bool ShouldInit()
Return whether initialization should be done.
Definition: SDL3pp_mutex.h:2024
bool WaitTimeout(std::chrono::milliseconds timeout)
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1440
void SignalCondition(ConditionParam cond)
Restart one of the threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1756
Semaphore CreateSemaphore(Uint32 initial_value)
Create a semaphore.
Definition: SDL3pp_mutex.h:1349
void LockRWLockForWriting(RWLockParam rwlock)
Lock the read/write lock for write operations.
Definition: SDL3pp_mutex.h:962
void Wait()
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1390
constexpr InitStatus INIT_STATUS_INITIALIZED
INIT_STATUS_INITIALIZED.
Definition: SDL3pp_mutex.h:1870
void Lock()
Lock the mutex.
Definition: SDL3pp_mutex.h:450
void Destroy()
Destroy a semaphore.
Definition: SDL3pp_mutex.h:1368
void Broadcast()
Restart all threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1778
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:1851
void Signal()
Atomically increment a semaphore's value and wake waiting threads.
Definition: SDL3pp_mutex.h:1458
RWLock CreateRWLock()
Create a new read/write lock.
Definition: SDL3pp_mutex.h:889
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:1030
void DestroySemaphore(SemaphoreRaw sem)
Destroy a semaphore.
Definition: SDL3pp_mutex.h:1366
void LockForWriting()
Lock the read/write lock for write operations.
Definition: SDL3pp_mutex.h:967
void Destroy()
Destroy a read/write lock created with RWLock.RWLock().
Definition: SDL3pp_mutex.h:1084
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:1844
void SetInitialized(InitStateRaw *state, bool initialized)
Finish an initialization state transition.
Definition: SDL3pp_mutex.h:2066
void TryLockForReading()
Try to lock a read/write lock for reading without blocking.
Definition: SDL3pp_mutex.h:997
void DestroyCondition(ConditionRaw cond)
Destroy a condition variable.
Definition: SDL3pp_mutex.h:1739
void Destroy()
Destroy a condition variable.
Definition: SDL3pp_mutex.h:1741
void LockRWLockForReading(RWLockParam rwlock)
Lock the read/write lock for read only operations.
Definition: SDL3pp_mutex.h:926
void WaitCondition(ConditionParam cond, MutexParam mutex)
Wait until a condition variable is signaled.
Definition: SDL3pp_mutex.h:1806
void SetInitialized(bool initialized)
Finish an initialization state transition.
Definition: SDL3pp_mutex.h:2071
void WaitSemaphore(SemaphoreParam sem)
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1388
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:341
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:1677
Condition()
Create a condition variable.
Definition: SDL3pp_mutex.h:1543
ConditionRef(ConditionParam resource) noexcept
Constructs from ConditionParam.
Definition: SDL3pp_mutex.h:1687
ConditionRef(const ConditionRef &other) noexcept
Copy constructor.
Definition: SDL3pp_mutex.h:1705
ConditionRef(ConditionRaw resource) noexcept
Constructs from ConditionParam.
Definition: SDL3pp_mutex.h:1699
~ConditionRef()
Destructor.
Definition: SDL3pp_mutex.h:1711
A structure used for thread-safe initialization and shutdown.
Definition: SDL3pp_mutex.h:1934
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:367
MutexRef(MutexRaw resource) noexcept
Constructs from MutexParam.
Definition: SDL3pp_mutex.h:389
Mutex()
Create a new mutex.
Definition: SDL3pp_mutex.h:250
MutexRef(const MutexRef &other) noexcept
Copy constructor.
Definition: SDL3pp_mutex.h:395
MutexRef(MutexParam resource) noexcept
Constructs from MutexParam.
Definition: SDL3pp_mutex.h:377
~MutexRef()
Destructor.
Definition: SDL3pp_mutex.h:401
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:812
RWLockRef(RWLockParam resource) noexcept
Constructs from RWLockParam.
Definition: SDL3pp_mutex.h:822
RWLock()
Create a new read/write lock.
Definition: SDL3pp_mutex.h:614
~RWLockRef()
Destructor.
Definition: SDL3pp_mutex.h:846
RWLockRef(RWLockRaw resource) noexcept
Constructs from RWLockParam.
Definition: SDL3pp_mutex.h:834
RWLockRef(const RWLockRef &other) noexcept
Copy constructor.
Definition: SDL3pp_mutex.h:840
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:1290
SemaphoreRef(SemaphoreRaw resource) noexcept
Constructs from SemaphoreParam.
Definition: SDL3pp_mutex.h:1312
SemaphoreRef(SemaphoreParam resource) noexcept
Constructs from SemaphoreParam.
Definition: SDL3pp_mutex.h:1300
SemaphoreRef(const SemaphoreRef &other) noexcept
Copy constructor.
Definition: SDL3pp_mutex.h:1318
~SemaphoreRef()
Destructor.
Definition: SDL3pp_mutex.h:1324