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
|
|
|
|
|
2018-07-14 11:26:55 +00:00
|
|
|
#ifdef __linux__
|
2018-07-24 11:43:25 +00:00
|
|
|
# ifndef __ANDROID__
|
|
|
|
# include <syscall.h>
|
|
|
|
# endif
|
|
|
|
# include <fcntl.h>
|
2018-07-14 11:26:55 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-25 19:13:59 +00:00
|
|
|
#include <inttypes.h>
|
2017-10-30 20:51:24 +00:00
|
|
|
#include <stdio.h>
|
2017-09-10 15:46:20 +00:00
|
|
|
|
|
|
|
#include "TracySystem.hpp"
|
|
|
|
|
2017-10-30 19:45:00 +00:00
|
|
|
#ifdef TRACY_COLLECT_THREAD_NAMES
|
|
|
|
# include <atomic>
|
2017-11-02 11:57:02 +00:00
|
|
|
# include "TracyAlloc.hpp"
|
2017-10-30 19:45:00 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-10 15:46:20 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2017-10-30 19:45:00 +00:00
|
|
|
#ifdef TRACY_COLLECT_THREAD_NAMES
|
|
|
|
struct ThreadNameData
|
|
|
|
{
|
|
|
|
uint64_t id;
|
|
|
|
const char* name;
|
|
|
|
ThreadNameData* next;
|
|
|
|
};
|
2018-07-30 16:12:42 +00:00
|
|
|
extern std::atomic<ThreadNameData*>& s_threadNameData;
|
2017-10-30 19:45:00 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-10 15:46:20 +00:00
|
|
|
void SetThreadName( std::thread& thread, const char* name )
|
2017-10-16 19:01:57 +00:00
|
|
|
{
|
|
|
|
SetThreadName( thread.native_handle(), name );
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetThreadName( std::thread::native_handle_type handle, const char* name )
|
2017-09-10 15:46:20 +00:00
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2018-07-18 07:12:15 +00:00
|
|
|
# if defined NTDDI_WIN10_RS2 && NTDDI_VERSION >= NTDDI_WIN10_RS2
|
2017-09-21 20:54:44 +00:00
|
|
|
wchar_t buf[256];
|
|
|
|
mbstowcs( buf, name, 256 );
|
2017-10-16 19:01:57 +00:00
|
|
|
SetThreadDescription( static_cast<HANDLE>( 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)
|
|
|
|
|
2017-10-16 19:01:57 +00:00
|
|
|
DWORD ThreadId = GetThreadId( static_cast<HANDLE>( handle ) );
|
2017-09-21 22:37:28 +00:00
|
|
|
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-11-29 10:11:16 +00:00
|
|
|
#elif defined _GNU_SOURCE && !defined __EMSCRIPTEN__
|
2017-09-22 19:45:35 +00:00
|
|
|
{
|
2018-06-30 13:36:06 +00:00
|
|
|
const auto sz = strlen( name );
|
|
|
|
if( sz <= 15 )
|
|
|
|
{
|
|
|
|
pthread_setname_np( handle, name );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char buf[16];
|
|
|
|
memcpy( buf, name, 15 );
|
|
|
|
buf[15] = '\0';
|
|
|
|
pthread_setname_np( handle, buf );
|
|
|
|
}
|
2017-09-22 19:45:35 +00:00
|
|
|
}
|
2017-09-10 15:46:20 +00:00
|
|
|
#endif
|
2017-10-30 19:45:00 +00:00
|
|
|
#ifdef TRACY_COLLECT_THREAD_NAMES
|
|
|
|
{
|
2018-03-30 12:39:25 +00:00
|
|
|
rpmalloc_thread_initialize();
|
2018-06-30 13:36:06 +00:00
|
|
|
const auto sz = strlen( name );
|
|
|
|
char* buf = (char*)tracy_malloc( sz+1 );
|
|
|
|
memcpy( buf, name, sz );
|
|
|
|
buf[sz+1] = '\0';
|
2017-11-02 11:57:02 +00:00
|
|
|
auto data = (ThreadNameData*)tracy_malloc( sizeof( ThreadNameData ) );
|
2017-10-30 19:45:00 +00:00
|
|
|
# ifdef _WIN32
|
|
|
|
data->id = GetThreadId( static_cast<HANDLE>( handle ) );
|
2017-11-03 10:09:31 +00:00
|
|
|
# elif defined __APPLE__
|
2017-11-03 10:13:02 +00:00
|
|
|
pthread_threadid_np( handle, &data->id );
|
2017-10-30 19:45:00 +00:00
|
|
|
# else
|
|
|
|
data->id = (uint64_t)handle;
|
|
|
|
# endif
|
|
|
|
data->name = buf;
|
|
|
|
data->next = s_threadNameData.load( std::memory_order_relaxed );
|
|
|
|
while( !s_threadNameData.compare_exchange_weak( data->next, data, std::memory_order_release, std::memory_order_relaxed ) ) {}
|
|
|
|
}
|
|
|
|
#endif
|
2017-09-10 15:46:20 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 19:13:59 +00:00
|
|
|
const char* GetThreadName( uint64_t id )
|
|
|
|
{
|
|
|
|
static char buf[256];
|
2017-10-30 19:45:00 +00:00
|
|
|
#ifdef TRACY_COLLECT_THREAD_NAMES
|
|
|
|
auto ptr = s_threadNameData.load( std::memory_order_relaxed );
|
|
|
|
while( ptr )
|
|
|
|
{
|
|
|
|
if( ptr->id == id )
|
|
|
|
{
|
2018-03-30 23:38:57 +00:00
|
|
|
return ptr->name;
|
2017-10-30 19:45:00 +00:00
|
|
|
}
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# ifdef _WIN32
|
2018-07-18 07:12:15 +00:00
|
|
|
# if defined NTDDI_WIN10_RS2 && NTDDI_VERSION >= NTDDI_WIN10_RS2
|
2017-09-25 19:13:59 +00:00
|
|
|
auto hnd = OpenThread( THREAD_QUERY_LIMITED_INFORMATION, FALSE, (DWORD)id );
|
2017-10-15 11:26:05 +00:00
|
|
|
if( hnd != 0 )
|
2017-09-25 19:13:59 +00:00
|
|
|
{
|
2017-10-15 11:26:05 +00:00
|
|
|
PWSTR tmp;
|
|
|
|
GetThreadDescription( hnd, &tmp );
|
|
|
|
auto ret = wcstombs( buf, tmp, 256 );
|
|
|
|
CloseHandle( hnd );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
2017-09-25 19:13:59 +00:00
|
|
|
}
|
2017-10-30 19:45:00 +00:00
|
|
|
# endif
|
2018-07-14 11:26:55 +00:00
|
|
|
# elif defined __GLIBC__ && !defined __ANDROID__ && !defined __EMSCRIPTEN__
|
2017-09-25 19:13:59 +00:00
|
|
|
if( pthread_getname_np( (pthread_t)id, buf, 256 ) == 0 )
|
|
|
|
{
|
|
|
|
return buf;
|
|
|
|
}
|
2018-07-14 11:26:55 +00:00
|
|
|
# elif defined __linux__
|
|
|
|
int cs, fd;
|
|
|
|
char path[32];
|
2018-07-24 11:43:25 +00:00
|
|
|
# ifdef __ANDROID__
|
|
|
|
int tid = gettid();
|
|
|
|
# else
|
2018-07-14 11:46:25 +00:00
|
|
|
int tid = (int) syscall( SYS_gettid );
|
2018-07-24 11:43:25 +00:00
|
|
|
# endif
|
2018-07-14 11:46:25 +00:00
|
|
|
snprintf( path, sizeof( path ), "/proc/self/task/%d/comm", tid );
|
2018-07-14 11:26:55 +00:00
|
|
|
sprintf( buf, "%" PRIu64, id );
|
|
|
|
pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, &cs );
|
2018-07-14 11:46:25 +00:00
|
|
|
if ( ( fd = open( path, O_RDONLY ) ) > 0) {
|
2018-07-14 11:26:55 +00:00
|
|
|
int len = read( fd, buf, 255 );
|
2018-07-14 11:46:25 +00:00
|
|
|
if ( len > 0 )
|
2018-07-14 11:26:55 +00:00
|
|
|
buf[len] = 0;
|
2018-07-14 11:46:25 +00:00
|
|
|
close( fd );
|
2018-07-14 11:26:55 +00:00
|
|
|
}
|
2018-07-14 11:46:25 +00:00
|
|
|
pthread_setcancelstate( cs, 0 );
|
2018-07-14 11:26:55 +00:00
|
|
|
return buf;
|
2017-10-30 19:45:00 +00:00
|
|
|
# endif
|
2017-09-25 19:13:59 +00:00
|
|
|
#endif
|
|
|
|
sprintf( buf, "%" PRIu64, id );
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:48:21 +00:00
|
|
|
}
|