Merge TracyThread.hpp to TracySystem.cpp.

Keeping threading functions inside a source file prevents poisoning by
including windows.h.
This commit is contained in:
Bartosz Taudul 2017-09-25 21:13:59 +02:00
parent 0d24a2739d
commit 206305fbd2
5 changed files with 41 additions and 57 deletions

View File

@ -21,7 +21,6 @@
#include "concurrentqueue.h"
#include "TracyScoped.hpp"
#include "TracyProfiler.hpp"
#include "TracyThread.hpp"
#ifdef _DEBUG
# define DISABLE_LZ4

View File

@ -3,8 +3,8 @@
#include <stdint.h>
#include "../common/TracySystem.hpp"
#include "TracyProfiler.hpp"
#include "TracyThread.hpp"
namespace tracy
{

View File

@ -1,55 +0,0 @@
#ifndef __TRACYTHREAD_HPP__
#define __TRACYTHREAD_HPP__
#include <inttypes.h>
#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
}
static inline const char* GetThreadName( uint64_t id )
{
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
sprintf( buf, "%" PRIu64, id );
return buf;
}
}
#endif

View File

@ -6,12 +6,24 @@
# include <unistd.h>
#endif
#include <inttypes.h>
#include "TracySystem.hpp"
namespace tracy
{
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
}
void SetThreadName( std::thread& thread, const char* name )
{
#ifdef _WIN32
@ -62,4 +74,29 @@ void SetThreadName( std::thread& thread, const char* name )
#endif
}
const char* GetThreadName( uint64_t id )
{
static char buf[256];
#ifdef _MSC_VER
# 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;
}
}

View File

@ -1,12 +1,15 @@
#ifndef __TRACYSYSTEM_HPP__
#define __TRACYSYSTEM_HPP__
#include <stdint.h>
#include <thread>
namespace tracy
{
uint64_t GetThreadHandle();
void SetThreadName( std::thread& thread, const char* name );
const char* GetThreadName( uint64_t id );
}