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
72using ThreadFunction = SDL_ThreadFunction;
73
82using ThreadCB = std::function<int()>;
83
95using TLSDestructorCallback = SDL_TLSDestructorCallback;
96
97// Forward decl
98struct ThreadRef;
99
100// Forward decl
101struct Thread;
102
112
122
134using ThreadPriority = SDL_ThreadPriority;
135
136constexpr ThreadPriority THREAD_PRIORITY_LOW = SDL_THREAD_PRIORITY_LOW;
137
139 SDL_THREAD_PRIORITY_NORMAL;
140
142 SDL_THREAD_PRIORITY_HIGH;
143
145 SDL_THREAD_PRIORITY_TIME_CRITICAL;
146
156using ThreadState = SDL_ThreadState;
157
159 SDL_THREAD_UNKNOWN;
160
162 SDL_THREAD_ALIVE;
163
165 SDL_THREAD_DETACHED;
166
170constexpr ThreadState THREAD_COMPLETE = SDL_THREAD_COMPLETE;
171
185struct ThreadRef : Resource<SDL_Thread*>
186{
187 using Resource::Resource;
188
197 const char* GetName() const { return SDL_GetThreadName(get()); }
198
213 ThreadID GetID() const { return SDL_GetThreadID(get()); }
214
227 static void SetCurrentPriority(ThreadPriority priority)
228 {
229 CheckError(SDL_SetCurrentThreadPriority(priority));
230 }
231
263 void Wait(int* status) { SDL_WaitThread(get(), status); }
264
275 ThreadState GetState() const { return SDL_GetThreadState(get()); }
276
311 static void reset(SDL_Thread* resource) { SDL_DetachThread(resource); }
312};
313
321struct Thread : ResourceUnique<ThreadRef>
322{
324
339 {
340 return Create(
341 [](void* handler) {
343 },
344 std::move(name),
345 CallbackWrapper<ThreadCB>::Wrap(std::move(fn)));
346 }
347
372 static Thread Create(ThreadFunction fn, StringParam name, void* data)
373 {
374 return Thread(CheckError(SDL_CreateThread(fn, name, data)));
375 }
376
441 {
442 return Thread(CheckError(SDL_CreateThreadWithProperties(props)));
443 }
444
474 void Detach() { reset(); }
479
480};
481
482
484{
485 return ThreadShared(std::move(*this));
486}
487
497struct ThreadUnsafe : ResourceUnsafe<ThreadRef>
498{
500
504 constexpr explicit ThreadUnsafe(Thread&& other)
505 : ThreadUnsafe(other.release())
506 {
507 }
508};
509
510namespace prop::thread {
511
512constexpr auto CREATE_ENTRY_FUNCTION_POINTER =
513 SDL_PROP_THREAD_CREATE_ENTRY_FUNCTION_POINTER;
514
515constexpr auto CREATE_NAME_STRING = SDL_PROP_THREAD_CREATE_NAME_STRING;
516
517constexpr auto CREATE_USERDATA_POINTER =
518 SDL_PROP_THREAD_CREATE_USERDATA_POINTER;
519
520constexpr auto CREATE_STACKSIZE_NUMBER =
521 SDL_PROP_THREAD_CREATE_STACKSIZE_NUMBER;
522
523} // namespace prop::thread
524
541inline ThreadID GetCurrentThreadID() { return SDL_GetCurrentThreadID(); }
542
556inline void* GetTLS(TLSID* id) { return SDL_GetTLS(*id); }
557
583inline void SetTLS(TLSID* id,
584 const void* value,
585 TLSDestructorCallback destructor)
586{
587 CheckError(SDL_SetTLS(*id, value, destructor));
588}
589
601inline void CleanupTLS() { SDL_CleanupTLS(); }
602
604} // namespace SDL
605
606#endif /* SDL3PP_THREAD_H_ */
A type representing an atomic integer value.
Definition SDL3pp_atomic.h:210
RESOURCE release()
Returns reference and reset this.
Definition SDL3pp_resource.h:178
Implement shared ownership for a resource.
Definition SDL3pp_resource.h:283
Implement unique ownership for a resource.
Definition SDL3pp_resource.h:226
constexpr ResourceUnique(std::nullptr_t=nullptr)
Default constructor.
Definition SDL3pp_resource.h:231
void reset()
Resets the value, destroying the resource if not nullptr.
Definition SDL3pp_resource.h:265
A dumb pointer to resource.
Definition SDL3pp_resource.h:197
constexpr ResourceUnsafe()=default
Default constructor.
Implement weak ownership for a resource.
Definition SDL3pp_resource.h:328
A SDL managed resource.
Definition SDL3pp_resource.h:29
constexpr Resource(T resource={})
Constructs from the underlying resource.
Definition SDL3pp_resource.h:37
constexpr SDL_Thread * get() const
Return contained resource;.
Definition SDL3pp_resource.h:76
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:141
void CleanupTLS()
Cleanup all TLS data for this thread.
Definition SDL3pp_thread.h:601
constexpr ThreadPriority THREAD_PRIORITY_TIME_CRITICAL
TIME_CRITICAL.
Definition SDL3pp_thread.h:144
constexpr ThreadState THREAD_DETACHED
The thread is detached and can't be waited on.
Definition SDL3pp_thread.h:164
SDL_TLSDestructorCallback TLSDestructorCallback
The callback used to cleanup data passed to SetTLS.
Definition SDL3pp_thread.h:95
constexpr ThreadState THREAD_ALIVE
The thread is currently running.
Definition SDL3pp_thread.h:161
constexpr ThreadPriority THREAD_PRIORITY_LOW
LOW.
Definition SDL3pp_thread.h:136
SDL_ThreadState ThreadState
The SDL thread state.
Definition SDL3pp_thread.h:156
std::function< int()> ThreadCB
The function passed to Thread.Create() as the new thread's entry point.
Definition SDL3pp_thread.h:82
ThreadID GetCurrentThreadID()
Get the thread identifier for the current thread.
Definition SDL3pp_thread.h:541
constexpr ThreadState THREAD_UNKNOWN
The thread is not valid.
Definition SDL3pp_thread.h:158
SDL_ThreadPriority ThreadPriority
The SDL thread priority.
Definition SDL3pp_thread.h:134
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:583
ThreadShared share()
Move this thread into a ThreadShared.
Definition SDL3pp_thread.h:483
constexpr ThreadPriority THREAD_PRIORITY_NORMAL
NORMAL.
Definition SDL3pp_thread.h:138
constexpr ThreadState THREAD_COMPLETE
The thread has finished and should be cleaned up with ThreadRef.Wait()
Definition SDL3pp_thread.h:170
ResourceShared< Thread > ThreadShared
Handle to a shared thread.
Definition SDL3pp_thread.h:111
SDL_ThreadFunction ThreadFunction
The function passed to Thread.Create() as the new thread's entry point.
Definition SDL3pp_thread.h:72
void * GetTLS(TLSID *id)
Get the current thread's value associated with a thread local storage ID.
Definition SDL3pp_thread.h:556
the main namespace where all SDL3pp public functions and types live
Definition SDL3pp_assert.h:7
Definition SDL3pp_callbackWrapper.h:66
SDL properties ID.
Definition SDL3pp_properties.h:209
The SDL thread object.
Definition SDL3pp_thread.h:186
ThreadState GetState() const
Get the current state of a thread.
Definition SDL3pp_thread.h:275
void Wait(int *status)
Wait for a thread to finish.
Definition SDL3pp_thread.h:263
const char * GetName() const
Get the thread name as it was specified in (Thread.Create).
Definition SDL3pp_thread.h:197
static void SetCurrentPriority(ThreadPriority priority)
Set the priority for the current thread.
Definition SDL3pp_thread.h:227
ThreadID GetID() const
Get the thread identifier for the specified thread.
Definition SDL3pp_thread.h:213
static void reset(SDL_Thread *resource)
Let a thread clean up on exit without intervention.
Definition SDL3pp_thread.h:311
Unsafe Handle to thread.
Definition SDL3pp_thread.h:498
constexpr ThreadUnsafe(Thread &&other)
Constructs ThreadUnsafe from Thread.
Definition SDL3pp_thread.h:504
Handle to an owned thread.
Definition SDL3pp_thread.h:322
static Thread Create(ThreadCB fn, StringParam name)
Create a new thread with a default stack size.
Definition SDL3pp_thread.h:338
static Thread Create(ThreadFunction fn, StringParam name, void *data)
Create a new thread with a default stack size.
Definition SDL3pp_thread.h:372
void Detach()
Let a thread clean up on exit without intervention.
Definition SDL3pp_thread.h:474
static Thread CreateWithProperties(PropertiesRef props)
Create a new thread with with the specified properties.
Definition SDL3pp_thread.h:440