1
0
mirror of https://github.com/wolfpld/tracy synced 2025-04-30 04:43:53 +00:00

Allow setting thread name using thread handle.

This commit is contained in:
Bartosz Taudul 2017-10-16 21:01:57 +02:00
parent 31fc2335dd
commit 1788408ceb
2 changed files with 10 additions and 4 deletions

View File

@ -14,12 +14,17 @@ namespace tracy
{ {
void SetThreadName( std::thread& thread, const char* name ) void SetThreadName( std::thread& thread, const char* name )
{
SetThreadName( thread.native_handle(), name );
}
void SetThreadName( std::thread::native_handle_type handle, const char* name )
{ {
#ifdef _WIN32 #ifdef _WIN32
# ifdef NTDDI_WIN10_RS2 # ifdef NTDDI_WIN10_RS2
wchar_t buf[256]; wchar_t buf[256];
mbstowcs( buf, name, 256 ); mbstowcs( buf, name, 256 );
SetThreadDescription( static_cast<HANDLE>( thread.native_handle() ), buf ); SetThreadDescription( static_cast<HANDLE>( handle ), buf );
# else # else
const DWORD MS_VC_EXCEPTION=0x406D1388; const DWORD MS_VC_EXCEPTION=0x406D1388;
# pragma pack( push, 8 ) # pragma pack( push, 8 )
@ -32,7 +37,7 @@ void SetThreadName( std::thread& thread, const char* name )
}; };
# pragma pack(pop) # pragma pack(pop)
DWORD ThreadId = GetThreadId( static_cast<HANDLE>( thread.native_handle() ) ); DWORD ThreadId = GetThreadId( static_cast<HANDLE>( handle ) );
THREADNAME_INFO info; THREADNAME_INFO info;
info.dwType = 0x1000; info.dwType = 0x1000;
info.szName = name; info.szName = name;
@ -51,14 +56,14 @@ void SetThreadName( std::thread& thread, const char* name )
const auto sz = strlen( name ); const auto sz = strlen( name );
if( sz <= 15 ) if( sz <= 15 )
{ {
pthread_setname_np( thread.native_handle(), name ); pthread_setname_np( handle, name );
} }
else else
{ {
char buf[16]; char buf[16];
memcpy( buf, name, 15 ); memcpy( buf, name, 15 );
buf[15] = '\0'; buf[15] = '\0';
pthread_setname_np( thread.native_handle(), buf ); pthread_setname_np( handle, buf );
} }
#endif #endif
} }

View File

@ -25,6 +25,7 @@ static inline uint64_t GetThreadHandle()
} }
void SetThreadName( std::thread& thread, const char* name ); void SetThreadName( std::thread& thread, const char* name );
void SetThreadName( std::thread::native_handle_type handle, const char* name );
const char* GetThreadName( uint64_t id ); const char* GetThreadName( uint64_t id );
} }