SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_thread.h
1#ifndef SDL3PP_THREAD_H_
2#define SDL3PP_THREAD_H_
3
4#include <SDL3/SDL_thread.h>
5#include "SDL3pp_atomic.h"
6#include "SDL3pp_properties.h"
7#include "SDL3pp_stdinc.h"
8
9namespace SDL {
10
31
32// Forward decl
33struct ThreadBase;
34
35// Forward decl
36struct Thread;
37
39using ThreadRaw = SDL_Thread*;
40
47
59using ThreadPriority = SDL_ThreadPriority;
60
61constexpr ThreadPriority THREAD_PRIORITY_LOW = SDL_THREAD_PRIORITY_LOW;
62
64 SDL_THREAD_PRIORITY_NORMAL;
65
67 SDL_THREAD_PRIORITY_HIGH;
68
70 SDL_THREAD_PRIORITY_TIME_CRITICAL;
71
81using ThreadState = SDL_ThreadState;
82
84 SDL_THREAD_UNKNOWN;
85
87 SDL_THREAD_ALIVE;
88
90 SDL_THREAD_DETACHED;
91
93constexpr ThreadState THREAD_COMPLETE = SDL_THREAD_COMPLETE;
94
107using ThreadID = SDL_ThreadID;
108
117using ThreadFunction = int(SDLCALL*)(void* data);
118
128using ThreadCB = std::function<int()>;
129
141using TLSDestructorCallback = void(SDLCALL*)(void* value);
142
148struct ThreadBase : ResourceBaseT<ThreadRaw>
149{
151
185 void Detach();
186
197 const char* GetName() const;
198
215 ThreadID GetID() const;
216
231 static void SetCurrentPriority(ThreadPriority priority);
232
267 void Wait(int* status);
268
281 ThreadState GetState() const;
282};
283
297{
298 using ThreadBase::ThreadBase;
299
307 constexpr explicit Thread(ThreadRaw resource) noexcept
308 : ThreadBase(resource)
309 {
310 }
311
313 constexpr Thread(Thread&& other) noexcept
314 : Thread(other.release())
315 {
316 }
317
350 Thread(ThreadFunction fn, StringParam name, void* data);
351
383 Thread(ThreadCB fn, StringParam name);
384
449 Thread(PropertiesRef props);
450
452 ~Thread() { SDL_DetachThread(get()); }
453
455 constexpr Thread& operator=(Thread&& other) noexcept
456 {
457 swap(*this, other);
458 return *this;
459 }
460};
461
474
507inline Thread CreateThread(ThreadFunction fn, StringParam name, void* data)
508{
509 return Thread(fn, name, data);
510}
511
544{
545 return Thread(std::move(fn), name);
546}
547
548inline Thread::Thread(ThreadFunction fn, StringParam name, void* data)
549 : Thread(CheckError(SDL_CreateThread(fn, name, data)))
550{
551}
552
554 : Thread(&CallbackWrapper<ThreadCB>::CallOnce,
555 name,
556 CallbackWrapper<ThreadCB>::Wrap(std::move(fn)))
557{
558}
559
561 : Thread(CheckError(SDL_CreateThreadWithProperties(props)))
562{
563}
564
630{
631 return Thread(props);
632}
633
644
646 SDL_PROP_THREAD_CREATE_ENTRY_FUNCTION_POINTER;
647
648constexpr auto NAME_STRING =
649 SDL_PROP_THREAD_CREATE_NAME_STRING;
650
651constexpr auto USERDATA_POINTER =
652 SDL_PROP_THREAD_CREATE_USERDATA_POINTER;
653
654constexpr auto STACKSIZE_NUMBER =
655 SDL_PROP_THREAD_CREATE_STACKSIZE_NUMBER;
656
657} // namespace prop::Thread::Create
658
670inline const char* GetThreadName(ThreadRef thread)
671{
672 return SDL_GetThreadName(thread);
673}
674
675inline const char* ThreadBase::GetName() const
676{
677 return SDL::GetThreadName(get());
678}
679
698inline ThreadID GetCurrentThreadID() { return SDL_GetCurrentThreadID(); }
699
718{
719 return SDL_GetThreadID(thread);
720}
721
722inline ThreadID ThreadBase::GetID() const { return SDL::GetThreadID(get()); }
723
739{
740 CheckError(SDL_SetCurrentThreadPriority(priority));
741}
742
747
783inline void WaitThread(ThreadRef thread, int* status)
784{
785 SDL_WaitThread(thread, status);
786}
787
788inline void ThreadBase::Wait(int* status) { SDL::WaitThread(get(), status); }
789
804{
805 return SDL_GetThreadState(thread);
806}
807
809{
810 return SDL::GetThreadState(get());
811}
812
848inline void DetachThread(ThreadRaw thread) { SDL_DetachThread(thread); }
849
851
865inline void* GetTLS(TLSID* id) { return SDL_GetTLS(id); }
866
892inline void SetTLS(TLSID* id,
893 const void* value,
894 TLSDestructorCallback destructor)
895{
896 CheckError(SDL_SetTLS(id, value, destructor));
897}
898
910inline void CleanupTLS() { SDL_CleanupTLS(); }
911
913
914} // namespace SDL
915
916#endif /* SDL3PP_THREAD_H_ */
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:57
friend constexpr void swap(ResourceBaseT &lhs, ResourceBaseT &rhs) noexcept
Definition SDL3pp_resource.h:65
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:54
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:58
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
ResourceRefT< PropertiesBase > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:55
constexpr ThreadPriority THREAD_PRIORITY_HIGH
HIGH.
Definition SDL3pp_thread.h:66
void Wait(int *status)
Wait for a thread to finish.
Definition SDL3pp_thread.h:788
void CleanupTLS()
Cleanup all TLS data for this thread.
Definition SDL3pp_thread.h:910
SDL_Thread * ThreadRaw
Alias to raw representation for Thread.
Definition SDL3pp_thread.h:39
Thread CreateThreadWithProperties(PropertiesRef props)
Create a new thread with with the specified properties.
Definition SDL3pp_thread.h:629
ThreadState GetState() const
Get the current state of a thread.
Definition SDL3pp_thread.h:808
constexpr ThreadPriority THREAD_PRIORITY_TIME_CRITICAL
TIME_CRITICAL.
Definition SDL3pp_thread.h:69
constexpr ThreadState THREAD_DETACHED
The thread is detached and can't be waited on.
Definition SDL3pp_thread.h:89
const char * GetThreadName(ThreadRef thread)
Get the thread name as it was specified in CreateThread().
Definition SDL3pp_thread.h:670
void DetachThread(ThreadRaw thread)
Let a thread clean up on exit without intervention.
Definition SDL3pp_thread.h:848
AtomicInt TLSID
Thread local storage ID.
Definition SDL3pp_thread.h:473
constexpr ThreadState THREAD_ALIVE
The thread is currently running.
Definition SDL3pp_thread.h:86
void WaitThread(ThreadRef thread, int *status)
Wait for a thread to finish.
Definition SDL3pp_thread.h:783
ThreadState GetThreadState(ThreadRef thread)
Get the current state of a thread.
Definition SDL3pp_thread.h:803
constexpr ThreadPriority THREAD_PRIORITY_LOW
LOW.
Definition SDL3pp_thread.h:61
const char * GetName() const
Get the thread name as it was specified in CreateThread().
Definition SDL3pp_thread.h:675
SDL_ThreadID ThreadID
A unique numeric ID that identifies a thread.
Definition SDL3pp_thread.h:107
SDL_ThreadPriority ThreadPriority
The SDL thread priority.
Definition SDL3pp_thread.h:59
void SetCurrentThreadPriority(ThreadPriority priority)
Set the priority for the current thread.
Definition SDL3pp_thread.h:738
ResourceRefT< ThreadBase > ThreadRef
Reference for Thread.
Definition SDL3pp_thread.h:46
ThreadID GetCurrentThreadID()
Get the thread identifier for the current thread.
Definition SDL3pp_thread.h:698
constexpr ThreadState THREAD_UNKNOWN
The thread is not valid.
Definition SDL3pp_thread.h:83
static void SetCurrentPriority(ThreadPriority priority)
Set the priority for the current thread.
Definition SDL3pp_thread.h:743
SDL_ThreadState ThreadState
The SDL thread state.
Definition SDL3pp_thread.h:81
Thread CreateThread(ThreadFunction fn, StringParam name, void *data)
Create a new thread with a default stack size.
Definition SDL3pp_thread.h:507
void SetTLS(TLSID *id, const void *value, TLSDestructorCallback destructor)
Set the current thread's value associated with a thread local storage ID.
Definition SDL3pp_thread.h:892
ThreadID GetThreadID(ThreadRef thread)
Get the thread identifier for the specified thread.
Definition SDL3pp_thread.h:717
constexpr ThreadPriority THREAD_PRIORITY_NORMAL
NORMAL.
Definition SDL3pp_thread.h:63
constexpr ThreadState THREAD_COMPLETE
The thread has finished and should be cleaned up with WaitThread().
Definition SDL3pp_thread.h:93
std::function< int()> ThreadCB
The function passed to CreateThread() as the new thread's entry point.
Definition SDL3pp_thread.h:128
ThreadID GetID() const
Get the thread identifier for the specified thread.
Definition SDL3pp_thread.h:722
void * GetTLS(TLSID *id)
Get the current thread's value associated with a thread local storage ID.
Definition SDL3pp_thread.h:865
int(SDLCALL *)(void *data) ThreadFunction
The function passed to CreateThread() as the new thread's entry point.
Definition SDL3pp_thread.h:117
void Detach()
Let a thread clean up on exit without intervention.
Definition SDL3pp_thread.h:850
void(SDLCALL *)(void *value) TLSDestructorCallback
The callback used to cleanup data passed to SetTLS.
Definition SDL3pp_thread.h:141
Properties for CreateThreadWithProperties.
Definition SDL3pp_thread.h:643
constexpr auto USERDATA_POINTER
Pointer to userdata.
Definition SDL3pp_thread.h:651
constexpr auto ENTRY_FUNCTION_POINTER
Pointer to entry function.
Definition SDL3pp_thread.h:645
constexpr auto NAME_STRING
String for name.
Definition SDL3pp_thread.h:648
constexpr auto STACKSIZE_NUMBER
Number for stacksize.
Definition SDL3pp_thread.h:654
Main include header for the SDL3pp library.
A type representing an atomic integer value.
Definition SDL3pp_atomic.h:218
Definition SDL3pp_callbackWrapper.h:20
A non-owning reference wrapper for a given resource.
Definition SDL3pp_resource.h:93
Base class to Thread.
Definition SDL3pp_thread.h:149
constexpr ResourceBaseT()=default
Default constructor, creates null/invalid resource.
The SDL thread object.
Definition SDL3pp_thread.h:297
constexpr Thread & operator=(Thread &&other) noexcept
Assignment operator.
Definition SDL3pp_thread.h:455
constexpr Thread(ThreadRaw resource) noexcept
Constructs from raw Thread.
Definition SDL3pp_thread.h:307
~Thread()
Destructor.
Definition SDL3pp_thread.h:452
constexpr Thread(Thread &&other) noexcept
Move constructor.
Definition SDL3pp_thread.h:313