VERY experimental Nintendo Switch 2 emulator
C++
[compat] improve thread naming logic (#271)
lizziecf0628af466f
diff --git a/src/common/thread.cpp b/src/common/thread.cppindex da1fa9b6c5..2de7465a22 100644--- a/src/common/thread.cpp+++ b/src/common/thread.cpp@@ -1,6 +1,5 @@// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project// SPDX-License-Identifier: GPL-3.0-or-later-// SPDX-FileCopyrightText: 2013 Dolphin Emulator Project// SPDX-FileCopyrightText: 2014 Citra Emulator Project// SPDX-License-Identifier: GPL-2.0-or-later@@ -18,9 +17,8 @@#else#if defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)#include <pthread_np.h>-#else-#include <pthread.h>#endif+#include <pthread.h>#include <sched.h>#endif#ifndef _WIN32@@ -93,33 +91,35 @@ void SetCurrentThreadName(const char* name) {#else // !MSVC_VER, so must be POSIX threads// MinGW with the POSIX threading model does not support pthread_setname_np-#if !defined(_WIN32) || defined(_MSC_VER)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__pthread_setname_np(name);+#elif defined(__HAIKU__)+ rename_thread(find_thread(NULL), name);#elif defined(__Bitrig__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__)pthread_set_name_np(pthread_self(), name);#elif defined(__NetBSD__)pthread_setname_np(pthread_self(), "%s", (void*)name);-#elif defined(__linux__)- // Linux limits thread names to 15 characters and will outright reject any- // attempt to set a longer name with ERANGE.- std::string truncated(name, (std::min)(strlen(name), static_cast<size_t>(15)));- if (int e = pthread_setname_np(pthread_self(), truncated.c_str())) {- errno = e;- LOG_ERROR(Common, "Failed to set thread name to '{}': {}", truncated, GetLastErrorMsg());+#elif defined(__linux__) || defined(__CYGWIN__) || defined(__sun__) || defined(__glibc__) || defined(__managarm__)+ int ret = pthread_setname_np(pthread_self(), name);+ if (ret == ERANGE) {+ // Linux limits thread names to 15 characters and will outright reject any+ // attempt to set a longer name with ERANGE.+ char buf[16];+ 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;#elsepthread_setname_np(pthread_self(), name);#endif}-#endif--#if defined(_WIN32)-void SetCurrentThreadName(const char* name) {- // Do Nothing on MingW-}-#endif#endif