fix: Use a thread-local storage for thread names instead

This commit is contained in:
WerWolv 2024-01-09 16:16:20 +01:00
parent 929b5176ce
commit d5c8021b41
1 changed files with 3 additions and 20 deletions

View File

@ -365,7 +365,9 @@ namespace hex {
s_tasksFinishedCallbacks.push_back(function); s_tasksFinishedCallbacks.push_back(function);
} }
thread_local static std::string s_currentThreadName;
void TaskManager::setCurrentThreadName(const std::string &name) { void TaskManager::setCurrentThreadName(const std::string &name) {
s_currentThreadName = name;
#if defined(OS_WINDOWS) #if defined(OS_WINDOWS)
using SetThreadDescriptionFunc = HRESULT(WINAPI*)(HANDLE hThread, PCWSTR lpThreadDescription); using SetThreadDescriptionFunc = HRESULT(WINAPI*)(HANDLE hThread, PCWSTR lpThreadDescription);
@ -408,26 +410,7 @@ namespace hex {
} }
std::string TaskManager::getCurrentThreadName() { std::string TaskManager::getCurrentThreadName() {
#if defined(OS_WINDOWS) return s_currentThreadName;
PWSTR name;
if (SUCCEEDED(::GetThreadDescription(::GetCurrentThread(), &name))) {
auto utf8Name = hex::utf16ToUtf8(name);
LocalFree(name);
return utf8Name;
}
return "";
#elif defined(OS_MACOS) || defined(OS_LINUX)
std::array<char, 256> name = { };
pthread_getname_np(pthread_self(), name.data(), name.size());
return name.data();
#elif defined(OS_WEB)
return "";
#else
return "";
#endif
} }