thread: Added SDL_GetCurrentThreadName().

Fixes #15919.
This commit is contained in:
Ryan C. Gordon
2026-11-12 14:35:18 -05:00
parent 94c6bb4ef6
commit fff3d3516e
17 changed files with 197 additions and 28 deletions
+25
View File
@@ -377,6 +377,31 @@ extern SDL_DECLSPEC SDL_Thread * SDLCALL SDL_CreateThreadWithPropertiesRuntime(S
*/
extern SDL_DECLSPEC const char * SDLCALL SDL_GetThreadName(SDL_Thread *thread);
/**
* Get the thread name for the current thread.
*
* Thread names are always available for threads started through
* SDL_CreateThread() or SDL_CreateThreadWithProperties(). For other threads,
* SDL will query the OS if possible. This is not possible on every platform,
* and will return NULL in these cases.
*
* On supported platforms, and for SDL-created threads, this might still
* return NULL if there was an error, or the system is out of memory, etc.
*
* The returned string is owned by SDL and guaranteed to live until the
* calling thread terminates. If it needs to be passed to another thread that
* might outlive the caller's thread, you should make a copy of the string.
*
* \returns the name of the current thread, or NULL if not available.
*
* \threadsafety It is safe to call this function from any thread.
*
* \since This function is available since SDL 3.6.0.
*
* \sa SDL_GetThreadName
*/
extern SDL_DECLSPEC const char * SDLCALL SDL_GetCurrentThreadName(void);
/**
* Get the thread identifier for the current thread.
*
+1
View File
@@ -1320,3 +1320,4 @@ _SDL_SetSoftwareRendererSurface
_SDL_EncodeURL
_SDL_SetRenderHDRHeadroom
_SDL_GetRenderHDRHeadroom
_SDL_GetCurrentThreadName
+1
View File
@@ -1321,6 +1321,7 @@ SDL3_0.0.0 {
SDL_EncodeURL;
SDL_SetRenderHDRHeadroom;
SDL_GetRenderHDRHeadroom;
SDL_GetCurrentThreadName;
# extra symbols go here (don't modify this line)
local: *;
};
+1
View File
@@ -1347,3 +1347,4 @@
#define SDL_EncodeURL SDL_EncodeURL_REAL
#define SDL_SetRenderHDRHeadroom SDL_SetRenderHDRHeadroom_REAL
#define SDL_GetRenderHDRHeadroom SDL_GetRenderHDRHeadroom_REAL
#define SDL_GetCurrentThreadName SDL_GetCurrentThreadName_REAL
+1
View File
@@ -1355,3 +1355,4 @@ SDL_DYNAPI_PROC(bool,SDL_SetSoftwareRendererSurface,(SDL_Renderer *a,SDL_Surface
SDL_DYNAPI_PROC(char*,SDL_EncodeURL,(const char *a,const char *b),(a,b),return)
SDL_DYNAPI_PROC(bool,SDL_SetRenderHDRHeadroom,(SDL_Renderer *a,float b),(a,b),return)
SDL_DYNAPI_PROC(bool,SDL_GetRenderHDRHeadroom,(SDL_Renderer *a,float *b),(a,b),return)
SDL_DYNAPI_PROC(const char*,SDL_GetCurrentThreadName,(void),(),return)
+5
View File
@@ -66,7 +66,12 @@ extern bool SDL_SYS_SetTLSData(SDL_TLSData *data);
// Quit the global TLS data
extern void SDL_SYS_QuitTLSData(void);
// Fill the current thread's OS-level name, null-terminated, into buf. Only called for threads SDL didn't create.
// Return false if can't provide. Do NOT call SDL_SetError in this case, as we've already set a "Thread not created by SDL" error message.
extern bool SDL_SYS_GetCurrentThreadName(char *buf, int buflen);
// A helper function for setting up a thread with a stack size.
// !!! FIXME: this declaration should be in SDL_thread_c.h, right?
extern SDL_Thread *SDL_CreateThreadWithStackSize(SDL_ThreadFunction fn, const char *name, size_t stacksize, void *data);
// Ends C function definitions when using C++
+25
View File
@@ -325,6 +325,17 @@ static bool ThreadValid(SDL_Thread *thread)
return SDL_ObjectValid(thread, SDL_OBJECT_TYPE_THREAD);
}
static SDL_TLSID SDL_thread_ptr_tls;
SDL_Thread *SDL_GetCurrentThread(void)
{
SDL_Thread *retval = (SDL_Thread *) SDL_GetTLS(&SDL_thread_ptr_tls);
if (!retval) {
SDL_SetError("Thread was not created by SDL");
}
return retval;
}
void SDL_RunThread(SDL_Thread *thread)
{
void *userdata = thread->userdata;
@@ -335,6 +346,8 @@ void SDL_RunThread(SDL_Thread *thread)
// Perform any system-dependent setup - this function may not fail
SDL_SYS_SetupThread(thread->name);
SDL_SetTLS(&SDL_thread_ptr_tls, thread, NULL); // so we can always find the `SDL_Thread *` for the current thread.
// Run the function
*statusloc = userfunc(userdata);
@@ -460,6 +473,18 @@ const char *SDL_GetThreadName(SDL_Thread *thread)
}
}
const char *SDL_GetCurrentThreadName(void)
{
SDL_Thread *thread = SDL_GetCurrentThread();
if (thread) {
return SDL_GetPersistentString(thread->name);
}
// This wasn't an SDL-created thread, let's try for the OS level answer. Not all platforms support this!
char buf[128];
return SDL_SYS_GetCurrentThreadName(buf, sizeof (buf)) ? SDL_GetPersistentString(buf) : NULL;
}
bool SDL_SetCurrentThreadPriority(SDL_ThreadPriority priority)
{
return SDL_SYS_SetThreadPriority(priority);
+3
View File
@@ -98,4 +98,7 @@ extern SDL_TLSData *SDL_Generic_GetTLSData(void);
extern bool SDL_Generic_SetTLSData(SDL_TLSData *data);
extern void SDL_Generic_QuitTLSData(void);
// Obtain the current thread's `SDL_Thread *`. Thread must have been created with SDL_CreateThread. The main thread and other OS-level threads will return NULL!
extern SDL_Thread *SDL_GetCurrentThread(void);
#endif // SDL_thread_c_h_
+6
View File
@@ -86,4 +86,10 @@ bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
return true;
}
bool SDL_SYS_GetCurrentThreadName(char *buf, int buflen)
{
SDL_assert(!"Should never be called."); // SDL controls all threads on DOS, so they should all have names and we should never hit this function.
return false;
}
#endif /* SDL_THREAD_DOS */
+5
View File
@@ -55,3 +55,8 @@ void SDL_SYS_DetachThread(SDL_Thread *thread)
{
return;
}
bool SDL_SYS_GetCurrentThreadName(char *buf, int buflen)
{
return false; // there are no threads, so there are no thread names.
}
+5
View File
@@ -143,4 +143,9 @@ void SDL_SYS_DetachThread(SDL_Thread *thread)
threadDetach(thread->handle);
}
bool SDL_SYS_GetCurrentThreadName(char *buf, int buflen)
{
return false; // no thread names on this platform.
}
#endif // SDL_THREAD_N3DS
+5
View File
@@ -143,4 +143,9 @@ bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
return true;
}
bool SDL_SYS_GetCurrentThreadName(char *buf, int buflen)
{
return false; // no thread names on this platform.
}
#endif // SDL_THREAD_PS2
+12
View File
@@ -119,4 +119,16 @@ bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
return true;
}
bool SDL_SYS_GetCurrentThreadName(char *buf, int buflen)
{
SceKernelThreadInfo info;
SDL_zero(info);
info.size = sizeof (info);
if (sceKernelReferThreadStatus(sceKernelGetThreadId(), &info) != 0) {
return false;
}
SDL_strlcpy(buf, info.name, buflen);
return true;
}
#endif // SDL_THREAD_PSP
+36 -12
View File
@@ -77,28 +77,39 @@ static void *RunThread(void *data)
#if (defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS)) && defined(HAVE_DLOPEN)
static bool checked_setname = false;
static int (*ppthread_setname_np)(const char *) = NULL;
static int (*ppthread_getname_np)(pthread_t, char *, size_t) = NULL;
#elif (defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_OPENHARMONY)) && defined(HAVE_DLOPEN)
static bool checked_setname = false;
static int (*ppthread_setname_np)(pthread_t, const char *) = NULL;
static int (*ppthread_getname_np)(pthread_t, char *, size_t) = NULL;
#endif
static void CheckThreadNameAPIs(void)
{
#if (defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_OPENHARMONY)) && defined(HAVE_DLOPEN)
if (!checked_setname) {
void *fn;
fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
#if defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS)
ppthread_setname_np = (int (*)(const char *))fn;
#elif defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_OPENHARMONY)
ppthread_setname_np = (int (*)(pthread_t, const char *))fn;
#endif
fn = dlsym(RTLD_DEFAULT, "pthread_getname_np");
ppthread_getname_np = (int (*)(pthread_t, char *, size_t))fn;
checked_setname = true;
}
#endif
}
bool SDL_SYS_CreateThread(SDL_Thread *thread,
SDL_FunctionPointer pfnBeginThread,
SDL_FunctionPointer pfnEndThread)
{
pthread_attr_t type;
// do this here before any threads exist, so there's no race condition.
#if (defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_OPENHARMONY)) && defined(HAVE_DLOPEN)
if (!checked_setname) {
void *fn = dlsym(RTLD_DEFAULT, "pthread_setname_np");
#if defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS)
ppthread_setname_np = (int (*)(const char *))fn;
#elif defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_OPENHARMONY)
ppthread_setname_np = (int (*)(pthread_t, const char *))fn;
#endif
checked_setname = true;
}
#endif
// do this here before any threads exist, so there's no race condition.
CheckThreadNameAPIs();
// Set the thread attributes
if (pthread_attr_init(&type) != 0) {
@@ -302,3 +313,16 @@ void SDL_SYS_DetachThread(SDL_Thread *thread)
{
pthread_detach(thread->handle);
}
bool SDL_SYS_GetCurrentThreadName(char *buf, int buflen)
{
#if (defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_OPENHARMONY)) && defined(HAVE_DLOPEN)
CheckThreadNameAPIs();
if (ppthread_getname_np) {
return (ppthread_getname_np(pthread_self(), buf, buflen) == 0);
}
#endif
return false; // no thread names on this platform.
}
+12
View File
@@ -138,4 +138,16 @@ bool SDL_SYS_SetThreadPriority(SDL_ThreadPriority priority)
return true;
}
bool SDL_SYS_GetCurrentThreadName(char *buf, int buflen)
{
SceKernelThreadInfo info;
SDL_zero(info);
info.size = sizeof (info);
if (sceKernelGetThreadInfo(sceKernelGetThreadId(), &info) != 0) {
return false;
}
SDL_strlcpy(buf, info.name, buflen);
return true;
}
#endif // SDL_THREAD_VITA
+43 -14
View File
@@ -112,26 +112,36 @@ static LONG NTAPI EmptyVectoredExceptionHandler(EXCEPTION_POINTERS *info)
}
typedef HRESULT (WINAPI *pfnSetThreadDescription)(HANDLE, PCWSTR);
typedef HRESULT (WINAPI *pfnGetThreadDescription)(HANDLE, PWSTR *);
static pfnSetThreadDescription pSetThreadDescription = NULL;
static pfnGetThreadDescription pGetThreadDescription = NULL;
static HMODULE kernel32 = NULL;
static void CheckThreadNameAPIs(void)
{
if (!kernel32) {
kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
if (kernel32) {
pSetThreadDescription = (pfnSetThreadDescription)GetProcAddress(kernel32, "SetThreadDescription");
pGetThreadDescription = (pfnGetThreadDescription)GetProcAddress(kernel32, "GetThreadDescription");
}
if (!kernel32 || !pSetThreadDescription) {
HMODULE kernelBase = GetModuleHandle(TEXT("KernelBase.dll"));
if (kernelBase) {
pSetThreadDescription = (pfnSetThreadDescription)GetProcAddress(kernelBase, "SetThreadDescription");
pGetThreadDescription = (pfnGetThreadDescription)GetProcAddress(kernelBase, "GetThreadDescription");
}
}
}
}
void SDL_SYS_SetupThread(const char *name)
{
if (name) {
PVOID exceptionHandlerHandle;
static pfnSetThreadDescription pSetThreadDescription = NULL;
static HMODULE kernel32 = NULL;
if (!kernel32) {
kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
if (kernel32) {
pSetThreadDescription = (pfnSetThreadDescription)GetProcAddress(kernel32, "SetThreadDescription");
}
if (!kernel32 || !pSetThreadDescription) {
HMODULE kernelBase = GetModuleHandle(TEXT("KernelBase.dll"));
if (kernelBase) {
pSetThreadDescription = (pfnSetThreadDescription)GetProcAddress(kernelBase, "SetThreadDescription");
}
}
}
CheckThreadNameAPIs();
if (pSetThreadDescription) {
WCHAR *strw = WIN_UTF8ToStringW(name);
@@ -197,4 +207,23 @@ void SDL_SYS_DetachThread(SDL_Thread *thread)
CloseHandle(thread->handle);
}
bool SDL_SYS_GetCurrentThreadName(char *buf, int buflen)
{
CheckThreadNameAPIs();
if (pGetThreadDescription) {
PWSTR wstr = NULL;
if (SUCCEEDED(pGetThreadDescription(GetCurrentThread(), &wstr))) {
char *utf8 = WIN_StringToUTF8W(wstr);
LocalFree(wstr);
if (utf8) {
SDL_strlcpy(buf, utf8, buflen);
SDL_free(utf8);
return true;
}
}
}
return false; // not supported or something failed.
}
#endif // SDL_THREAD_WINDOWS
+11 -2
View File
@@ -65,9 +65,10 @@ static int SDLCALL ThreadFunc(void *data)
{
SDL_ThreadPriority prio = SDL_THREAD_PRIORITY_NORMAL;
SDL_SetTLS(&tls, "baby thread", NULL);
SDL_Log("Started thread %s: My thread id is %" SDL_PRIu64 ", thread data = %s",
(char *)data, SDL_GetCurrentThreadID(), (const char *)SDL_GetTLS(&tls));
SDL_Log("Started thread %s: My thread id is %" SDL_PRIu64 ", thread name = '%s', thread data = %s",
(char *)data, SDL_GetCurrentThreadID(), SDL_GetCurrentThreadName(), (const char *)SDL_GetTLS(&tls));
while (SDL_GetAtomicInt(&alive)) {
SDL_Log("Thread '%s' is alive!", (char *)data);
@@ -131,6 +132,14 @@ int main(int argc, char *argv[])
quit(1);
}
const char *main_thread_name = SDL_GetCurrentThreadName();
if (!main_thread_name) {
SDL_Log("Couldn't determine main thread name: %s", SDL_GetError());
SDL_Log("(this is not necessarily a bug.)");
} else {
SDL_Log("Main thread's name is: %s", main_thread_name);
}
thread = SDL_CreateThread(CheckMainThread, "CheckMainThread", &child_is_main);
if (!thread) {
SDL_Log("Couldn't create thread: %s", SDL_GetError());