2017-09-21 23:11:14 +00:00
|
|
|
#ifndef __TRACYTHREAD_HPP__
|
|
|
|
#define __TRACYTHREAD_HPP__
|
|
|
|
|
2017-09-21 23:50:14 +00:00
|
|
|
#include <inttypes.h>
|
2017-09-21 23:11:14 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
|
|
|
static inline uint64_t GetThreadHandle()
|
|
|
|
{
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
static_assert( sizeof( decltype( GetCurrentThreadId() ) ) <= sizeof( uint64_t ), "Thread handle too big to fit in protocol" );
|
|
|
|
return uint64_t( GetCurrentThreadId() );
|
|
|
|
#else
|
|
|
|
static_assert( sizeof( decltype( pthread_self() ) ) <= sizeof( uint64_t ), "Thread handle too big to fit in protocol" );
|
|
|
|
return uint64_t( pthread_self() );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-09-21 23:50:14 +00:00
|
|
|
static inline const char* GetThreadName( uint64_t id )
|
|
|
|
{
|
2017-09-22 00:09:39 +00:00
|
|
|
static char buf[256];
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
# ifdef NTDDI_WIN10_RS2
|
|
|
|
auto hnd = OpenThread( THREAD_QUERY_LIMITED_INFORMATION, FALSE, 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
|
2017-09-21 23:50:14 +00:00
|
|
|
sprintf( buf, "%" PRIu64, id );
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-09-21 23:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|