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 Thread;
34
36using ThreadRaw = SDL_Thread*;
37
44
56using ThreadPriority = SDL_ThreadPriority;
57
58constexpr ThreadPriority THREAD_PRIORITY_LOW = SDL_THREAD_PRIORITY_LOW;
59
61 SDL_THREAD_PRIORITY_NORMAL;
62
64 SDL_THREAD_PRIORITY_HIGH;
65
67 SDL_THREAD_PRIORITY_TIME_CRITICAL;
68
78using ThreadState = SDL_ThreadState;
79
81 SDL_THREAD_UNKNOWN;
82
84 SDL_THREAD_ALIVE;
85
87 SDL_THREAD_DETACHED;
88
90constexpr ThreadState THREAD_COMPLETE = SDL_THREAD_COMPLETE;
91
104using ThreadID = SDL_ThreadID;
105
114using ThreadFunction = int(SDLCALL*)(void* data);
115
125using ThreadCB = std::function<int()>;
126
138using TLSDestructorCallback = void(SDLCALL*)(void* value);
139
152struct Thread : ResourceBase<ThreadRaw>
153{
155
163 constexpr explicit Thread(ThreadRaw resource) noexcept
164 : ResourceBase(resource)
165 {
166 }
167
169 constexpr Thread(const Thread& other) = delete;
170
172 constexpr Thread(Thread&& other) noexcept
173 : Thread(other.release())
174 {
175 }
176
177 constexpr Thread(const ThreadRef& other) = delete;
178
179 constexpr Thread(ThreadRef&& other) = delete;
180
213 Thread(ThreadFunction fn, StringParam name, void* data);
214
246 Thread(ThreadCB fn, StringParam name);
247
312 Thread(PropertiesRef props);
313
315 ~Thread() { SDL_DetachThread(get()); }
316
318 constexpr Thread& operator=(Thread&& other) noexcept
319 {
320 swap(*this, other);
321 return *this;
322 }
323
325 Thread& operator=(const Thread& other) = delete;
326
360 void Detach();
361
372 const char* GetName() const;
373
390 ThreadID GetID() const;
391
406 static void SetCurrentPriority(ThreadPriority priority);
407
442 void Wait(int* status);
443
456 ThreadState GetState() const;
457};
458
471
504inline Thread CreateThread(ThreadFunction fn, StringParam name, void* data)
505{
506 return Thread(fn, std::move(name), data);
507}
508
541{
542 return Thread(std::move(fn), std::move(name));
543}
544
545inline Thread::Thread(ThreadFunction fn, StringParam name, void* data)
546 : Thread(CheckError(SDL_CreateThread(fn, name, data)))
547{
548}
549
551 : Thread(&CallbackWrapper<ThreadCB>::CallOnce,
552 std::move(name),
553 CallbackWrapper<ThreadCB>::Wrap(std::move(fn)))
554{
555}
556
558 : Thread(CheckError(SDL_CreateThreadWithProperties(props)))
559{
560}
561
627{
628 return Thread(props);
629}
630
640namespace prop::thread {
641
642constexpr auto CREATE_ENTRY_FUNCTION_POINTER =
643 SDL_PROP_THREAD_CREATE_ENTRY_FUNCTION_POINTER;
644
645constexpr auto CREATE_NAME_STRING = SDL_PROP_THREAD_CREATE_NAME_STRING;
646
647constexpr auto CREATE_USERDATA_POINTER =
648 SDL_PROP_THREAD_CREATE_USERDATA_POINTER;
649
650constexpr auto CREATE_STACKSIZE_NUMBER =
651 SDL_PROP_THREAD_CREATE_STACKSIZE_NUMBER;
652
653} // namespace prop::thread
654
666inline const char* GetThreadName(ThreadRef thread)
667{
668 return SDL_GetThreadName(thread);
669}
670
671inline const char* Thread::GetName() const { return SDL::GetThreadName(get()); }
672
691inline ThreadID GetCurrentThreadID() { return SDL_GetCurrentThreadID(); }
692
711{
712 return SDL_GetThreadID(thread);
713}
714
715inline ThreadID Thread::GetID() const { return SDL::GetThreadID(get()); }
716
732{
733 CheckError(SDL_SetCurrentThreadPriority(priority));
734}
735
737{
739}
740
776inline void WaitThread(ThreadRef thread, int* status)
777{
778 SDL_WaitThread(thread, status);
779}
780
781inline void Thread::Wait(int* status) { SDL::WaitThread(get(), status); }
782
797{
798 return SDL_GetThreadState(thread);
799}
800
802{
803 return SDL::GetThreadState(get());
804}
805
841inline void DetachThread(ThreadRaw thread) { SDL_DetachThread(thread); }
842
844
858inline void* GetTLS(TLSID* id) { return SDL_GetTLS(id); }
859
885inline void SetTLS(TLSID* id,
886 const void* value,
887 TLSDestructorCallback destructor)
888{
889 CheckError(SDL_SetTLS(id, value, destructor));
890}
891
903inline void CleanupTLS() { SDL_CleanupTLS(); }
904
906
907} // namespace SDL
908
909#endif /* SDL3PP_THREAD_H_ */
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:53
constexpr RawPointer release() noexcept
Definition SDL3pp_resource.h:56
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
ResourceRef< Properties > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:54
constexpr ThreadPriority THREAD_PRIORITY_HIGH
HIGH.
Definition SDL3pp_thread.h:63
void CleanupTLS()
Cleanup all TLS data for this thread.
Definition SDL3pp_thread.h:903
SDL_Thread * ThreadRaw
Alias to raw representation for Thread.
Definition SDL3pp_thread.h:36
Thread CreateThreadWithProperties(PropertiesRef props)
Create a new thread with with the specified properties.
Definition SDL3pp_thread.h:626
constexpr ThreadPriority THREAD_PRIORITY_TIME_CRITICAL
TIME_CRITICAL.
Definition SDL3pp_thread.h:66
constexpr ThreadState THREAD_DETACHED
The thread is detached and can't be waited on.
Definition SDL3pp_thread.h:86
ResourceRef< Thread > ThreadRef
Reference for Thread.
Definition SDL3pp_thread.h:43
const char * GetName() const
Get the thread name as it was specified in CreateThread().
Definition SDL3pp_thread.h:671
const char * GetThreadName(ThreadRef thread)
Get the thread name as it was specified in CreateThread().
Definition SDL3pp_thread.h:666
void DetachThread(ThreadRaw thread)
Let a thread clean up on exit without intervention.
Definition SDL3pp_thread.h:841
AtomicInt TLSID
Thread local storage ID.
Definition SDL3pp_thread.h:470
constexpr ThreadState THREAD_ALIVE
The thread is currently running.
Definition SDL3pp_thread.h:83
void WaitThread(ThreadRef thread, int *status)
Wait for a thread to finish.
Definition SDL3pp_thread.h:776
ThreadState GetThreadState(ThreadRef thread)
Get the current state of a thread.
Definition SDL3pp_thread.h:796
constexpr ThreadPriority THREAD_PRIORITY_LOW
LOW.
Definition SDL3pp_thread.h:58
SDL_ThreadID ThreadID
A unique numeric ID that identifies a thread.
Definition SDL3pp_thread.h:104
SDL_ThreadPriority ThreadPriority
The SDL thread priority.
Definition SDL3pp_thread.h:56
void SetCurrentThreadPriority(ThreadPriority priority)
Set the priority for the current thread.
Definition SDL3pp_thread.h:731
ThreadID GetCurrentThreadID()
Get the thread identifier for the current thread.
Definition SDL3pp_thread.h:691
constexpr ThreadState THREAD_UNKNOWN
The thread is not valid.
Definition SDL3pp_thread.h:80
void Detach()
Let a thread clean up on exit without intervention.
Definition SDL3pp_thread.h:843
static void SetCurrentPriority(ThreadPriority priority)
Set the priority for the current thread.
Definition SDL3pp_thread.h:736
SDL_ThreadState ThreadState
The SDL thread state.
Definition SDL3pp_thread.h:78
void Wait(int *status)
Wait for a thread to finish.
Definition SDL3pp_thread.h:781
Thread CreateThread(ThreadFunction fn, StringParam name, void *data)
Create a new thread with a default stack size.
Definition SDL3pp_thread.h:504
ThreadID GetID() const
Get the thread identifier for the specified thread.
Definition SDL3pp_thread.h:715
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:885
ThreadID GetThreadID(ThreadRef thread)
Get the thread identifier for the specified thread.
Definition SDL3pp_thread.h:710
constexpr ThreadPriority THREAD_PRIORITY_NORMAL
NORMAL.
Definition SDL3pp_thread.h:60
ThreadState GetState() const
Get the current state of a thread.
Definition SDL3pp_thread.h:801
constexpr ThreadState THREAD_COMPLETE
The thread has finished and should be cleaned up with Thread.Wait().
Definition SDL3pp_thread.h:90
std::function< int()> ThreadCB
The function passed to CreateThread() as the new thread's entry point.
Definition SDL3pp_thread.h:125
void * GetTLS(TLSID *id)
Get the current thread's value associated with a thread local storage ID.
Definition SDL3pp_thread.h:858
int(SDLCALL *)(void *data) ThreadFunction
The function passed to CreateThread() as the new thread's entry point.
Definition SDL3pp_thread.h:114
void(SDLCALL *)(void *value) TLSDestructorCallback
The callback used to cleanup data passed to SetTLS.
Definition SDL3pp_thread.h:138
Properties for CreateThreadWithProperties.
Definition SDL3pp_thread.h:640
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:156
The SDL thread object.
Definition SDL3pp_thread.h:153
constexpr ResourceBase(RawPointer resource)
Constructs from resource pointer.
Definition SDL3pp_resource.h:29
constexpr Thread & operator=(Thread &&other) noexcept
Assignment operator.
Definition SDL3pp_thread.h:318
constexpr Thread(ThreadRaw resource) noexcept
Constructs from raw Thread.
Definition SDL3pp_thread.h:163
~Thread()
Destructor.
Definition SDL3pp_thread.h:315
constexpr Thread(Thread &&other) noexcept
Move constructor.
Definition SDL3pp_thread.h:172
constexpr Thread(const Thread &other)=delete
Copy constructor.
Thread & operator=(const Thread &other)=delete
Assignment operator.