1
0
mirror of https://github.com/wolfpld/tracy.git synced 2025-03-20 07:40:02 +08:00

SetThreadName() only works on the current thread.

This breaking change is required, because kernel trace facilities use
kernel thread ids, which are inaccessible from the pthread_t level.
This commit is contained in:
Bartosz Taudul
2019-08-14 02:22:45 +02:00
parent 339b7fd2a6
commit 92b6da7cc2
6 changed files with 34 additions and 71 deletions
+7 -40
View File
@@ -26,6 +26,7 @@
#endif
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include "TracySystem.hpp"
@@ -34,21 +35,6 @@
# include "TracyAlloc.hpp"
#endif
#ifdef __CYGWIN__
class stub1 // verifyable_object
{
public:
uint32_t x;
virtual ~stub1();
};
class stub2 : public stub1 // pthread
{
public:
HANDLE hnd;
virtual ~stub2();
};
#endif
namespace tracy
{
@@ -63,18 +49,13 @@ TRACY_API std::atomic<ThreadNameData*>& GetThreadNameData();
TRACY_API void InitRPMallocThread();
#endif
void SetThreadName( std::thread& thread, const char* name )
{
SetThreadName( thread.native_handle(), name );
}
void SetThreadName( std::thread::native_handle_type handle, const char* name )
void SetThreadName( const char* name )
{
#if defined _WIN32 && !defined PTW32_VERSION && !defined __WINPTHREADS_VERSION
# if defined NTDDI_WIN10_RS2 && NTDDI_VERSION >= NTDDI_WIN10_RS2
wchar_t buf[256];
mbstowcs( buf, name, 256 );
SetThreadDescription( static_cast<HANDLE>( handle ), buf );
SetThreadDescription( GetCurrentThread(), buf );
# else
const DWORD MS_VC_EXCEPTION=0x406D1388;
# pragma pack( push, 8 )
@@ -87,7 +68,7 @@ void SetThreadName( std::thread::native_handle_type handle, const char* name )
};
# pragma pack(pop)
DWORD ThreadId = GetThreadId( static_cast<HANDLE>( handle ) );
DWORD ThreadId = GetCurrentThreadId();
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = name;
@@ -107,14 +88,14 @@ void SetThreadName( std::thread::native_handle_type handle, const char* name )
const auto sz = strlen( name );
if( sz <= 15 )
{
pthread_setname_np( handle, name );
pthread_setname_np( pthread_self(), name );
}
else
{
char buf[16];
memcpy( buf, name, 15 );
buf[15] = '\0';
pthread_setname_np( handle, buf );
pthread_setname_np( pthread_self(), buf );
}
}
#endif
@@ -126,21 +107,7 @@ void SetThreadName( std::thread::native_handle_type handle, const char* name )
memcpy( buf, name, sz );
buf[sz+1] = '\0';
auto data = (ThreadNameData*)tracy_malloc( sizeof( ThreadNameData ) );
# ifdef _WIN32
# if defined PTW32_VERSION
data->id = pthread_getw32threadid_np( static_cast<pthread_t>( handle ) );
# elif defined __WINPTHREADS_VERSION
data->id = GetThreadId( pthread_gethandle( static_cast<pthread_t>( handle ) ) );
# else
data->id = GetThreadId( static_cast<HANDLE>( handle ) );
# endif
# elif defined __APPLE__
pthread_threadid_np( handle, &data->id );
# elif defined __CYGWIN__
data->id = GetThreadId( ((stub2*)handle)->hnd );
# else
data->id = (uint64_t)handle;
# endif
data->id = detail::GetThreadHandleImpl();
data->name = buf;
data->next = GetThreadNameData().load( std::memory_order_relaxed );
while( !GetThreadNameData().compare_exchange_weak( data->next, data, std::memory_order_release, std::memory_order_relaxed ) ) {}
+3 -5
View File
@@ -7,7 +7,7 @@
# endif
#endif
#ifdef _WIN32
#if defined _WIN32 || defined __CYGWIN__
# ifndef _WINDOWS_
extern "C" __declspec(dllimport) unsigned long __stdcall GetCurrentThreadId(void);
# endif
@@ -23,7 +23,6 @@ extern "C" __declspec(dllimport) unsigned long __stdcall GetCurrentThreadId(void
#endif
#include <stdint.h>
#include <thread>
#include "TracyApi.h"
@@ -34,7 +33,7 @@ namespace detail
{
static inline uint64_t GetThreadHandleImpl()
{
#ifdef _WIN32
#if defined _WIN32 || defined __CYGWIN__
static_assert( sizeof( decltype( GetCurrentThreadId() ) ) <= sizeof( uint64_t ), "Thread handle too big to fit in protocol" );
return uint64_t( GetCurrentThreadId() );
#elif defined __APPLE__
@@ -61,8 +60,7 @@ static inline uint64_t GetThreadHandle()
}
#endif
void SetThreadName( std::thread& thread, const char* name );
void SetThreadName( std::thread::native_handle_type handle, const char* name );
void SetThreadName( const char* name );
const char* GetThreadName( uint64_t id );
}