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// Forward decl
32struct Thread;
33
35using ThreadRaw = SDL_Thread*;
36
37// Forward decl
38struct ThreadRef;
39
42{
44
47 : value(value)
48 {
49 }
50
52 constexpr ThreadParam(std::nullptr_t _ = nullptr)
53 : value(nullptr)
54 {
55 }
56
58 constexpr explicit operator bool() const { return !!value; }
59
61 constexpr auto operator<=>(const ThreadParam& other) const = default;
62
64 constexpr operator ThreadRaw() const { return value; }
65};
66
78using ThreadPriority = SDL_ThreadPriority;
79
80constexpr ThreadPriority THREAD_PRIORITY_LOW = SDL_THREAD_PRIORITY_LOW;
81
83 SDL_THREAD_PRIORITY_NORMAL;
84
86 SDL_THREAD_PRIORITY_HIGH;
87
89 SDL_THREAD_PRIORITY_TIME_CRITICAL;
90
100using ThreadState = SDL_ThreadState;
101
103 SDL_THREAD_UNKNOWN;
104
106 SDL_THREAD_ALIVE;
107
109 SDL_THREAD_DETACHED;
110
112constexpr ThreadState THREAD_COMPLETE = SDL_THREAD_COMPLETE;
113
126using ThreadID = SDL_ThreadID;
127
136using ThreadFunction = int(SDLCALL*)(void* data);
137
147using ThreadCB = std::function<int()>;
148
160using TLSDestructorCallback = void(SDLCALL*)(void* value);
161
175{
176 ThreadRaw m_resource = nullptr;
177
178public:
180 constexpr Thread(std::nullptr_t = nullptr) noexcept
181 : m_resource(0)
182 {
183 }
184
192 constexpr explicit Thread(const ThreadRaw resource) noexcept
193 : m_resource(resource)
194 {
195 }
196
198 constexpr Thread(const Thread& other) = delete;
199
201 constexpr Thread(Thread&& other) noexcept
202 : Thread(other.release())
203 {
204 }
205
206 constexpr Thread(const ThreadRef& other) = delete;
207
208 constexpr Thread(ThreadRef&& other) = delete;
209
212
243 Thread(ThreadFunction fn, StringParam name, void* data)
244 : m_resource(CheckError(SDL_CreateThread(fn, name, data)))
245 {
246 }
247
311 : m_resource(CheckError(SDL_CreateThreadWithProperties(props)))
312 {
313 }
314
316 ~Thread() { SDL_DetachThread(m_resource); }
317
319 constexpr Thread& operator=(Thread&& other) noexcept
320 {
321 std::swap(m_resource, other.m_resource);
322 return *this;
323 }
324
325protected:
327 constexpr Thread& operator=(const Thread& other) noexcept = default;
328
329public:
331 constexpr ThreadRaw get() const noexcept { return m_resource; }
332
334 constexpr ThreadRaw release() noexcept
335 {
336 auto r = m_resource;
337 m_resource = nullptr;
338 return r;
339 }
340
342 constexpr auto operator<=>(const Thread& other) const noexcept = default;
343
345 constexpr explicit operator bool() const noexcept { return !!m_resource; }
346
348 constexpr operator ThreadParam() const noexcept { return {m_resource}; }
349
381 void Detach();
382
391 const char* GetName() const;
392
407 ThreadID GetID() const;
408
421 static void SetCurrentPriority(ThreadPriority priority);
422
454 void Wait(int* status);
455
466 ThreadState GetState() const;
467};
468
471{
472 using Thread::Thread;
473
481 ThreadRef(ThreadParam resource) noexcept
482 : Thread(resource.value)
483 {
484 }
485
493 ThreadRef(ThreadRaw resource) noexcept
494 : Thread(resource)
495 {
496 }
497
499 ThreadRef(const ThreadRef& other) noexcept
500 : Thread(other.get())
501 {
502 }
503
506};
507
520
551inline Thread CreateThread(ThreadFunction fn, StringParam name, void* data)
552{
553 return Thread(fn, std::move(name), data);
554}
555
619{
620 return Thread(props);
621}
622
623namespace prop::thread {
624
625constexpr auto CREATE_ENTRY_FUNCTION_POINTER =
626 SDL_PROP_THREAD_CREATE_ENTRY_FUNCTION_POINTER;
627
628constexpr auto CREATE_NAME_STRING = SDL_PROP_THREAD_CREATE_NAME_STRING;
629
630constexpr auto CREATE_USERDATA_POINTER =
631 SDL_PROP_THREAD_CREATE_USERDATA_POINTER;
632
633constexpr auto CREATE_STACKSIZE_NUMBER =
634 SDL_PROP_THREAD_CREATE_STACKSIZE_NUMBER;
635
636} // namespace prop::thread
637
647inline const char* GetThreadName(ThreadParam thread)
648{
649 return SDL_GetThreadName(thread);
650}
651
652inline const char* Thread::GetName() const
653{
654 return SDL::GetThreadName(m_resource);
655}
656
673inline ThreadID GetCurrentThreadID() { return SDL_GetCurrentThreadID(); }
674
691{
692 return SDL_GetThreadID(thread);
693}
694
695inline ThreadID Thread::GetID() const { return SDL::GetThreadID(m_resource); }
696
710{
711 CheckError(SDL_SetCurrentThreadPriority(priority));
712}
713
715{
717}
718
751inline void WaitThread(ThreadParam thread, int* status)
752{
753 SDL_WaitThread(thread, status);
754}
755
756inline void Thread::Wait(int* status) { SDL::WaitThread(m_resource, status); }
757
770{
771 return SDL_GetThreadState(thread);
772}
773
775{
776 return SDL::GetThreadState(m_resource);
777}
778
812inline void DetachThread(ThreadRaw thread) { SDL_DetachThread(thread); }
813
815
829inline void* GetTLS(TLSID* id) { return SDL_GetTLS(id); }
830
856inline void SetTLS(TLSID* id,
857 const void* value,
858 TLSDestructorCallback destructor)
859{
860 CheckError(SDL_SetTLS(id, value, destructor));
861}
862
874inline void CleanupTLS() { SDL_CleanupTLS(); }
875
877
878} // namespace SDL
879
880#endif /* SDL3PP_THREAD_H_ */
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
The SDL thread object.
Definition: SDL3pp_thread.h:175
Thread(ThreadCB fn, StringParam name)
Default ctor.
Definition: SDL3pp_thread.h:211
constexpr auto operator<=>(const Thread &other) const noexcept=default
Comparison.
constexpr Thread(const ThreadRaw resource) noexcept
Constructs from ThreadParam.
Definition: SDL3pp_thread.h:192
constexpr Thread(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_thread.h:180
constexpr Thread & operator=(Thread &&other) noexcept
Assignment operator.
Definition: SDL3pp_thread.h:319
constexpr ThreadRaw release() noexcept
Retrieves underlying ThreadRaw and clear this.
Definition: SDL3pp_thread.h:334
Thread(ThreadFunction fn, StringParam name, void *data)
Create a new thread with a default stack size.
Definition: SDL3pp_thread.h:243
~Thread()
Destructor.
Definition: SDL3pp_thread.h:316
Thread(PropertiesParam props)
Create a new thread with with the specified properties.
Definition: SDL3pp_thread.h:310
constexpr Thread(Thread &&other) noexcept
Move constructor.
Definition: SDL3pp_thread.h:201
constexpr ThreadRaw get() const noexcept
Retrieves underlying ThreadRaw.
Definition: SDL3pp_thread.h:331
constexpr Thread & operator=(const Thread &other) noexcept=default
Assignment operator.
constexpr Thread(const Thread &other)=delete
Copy constructor.
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
constexpr ThreadPriority THREAD_PRIORITY_HIGH
HIGH.
Definition: SDL3pp_thread.h:85
ThreadState GetThreadState(ThreadParam thread)
Get the current state of a thread.
Definition: SDL3pp_thread.h:769
void CleanupTLS()
Cleanup all TLS data for this thread.
Definition: SDL3pp_thread.h:874
constexpr ThreadPriority THREAD_PRIORITY_TIME_CRITICAL
TIME_CRITICAL.
Definition: SDL3pp_thread.h:88
constexpr ThreadState THREAD_DETACHED
The thread is detached and can't be waited on.
Definition: SDL3pp_thread.h:108
const char * GetName() const
Get the thread name as it was specified in Thread.Thread().
Definition: SDL3pp_thread.h:652
int(SDLCALL *)(void *data) ThreadFunction
The function passed to Thread.Thread() as the new thread's entry point.
Definition: SDL3pp_thread.h:136
void(SDLCALL *)(void *value) TLSDestructorCallback
The callback used to cleanup data passed to SetTLS.
Definition: SDL3pp_thread.h:160
Thread CreateThreadWithProperties(PropertiesParam props)
Create a new thread with with the specified properties.
Definition: SDL3pp_thread.h:618
void DetachThread(ThreadRaw thread)
Let a thread clean up on exit without intervention.
Definition: SDL3pp_thread.h:812
constexpr ThreadState THREAD_ALIVE
The thread is currently running.
Definition: SDL3pp_thread.h:105
constexpr ThreadPriority THREAD_PRIORITY_LOW
LOW.
Definition: SDL3pp_thread.h:80
void SetCurrentThreadPriority(ThreadPriority priority)
Set the priority for the current thread.
Definition: SDL3pp_thread.h:709
ThreadID GetThreadID(ThreadParam thread)
Get the thread identifier for the specified thread.
Definition: SDL3pp_thread.h:690
SDL_ThreadState ThreadState
The SDL thread state.
Definition: SDL3pp_thread.h:100
const char * GetThreadName(ThreadParam thread)
Get the thread name as it was specified in Thread.Thread().
Definition: SDL3pp_thread.h:647
std::function< int()> ThreadCB
The function passed to Thread.Thread() as the new thread's entry point.
Definition: SDL3pp_thread.h:147
ThreadID GetCurrentThreadID()
Get the thread identifier for the current thread.
Definition: SDL3pp_thread.h:673
constexpr ThreadState THREAD_UNKNOWN
The thread is not valid.
Definition: SDL3pp_thread.h:102
SDL_ThreadPriority ThreadPriority
The SDL thread priority.
Definition: SDL3pp_thread.h:78
void Detach()
Let a thread clean up on exit without intervention.
Definition: SDL3pp_thread.h:814
static void SetCurrentPriority(ThreadPriority priority)
Set the priority for the current thread.
Definition: SDL3pp_thread.h:714
SDL_ThreadID ThreadID
A unique numeric ID that identifies a thread.
Definition: SDL3pp_thread.h:126
void Wait(int *status)
Wait for a thread to finish.
Definition: SDL3pp_thread.h:756
Thread CreateThread(ThreadFunction fn, StringParam name, void *data)
Create a new thread with a default stack size.
Definition: SDL3pp_thread.h:551
ThreadID GetID() const
Get the thread identifier for the specified thread.
Definition: SDL3pp_thread.h:695
SDL_Thread * ThreadRaw
Alias to raw representation for Thread.
Definition: SDL3pp_thread.h:35
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:856
constexpr ThreadPriority THREAD_PRIORITY_NORMAL
NORMAL.
Definition: SDL3pp_thread.h:82
ThreadState GetState() const
Get the current state of a thread.
Definition: SDL3pp_thread.h:774
constexpr ThreadState THREAD_COMPLETE
The thread has finished and should be cleaned up with Thread.Wait()
Definition: SDL3pp_thread.h:112
void * GetTLS(TLSID *id)
Get the current thread's value associated with a thread local storage ID.
Definition: SDL3pp_thread.h:829
void WaitThread(ThreadParam thread, int *status)
Wait for a thread to finish.
Definition: SDL3pp_thread.h:751
Main include header for the SDL3pp library.
A type representing an atomic integer value.
Definition: SDL3pp_atomic.h:216
Safely wrap Properties for non owning parameters.
Definition: SDL3pp_properties.h:52
Safely wrap Thread for non owning parameters.
Definition: SDL3pp_thread.h:42
constexpr auto operator<=>(const ThreadParam &other) const =default
Comparison.
constexpr ThreadParam(std::nullptr_t _=nullptr)
Constructs null/invalid.
Definition: SDL3pp_thread.h:52
ThreadRaw value
parameter's ThreadRaw
Definition: SDL3pp_thread.h:43
constexpr ThreadParam(ThreadRaw value)
Constructs from ThreadRaw.
Definition: SDL3pp_thread.h:46
Semi-safe reference for Thread.
Definition: SDL3pp_thread.h:471
~ThreadRef()
Destructor.
Definition: SDL3pp_thread.h:505
ThreadRef(const ThreadRef &other) noexcept
Copy constructor.
Definition: SDL3pp_thread.h:499
ThreadRef(ThreadRaw resource) noexcept
Constructs from ThreadParam.
Definition: SDL3pp_thread.h:493
ThreadRef(ThreadParam resource) noexcept
Constructs from ThreadParam.
Definition: SDL3pp_thread.h:481