[compat] improve thread naming logic
All checks were successful
eden-license / license-header (pull_request) Successful in 28s
All checks were successful
eden-license / license-header (pull_request) Successful in 28s
Signed-off-by: lizzie <lizzie@eden-emu.dev>
This commit is contained in:
parent
47b703067e
commit
0bce00edc2
1 changed files with 20 additions and 17 deletions
|
@ -1,3 +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: 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
|
||||||
|
@ -15,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
|
||||||
|
@ -90,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(truncated, 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
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue