From 8ce41b3543a187b4634e13b2a0741f28d14fea53 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Wed, 26 Jun 2019 19:32:52 +0200 Subject: [PATCH] Proper init order of thread local thread handle. --- client/TracyProfiler.cpp | 9 +++++++++ client/TracyProfiler.hpp | 1 + common/TracySystem.hpp | 7 +------ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index 6384ae30..ec3c4b07 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -142,6 +142,11 @@ struct ProducerWrapper { tracy::moodycamel::ConcurrentQueue::ExplicitProducer* ptr; }; + +struct ThreadHandleWrapper +{ + uint64_t val; +}; #endif @@ -855,6 +860,7 @@ TRACY_API int64_t GetInitTime() { return GetProfilerData().initTime; } TRACY_API std::atomic& GetLockCounter() { return GetProfilerData().lockCounter; } TRACY_API std::atomic& GetGpuCtxCounter() { return GetProfilerData().gpuCtxCounter; } TRACY_API GpuCtxWrapper& GetGpuCtx() { return GetProfilerThreadData().gpuCtx; } +TRACY_API uint64_t GetThreadHandle() { return detail::GetThreadHandleImpl(); } # ifdef TRACY_COLLECT_THREAD_NAMES TRACY_API std::atomic& GetThreadNameData() { return GetProfilerData().threadNameData; } @@ -880,6 +886,7 @@ thread_local RPMallocThreadInit init_order(106) s_rpmalloc_thread_init; // 2. If these variables would be in the .CRT$XCB section, they would be initialized only in main thread. thread_local moodycamel::ProducerToken init_order(107) s_token_detail( s_queue ); thread_local ProducerWrapper init_order(108) s_token { s_queue.get_explicit_producer( s_token_detail ) }; +thread_local ThreadHandleWrapper init_order(104) s_threadHandle { detail::GetThreadHandleImpl() }; # ifdef _MSC_VER // 1. Initialize these static variables before all other variables. @@ -914,6 +921,7 @@ TRACY_API int64_t GetInitTime() { return s_initTime.val; } TRACY_API std::atomic& GetLockCounter() { return s_lockCounter; } TRACY_API std::atomic& GetGpuCtxCounter() { return s_gpuCtxCounter; } TRACY_API GpuCtxWrapper& GetGpuCtx() { return s_gpuCtx; } +TRACY_API uint64_t GetThreadHandle() { return s_threadHandle.val; } # ifdef TRACY_COLLECT_THREAD_NAMES TRACY_API std::atomic& GetThreadNameData() { return s_threadNameData; } @@ -962,6 +970,7 @@ Profiler::Profiler() // 3. But these variables need to be initialized in main thread within the .CRT$XCB section. Do it here. s_token_detail = moodycamel::ProducerToken( s_queue ); s_token = ProducerWrapper { s_queue.get_explicit_producer( s_token_detail ) }; + s_threadHandle = ThreadHandleWrapper { m_mainThread }; # endif #endif diff --git a/client/TracyProfiler.hpp b/client/TracyProfiler.hpp index 6c65a6dc..cb4ed20c 100644 --- a/client/TracyProfiler.hpp +++ b/client/TracyProfiler.hpp @@ -59,6 +59,7 @@ TRACY_API Profiler& GetProfiler(); TRACY_API std::atomic& GetLockCounter(); TRACY_API std::atomic& GetGpuCtxCounter(); TRACY_API GpuCtxWrapper& GetGpuCtx(); +TRACY_API uint64_t GetThreadHandle(); TRACY_API void InitRPMallocThread(); diff --git a/common/TracySystem.hpp b/common/TracySystem.hpp index cdb051d7..6cdc7fe2 100644 --- a/common/TracySystem.hpp +++ b/common/TracySystem.hpp @@ -42,12 +42,7 @@ static inline uint64_t GetThreadHandleImpl() } #ifdef TRACY_ENABLE -const thread_local auto s_threadHandle = detail::GetThreadHandleImpl(); - -static inline uint64_t GetThreadHandle() -{ - return s_threadHandle; -} +TRACY_API uint64_t GetThreadHandle(); #else static inline uint64_t GetThreadHandle() {