From 866081bf29365785e48455205a270a48aed77999 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Tue, 17 Oct 2017 00:36:15 +0200 Subject: [PATCH] Initialize tracy before anything else. --- client/TracyProfiler.cpp | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index 7fe4e2f5..36aaf98c 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -69,17 +69,31 @@ static const char* GetProcessName() enum { QueuePrealloc = 256 * 1024 }; -static RPMallocInit init_order(101) s_rpmalloc_init; -static thread_local RPMallocThreadInit init_order(102) s_rpmalloc_thread_init; -static moodycamel::ConcurrentQueue init_order(103) s_queue( QueuePrealloc ); -static thread_local moodycamel::ProducerToken init_order(104) s_token_detail( s_queue ); -thread_local ProducerWrapper init_order(105) s_token { s_queue.get_explicit_producer( s_token_detail ) }; - -static Profiler init_order(106) s_profiler; +// MSVC static initialization order solution. gcc/clang uses init_order() to avoid all this. static Profiler* s_instance = nullptr; static Thread* s_thread = nullptr; +// 1a. But s_queue is needed for initialization of variables in point 2. +extern moodycamel::ConcurrentQueue s_queue; + +static thread_local RPMallocThreadInit init_order(104) s_rpmalloc_thread_init; + +// 2. If these variables would be in the .CRT$XCB section, they would be initialized only in main thread. +static thread_local moodycamel::ProducerToken init_order(105) s_token_detail( s_queue ); +thread_local ProducerWrapper init_order(106) s_token { s_queue.get_explicit_producer( s_token_detail ) }; + +#ifdef _MSC_VER +// 1. Initialize these static variables before all other variables. +# pragma warning( disable : 4075 ) +# pragma init_seg( ".CRT$XCB" ) +#endif + +static RPMallocInit init_order(101) s_rpmalloc_init; +moodycamel::ConcurrentQueue init_order(102) s_queue( QueuePrealloc ); +static Profiler init_order(103) s_profiler; + + Profiler::Profiler() : m_mainThread( GetThreadHandle() ) , m_epoch( std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch() ).count() ) @@ -91,6 +105,12 @@ Profiler::Profiler() assert( !s_instance ); s_instance = this; +#ifdef _MSC_VER + // 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 ) }; +#endif + CalibrateTimer(); CalibrateDelay(); uint32_t cpu;