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)
224 :
Mutex(other.release())
253 : m_resource(SDL_CreateMutex())
258 ~Mutex() { SDL_DestroyMutex(m_resource); }
263 std::swap(m_resource, other.m_resource);
279 m_resource =
nullptr;
287 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
290 constexpr operator MutexParam() const noexcept {
return {m_resource}; }
380 :
Mutex(resource.value)
543 constexpr RWLock(std::nullptr_t) noexcept
556 : m_resource(resource)
616 : m_resource(SDL_CreateRWLock())
626 std::swap(m_resource, other.m_resource);
642 m_resource =
nullptr;
650 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
653 constexpr operator RWLockParam() const noexcept {
return {m_resource}; }
926 SDL_LockRWLockForReading(rwlock);
962 SDL_LockRWLockForWriting(rwlock);
992 CheckError(SDL_TryLockRWLockForReading(rwlock));
1030 CheckError(SDL_TryLockRWLockForWriting(rwlock));
1119 : 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);
1408 return SDL_TryWaitSemaphore(sem);
1432 std::chrono::milliseconds timeout)
1434 return SDL_WaitSemaphoreTimeout(sem, timeout.count());
1467 return SDL_GetSemaphoreValue(sem);
1509 : m_resource(resource)
1543 : m_resource(SDL_CreateCondition())
1553 std::swap(m_resource, other.m_resource);
1568 auto r = m_resource;
1569 m_resource =
nullptr;
1577 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
1771 SDL_BroadcastCondition(cond);
1804 SDL_WaitCondition(cond, mutex);
1842 std::chrono::milliseconds timeout)
1844 return SDL_WaitConditionTimeout(cond, mutex, timeout.count());
1848 std::chrono::milliseconds timeout)
1861 SDL_INIT_STATUS_UNINITIALIZED;
1864 SDL_INIT_STATUS_INITIALIZING;
1867 SDL_INIT_STATUS_INITIALIZED;
1870 SDL_INIT_STATUS_UNINITIALIZING;
2064 SDL_SetInitialized(state, initialized);
A means to block multiple threads until a condition is satisfied.
Definition: SDL3pp_mutex.h:1491
constexpr Condition(const ConditionRaw resource) noexcept
Constructs from ConditionParam.
Definition: SDL3pp_mutex.h:1508
constexpr ConditionRaw get() const noexcept
Retrieves underlying ConditionRaw.
Definition: SDL3pp_mutex.h:1563
Condition()
Create a condition variable.
Definition: SDL3pp_mutex.h:1542
constexpr Condition(Condition &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:1519
constexpr Condition(std::nullptr_t) noexcept
Default ctor.
Definition: SDL3pp_mutex.h:1496
constexpr ConditionRaw release() noexcept
Retrieves underlying ConditionRaw and clear this.
Definition: SDL3pp_mutex.h:1566
constexpr Condition & operator=(const Condition &other) noexcept=default
Assignment operator.
constexpr Condition(const Condition &other) noexcept=default
Copy constructor.
~Condition()
Destructor.
Definition: SDL3pp_mutex.h:1548
constexpr auto operator<=>(const Condition &other) const noexcept=default
Comparison.
constexpr Condition & operator=(Condition &&other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:1551
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:261
~Mutex()
Destructor.
Definition: SDL3pp_mutex.h:258
constexpr MutexRaw get() const noexcept
Retrieves underlying MutexRaw.
Definition: SDL3pp_mutex.h:273
Mutex()
Create a new mutex.
Definition: SDL3pp_mutex.h:252
constexpr Mutex(const Mutex &other) noexcept=default
Copy constructor.
constexpr auto operator<=>(const Mutex &other) const noexcept=default
Comparison.
constexpr Mutex & operator=(const Mutex &other) noexcept=default
Assignment operator.
constexpr MutexRaw release() noexcept
Retrieves underlying MutexRaw and clear this.
Definition: SDL3pp_mutex.h:276
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:223
A mutex that allows read-only threads to run in parallel.
Definition: SDL3pp_mutex.h:538
constexpr auto operator<=>(const RWLock &other) const noexcept=default
Comparison.
~RWLock()
Destructor.
Definition: SDL3pp_mutex.h:621
constexpr RWLock(const RWLockRaw resource) noexcept
Constructs from RWLockParam.
Definition: SDL3pp_mutex.h:555
constexpr RWLock(RWLock &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:566
constexpr RWLock & operator=(const RWLock &other) noexcept=default
Assignment operator.
RWLock()
Create a new read/write lock.
Definition: SDL3pp_mutex.h:615
constexpr RWLock(std::nullptr_t) noexcept
Default ctor.
Definition: SDL3pp_mutex.h:543
constexpr RWLockRaw get() const noexcept
Retrieves underlying RWLockRaw.
Definition: SDL3pp_mutex.h:636
constexpr RWLock & operator=(RWLock &&other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:624
constexpr RWLock(const RWLock &other) noexcept=default
Copy constructor.
constexpr RWLockRaw release() noexcept
Retrieves underlying RWLockRaw and clear this.
Definition: SDL3pp_mutex.h:639
A means to manage access to a resource, by count, between threads.
Definition: SDL3pp_mutex.h:1101
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.
constexpr Semaphore(const Semaphore &other) noexcept=default
Copy constructor.
Semaphore(Uint32 initial_value)
Create a semaphore.
Definition: SDL3pp_mutex.h:1160
constexpr Semaphore(const SemaphoreRaw resource) noexcept
Constructs from SemaphoreParam.
Definition: SDL3pp_mutex.h:1118
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:1106
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:2041
Uint32 GetSemaphoreValue(SemaphoreParam sem)
Get the current value of a semaphore.
Definition: SDL3pp_mutex.h:1465
constexpr InitStatus INIT_STATUS_UNINITIALIZING
INIT_STATUS_UNINITIALIZING.
Definition: SDL3pp_mutex.h:1869
void Unlock()
Unlock the read/write lock.
Definition: SDL3pp_mutex.h:1063
void LockMutex(MutexParam mutex)
Lock the mutex.
Definition: SDL3pp_mutex.h:447
bool TryWaitSemaphore(SemaphoreParam sem)
See if a semaphore has a positive value and decrement it if it does.
Definition: SDL3pp_mutex.h:1406
void TryLockForWriting()
Try to lock a read/write lock for writing without blocking.
Definition: SDL3pp_mutex.h:1033
void Wait(MutexParam mutex)
Wait until a condition variable is signaled.
Definition: SDL3pp_mutex.h:1807
constexpr InitStatus INIT_STATUS_INITIALIZING
INIT_STATUS_INITIALIZING.
Definition: SDL3pp_mutex.h:1863
void Signal()
Restart one of the threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1754
Mutex CreateMutex()
Create a new mutex.
Definition: SDL3pp_mutex.h:423
constexpr InitStatus INIT_STATUS_UNINITIALIZED
INIT_STATUS_UNINITIALIZED.
Definition: SDL3pp_mutex.h:1860
void SignalSemaphore(SemaphoreParam sem)
Atomically increment a semaphore's value and wake waiting threads.
Definition: SDL3pp_mutex.h:1453
bool TryWait()
See if a semaphore has a positive value and decrement it if it does.
Definition: SDL3pp_mutex.h:1411
void TryLockRWLockForReading(RWLockParam rwlock)
Try to lock a read/write lock for reading without blocking.
Definition: SDL3pp_mutex.h:990
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:1431
void Destroy()
Destroy a mutex created with Mutex.Mutex().
Definition: SDL3pp_mutex.h:515
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:475
void Unlock()
Unlock the mutex.
Definition: SDL3pp_mutex.h:496
void UnlockMutex(MutexParam mutex)
Unlock the mutex.
Definition: SDL3pp_mutex.h:494
SDL_InitStatus InitStatus
The current status of an InitState structure.
Definition: SDL3pp_mutex.h:1858
Condition CreateCondition()
Create a condition variable.
Definition: SDL3pp_mutex.h:1724
void DestroyRWLock(RWLockRaw rwlock)
Destroy a read/write lock created with RWLock.RWLock().
Definition: SDL3pp_mutex.h:1080
bool ShouldInit(InitStateRaw *state)
Return whether initialization should be done.
Definition: SDL3pp_mutex.h:2018
void BroadcastCondition(ConditionParam cond)
Restart all threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1769
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:2043
void LockForReading()
Lock the read/write lock for read only operations.
Definition: SDL3pp_mutex.h:929
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:1470
void UnlockRWLock(RWLockParam rwlock)
Unlock the read/write lock.
Definition: SDL3pp_mutex.h:1061
void TryLockMutex(MutexParam mutex)
Try to lock a mutex without blocking.
Definition: SDL3pp_mutex.h:470
void DestroyMutex(MutexRaw mutex)
Destroy a mutex created with Mutex.Mutex().
Definition: SDL3pp_mutex.h:513
bool ShouldInit()
Return whether initialization should be done.
Definition: SDL3pp_mutex.h:2020
bool WaitTimeout(std::chrono::milliseconds timeout)
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1437
void SignalCondition(ConditionParam cond)
Restart one of the threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1752
Semaphore CreateSemaphore(Uint32 initial_value)
Create a semaphore.
Definition: SDL3pp_mutex.h:1346
void LockRWLockForWriting(RWLockParam rwlock)
Lock the read/write lock for write operations.
Definition: SDL3pp_mutex.h:960
void Wait()
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1387
constexpr InitStatus INIT_STATUS_INITIALIZED
INIT_STATUS_INITIALIZED.
Definition: SDL3pp_mutex.h:1866
void Lock()
Lock the mutex.
Definition: SDL3pp_mutex.h:449
void Destroy()
Destroy a semaphore.
Definition: SDL3pp_mutex.h:1365
void Broadcast()
Restart all threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1774
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:1847
void Signal()
Atomically increment a semaphore's value and wake waiting threads.
Definition: SDL3pp_mutex.h:1455
RWLock CreateRWLock()
Create a new read/write lock.
Definition: SDL3pp_mutex.h:887
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:1028
void DestroySemaphore(SemaphoreRaw sem)
Destroy a semaphore.
Definition: SDL3pp_mutex.h:1363
void LockForWriting()
Lock the read/write lock for write operations.
Definition: SDL3pp_mutex.h:965
void Destroy()
Destroy a read/write lock created with RWLock.RWLock().
Definition: SDL3pp_mutex.h:1082
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:1840
void SetInitialized(InitStateRaw *state, bool initialized)
Finish an initialization state transition.
Definition: SDL3pp_mutex.h:2062
void TryLockForReading()
Try to lock a read/write lock for reading without blocking.
Definition: SDL3pp_mutex.h:995
void DestroyCondition(ConditionRaw cond)
Destroy a condition variable.
Definition: SDL3pp_mutex.h:1735
void Destroy()
Destroy a condition variable.
Definition: SDL3pp_mutex.h:1737
void LockRWLockForReading(RWLockParam rwlock)
Lock the read/write lock for read only operations.
Definition: SDL3pp_mutex.h:924
void WaitCondition(ConditionParam cond, MutexParam mutex)
Wait until a condition variable is signaled.
Definition: SDL3pp_mutex.h:1802
void SetInitialized(bool initialized)
Finish an initialization state transition.
Definition: SDL3pp_mutex.h:2067
void WaitSemaphore(SemaphoreParam sem)
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1385
::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(ConditionRaw value)
Constructs from ConditionRaw.
Definition: SDL3pp_mutex.h:153
constexpr auto operator<=>(const ConditionParam &other) const =default
Comparison.
constexpr ConditionParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_mutex.h:159
Semi-safe reference for Condition.
Definition: SDL3pp_mutex.h:1676
Condition()
Create a condition variable.
Definition: SDL3pp_mutex.h:1542
ConditionRef(ConditionParam resource) noexcept
Constructs from ConditionParam.
Definition: SDL3pp_mutex.h:1686
constexpr ConditionRef(const ConditionRef &other) noexcept=default
Copy constructor.
ConditionRef(ConditionRaw resource) noexcept
Constructs from ConditionParam.
Definition: SDL3pp_mutex.h:1698
~ConditionRef()
Destructor.
Definition: SDL3pp_mutex.h:1707
A structure used for thread-safe initialization and shutdown.
Definition: SDL3pp_mutex.h:1930
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:369
MutexRef(MutexRaw resource) noexcept
Constructs from MutexParam.
Definition: SDL3pp_mutex.h:391
Mutex()
Create a new mutex.
Definition: SDL3pp_mutex.h:252
constexpr MutexRef(const MutexRef &other) noexcept=default
Copy constructor.
MutexRef(MutexParam resource) noexcept
Constructs from MutexParam.
Definition: SDL3pp_mutex.h:379
~MutexRef()
Destructor.
Definition: SDL3pp_mutex.h:400
Safely wrap RWLock for non owning parameters.
Definition: SDL3pp_mutex.h:77
RWLockRaw value
parameter's RWLockRaw
Definition: SDL3pp_mutex.h:78
constexpr RWLockParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_mutex.h:87
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:813
RWLockRef(RWLockParam resource) noexcept
Constructs from RWLockParam.
Definition: SDL3pp_mutex.h:823
RWLock()
Create a new read/write lock.
Definition: SDL3pp_mutex.h:615
~RWLockRef()
Destructor.
Definition: SDL3pp_mutex.h:844
RWLockRef(RWLockRaw resource) noexcept
Constructs from RWLockParam.
Definition: SDL3pp_mutex.h:835
constexpr RWLockRef(const RWLockRef &other) noexcept=default
Copy constructor.
Safely wrap Semaphore for non owning parameters.
Definition: SDL3pp_mutex.h:113
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.
constexpr SemaphoreParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_mutex.h:123
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()
Destructor.
Definition: SDL3pp_mutex.h:1321
constexpr SemaphoreRef(const SemaphoreRef &other) noexcept=default
Copy constructor.