mirror of
https://github.com/ran-j/PS2Recomp.git
synced 2026-09-26 16:59:35 -04:00
e567c4cf60
feat: improve function name sanitization
54 lines
1.3 KiB
C++
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
|
|
}
|
|
}
|