[compat] improve thread naming logic (#271)

Signed-off-by: lizzie <lizzie@eden-emu.dev>
Reviewed-on: #271
Reviewed-by: MaranBr <maranbr@eden-emu.dev>
Reviewed-by: CamilleLaVey <camillelavey99@gmail.com>
Co-authored-by: lizzie <lizzie@eden-emu.dev>
Co-committed-by: lizzie <lizzie@eden-emu.dev>
This commit is contained in:
lizzie 2025-10-07 06:38:22 +02:00 committed by crueter
parent dbeae7add0
commit cf0628af46
Signed by: crueter
GPG key ID: 425ACD2D4830EBC6

View file

@ -1,6 +1,5 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project // SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2013 Dolphin Emulator Project // SPDX-FileCopyrightText: 2013 Dolphin Emulator Project
// SPDX-FileCopyrightText: 2014 Citra Emulator Project // SPDX-FileCopyrightText: 2014 Citra Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
@ -18,9 +17,8 @@
#else #else
#if defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) #if defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#include <pthread_np.h> #include <pthread_np.h>
#else
#include <pthread.h>
#endif #endif
#include <pthread.h>
#include <sched.h> #include <sched.h>
#endif #endif
#ifndef _WIN32 #ifndef _WIN32
@ -93,33 +91,35 @@ void SetCurrentThreadName(const char* name) {
#else // !MSVC_VER, so must be POSIX threads #else // !MSVC_VER, so must be POSIX threads
// MinGW with the POSIX threading model does not support pthread_setname_np // MinGW with the POSIX threading model does not support pthread_setname_np
#if !defined(_WIN32) || defined(_MSC_VER)
void SetCurrentThreadName(const char* name) { void SetCurrentThreadName(const char* name) {
// See for reference
// https://gitlab.freedesktop.org/mesa/mesa/-/blame/main/src/util/u_thread.c?ref_type=heads#L75
#ifdef __APPLE__ #ifdef __APPLE__
pthread_setname_np(name); pthread_setname_np(name);
#elif defined(__HAIKU__)
rename_thread(find_thread(NULL), name);
#elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) #elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)
pthread_set_name_np(pthread_self(), name); pthread_set_name_np(pthread_self(), name);
#elif defined(__NetBSD__) #elif defined(__NetBSD__)
pthread_setname_np(pthread_self(), "%s", (void*)name); pthread_setname_np(pthread_self(), "%s", (void*)name);
#elif defined(__linux__) #elif defined(__linux__) || defined(__CYGWIN__) || defined(__sun__) || defined(__glibc__) || defined(__managarm__)
// Linux limits thread names to 15 characters and will outright reject any int ret = pthread_setname_np(pthread_self(), name);
// attempt to set a longer name with ERANGE. if (ret == ERANGE) {
std::string truncated(name, (std::min)(strlen(name), static_cast<size_t>(15))); // Linux limits thread names to 15 characters and will outright reject any
if (int e = pthread_setname_np(pthread_self(), truncated.c_str())) { // attempt to set a longer name with ERANGE.
errno = e; char buf[16];
LOG_ERROR(Common, "Failed to set thread name to '{}': {}", truncated, GetLastErrorMsg()); size_t const len = std::min<size_t>(std::strlen(name), sizeof(buf) - 1);
std::memcpy(buf, name, len);
buf[len] = '\0';
pthread_setname_np(pthread_self(), buf);
} }
#elif !defined(_WIN32) || defined(_MSC_VER)
// mingw stub
(void)name;
#else #else
pthread_setname_np(pthread_self(), name); pthread_setname_np(pthread_self(), name);
#endif #endif
} }
#endif
#if defined(_WIN32)
void SetCurrentThreadName(const char* name) {
// Do Nothing on MingW
}
#endif
#endif #endif