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
31// Forward decl
32struct MutexRef;
33
34// Forward decl
35struct Mutex;
36
46
56
57// Forward decl
58struct RWLockRef;
59
60// Forward decl
61struct RWLock;
62
72
82
83// Forward decl
84struct SemaphoreRef;
85
86// Forward decl
87struct Semaphore;
88
98
108
109// Forward decl
110struct ConditionRef;
111
112// Forward decl
113struct Condition;
114
124
134
151struct MutexRef : Resource<SDL_Mutex*>
152{
153 using Resource::Resource;
154
175 void Lock() { SDL_LockMutex(get()); }
176
195 void TryLock() { CheckError(SDL_TryLockMutex(get())); }
196
213 void Unlock() { SDL_UnlockMutex(get()); }
214
230 static void reset(SDL_Mutex* resource) { SDL_DestroyMutex(resource); }
231};
232
240struct Mutex : ResourceUnique<MutexRef>
241{
243
264 static Mutex Create() { return Mutex(SDL_CreateMutex()); }
265
280 void Destroy() { reset(); }
285
286};
287
288
290{
291 return MutexShared(std::move(*this));
292}
293
303struct MutexUnsafe : ResourceUnsafe<MutexRef>
304{
306
310 constexpr explicit MutexUnsafe(Mutex&& other)
311 : MutexUnsafe(other.release())
312 {
313 }
314};
315
338struct RWLockRef : Resource<SDL_RWLock*>
339{
340 using Resource::Resource;
341
376 void LockForReading() { SDL_LockRWLockForReading(get()); }
377
406 void LockForWriting() { SDL_LockRWLockForWriting(get()); }
407
430 void TryLockForReading() { CheckError(SDL_TryLockRWLockForReading(get())); }
431
459 void TryLockForWriting() { CheckError(SDL_TryLockRWLockForWriting(get())); }
460
483 void Unlock() { SDL_UnlockRWLock(get()); }
484
500 static void reset(SDL_RWLock* resource) { SDL_DestroyRWLock(resource); }
501};
502
510struct RWLock : ResourceUnique<RWLockRef>
511{
513
554 static RWLock Create() { return RWLock(SDL_CreateRWLock()); }
555
570 void Destroy() { reset(); }
575
576};
577
578
580{
581 return RWLockShared(std::move(*this));
582}
583
593struct RWLockUnsafe : ResourceUnsafe<RWLockRef>
594{
596
600 constexpr explicit RWLockUnsafe(RWLock&& other)
601 : RWLockUnsafe(other.release())
602 {
603 }
604};
605
624struct SemaphoreRef : Resource<SDL_Semaphore*>
625{
626 using Resource::Resource;
627
645 void Wait() { SDL_WaitSemaphore(get()); }
646
663 bool TryWait() { return SDL_TryWaitSemaphore(get()); }
664
682 bool WaitTimeout(std::chrono::milliseconds timeout)
683 {
684 return SDL_WaitSemaphoreTimeout(get(), timeout.count());
685 }
686
697 void Signal() { SDL_SignalSemaphore(get()); }
698
706 Uint32 GetValue() const { return SDL_GetSemaphoreValue(get()); }
707
720 static void reset(SDL_Semaphore* resource) { SDL_DestroySemaphore(resource); }
721};
722
730struct Semaphore : ResourceUnique<SemaphoreRef>
731{
733
756 static Semaphore Create(Uint32 initial_value)
757 {
758 return Semaphore(SDL_CreateSemaphore(initial_value));
759 }
760
772 void Destroy() { reset(); }
777
778};
779
780
782{
783 return SemaphoreShared(std::move(*this));
784}
785
795struct SemaphoreUnsafe : ResourceUnsafe<SemaphoreRef>
796{
798
802 constexpr explicit SemaphoreUnsafe(Semaphore&& other)
803 : SemaphoreUnsafe(other.release())
804 {
805 }
806};
807
825struct ConditionRef : Resource<SDL_Condition*>
826{
827 using Resource::Resource;
828
841 void Signal() { SDL_SignalCondition(get()); }
842
855 void Broadcast() { SDL_BroadcastCondition(get()); }
856
882 void Wait(MutexRef mutex) { SDL_WaitCondition(get(), mutex); }
883
911 bool WaitTimeout(MutexRef mutex, std::chrono::milliseconds timeout)
912 {
913 return SDL_WaitConditionTimeout(get(), mutex, timeout.count());
914 }
915
925 static void reset(SDL_Condition* resource) { SDL_DestroyCondition(resource); }
926};
927
935struct Condition : ResourceUnique<ConditionRef>
936{
938
953 static Condition Create() { return Condition(SDL_CreateCondition()); }
954
963 void Destroy() { reset(); }
968
969};
970
971
973{
974 return ConditionShared(std::move(*this));
975}
976
986struct ConditionUnsafe : ResourceUnsafe<ConditionRef>
987{
989
993 constexpr explicit ConditionUnsafe(Condition&& other)
994 : ConditionUnsafe(other.release())
995 {
996 }
997};
998
1004using InitStatus = SDL_InitStatus;
1005
1007 SDL_INIT_STATUS_UNINITIALIZED;
1008
1010 SDL_INIT_STATUS_INITIALIZING;
1011
1013 SDL_INIT_STATUS_INITIALIZED;
1014
1016 SDL_INIT_STATUS_UNINITIALIZING;
1017
1076struct InitState : SDL_InitState
1077{
1081 constexpr InitState()
1082 : SDL_InitState{0}
1083 {
1084 }
1085
1106 bool ShouldInit() { return SDL_ShouldInit(this); }
1107
1126 bool ShouldQuit() { return SDL_ShouldQuit(this); }
1127
1144 void SetInitialized(bool initialized)
1145 {
1146 SDL_SetInitialized(this, initialized);
1147 }
1148};
1149
1151
1152} // namespace SDL
1153
1154#endif /* SDL3PP_MUTEX_H_ */
RESOURCE release()
Returns reference and reset this.
Definition SDL3pp_resource.h:178
Implement shared ownership for a resource.
Definition SDL3pp_resource.h:283
Implement unique ownership for a resource.
Definition SDL3pp_resource.h:226
constexpr ResourceUnique(std::nullptr_t=nullptr)
Default constructor.
Definition SDL3pp_resource.h:231
void reset()
Resets the value, destroying the resource if not nullptr.
Definition SDL3pp_resource.h:265
A dumb pointer to resource.
Definition SDL3pp_resource.h:197
constexpr ResourceUnsafe()=default
Default constructor.
Implement weak ownership for a resource.
Definition SDL3pp_resource.h:328
A SDL managed resource.
Definition SDL3pp_resource.h:29
constexpr Resource(T resource={})
Constructs from the underlying resource.
Definition SDL3pp_resource.h:37
constexpr SDL_Mutex * get() const
Return contained resource;.
Definition SDL3pp_resource.h:76
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
ResourceShared< RWLock > RWLockShared
Handle to a shared rWLock.
Definition SDL3pp_mutex.h:71
RWLockShared share()
Move this rWLock into a RWLockShared.
Definition SDL3pp_mutex.h:579
constexpr InitStatus INIT_STATUS_UNINITIALIZING
INIT_STATUS_UNINITIALIZING.
Definition SDL3pp_mutex.h:1015
ResourceShared< Semaphore > SemaphoreShared
Handle to a shared semaphore.
Definition SDL3pp_mutex.h:97
ResourceShared< Mutex > MutexShared
Handle to a shared mutex.
Definition SDL3pp_mutex.h:45
constexpr InitStatus INIT_STATUS_INITIALIZING
INIT_STATUS_INITIALIZING.
Definition SDL3pp_mutex.h:1009
constexpr InitStatus INIT_STATUS_UNINITIALIZED
INIT_STATUS_UNINITIALIZED.
Definition SDL3pp_mutex.h:1006
SemaphoreShared share()
Move this semaphore into a SemaphoreShared.
Definition SDL3pp_mutex.h:781
SDL_InitStatus InitStatus
The current status of an InitState structure.
Definition SDL3pp_mutex.h:1004
constexpr InitStatus INIT_STATUS_INITIALIZED
INIT_STATUS_INITIALIZED.
Definition SDL3pp_mutex.h:1012
ConditionShared share()
Move this condition into a ConditionShared.
Definition SDL3pp_mutex.h:972
MutexShared share()
Move this mutex into a MutexShared.
Definition SDL3pp_mutex.h:289
ResourceShared< Condition > ConditionShared
Handle to a shared condition.
Definition SDL3pp_mutex.h:123
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
A means to block multiple threads until a condition is satisfied.
Definition SDL3pp_mutex.h:826
void Broadcast()
Restart all threads that are waiting on the condition variable.
Definition SDL3pp_mutex.h:855
void Signal()
Restart one of the threads that are waiting on the condition variable.
Definition SDL3pp_mutex.h:841
void Wait(MutexRef mutex)
Wait until a condition variable is signaled.
Definition SDL3pp_mutex.h:882
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:911
static void reset(SDL_Condition *resource)
Destroy a condition variable.
Definition SDL3pp_mutex.h:925
Unsafe Handle to condition.
Definition SDL3pp_mutex.h:987
constexpr ConditionUnsafe(Condition &&other)
Constructs ConditionUnsafe from Condition.
Definition SDL3pp_mutex.h:993
Handle to an owned condition.
Definition SDL3pp_mutex.h:936
static Condition Create()
Create a condition variable.
Definition SDL3pp_mutex.h:953
void Destroy()
Destroy a condition variable.
Definition SDL3pp_mutex.h:963
A structure used for thread-safe initialization and shutdown.
Definition SDL3pp_mutex.h:1077
bool ShouldQuit()
Return whether cleanup should be done.
Definition SDL3pp_mutex.h:1126
bool ShouldInit()
Return whether initialization should be done.
Definition SDL3pp_mutex.h:1106
constexpr InitState()
Constructor.
Definition SDL3pp_mutex.h:1081
void SetInitialized(bool initialized)
Finish an initialization state transition.
Definition SDL3pp_mutex.h:1144
A means to serialize access to a resource between threads.
Definition SDL3pp_mutex.h:152
void TryLock()
Try to lock a mutex without blocking.
Definition SDL3pp_mutex.h:195
void Unlock()
Unlock the mutex.
Definition SDL3pp_mutex.h:213
static void reset(SDL_Mutex *resource)
Destroy a mutex created with Mutex.Create().
Definition SDL3pp_mutex.h:230
void Lock()
Lock the mutex.
Definition SDL3pp_mutex.h:175
Unsafe Handle to mutex.
Definition SDL3pp_mutex.h:304
constexpr MutexUnsafe(Mutex &&other)
Constructs MutexUnsafe from Mutex.
Definition SDL3pp_mutex.h:310
Handle to an owned mutex.
Definition SDL3pp_mutex.h:241
void Destroy()
Destroy a mutex created with Mutex.Create().
Definition SDL3pp_mutex.h:280
static Mutex Create()
Create a new mutex.
Definition SDL3pp_mutex.h:264
A mutex that allows read-only threads to run in parallel.
Definition SDL3pp_mutex.h:339
void LockForWriting()
Lock the read/write lock for write operations.
Definition SDL3pp_mutex.h:406
static void reset(SDL_RWLock *resource)
Destroy a read/write lock created with RWLock.Create().
Definition SDL3pp_mutex.h:500
void TryLockForWriting()
Try to lock a read/write lock for writing without blocking.
Definition SDL3pp_mutex.h:459
void Unlock()
Unlock the read/write lock.
Definition SDL3pp_mutex.h:483
void TryLockForReading()
Try to lock a read/write lock for reading without blocking.
Definition SDL3pp_mutex.h:430
void LockForReading()
Lock the read/write lock for read only operations.
Definition SDL3pp_mutex.h:376
Unsafe Handle to rWLock.
Definition SDL3pp_mutex.h:594
constexpr RWLockUnsafe(RWLock &&other)
Constructs RWLockUnsafe from RWLock.
Definition SDL3pp_mutex.h:600
Handle to an owned rWLock.
Definition SDL3pp_mutex.h:511
static RWLock Create()
Create a new read/write lock.
Definition SDL3pp_mutex.h:554
void Destroy()
Destroy a read/write lock created with RWLock.Create().
Definition SDL3pp_mutex.h:570
A means to manage access to a resource, by count, between threads.
Definition SDL3pp_mutex.h:625
void Signal()
Atomically increment a semaphore's value and wake waiting threads.
Definition SDL3pp_mutex.h:697
bool TryWait()
See if a semaphore has a positive value and decrement it if it does.
Definition SDL3pp_mutex.h:663
static void reset(SDL_Semaphore *resource)
Destroy a semaphore.
Definition SDL3pp_mutex.h:720
Uint32 GetValue() const
Get the current value of a semaphore.
Definition SDL3pp_mutex.h:706
void Wait()
Wait until a semaphore has a positive value and then decrements it.
Definition SDL3pp_mutex.h:645
bool WaitTimeout(std::chrono::milliseconds timeout)
Wait until a semaphore has a positive value and then decrements it.
Definition SDL3pp_mutex.h:682
Unsafe Handle to semaphore.
Definition SDL3pp_mutex.h:796
constexpr SemaphoreUnsafe(Semaphore &&other)
Constructs SemaphoreUnsafe from Semaphore.
Definition SDL3pp_mutex.h:802
Handle to an owned semaphore.
Definition SDL3pp_mutex.h:731
void Destroy()
Destroy a semaphore.
Definition SDL3pp_mutex.h:772
static Semaphore Create(Uint32 initial_value)
Create a semaphore.
Definition SDL3pp_mutex.h:756