2017-09-10 15:46:20 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
#else
|
|
|
|
# include <pthread.h>
|
2017-09-22 19:48:21 +00:00
|
|
|
# include <string.h>
|
2017-09-10 15:46:20 +00:00
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2017-09-25 19:13:59 +00:00
|
|
|
#include <inttypes.h>
|
2017-09-10 15:46:20 +00:00
|
|
|
|
|
|
|
#include "TracySystem.hpp"
|
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
|
|
|
void SetThreadName( std::thread& thread, const char* name )
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2017-09-21 22:37:28 +00:00
|
|
|
# ifdef NTDDI_WIN10_RS2
|
2017-09-21 20:54:44 +00:00
|
|
|
wchar_t buf[256];
|
|
|
|
mbstowcs( buf, name, 256 );
|
|
|
|
SetThreadDescription( static_cast<HANDLE>( thread.native_handle() ), buf );
|
2017-09-21 22:37:28 +00:00
|
|
|
# else
|
|
|
|
const DWORD MS_VC_EXCEPTION=0x406D1388;
|
|
|
|
# pragma pack( push, 8 )
|
|
|
|
struct THREADNAME_INFO
|
|
|
|
{
|
|
|
|
DWORD dwType;
|
|
|
|
LPCSTR szName;
|
|
|
|
DWORD dwThreadID;
|
|
|
|
DWORD dwFlags;
|
|
|
|
};
|
|
|
|
# pragma pack(pop)
|
|
|
|
|
|
|
|
DWORD ThreadId = GetThreadId( static_cast<HANDLE>( thread.native_handle() ) );
|
|
|
|
THREADNAME_INFO info;
|
|
|
|
info.dwType = 0x1000;
|
|
|
|
info.szName = name;
|
|
|
|
info.dwThreadID = ThreadId;
|
|
|
|
info.dwFlags = 0;
|
|
|
|
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(ULONG_PTR), (ULONG_PTR*)&info );
|
|
|
|
}
|
|
|
|
__except(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
# endif
|
2017-09-10 15:46:20 +00:00
|
|
|
#else
|
2017-09-22 19:45:35 +00:00
|
|
|
const auto sz = strlen( name );
|
|
|
|
if( sz <= 15 )
|
|
|
|
{
|
|
|
|
pthread_setname_np( thread.native_handle(), name );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char buf[16];
|
|
|
|
memcpy( buf, name, 15 );
|
|
|
|
buf[15] = '\0';
|
2017-09-22 19:48:21 +00:00
|
|
|
pthread_setname_np( thread.native_handle(), buf );
|
2017-09-22 19:45:35 +00:00
|
|
|
}
|
2017-09-10 15:46:20 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-09-25 19:13:59 +00:00
|
|
|
const char* GetThreadName( uint64_t id )
|
|
|
|
{
|
|
|
|
static char buf[256];
|
2017-09-25 22:42:47 +00:00
|
|
|
#ifdef _WIN32
|
2017-09-25 19:13:59 +00:00
|
|
|
# ifdef NTDDI_WIN10_RS2
|
|
|
|
auto hnd = OpenThread( THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)id );
|
|
|
|
PWSTR tmp;
|
|
|
|
GetThreadDescription( hnd, &tmp );
|
|
|
|
auto ret = wcstombs( buf, tmp, 256 );
|
|
|
|
CloseHandle( hnd );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
if( pthread_getname_np( (pthread_t)id, buf, 256 ) == 0 )
|
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
sprintf( buf, "%" PRIu64, id );
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:48:21 +00:00
|
|
|
}
|