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 = SDL_ThreadFunction;
137
145using ThreadCB = std::function<int()>;
146
158using TLSDestructorCallback = SDL_TLSDestructorCallback;
159
173{
174 ThreadRaw m_resource = nullptr;
175
176public:
178 constexpr Thread() = default;
179
187 constexpr explicit Thread(const ThreadRaw resource)
188 : m_resource(resource)
189 {
190 }
191
193 constexpr Thread(const Thread& other) = delete;
194
196 constexpr Thread(Thread&& other)
197 : Thread(other.release())
198 {
199 }
200
201 constexpr Thread(const ThreadRef& other) = delete;
202
203 constexpr Thread(ThreadRef&& other) = delete;
204
207
238 Thread(ThreadFunction fn, StringParam name, void* data)
239 : m_resource(CheckError(SDL_CreateThread(fn, name, data)))
240 {
241 }
242
307 : m_resource(CheckError(SDL_CreateThreadWithProperties(props)))
308 {
309 }
310
312 ~Thread() { SDL_DetachThread(m_resource); }
313
316 {
317 std::swap(m_resource, other.m_resource);
318 return *this;
319 }
320
322 constexpr ThreadRaw get() const { return m_resource; }
323
325 constexpr ThreadRaw release()
326 {
327 auto r = m_resource;
328 m_resource = nullptr;
329 return r;
330 }
331
333 constexpr auto operator<=>(const Thread& other) const = default;
334
336 constexpr bool operator==(std::nullptr_t _) const { return !m_resource; }
337
339 constexpr explicit operator bool() const { return !!m_resource; }
340
342 constexpr operator ThreadParam() const { return {m_resource}; }
343
377 void Detach();
378
387 const char* GetName() const;
388
403 ThreadID GetID() const;
404
417 static void SetCurrentPriority(ThreadPriority priority);
418
451 void Wait(int* status);
452
463 ThreadState GetState() const;
464};
465
468{
477 : Thread(resource.value)
478 {
479 }
480
482 ThreadRef(const ThreadRef& other)
483 : Thread(other.get())
484 {
485 }
486
489};
490
503
534inline Thread CreateThread(ThreadFunction fn, StringParam name, void* data)
535{
536 return Thread(fn, std::move(name), data);
537}
538
603{
604 return Thread(props);
605}
606
607namespace prop::thread {
608
609constexpr auto CREATE_ENTRY_FUNCTION_POINTER =
610 SDL_PROP_THREAD_CREATE_ENTRY_FUNCTION_POINTER;
611
612constexpr auto CREATE_NAME_STRING = SDL_PROP_THREAD_CREATE_NAME_STRING;
613
614constexpr auto CREATE_USERDATA_POINTER =
615 SDL_PROP_THREAD_CREATE_USERDATA_POINTER;
616
617constexpr auto CREATE_STACKSIZE_NUMBER =
618 SDL_PROP_THREAD_CREATE_STACKSIZE_NUMBER;
619
620} // namespace prop::thread
621
631inline const char* GetThreadName(ThreadParam thread)
632{
633 return SDL_GetThreadName(thread);
634}
635
636inline const char* Thread::GetName() const
637{
638 return SDL::GetThreadName(m_resource);
639}
640
657inline ThreadID GetCurrentThreadID() { return SDL_GetCurrentThreadID(); }
658
675{
676 return SDL_GetThreadID(thread);
677}
678
679inline ThreadID Thread::GetID() const { return SDL::GetThreadID(m_resource); }
680
694{
695 CheckError(SDL_SetCurrentThreadPriority(priority));
696}
697
699{
701}
702
736inline void WaitThread(ThreadParam thread, int* status)
737{
738 SDL_WaitThread(thread, status);
739}
740
741inline void Thread::Wait(int* status) { SDL::WaitThread(m_resource, status); }
742
755{
756 return SDL_GetThreadState(thread);
757}
758
760{
761 return SDL::GetThreadState(m_resource);
762}
763
798inline void DetachThread(ThreadRaw thread) { SDL_DetachThread(thread); }
799
801
815inline void* GetTLS(TLSID* id) { return SDL_GetTLS(id); }
816
842inline void SetTLS(TLSID* id,
843 const void* value,
844 TLSDestructorCallback destructor)
845{
846 CheckError(SDL_SetTLS(id, value, destructor));
847}
848
860inline void CleanupTLS() { SDL_CleanupTLS(); }
861
863
864} // namespace SDL
865
866#endif /* SDL3PP_THREAD_H_ */
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
The SDL thread object.
Definition: SDL3pp_thread.h:173
Thread & operator=(Thread other)
Assignment operator.
Definition: SDL3pp_thread.h:315
Thread(ThreadCB fn, StringParam name)
Default ctor.
Definition: SDL3pp_thread.h:206
constexpr auto operator<=>(const Thread &other) const =default
Comparison.
constexpr Thread(Thread &&other)
Move constructor.
Definition: SDL3pp_thread.h:196
constexpr Thread(const ThreadRaw resource)
Constructs from ThreadParam.
Definition: SDL3pp_thread.h:187
constexpr ThreadRaw release()
Retrieves underlying ThreadRaw and clear this.
Definition: SDL3pp_thread.h:325
Thread(ThreadFunction fn, StringParam name, void *data)
Create a new thread with a default stack size.
Definition: SDL3pp_thread.h:238
~Thread()
Destructor.
Definition: SDL3pp_thread.h:312
constexpr ThreadRaw get() const
Retrieves underlying ThreadRaw.
Definition: SDL3pp_thread.h:322
constexpr Thread()=default
Default ctor.
Thread(PropertiesParam props)
Create a new thread with with the specified properties.
Definition: SDL3pp_thread.h:306
constexpr bool operator==(std::nullptr_t _) const
Comparison.
Definition: SDL3pp_thread.h:336
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:198
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:754
void CleanupTLS()
Cleanup all TLS data for this thread.
Definition: SDL3pp_thread.h:860
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:636
Thread CreateThreadWithProperties(PropertiesParam props)
Create a new thread with with the specified properties.
Definition: SDL3pp_thread.h:602
void DetachThread(ThreadRaw thread)
Let a thread clean up on exit without intervention.
Definition: SDL3pp_thread.h:798
SDL_TLSDestructorCallback TLSDestructorCallback
The callback used to cleanup data passed to SetTLS.
Definition: SDL3pp_thread.h:158
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:693
ThreadID GetThreadID(ThreadParam thread)
Get the thread identifier for the specified thread.
Definition: SDL3pp_thread.h:674
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:631
std::function< int()> ThreadCB
The function passed to Thread.Thread() as the new thread's entry point.
Definition: SDL3pp_thread.h:145
ThreadID GetCurrentThreadID()
Get the thread identifier for the current thread.
Definition: SDL3pp_thread.h:657
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:800
static void SetCurrentPriority(ThreadPriority priority)
Set the priority for the current thread.
Definition: SDL3pp_thread.h:698
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:741
Thread CreateThread(ThreadFunction fn, StringParam name, void *data)
Create a new thread with a default stack size.
Definition: SDL3pp_thread.h:534
ThreadID GetID() const
Get the thread identifier for the specified thread.
Definition: SDL3pp_thread.h:679
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:842
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:759
constexpr ThreadState THREAD_COMPLETE
The thread has finished and should be cleaned up with Thread.Wait()
Definition: SDL3pp_thread.h:112
SDL_ThreadFunction ThreadFunction
The function passed to Thread.Thread() as the new thread's entry point.
Definition: SDL3pp_thread.h:136
void * GetTLS(TLSID *id)
Get the current thread's value associated with a thread local storage ID.
Definition: SDL3pp_thread.h:815
void WaitThread(ThreadParam thread, int *status)
Wait for a thread to finish.
Definition: SDL3pp_thread.h:736
Main include header for the SDL3pp library.
A type representing an atomic integer value.
Definition: SDL3pp_atomic.h:221
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:468
~ThreadRef()
Destructor.
Definition: SDL3pp_thread.h:488
ThreadRef(ThreadParam resource)
Constructs from ThreadParam.
Definition: SDL3pp_thread.h:476
ThreadRef(const ThreadRef &other)
Copy constructor.
Definition: SDL3pp_thread.h:482