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
49using ThreadID = SDL_ThreadID;
50
63
73using ThreadFunction = SDL_ThreadFunction;
74
83using ThreadCB = std::function<int()>;
84
96using TLSDestructorCallback = SDL_TLSDestructorCallback;
97
98// Forward decl
99struct ThreadBase;
100
101// Forward decl
102struct ThreadRef;
103
104// Forward decl
105struct Thread;
106
118using ThreadPriority = SDL_ThreadPriority;
119
120constexpr ThreadPriority THREAD_PRIORITY_LOW = SDL_THREAD_PRIORITY_LOW;
121
123 SDL_THREAD_PRIORITY_NORMAL;
124
126 SDL_THREAD_PRIORITY_HIGH;
127
129 SDL_THREAD_PRIORITY_TIME_CRITICAL;
130
140using ThreadState = SDL_ThreadState;
141
143 SDL_THREAD_UNKNOWN;
144
146 SDL_THREAD_ALIVE;
147
149 SDL_THREAD_DETACHED;
150
154constexpr ThreadState THREAD_COMPLETE = SDL_THREAD_COMPLETE;
155
171struct ThreadBase : Resource<SDL_Thread*>
172{
173 using Resource::Resource;
174
189 : ThreadBase(
190 [](void* handler) {
192 },
193 std::move(name),
194 CallbackWrapper<ThreadCB>::Wrap(std::move(fn)))
195 {
196 }
197
223 : Resource(CheckError(SDL_CreateThread(fn, name, data)))
224 {
225 }
226
291 : Resource(CheckError(SDL_CreateThreadWithProperties(props.get())))
292 {
293 }
294
303 const char* GetName() const { return SDL_GetThreadName(get()); }
304
319 ThreadID GetID() const { return SDL_GetThreadID(get()); }
320
333 static void SetCurrentPriority(ThreadPriority priority)
334 {
335 CheckError(SDL_SetCurrentThreadPriority(priority));
336 }
337
370 void Wait(int* status) { SDL_WaitThread(get(), status); }
371
382 ThreadState GetState() const { return SDL_GetThreadState(get()); }
383};
384
394{
396
400 constexpr ThreadRef(const ThreadRef& other)
401 : ThreadBase(other.get())
402 {
403 }
404
408 constexpr ThreadRef(ThreadRef&& other)
409 : ThreadBase(other.release())
410 {
411 }
412
416 constexpr ~ThreadRef() = default;
417
422 {
423 release(other.release());
424 return *this;
425 }
426
458 void reset(SDL_Thread* newResource = {})
459 {
460 SDL_DetachThread(release(newResource));
461 }
462
494 void Detach() { reset(); }
495};
496
506{
508
512 constexpr explicit Thread(SDL_Thread* resource = {})
513 : ThreadRef(resource)
514 {
515 }
516
517 constexpr Thread(const Thread& other) = delete;
518
522 constexpr Thread(Thread&& other) = default;
523
527 ~Thread() { reset(); }
528
533 {
534 reset(other.release());
535 return *this;
536 }
537};
538
539namespace prop::thread {
540
541constexpr auto CREATE_ENTRY_FUNCTION_POINTER =
542 SDL_PROP_THREAD_CREATE_ENTRY_FUNCTION_POINTER;
543
544constexpr auto CREATE_NAME_STRING = SDL_PROP_THREAD_CREATE_NAME_STRING;
545
546constexpr auto CREATE_USERDATA_POINTER =
547 SDL_PROP_THREAD_CREATE_USERDATA_POINTER;
548
549constexpr auto CREATE_STACKSIZE_NUMBER =
550 SDL_PROP_THREAD_CREATE_STACKSIZE_NUMBER;
551
552} // namespace prop::thread
553
570inline ThreadID GetCurrentThreadID() { return SDL_GetCurrentThreadID(); }
571
585inline void* GetTLS(TLSID* id) { return SDL_GetTLS(*id); }
586
612inline void SetTLS(TLSID* id,
613 const void* value,
614 TLSDestructorCallback destructor)
615{
616 CheckError(SDL_SetTLS(*id, value, destructor));
617}
618
630inline void CleanupTLS() { SDL_CleanupTLS(); }
631
633} // namespace SDL
634
635#endif /* SDL3PP_THREAD_H_ */
A type representing an atomic integer value.
Definition SDL3pp_atomic.h:210
A SDL managed resource.
Definition SDL3pp_resource.h:17
constexpr SDL_Thread * release(SDL_Thread * 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_Thread * get() const
Return contained resource;.
Definition SDL3pp_resource.h:57
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:206
constexpr ThreadPriority THREAD_PRIORITY_HIGH
HIGH.
Definition SDL3pp_thread.h:125
void CleanupTLS()
Cleanup all TLS data for this thread.
Definition SDL3pp_thread.h:630
constexpr ThreadPriority THREAD_PRIORITY_TIME_CRITICAL
TIME_CRITICAL.
Definition SDL3pp_thread.h:128
constexpr ThreadState THREAD_DETACHED
The thread is detached and can't be waited on.
Definition SDL3pp_thread.h:148
SDL_TLSDestructorCallback TLSDestructorCallback
The callback used to cleanup data passed to SetTLS.
Definition SDL3pp_thread.h:96
constexpr ThreadState THREAD_ALIVE
The thread is currently running.
Definition SDL3pp_thread.h:145
constexpr ThreadPriority THREAD_PRIORITY_LOW
LOW.
Definition SDL3pp_thread.h:120
SDL_ThreadState ThreadState
The SDL thread state.
Definition SDL3pp_thread.h:140
std::function< int()> ThreadCB
The function passed to ThreadBase.ThreadBase() as the new thread's entry point.
Definition SDL3pp_thread.h:83
ThreadID GetCurrentThreadID()
Get the thread identifier for the current thread.
Definition SDL3pp_thread.h:570
constexpr ThreadState THREAD_UNKNOWN
The thread is not valid.
Definition SDL3pp_thread.h:142
SDL_ThreadPriority ThreadPriority
The SDL thread priority.
Definition SDL3pp_thread.h:118
SDL_ThreadID ThreadID
A unique numeric ID that identifies a thread.
Definition SDL3pp_thread.h:49
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:612
constexpr ThreadPriority THREAD_PRIORITY_NORMAL
NORMAL.
Definition SDL3pp_thread.h:122
constexpr ThreadState THREAD_COMPLETE
The thread has finished and should be cleaned up with ThreadBase.Wait()
Definition SDL3pp_thread.h:154
SDL_ThreadFunction ThreadFunction
The function passed to ThreadBase.ThreadBase() as the new thread's entry point.
Definition SDL3pp_thread.h:73
void * GetTLS(TLSID *id)
Get the current thread's value associated with a thread local storage ID.
Definition SDL3pp_thread.h:585
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Definition SDL3pp_callbackWrapper.h:66
Wrap properties id.
Definition SDL3pp_properties.h:203
The SDL thread object.
Definition SDL3pp_thread.h:172
void Wait(int *status)
Wait for a thread to finish.
Definition SDL3pp_thread.h:370
ThreadState GetState() const
Get the current state of a thread.
Definition SDL3pp_thread.h:382
const char * GetName() const
Get the thread name as it was specified in ThreadBase.ThreadBase().
Definition SDL3pp_thread.h:303
ThreadBase(PropertiesBase &props)
Create a new thread with with the specified properties.
Definition SDL3pp_thread.h:290
ThreadBase(ThreadCB fn, StringParam name)
Create a new thread with a default stack size.
Definition SDL3pp_thread.h:188
ThreadID GetID() const
Get the thread identifier for the specified thread.
Definition SDL3pp_thread.h:319
static void SetCurrentPriority(ThreadPriority priority)
Set the priority for the current thread.
Definition SDL3pp_thread.h:333
ThreadBase(ThreadFunction fn, StringParam name, void *data)
Create a new thread with a default stack size.
Definition SDL3pp_thread.h:222
Handle to a non owned thread.
Definition SDL3pp_thread.h:394
constexpr ThreadRef(ThreadRef &&other)
Move constructor.
Definition SDL3pp_thread.h:408
constexpr ThreadRef(const ThreadRef &other)
Copy constructor.
Definition SDL3pp_thread.h:400
void reset(SDL_Thread *newResource={})
Let a thread clean up on exit without intervention.
Definition SDL3pp_thread.h:458
constexpr ~ThreadRef()=default
Default constructor.
void Detach()
Let a thread clean up on exit without intervention.
Definition SDL3pp_thread.h:494
ThreadRef & operator=(ThreadRef other)
Assignment operator.
Definition SDL3pp_thread.h:421
Handle to an owned thread.
Definition SDL3pp_thread.h:506
constexpr Thread(SDL_Thread *resource={})
Constructs from the underlying resource.
Definition SDL3pp_thread.h:512
Thread & operator=(Thread other)
Assignment operator.
Definition SDL3pp_thread.h:532
~Thread()
Frees up resource when object goes out of scope.
Definition SDL3pp_thread.h:527
constexpr Thread(Thread &&other)=default
Move constructor.