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 MutexBase;
33
34// Forward decl
35struct MutexRef;
36
37// Forward decl
38struct Mutex;
39
40// Forward decl
41struct RWLockBase;
42
43// Forward decl
44struct RWLockRef;
45
46// Forward decl
47struct RWLock;
48
49// Forward decl
50struct SemaphoreBase;
51
52// Forward decl
53struct SemaphoreRef;
54
55// Forward decl
56struct Semaphore;
57
58// Forward decl
59struct ConditionBase;
60
61// Forward decl
62struct ConditionRef;
63
64// Forward decl
65struct Condition;
66
84struct MutexBase : Resource<SDL_Mutex*>
85{
87
109 : Resource(SDL_CreateMutex())
110 {
111 }
112
134 void Lock() { SDL_LockMutex(get()); }
135
154 void TryLock() { CheckError(SDL_TryLockMutex(get())); }
155
172 void Unlock() { SDL_UnlockMutex(get()); }
173};
174
184{
186
190 constexpr MutexRef(const MutexRef& other)
191 : MutexBase(other.get())
192 {
193 }
194
198 constexpr MutexRef(MutexRef&& other)
199 : MutexBase(other.release())
200 {
201 }
202
206 constexpr ~MutexRef() = default;
207
212 {
213 release(other.release());
214 return *this;
215 }
216
231 void reset(SDL_Mutex* newResource = {})
232 {
233 SDL_DestroyMutex(release(newResource));
234 }
235};
236
246{
247 using MutexRef::MutexRef;
248
252 constexpr explicit Mutex(SDL_Mutex* resource = {})
253 : MutexRef(resource)
254 {
255 }
256
257 constexpr Mutex(const Mutex& other) = delete;
258
262 constexpr Mutex(Mutex&& other) = default;
263
267 ~Mutex() { reset(); }
268
273 {
274 reset(other.release());
275 return *this;
276 }
277};
278
302struct RWLockBase : Resource<SDL_RWLock*>
303{
304 using Resource::Resource;
305
346 : Resource(SDL_CreateRWLock())
347 {
348 }
349
384 void LockForReading() { SDL_LockRWLockForReading(get()); }
385
414 void LockForWriting() { SDL_LockRWLockForWriting(get()); }
415
438 void TryLockForReading() { CheckError(SDL_TryLockRWLockForReading(get())); }
439
467 void TryLockForWriting() { CheckError(SDL_TryLockRWLockForWriting(get())); }
468
491 void Unlock() { SDL_UnlockRWLock(get()); }
492};
493
503{
505
509 constexpr RWLockRef(const RWLockRef& other)
510 : RWLockBase(other.get())
511 {
512 }
513
517 constexpr RWLockRef(RWLockRef&& other)
518 : RWLockBase(other.release())
519 {
520 }
521
525 constexpr ~RWLockRef() = default;
526
531 {
532 release(other.release());
533 return *this;
534 }
535
549 void reset(SDL_RWLock* newResource = {})
550 {
551 SDL_DestroyRWLock(release(newResource));
552 }
553};
554
564{
566
570 constexpr explicit RWLock(SDL_RWLock* resource = {})
571 : RWLockRef(resource)
572 {
573 }
574
575 constexpr RWLock(const RWLock& other) = delete;
576
580 constexpr RWLock(RWLock&& other) = default;
581
585 ~RWLock() { reset(); }
586
591 {
592 reset(other.release());
593 return *this;
594 }
595};
596
616struct SemaphoreBase : Resource<SDL_Semaphore*>
617{
618 using Resource::Resource;
619
641 SemaphoreBase(Uint32 initial_value)
642 : Resource(SDL_CreateSemaphore(initial_value))
643 {
644 }
645
663 void Wait() { SDL_WaitSemaphore(get()); }
664
681 bool TryWait() { return SDL_TryWaitSemaphore(get()); }
682
700 bool WaitTimeout(std::chrono::milliseconds timeout)
701 {
702 return SDL_WaitSemaphoreTimeout(get(), timeout.count());
703 }
704
715 void Signal() { SDL_SignalSemaphore(get()); }
716
724 Uint32 GetValue() const { return SDL_GetSemaphoreValue(get()); }
725};
726
736{
738
742 constexpr SemaphoreRef(const SemaphoreRef& other)
743 : SemaphoreBase(other.get())
744 {
745 }
746
750 constexpr SemaphoreRef(SemaphoreRef&& other)
751 : SemaphoreBase(other.release())
752 {
753 }
754
758 constexpr ~SemaphoreRef() = default;
759
764 {
765 release(other.release());
766 return *this;
767 }
768
779 void reset(SDL_Semaphore* newResource = {})
780 {
781 SDL_DestroySemaphore(release(newResource));
782 }
783};
784
794{
796
800 constexpr explicit Semaphore(SDL_Semaphore* resource = {})
801 : SemaphoreRef(resource)
802 {
803 }
804
805 constexpr Semaphore(const Semaphore& other) = delete;
806
810 constexpr Semaphore(Semaphore&& other) = default;
811
816
821 {
822 reset(other.release());
823 return *this;
824 }
825};
826
845struct ConditionBase : Resource<SDL_Condition*>
846{
847 using Resource::Resource;
848
863 : Resource(SDL_CreateCondition())
864 {
865 }
866
879 void Signal() { SDL_SignalCondition(get()); }
880
893 void Broadcast() { SDL_BroadcastCondition(get()); }
894
920 void Wait(MutexBase& mutex) { SDL_WaitCondition(get(), mutex.get()); }
921
949 bool WaitTimeout(MutexBase& mutex, std::chrono::milliseconds timeout)
950 {
951 return SDL_WaitConditionTimeout(get(), mutex.get(), timeout.count());
952 }
953};
954
964{
966
970 constexpr ConditionRef(const ConditionRef& other)
971 : ConditionBase(other.get())
972 {
973 }
974
978 constexpr ConditionRef(ConditionRef&& other)
979 : ConditionBase(other.release())
980 {
981 }
982
986 constexpr ~ConditionRef() = default;
987
992 {
993 release(other.release());
994 return *this;
995 }
996
1004 void reset(SDL_Condition* newResource = {})
1005 {
1006 SDL_DestroyCondition(release(newResource));
1007 }
1008};
1009
1019{
1021
1025 constexpr explicit Condition(SDL_Condition* resource = {})
1026 : ConditionRef(resource)
1027 {
1028 }
1029
1030 constexpr Condition(const Condition& other) = delete;
1031
1035 constexpr Condition(Condition&& other) = default;
1036
1041
1046 {
1047 reset(other.release());
1048 return *this;
1049 }
1050};
1051
1057using InitStatus = SDL_InitStatus;
1058
1060 SDL_INIT_STATUS_UNINITIALIZED;
1061
1063 SDL_INIT_STATUS_INITIALIZING;
1064
1066 SDL_INIT_STATUS_INITIALIZED;
1067
1069 SDL_INIT_STATUS_UNINITIALIZING;
1070
1129struct InitState : SDL_InitState
1130{
1134 constexpr InitState()
1135 : SDL_InitState{0}
1136 {
1137 }
1138
1159 bool ShouldInit() { return SDL_ShouldInit(this); }
1160
1179 bool ShouldQuit() { return SDL_ShouldQuit(this); }
1180
1197 void SetInitialized(bool initialized)
1198 {
1199 SDL_SetInitialized(this, initialized);
1200 }
1201};
1202
1204
1205} // namespace SDL
1206
1207#endif /* SDL3PP_MUTEX_H_ */
A SDL managed resource.
Definition SDL3pp_resource.h:17
constexpr SDL_Mutex * release(SDL_Mutex * newResource={})
Return contained resource and empties or replace value.
Definition SDL3pp_resource.h:60
constexpr Resource(T resource={})
Constructs the underlying resource.
Definition SDL3pp_resource.h:22
constexpr SDL_Mutex * get() const
Return contained resource;.
Definition SDL3pp_resource.h:57
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:206
constexpr InitStatus INIT_STATUS_UNINITIALIZING
INIT_STATUS_UNINITIALIZING.
Definition SDL3pp_mutex.h:1068
constexpr InitStatus INIT_STATUS_INITIALIZING
INIT_STATUS_INITIALIZING.
Definition SDL3pp_mutex.h:1062
constexpr InitStatus INIT_STATUS_UNINITIALIZED
INIT_STATUS_UNINITIALIZED.
Definition SDL3pp_mutex.h:1059
SDL_InitStatus InitStatus
The current status of an InitState structure.
Definition SDL3pp_mutex.h:1057
constexpr InitStatus INIT_STATUS_INITIALIZED
INIT_STATUS_INITIALIZED.
Definition SDL3pp_mutex.h:1065
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:846
bool WaitTimeout(MutexBase &mutex, std::chrono::milliseconds timeout)
Wait until a condition variable is signaled or a certain time has passed.
Definition SDL3pp_mutex.h:949
void Signal()
Restart one of the threads that are waiting on the condition variable.
Definition SDL3pp_mutex.h:879
ConditionBase()
Create a condition variable.
Definition SDL3pp_mutex.h:862
void Broadcast()
Restart all threads that are waiting on the condition variable.
Definition SDL3pp_mutex.h:893
void Wait(MutexBase &mutex)
Wait until a condition variable is signaled.
Definition SDL3pp_mutex.h:920
Handle to a non owned condition.
Definition SDL3pp_mutex.h:964
void reset(SDL_Condition *newResource={})
Destroy a condition variable.
Definition SDL3pp_mutex.h:1004
constexpr ConditionRef(ConditionRef &&other)
Move constructor.
Definition SDL3pp_mutex.h:978
constexpr ConditionRef(const ConditionRef &other)
Copy constructor.
Definition SDL3pp_mutex.h:970
ConditionRef & operator=(ConditionRef other)
Assignment operator.
Definition SDL3pp_mutex.h:991
constexpr ~ConditionRef()=default
Default constructor.
Handle to an owned condition.
Definition SDL3pp_mutex.h:1019
constexpr Condition(SDL_Condition *resource={})
Constructs from the underlying resource.
Definition SDL3pp_mutex.h:1025
Condition & operator=(Condition other)
Assignment operator.
Definition SDL3pp_mutex.h:1045
constexpr Condition(Condition &&other)=default
Move constructor.
~Condition()
Frees up resource when object goes out of scope.
Definition SDL3pp_mutex.h:1040
A structure used for thread-safe initialization and shutdown.
Definition SDL3pp_mutex.h:1130
bool ShouldQuit()
Return whether cleanup should be done.
Definition SDL3pp_mutex.h:1179
bool ShouldInit()
Return whether initialization should be done.
Definition SDL3pp_mutex.h:1159
constexpr InitState()
Constructor.
Definition SDL3pp_mutex.h:1134
void SetInitialized(bool initialized)
Finish an initialization state transition.
Definition SDL3pp_mutex.h:1197
A means to serialize access to a resource between threads.
Definition SDL3pp_mutex.h:85
void Unlock()
Unlock the mutex.
Definition SDL3pp_mutex.h:172
void TryLock()
Try to lock a mutex without blocking.
Definition SDL3pp_mutex.h:154
void Lock()
Lock the mutex.
Definition SDL3pp_mutex.h:134
MutexBase()
Create a new mutex.
Definition SDL3pp_mutex.h:108
Handle to a non owned mutex.
Definition SDL3pp_mutex.h:184
constexpr ~MutexRef()=default
Default constructor.
void reset(SDL_Mutex *newResource={})
Destroy a mutex created with MutexBase.MutexBase().
Definition SDL3pp_mutex.h:231
constexpr MutexRef(const MutexRef &other)
Copy constructor.
Definition SDL3pp_mutex.h:190
constexpr MutexRef(MutexRef &&other)
Move constructor.
Definition SDL3pp_mutex.h:198
MutexRef & operator=(MutexRef other)
Assignment operator.
Definition SDL3pp_mutex.h:211
Handle to an owned mutex.
Definition SDL3pp_mutex.h:246
constexpr Mutex(SDL_Mutex *resource={})
Constructs from the underlying resource.
Definition SDL3pp_mutex.h:252
~Mutex()
Frees up resource when object goes out of scope.
Definition SDL3pp_mutex.h:267
Mutex & operator=(Mutex other)
Assignment operator.
Definition SDL3pp_mutex.h:272
constexpr Mutex(Mutex &&other)=default
Move constructor.
A mutex that allows read-only threads to run in parallel.
Definition SDL3pp_mutex.h:303
void TryLockForReading()
Try to lock a read/write lock for reading without blocking.
Definition SDL3pp_mutex.h:438
void LockForWriting()
Lock the read/write lock for write operations.
Definition SDL3pp_mutex.h:414
void TryLockForWriting()
Try to lock a read/write lock for writing without blocking.
Definition SDL3pp_mutex.h:467
RWLockBase()
Create a new read/write lock.
Definition SDL3pp_mutex.h:345
void LockForReading()
Lock the read/write lock for read only operations.
Definition SDL3pp_mutex.h:384
void Unlock()
Unlock the read/write lock.
Definition SDL3pp_mutex.h:491
Handle to a non owned rWLock.
Definition SDL3pp_mutex.h:503
constexpr ~RWLockRef()=default
Default constructor.
RWLockRef & operator=(RWLockRef other)
Assignment operator.
Definition SDL3pp_mutex.h:530
constexpr RWLockRef(const RWLockRef &other)
Copy constructor.
Definition SDL3pp_mutex.h:509
constexpr RWLockRef(RWLockRef &&other)
Move constructor.
Definition SDL3pp_mutex.h:517
void reset(SDL_RWLock *newResource={})
Destroy a read/write lock created with RWLockBase.RWLockBase().
Definition SDL3pp_mutex.h:549
Handle to an owned rWLock.
Definition SDL3pp_mutex.h:564
constexpr RWLock(RWLock &&other)=default
Move constructor.
~RWLock()
Frees up resource when object goes out of scope.
Definition SDL3pp_mutex.h:585
constexpr RWLock(SDL_RWLock *resource={})
Constructs from the underlying resource.
Definition SDL3pp_mutex.h:570
RWLock & operator=(RWLock other)
Assignment operator.
Definition SDL3pp_mutex.h:590
A means to manage access to a resource, by count, between threads.
Definition SDL3pp_mutex.h:617
Uint32 GetValue() const
Get the current value of a semaphore.
Definition SDL3pp_mutex.h:724
bool TryWait()
See if a semaphore has a positive value and decrement it if it does.
Definition SDL3pp_mutex.h:681
void Wait()
Wait until a semaphore has a positive value and then decrements it.
Definition SDL3pp_mutex.h:663
void Signal()
Atomically increment a semaphore's value and wake waiting threads.
Definition SDL3pp_mutex.h:715
bool WaitTimeout(std::chrono::milliseconds timeout)
Wait until a semaphore has a positive value and then decrements it.
Definition SDL3pp_mutex.h:700
SemaphoreBase(Uint32 initial_value)
Create a semaphore.
Definition SDL3pp_mutex.h:641
Handle to a non owned semaphore.
Definition SDL3pp_mutex.h:736
SemaphoreRef & operator=(SemaphoreRef other)
Assignment operator.
Definition SDL3pp_mutex.h:763
constexpr SemaphoreRef(SemaphoreRef &&other)
Move constructor.
Definition SDL3pp_mutex.h:750
void reset(SDL_Semaphore *newResource={})
Destroy a semaphore.
Definition SDL3pp_mutex.h:779
constexpr SemaphoreRef(const SemaphoreRef &other)
Copy constructor.
Definition SDL3pp_mutex.h:742
constexpr ~SemaphoreRef()=default
Default constructor.
Handle to an owned semaphore.
Definition SDL3pp_mutex.h:794
constexpr Semaphore(SDL_Semaphore *resource={})
Constructs from the underlying resource.
Definition SDL3pp_mutex.h:800
Semaphore & operator=(Semaphore other)
Assignment operator.
Definition SDL3pp_mutex.h:820
~Semaphore()
Frees up resource when object goes out of scope.
Definition SDL3pp_mutex.h:815
constexpr Semaphore(Semaphore &&other)=default
Move constructor.