Skip to content

Commit

Permalink
Implement set_current_thread_name on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
Themaister committed Feb 25, 2024
1 parent 8d3855d commit 2265b69
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions util/thread_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

#ifdef __linux__
#include <pthread.h>
#else
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <string>
#endif

namespace Util
Expand All @@ -33,8 +37,21 @@ void set_current_thread_name(const char *name)
#ifdef __linux__
pthread_setname_np(pthread_self(), name);
#else
// TODO: Kinda messy.
(void)name;
using PFN_SetThreadDescription = HRESULT (WINAPI *)(HANDLE, PCWSTR);
auto module = GetModuleHandleA("kernel32.dll");
PFN_SetThreadDescription SetThreadDescription = module ? reinterpret_cast<PFN_SetThreadDescription>(
(void *)GetProcAddress(module, "SetThreadDescription")) : nullptr;

if (SetThreadDescription)
{
std::wstring wname;
while (*name != '\0')
{
wname.push_back(*name);
name++;
}
SetThreadDescription(GetCurrentThread(), wname.c_str());
}
#endif
}
}

0 comments on commit 2265b69

Please sign in to comment.