4#include <SDL3/SDL_mutex.h>
5#include "SDL3pp_stdinc.h"
6#include "SDL3pp_thread.h"
92 constexpr Mutex(std::nullptr_t) noexcept
105 : m_resource(resource)
114 :
Mutex(other.release())
147 ~Mutex() { SDL_DestroyMutex(m_resource); }
152 std::swap(m_resource, other.m_resource);
166 m_resource =
nullptr;
174 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
289 :
Mutex(resource.get())
357 : m_resource(SDL_CreateMutex())
485 constexpr RWLock(std::nullptr_t) noexcept
486 : m_resource(
nullptr)
498 : m_resource(resource)
565 std::swap(m_resource, other.m_resource);
579 m_resource =
nullptr;
587 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
875 : m_resource(SDL_CreateRWLock())
918 SDL_LockRWLockForReading(rwlock);
956 SDL_LockRWLockForWriting(rwlock);
988 return SDL_TryLockRWLockForReading(rwlock);
1028 return SDL_TryLockRWLockForWriting(rwlock);
1110 : m_resource(
nullptr)
1122 : m_resource(resource)
1171 std::swap(m_resource, other.m_resource);
1184 auto r = m_resource;
1185 m_resource =
nullptr;
1193 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
1268 bool WaitTimeout(std::chrono::milliseconds timeout);
1397 : m_resource(SDL_CreateSemaphore(initial_value))
1464 return SDL_TryWaitSemaphore(sem);
1490 std::chrono::milliseconds timeout)
1492 return SDL_WaitSemaphoreTimeout(sem,
narrowS32(timeout.count()));
1529 return SDL_GetSemaphoreValue(sem);
1559 : m_resource(
nullptr)
1571 : m_resource(resource)
1612 std::swap(m_resource, other.m_resource);
1625 auto r = m_resource;
1626 m_resource =
nullptr;
1634 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
1821 : m_resource(SDL_CreateCondition())
1872 SDL_BroadcastCondition(cond);
1905 SDL_WaitCondition(cond, mutex);
1943 std::chrono::milliseconds timeout)
1945 return SDL_WaitConditionTimeout(cond, mutex,
narrowS32(timeout.count()));
1949 std::chrono::milliseconds timeout)
1962 SDL_INIT_STATUS_UNINITIALIZED;
1965 SDL_INIT_STATUS_INITIALIZING;
1968 SDL_INIT_STATUS_INITIALIZED;
1971 SDL_INIT_STATUS_UNINITIALIZING;
2165 SDL_SetInitialized(state, initialized);
A means to block multiple threads until a condition is satisfied.
Definition: SDL3pp_mutex.h:1553
constexpr ConditionRaw get() const noexcept
Retrieves underlying ConditionRaw.
Definition: SDL3pp_mutex.h:1620
constexpr Condition(Condition &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:1579
constexpr Condition(std::nullptr_t) noexcept
Default ctor.
Definition: SDL3pp_mutex.h:1558
Condition & operator=(const Condition &other)=delete
Assignment operator.
constexpr ConditionRaw release() noexcept
Retrieves underlying ConditionRaw and clear this.
Definition: SDL3pp_mutex.h:1623
constexpr Condition(const Condition &other) noexcept=delete
Copy constructor.
~Condition()
Destructor.
Definition: SDL3pp_mutex.h:1607
constexpr Condition(ConditionRaw resource) noexcept
Constructs from raw Condition.
Definition: SDL3pp_mutex.h:1570
constexpr auto operator<=>(const Condition &other) const noexcept=default
Comparison.
constexpr Condition & operator=(Condition &&other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:1610
A means to serialize access to a resource between threads.
Definition: SDL3pp_mutex.h:87
constexpr Mutex & operator=(Mutex &&other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:150
~Mutex()
Destructor.
Definition: SDL3pp_mutex.h:147
constexpr MutexRaw get() const noexcept
Retrieves underlying MutexRaw.
Definition: SDL3pp_mutex.h:160
constexpr auto operator<=>(const Mutex &other) const noexcept=default
Comparison.
constexpr Mutex(const Mutex &other) noexcept=delete
Copy constructor.
constexpr MutexRaw release() noexcept
Retrieves underlying MutexRaw and clear this.
Definition: SDL3pp_mutex.h:163
constexpr Mutex(std::nullptr_t) noexcept
Default ctor.
Definition: SDL3pp_mutex.h:92
constexpr Mutex(MutexRaw resource) noexcept
Constructs from raw Mutex.
Definition: SDL3pp_mutex.h:104
constexpr Mutex(Mutex &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:113
Mutex & operator=(const Mutex &other)=delete
Assignment operator.
A mutex that allows read-only threads to run in parallel.
Definition: SDL3pp_mutex.h:480
constexpr auto operator<=>(const RWLock &other) const noexcept=default
Comparison.
~RWLock()
Destructor.
Definition: SDL3pp_mutex.h:560
constexpr RWLock(RWLock &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:506
constexpr RWLock(const RWLock &other) noexcept=delete
Copy constructor.
constexpr RWLock(std::nullptr_t) noexcept
Default ctor.
Definition: SDL3pp_mutex.h:485
constexpr RWLockRaw get() const noexcept
Retrieves underlying RWLockRaw.
Definition: SDL3pp_mutex.h:573
constexpr RWLock & operator=(RWLock &&other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:563
constexpr RWLock(RWLockRaw resource) noexcept
Constructs from raw RWLock.
Definition: SDL3pp_mutex.h:497
constexpr RWLockRaw release() noexcept
Retrieves underlying RWLockRaw and clear this.
Definition: SDL3pp_mutex.h:576
RWLock & operator=(const RWLock &other)=delete
Assignment operator.
A means to manage access to a resource, by count, between threads.
Definition: SDL3pp_mutex.h:1104
constexpr Semaphore(SemaphoreRaw resource) noexcept
Constructs from raw Semaphore.
Definition: SDL3pp_mutex.h:1121
constexpr SemaphoreRaw release() noexcept
Retrieves underlying SemaphoreRaw and clear this.
Definition: SDL3pp_mutex.h:1182
constexpr Semaphore & operator=(Semaphore &&other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:1169
~Semaphore()
Destructor.
Definition: SDL3pp_mutex.h:1166
constexpr Semaphore(const Semaphore &other) noexcept=delete
Copy constructor.
constexpr auto operator<=>(const Semaphore &other) const noexcept=default
Comparison.
Semaphore & operator=(const Semaphore &other)=delete
Assignment operator.
constexpr SemaphoreRaw get() const noexcept
Retrieves underlying SemaphoreRaw.
Definition: SDL3pp_mutex.h:1179
constexpr Semaphore(Semaphore &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:1130
constexpr Semaphore(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_mutex.h:1109
bool ShouldQuit(InitStateRaw *state)
Return whether cleanup should be done.
Definition: SDL3pp_mutex.h:2142
void BroadcastCondition(ConditionRef cond)
Restart all threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1870
constexpr InitStatus INIT_STATUS_UNINITIALIZING
INIT_STATUS_UNINITIALIZING.
Definition: SDL3pp_mutex.h:1970
void Unlock()
Unlock the read/write lock.
Definition: SDL3pp_mutex.h:1064
Condition()
Create a condition variable.
Definition: SDL3pp_mutex.h:1820
constexpr InitStatus INIT_STATUS_INITIALIZING
INIT_STATUS_INITIALIZING.
Definition: SDL3pp_mutex.h:1964
void Signal()
Restart one of the threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1855
Mutex CreateMutex()
Create a new mutex.
Definition: SDL3pp_mutex.h:354
bool WaitTimeout(MutexRef mutex, std::chrono::milliseconds timeout)
Wait until a condition variable is signaled or a certain time has passed.
Definition: SDL3pp_mutex.h:1948
constexpr InitStatus INIT_STATUS_UNINITIALIZED
INIT_STATUS_UNINITIALIZED.
Definition: SDL3pp_mutex.h:1961
bool TryWait()
See if a semaphore has a positive value and decrement it if it does.
Definition: SDL3pp_mutex.h:1467
SDL_RWLock * RWLockRaw
Alias to raw representation for RWLock.
Definition: SDL3pp_mutex.h:43
void Destroy()
Destroy a mutex created with CreateMutex().
Definition: SDL3pp_mutex.h:457
Uint32 GetSemaphoreValue(SemaphoreRef sem)
Get the current value of a semaphore.
Definition: SDL3pp_mutex.h:1527
SDL_InitState InitStateRaw
Alias to raw representation for InitState.
Definition: SDL3pp_mutex.h:67
void Unlock()
Unlock the mutex.
Definition: SDL3pp_mutex.h:436
bool TryLockRWLockForReading(RWLockRef rwlock)
Try to lock a read/write lock for reading without blocking.
Definition: SDL3pp_mutex.h:986
void UnlockRWLock(RWLockRef rwlock)
Unlock the read/write lock.
Definition: SDL3pp_mutex.h:1062
SDL_InitStatus InitStatus
The current status of an InitState structure.
Definition: SDL3pp_mutex.h:1959
Condition CreateCondition()
Create a condition variable.
Definition: SDL3pp_mutex.h:1818
void DestroyRWLock(RWLockRaw rwlock)
Destroy a read/write lock created with CreateRWLock().
Definition: SDL3pp_mutex.h:1083
bool ShouldInit(InitStateRaw *state)
Return whether initialization should be done.
Definition: SDL3pp_mutex.h:2119
Mutex()
Create a new mutex.
Definition: SDL3pp_mutex.h:356
SDL_Condition * ConditionRaw
Alias to raw representation for Condition.
Definition: SDL3pp_mutex.h:61
void WaitSemaphore(SemaphoreRef sem)
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1439
bool ShouldQuit()
Return whether cleanup should be done.
Definition: SDL3pp_mutex.h:2144
void LockForReading()
Lock the read/write lock for read only operations.
Definition: SDL3pp_mutex.h:921
bool TryWaitSemaphore(SemaphoreRef sem)
See if a semaphore has a positive value and decrement it if it does.
Definition: SDL3pp_mutex.h:1462
SDL_Semaphore * SemaphoreRaw
Alias to raw representation for Semaphore.
Definition: SDL3pp_mutex.h:52
Uint32 GetValue() const
Get the current value of a semaphore.
Definition: SDL3pp_mutex.h:1532
void Wait(MutexRef mutex)
Wait until a condition variable is signaled.
Definition: SDL3pp_mutex.h:1908
bool TryLockForWriting()
Try to lock a read/write lock for writing without blocking.
Definition: SDL3pp_mutex.h:1031
void DestroyMutex(MutexRaw mutex)
Destroy a mutex created with CreateMutex().
Definition: SDL3pp_mutex.h:455
bool ShouldInit()
Return whether initialization should be done.
Definition: SDL3pp_mutex.h:2121
void LockRWLockForWriting(RWLockRef rwlock)
Lock the read/write lock for write operations.
Definition: SDL3pp_mutex.h:954
RWLock()
Create a new read/write lock.
Definition: SDL3pp_mutex.h:874
bool WaitTimeout(std::chrono::milliseconds timeout)
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1495
bool TryLockRWLockForWriting(RWLockRef rwlock)
Try to lock a read/write lock for writing without blocking.
Definition: SDL3pp_mutex.h:1026
Semaphore CreateSemaphore(Uint32 initial_value)
Create a semaphore.
Definition: SDL3pp_mutex.h:1391
void UnlockMutex(MutexRef mutex)
Unlock the mutex.
Definition: SDL3pp_mutex.h:434
void LockMutex(MutexRef mutex)
Lock the mutex.
Definition: SDL3pp_mutex.h:385
void Wait()
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1441
void SignalSemaphore(SemaphoreRef sem)
Atomically increment a semaphore's value and wake waiting threads.
Definition: SDL3pp_mutex.h:1513
constexpr InitStatus INIT_STATUS_INITIALIZED
INIT_STATUS_INITIALIZED.
Definition: SDL3pp_mutex.h:1967
void Lock()
Lock the mutex.
Definition: SDL3pp_mutex.h:387
void Destroy()
Destroy a semaphore.
Definition: SDL3pp_mutex.h:1417
void Broadcast()
Restart all threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1875
bool TryLockForReading()
Try to lock a read/write lock for reading without blocking.
Definition: SDL3pp_mutex.h:991
void Signal()
Atomically increment a semaphore's value and wake waiting threads.
Definition: SDL3pp_mutex.h:1515
RWLock CreateRWLock()
Create a new read/write lock.
Definition: SDL3pp_mutex.h:872
bool TryLockMutex(MutexRef mutex)
Try to lock a mutex without blocking.
Definition: SDL3pp_mutex.h:410
SDL_Mutex * MutexRaw
Alias to raw representation for Mutex.
Definition: SDL3pp_mutex.h:34
void DestroySemaphore(SemaphoreRaw sem)
Destroy a semaphore.
Definition: SDL3pp_mutex.h:1415
void LockForWriting()
Lock the read/write lock for write operations.
Definition: SDL3pp_mutex.h:959
void Destroy()
Destroy a read/write lock created with CreateRWLock().
Definition: SDL3pp_mutex.h:1085
void LockRWLockForReading(RWLockRef rwlock)
Lock the read/write lock for read only operations.
Definition: SDL3pp_mutex.h:916
void SetInitialized(InitStateRaw *state, bool initialized)
Finish an initialization state transition.
Definition: SDL3pp_mutex.h:2163
bool WaitSemaphoreTimeout(SemaphoreRef sem, std::chrono::milliseconds timeout)
Wait until a semaphore has a positive value and then decrements it.
Definition: SDL3pp_mutex.h:1489
void DestroyCondition(ConditionRaw cond)
Destroy a condition variable.
Definition: SDL3pp_mutex.h:1836
bool TryLock()
Try to lock a mutex without blocking.
Definition: SDL3pp_mutex.h:412
void Destroy()
Destroy a condition variable.
Definition: SDL3pp_mutex.h:1838
bool WaitConditionTimeout(ConditionRef cond, MutexRef mutex, std::chrono::milliseconds timeout)
Wait until a condition variable is signaled or a certain time has passed.
Definition: SDL3pp_mutex.h:1941
void SetInitialized(bool initialized)
Finish an initialization state transition.
Definition: SDL3pp_mutex.h:2168
void WaitCondition(ConditionRef cond, MutexRef mutex)
Wait until a condition variable is signaled.
Definition: SDL3pp_mutex.h:1903
void SignalCondition(ConditionRef cond)
Restart one of the threads that are waiting on the condition variable.
Definition: SDL3pp_mutex.h:1853
::Uint32 Uint32
An unsigned 32-bit integer type.
Definition: SDL3pp_stdinc.h:281
Main include header for the SDL3pp library.
Sint32 narrowS32(T value)
Narrows to Sint32.
Definition: SDL3pp_stdinc.h:6425
Reference for Condition.
Definition: SDL3pp_mutex.h:1736
constexpr ConditionRef(ConditionRaw resource) noexcept
Constructs from raw Condition.
Definition: SDL3pp_mutex.h:1746
Condition()
Create a condition variable.
Definition: SDL3pp_mutex.h:1820
constexpr ConditionRef(const Condition &resource) noexcept
Constructs from Condition.
Definition: SDL3pp_mutex.h:1758
constexpr ConditionRef(Condition &&resource) noexcept
Constructs from Condition.
Definition: SDL3pp_mutex.h:1770
ConditionRef & operator=(const ConditionRef &other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:1791
constexpr ConditionRef(const ConditionRef &other) noexcept
Copy constructor.
Definition: SDL3pp_mutex.h:1776
~ConditionRef()
Destructor.
Definition: SDL3pp_mutex.h:1788
constexpr ConditionRef(ConditionRef &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:1782
A structure used for thread-safe initialization and shutdown.
Definition: SDL3pp_mutex.h:2031
Reference for Mutex.
Definition: SDL3pp_mutex.h:266
constexpr MutexRef(MutexRef &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:312
Mutex()
Create a new mutex.
Definition: SDL3pp_mutex.h:356
constexpr MutexRef(const MutexRef &other) noexcept
Copy constructor.
Definition: SDL3pp_mutex.h:306
MutexRef & operator=(const MutexRef &other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:321
~MutexRef()
Destructor.
Definition: SDL3pp_mutex.h:318
constexpr MutexRef(MutexRaw resource) noexcept
Constructs from raw Mutex.
Definition: SDL3pp_mutex.h:276
constexpr MutexRef(const Mutex &resource) noexcept
Constructs from Mutex.
Definition: SDL3pp_mutex.h:288
constexpr MutexRef(Mutex &&resource) noexcept
Constructs from Mutex.
Definition: SDL3pp_mutex.h:300
Reference for RWLock.
Definition: SDL3pp_mutex.h:764
constexpr RWLockRef(RWLock &&resource) noexcept
Constructs from RWLock.
Definition: SDL3pp_mutex.h:798
constexpr RWLockRef(const RWLockRef &other) noexcept
Copy constructor.
Definition: SDL3pp_mutex.h:804
constexpr RWLockRef(RWLockRaw resource) noexcept
Constructs from raw RWLock.
Definition: SDL3pp_mutex.h:774
constexpr RWLockRef(const RWLock &resource) noexcept
Constructs from RWLock.
Definition: SDL3pp_mutex.h:786
RWLockRef & operator=(const RWLockRef &other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:819
RWLock()
Create a new read/write lock.
Definition: SDL3pp_mutex.h:874
constexpr RWLockRef(RWLockRef &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:810
~RWLockRef()
Destructor.
Definition: SDL3pp_mutex.h:816
Reference for Semaphore.
Definition: SDL3pp_mutex.h:1301
constexpr SemaphoreRef(const Semaphore &resource) noexcept
Constructs from Semaphore.
Definition: SDL3pp_mutex.h:1323
constexpr SemaphoreRef(SemaphoreRaw resource) noexcept
Constructs from raw Semaphore.
Definition: SDL3pp_mutex.h:1311
constexpr SemaphoreRef(SemaphoreRef &&other) noexcept
Move constructor.
Definition: SDL3pp_mutex.h:1347
~SemaphoreRef()
Destructor.
Definition: SDL3pp_mutex.h:1353
constexpr SemaphoreRef(Semaphore &&resource) noexcept
Constructs from Semaphore.
Definition: SDL3pp_mutex.h:1335
constexpr SemaphoreRef(const SemaphoreRef &other) noexcept
Copy constructor.
Definition: SDL3pp_mutex.h:1341
SemaphoreRef & operator=(const SemaphoreRef &other) noexcept
Assignment operator.
Definition: SDL3pp_mutex.h:1356