TracySystem.hpp (1861B)
1 #ifndef __TRACYSYSTEM_HPP__ 2 #define __TRACYSYSTEM_HPP__ 3 4 #if defined _WIN32 || defined __CYGWIN__ 5 # ifndef _WINDOWS_ 6 extern "C" __declspec(dllimport) unsigned long __stdcall GetCurrentThreadId(void); 7 # endif 8 #elif defined __APPLE__ || ( !defined __ANDROID__ && !defined __linux__ ) 9 # include <pthread.h> 10 #endif 11 12 #ifdef __linux__ 13 # include <unistd.h> 14 # ifdef __ANDROID__ 15 # include <sys/types.h> 16 # else 17 # include <sys/syscall.h> 18 # endif 19 #elif defined __FreeBSD__ 20 # include <sys/thr.h> 21 #elif defined __NetBSD__ || defined __DragonFly__ 22 # include <sys/lwp.h> 23 #elif defined __OpenBSD__ 24 # include <unistd.h> 25 #endif 26 27 #include <stdint.h> 28 29 #include "TracyApi.h" 30 31 namespace tracy 32 { 33 34 namespace detail 35 { 36 static inline uint64_t GetThreadHandleImpl() 37 { 38 #if defined _WIN32 || defined __CYGWIN__ 39 static_assert( sizeof( decltype( GetCurrentThreadId() ) ) <= sizeof( uint64_t ), "Thread handle too big to fit in protocol" ); 40 return uint64_t( GetCurrentThreadId() ); 41 #elif defined __APPLE__ 42 uint64_t id; 43 pthread_threadid_np( pthread_self(), &id ); 44 return id; 45 #elif defined __ANDROID__ 46 return (uint64_t)gettid(); 47 #elif defined __linux__ 48 return (uint64_t)syscall( SYS_gettid ); 49 #elif defined __FreeBSD__ 50 long id; 51 thr_self( &id ); 52 return id; 53 #elif defined __NetBSD__ 54 return _lwp_self(); 55 #elif defined __DragonFly__ 56 return lwp_gettid(); 57 #elif defined __OpenBSD__ 58 return getthrid(); 59 #else 60 static_assert( sizeof( decltype( pthread_self() ) ) <= sizeof( uint64_t ), "Thread handle too big to fit in protocol" ); 61 return uint64_t( pthread_self() ); 62 #endif 63 } 64 } 65 66 #ifdef TRACY_ENABLE 67 TRACY_API uint64_t GetThreadHandle(); 68 #else 69 static inline uint64_t GetThreadHandle() 70 { 71 return detail::GetThreadHandleImpl(); 72 } 73 #endif 74 75 void SetThreadName( const char* name ); 76 const char* GetThreadName( uint64_t id ); 77 78 } 79 80 #endif