1#ifndef SDL3PP_THREAD_H_
2#define SDL3PP_THREAD_H_
4#include <SDL3/SDL_thread.h>
5#include "SDL3pp_atomic.h"
6#include "SDL3pp_properties.h"
7#include "SDL3pp_stdinc.h"
59 constexpr explicit operator bool()
const {
return !!
value; }
84 SDL_THREAD_PRIORITY_NORMAL;
87 SDL_THREAD_PRIORITY_HIGH;
90 SDL_THREAD_PRIORITY_TIME_CRITICAL;
181 constexpr Thread(std::nullptr_t =
nullptr) noexcept
194 : m_resource(resource)
247 : m_resource(
CheckError(SDL_CreateThread(fn, name, data)))
314 : m_resource(
CheckError(SDL_CreateThreadWithProperties(props)))
324 std::swap(m_resource, other.m_resource);
340 m_resource =
nullptr;
348 constexpr explicit operator bool() const noexcept {
return !!m_resource; }
351 constexpr operator ThreadParam() const noexcept {
return {m_resource}; }
457 void Wait(
int* status);
553 return Thread(fn, std::move(name), data);
623namespace prop::thread {
625constexpr auto CREATE_ENTRY_FUNCTION_POINTER =
626 SDL_PROP_THREAD_CREATE_ENTRY_FUNCTION_POINTER;
628constexpr auto CREATE_NAME_STRING = SDL_PROP_THREAD_CREATE_NAME_STRING;
630constexpr auto CREATE_USERDATA_POINTER =
631 SDL_PROP_THREAD_CREATE_USERDATA_POINTER;
633constexpr auto CREATE_STACKSIZE_NUMBER =
634 SDL_PROP_THREAD_CREATE_STACKSIZE_NUMBER;
649 return SDL_GetThreadName(thread);
692 return SDL_GetThreadID(thread);
711 CheckError(SDL_SetCurrentThreadPriority(priority));
753 SDL_WaitThread(thread, status);
771 return SDL_GetThreadState(thread);
860 CheckError(SDL_SetTLS(
id, value, destructor));
Helpers to use C++ strings parameters.
Definition: SDL3pp_strings.h:43
The SDL thread object.
Definition: SDL3pp_thread.h:176
Thread(ThreadCB fn, StringParam name)
Default ctor.
Definition: SDL3pp_thread.h:214
constexpr auto operator<=>(const Thread &other) const noexcept=default
Comparison.
constexpr Thread(const ThreadRaw resource) noexcept
Constructs from ThreadParam.
Definition: SDL3pp_thread.h:193
constexpr Thread(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_thread.h:181
constexpr Thread & operator=(Thread &&other) noexcept
Assignment operator.
Definition: SDL3pp_thread.h:322
constexpr ThreadRaw release() noexcept
Retrieves underlying ThreadRaw and clear this.
Definition: SDL3pp_thread.h:337
Thread(ThreadFunction fn, StringParam name, void *data)
Create a new thread with a default stack size.
Definition: SDL3pp_thread.h:246
~Thread()
Destructor.
Definition: SDL3pp_thread.h:319
Thread(PropertiesParam props)
Create a new thread with with the specified properties.
Definition: SDL3pp_thread.h:313
constexpr Thread(Thread &&other) noexcept
Move constructor.
Definition: SDL3pp_thread.h:204
constexpr ThreadRaw get() const noexcept
Retrieves underlying ThreadRaw.
Definition: SDL3pp_thread.h:334
constexpr Thread(const Thread &other) noexcept=default
Copy constructor.
constexpr Thread & operator=(const Thread &other) noexcept=default
Assignment operator.
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:86
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:89
constexpr ThreadState THREAD_DETACHED
The thread is detached and can't be waited on.
Definition: SDL3pp_thread.h:109
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:137
void(SDLCALL *)(void *value) TLSDestructorCallback
The callback used to cleanup data passed to SetTLS.
Definition: SDL3pp_thread.h:161
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:106
constexpr ThreadPriority THREAD_PRIORITY_LOW
LOW.
Definition: SDL3pp_thread.h:81
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:101
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:148
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:103
SDL_ThreadPriority ThreadPriority
The SDL thread priority.
Definition: SDL3pp_thread.h:79
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:127
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:36
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:83
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:113
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:218
Safely wrap Properties for non owning parameters.
Definition: SDL3pp_properties.h:53
Safely wrap Thread for non owning parameters.
Definition: SDL3pp_thread.h:43
constexpr auto operator<=>(const ThreadParam &other) const =default
Comparison.
ThreadRaw value
parameter's ThreadRaw
Definition: SDL3pp_thread.h:44
constexpr ThreadParam(ThreadRaw value)
Constructs from ThreadRaw.
Definition: SDL3pp_thread.h:47
constexpr ThreadParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_thread.h:53
Semi-safe reference for Thread.
Definition: SDL3pp_thread.h:474
~ThreadRef()
Destructor.
Definition: SDL3pp_thread.h:505
ThreadRef(ThreadRaw resource) noexcept
Constructs from ThreadParam.
Definition: SDL3pp_thread.h:496
constexpr ThreadRef(const ThreadRef &other) noexcept=default
Copy constructor.
ThreadRef(ThreadParam resource) noexcept
Constructs from ThreadParam.
Definition: SDL3pp_thread.h:484