From e9290eeedf14d73c61a2e51836adcbd24c513fbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Toma=C5=BEi=C4=8D?= Date: Tue, 24 Dec 2024 08:17:18 +0100 Subject: [PATCH] Use `pthread_setname_np` also on Android Set thread name on Android the same way as we do on Linux. Acording to Bionic source code this function is available since 2013 [1] and hase the same signature. [1] https://android.googlesource.com/platform/bionic/+/2a1bb4e64677b9abbc17173c79768ed494565047 (cherry picked from commit e79b0ce2e4660e7621719ca3fce362545c90cbf7) --- src/thread/pthread/SDL_systhread.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/thread/pthread/SDL_systhread.c b/src/thread/pthread/SDL_systhread.c index 9ece731222..d23b984532 100644 --- a/src/thread/pthread/SDL_systhread.c +++ b/src/thread/pthread/SDL_systhread.c @@ -40,7 +40,7 @@ #include "../../core/linux/SDL_dbus.h" #endif // SDL_PLATFORM_LINUX -#if (defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS)) && defined(HAVE_DLOPEN) +#if (defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS)) && defined(HAVE_DLOPEN) #include #ifndef RTLD_DEFAULT #define RTLD_DEFAULT NULL @@ -77,7 +77,7 @@ 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; -#elif defined(SDL_PLATFORM_LINUX) && defined(HAVE_DLOPEN) +#elif (defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID)) && defined(HAVE_DLOPEN) static bool checked_setname = false; static int (*ppthread_setname_np)(pthread_t, const char *) = NULL; #endif @@ -88,17 +88,17 @@ bool SDL_SYS_CreateThread(SDL_Thread *thread, 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(HAVE_DLOPEN) +#if (defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID)) && 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) +#elif defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID) ppthread_setname_np = (int (*)(pthread_t, const char *))fn; #endif checked_setname = true; } -#endif + #endif // Set the thread attributes if (pthread_attr_init(&type) != 0) { @@ -127,12 +127,12 @@ void SDL_SYS_SetupThread(const char *name) #endif if (name) { -#if (defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_LINUX)) && defined(HAVE_DLOPEN) +#if (defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID)) && defined(HAVE_DLOPEN) SDL_assert(checked_setname); if (ppthread_setname_np) { #if defined(SDL_PLATFORM_MACOS) || defined(SDL_PLATFORM_IOS) ppthread_setname_np(name); -#elif defined(SDL_PLATFORM_LINUX) +#elif defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_ANDROID) if (ppthread_setname_np(pthread_self(), name) == ERANGE) { char namebuf[16]; // Limited to 16 char SDL_strlcpy(namebuf, name, sizeof(namebuf));