From 0d2e7f72a740b6a62f509295ceb472035e32737a Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Tue, 11 Feb 2020 14:40:49 +0100 Subject: [PATCH] Move GetThreadHandleImpl() from header to source file. This removes dependency on unistd.h header (required for syscall() on linux), which includes a definition of getopt(), which may conflict with a custom getopt implementation (for example, one that does work on windows). --- common/TracySystem.cpp | 45 ++++++++++++++++++++++++++++++++++-- common/TracySystem.hpp | 52 +----------------------------------------- 2 files changed, 44 insertions(+), 53 deletions(-) diff --git a/common/TracySystem.cpp b/common/TracySystem.cpp index 0df230c8..0e5480d1 100644 --- a/common/TracySystem.cpp +++ b/common/TracySystem.cpp @@ -18,10 +18,16 @@ #endif #ifdef __linux__ -# ifndef __ANDROID__ -# include +# ifdef __ANDROID__ +# include +# else +# include # endif # include +#elif defined __FreeBSD__ +# include +#elif defined __NetBSD__ || defined __DragonFly__ +# include #endif #ifdef __MINGW32__ @@ -41,6 +47,41 @@ namespace tracy { +namespace detail +{ + +TRACY_API uint64_t GetThreadHandleImpl() +{ +#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__ + uint64_t id; + pthread_threadid_np( pthread_self(), &id ); + return id; +#elif defined __ANDROID__ + return (uint64_t)gettid(); +#elif defined __linux__ + return (uint64_t)syscall( SYS_gettid ); +#elif defined __FreeBSD__ + long id; + thr_self( &id ); + return id; +#elif defined __NetBSD__ + return _lwp_self(); +#elif defined __DragonFly__ + return lwp_gettid(); +#elif defined __OpenBSD__ + return getthrid(); +#else + static_assert( sizeof( decltype( pthread_self() ) ) <= sizeof( uint64_t ), "Thread handle too big to fit in protocol" ); + return uint64_t( pthread_self() ); +#endif + +} + +} + #ifdef TRACY_ENABLE struct ThreadNameData { diff --git a/common/TracySystem.hpp b/common/TracySystem.hpp index be147b06..2f539c2e 100644 --- a/common/TracySystem.hpp +++ b/common/TracySystem.hpp @@ -1,29 +1,6 @@ #ifndef __TRACYSYSTEM_HPP__ #define __TRACYSYSTEM_HPP__ -#if defined _WIN32 || defined __CYGWIN__ -# ifndef _WINDOWS_ -extern "C" __declspec(dllimport) unsigned long __stdcall GetCurrentThreadId(void); -# endif -#elif defined __APPLE__ || ( !defined __ANDROID__ && !defined __linux__ ) -# include -#endif - -#ifdef __linux__ -# include -# ifdef __ANDROID__ -# include -# else -# include -# endif -#elif defined __FreeBSD__ -# include -#elif defined __NetBSD__ || defined __DragonFly__ -# include -#elif defined __OpenBSD__ -# include -#endif - #include #include "TracyApi.h" @@ -33,34 +10,7 @@ namespace tracy namespace detail { -static inline uint64_t GetThreadHandleImpl() -{ -#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__ - uint64_t id; - pthread_threadid_np( pthread_self(), &id ); - return id; -#elif defined __ANDROID__ - return (uint64_t)gettid(); -#elif defined __linux__ - return (uint64_t)syscall( SYS_gettid ); -#elif defined __FreeBSD__ - long id; - thr_self( &id ); - return id; -#elif defined __NetBSD__ - return _lwp_self(); -#elif defined __DragonFly__ - return lwp_gettid(); -#elif defined __OpenBSD__ - return getthrid(); -#else - static_assert( sizeof( decltype( pthread_self() ) ) <= sizeof( uint64_t ), "Thread handle too big to fit in protocol" ); - return uint64_t( pthread_self() ); -#endif -} +TRACY_API uint64_t GetThreadHandleImpl(); } #ifdef TRACY_ENABLE