Files
PS2Recomp/ps2xRuntime/include/ThreadNaming.h
T
Ranieri e567c4cf60 feat: added thread naming functionality (#44)
feat: improve function name sanitization
2026-02-05 02:07:49 -03:00

54 lines
1.3 KiB
C++

#pragma once
#include <string_view>
#include <string>
#if defined(_WIN32)
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WINAPI
#define WINAPI __stdcall
#endif
extern "C"
{
typedef void *HANDLE;
typedef void *HMODULE;
typedef const wchar_t *PCWSTR;
typedef long HRESULT;
__declspec(dllimport) HMODULE WINAPI GetModuleHandleW(const wchar_t *lpModuleName);
__declspec(dllimport) void *WINAPI GetProcAddress(HMODULE hModule, const char *lpProcName);
__declspec(dllimport) HANDLE WINAPI GetCurrentThread(void);
}
#elif defined(__APPLE__) || defined(__linux__)
#include <pthread.h>
#endif
namespace ThreadNaming
{
inline void SetCurrentThreadName(std::string_view name)
{
#if defined(_WIN32)
using SetThreadDescriptionFn = HRESULT(WINAPI*)(HANDLE, PCWSTR);
HMODULE kernel32 = ::GetModuleHandleW(L"Kernel32.dll");
auto setThreadDescription =
reinterpret_cast<SetThreadDescriptionFn>(::GetProcAddress(kernel32, "SetThreadDescription"));
if (setThreadDescription)
{
std::wstring wname(name.begin(), name.end());
setThreadDescription(::GetCurrentThread(), wname.c_str());
}
#elif defined(__APPLE__)
pthread_setname_np(name.data());
#elif defined(__linux__)
pthread_setname_np(pthread_self(), name.data());
#endif
}
}