diff --git a/include/SDL3/SDL_thread.h b/include/SDL3/SDL_thread.h index 83d11f90a2..ea2ac77358 100644 --- a/include/SDL3/SDL_thread.h +++ b/include/SDL3/SDL_thread.h @@ -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. * diff --git a/src/dynapi/SDL_dynapi.exports b/src/dynapi/SDL_dynapi.exports index da5623effd..a4c71021c3 100644 --- a/src/dynapi/SDL_dynapi.exports +++ b/src/dynapi/SDL_dynapi.exports @@ -1320,3 +1320,4 @@ _SDL_SetSoftwareRendererSurface _SDL_EncodeURL _SDL_SetRenderHDRHeadroom _SDL_GetRenderHDRHeadroom +_SDL_GetCurrentThreadName diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym index 100d637b27..0da9418059 100644 --- a/src/dynapi/SDL_dynapi.sym +++ b/src/dynapi/SDL_dynapi.sym @@ -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: *; }; diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h index 95dde5e83c..8c7eb34590 100644 --- a/src/dynapi/SDL_dynapi_overrides.h +++ b/src/dynapi/SDL_dynapi_overrides.h @@ -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 diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h index 4e2a54e993..ccecbfc10c 100644 --- a/src/dynapi/SDL_dynapi_procs.h +++ b/src/dynapi/SDL_dynapi_procs.h @@ -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) diff --git a/src/thread/SDL_systhread.h b/src/thread/SDL_systhread.h index 13d107fc59..7bcd015690 100644 --- a/src/thread/SDL_systhread.h +++ b/src/thread/SDL_systhread.h @@ -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++ diff --git a/src/thread/SDL_thread.c b/src/thread/SDL_thread.c index 639090c498..dd0f009550 100644 --- a/src/thread/SDL_thread.c +++ b/src/thread/SDL_thread.c @@ -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); diff --git a/src/thread/SDL_thread_c.h b/src/thread/SDL_thread_c.h index 4f35e13515..09cd2e6235 100644 --- a/src/thread/SDL_thread_c.h +++ b/src/thread/SDL_thread_c.h @@ -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_ diff --git a/src/thread/dos/SDL_systhread.c b/src/thread/dos/SDL_systhread.c index 88cf12010c..f288eded4e 100644 --- a/src/thread/dos/SDL_systhread.c +++ b/src/thread/dos/SDL_systhread.c @@ -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 */ diff --git a/src/thread/generic/SDL_systhread.c b/src/thread/generic/SDL_systhread.c index 3ef4facc2c..7f63a65400 100644 --- a/src/thread/generic/SDL_systhread.c +++ b/src/thread/generic/SDL_systhread.c @@ -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. +} diff --git a/src/thread/n3ds/SDL_systhread.c b/src/thread/n3ds/SDL_systhread.c index 5c1f0a1fa0..a4096dc7e4 100644 --- a/src/thread/n3ds/SDL_systhread.c +++ b/src/thread/n3ds/SDL_systhread.c @@ -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 diff --git a/src/thread/ps2/SDL_systhread.c b/src/thread/ps2/SDL_systhread.c index 6d038915c6..6be8843011 100644 --- a/src/thread/ps2/SDL_systhread.c +++ b/src/thread/ps2/SDL_systhread.c @@ -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 diff --git a/src/thread/psp/SDL_systhread.c b/src/thread/psp/SDL_systhread.c index f61dcea790..08f9ce8561 100644 --- a/src/thread/psp/SDL_systhread.c +++ b/src/thread/psp/SDL_systhread.c @@ -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 diff --git a/src/thread/pthread/SDL_systhread.c b/src/thread/pthread/SDL_systhread.c index 13b3ad0e0a..26bbffbd77 100644 --- a/src/thread/pthread/SDL_systhread.c +++ b/src/thread/pthread/SDL_systhread.c @@ -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. +} + diff --git a/src/thread/vita/SDL_systhread.c b/src/thread/vita/SDL_systhread.c index 32946522a1..55a4fee12e 100644 --- a/src/thread/vita/SDL_systhread.c +++ b/src/thread/vita/SDL_systhread.c @@ -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 diff --git a/src/thread/windows/SDL_systhread.c b/src/thread/windows/SDL_systhread.c index 1244c34464..cfca4a8081 100644 --- a/src/thread/windows/SDL_systhread.c +++ b/src/thread/windows/SDL_systhread.c @@ -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 diff --git a/test/testthread.c b/test/testthread.c index 110548cf00..9d0022bb8a 100644 --- a/test/testthread.c +++ b/test/testthread.c @@ -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());