From 3da84d1579722e77aacedf95276f78874e14d41f Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Mon, 31 May 2021 22:38:22 +0200 Subject: [PATCH] Hide rpmalloc init behind thread local boolean. --- TracyClient.cpp | 1 + client/TracyAlloc.cpp | 35 +++++++++++++++++++++++++++++++++++ client/TracyProfiler.cpp | 2 ++ common/TracyAlloc.hpp | 27 ++++----------------------- 4 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 client/TracyAlloc.cpp diff --git a/TracyClient.cpp b/TracyClient.cpp index 838c3bd8..67f99cd1 100644 --- a/TracyClient.cpp +++ b/TracyClient.cpp @@ -27,6 +27,7 @@ #include "common/TracySocket.cpp" #include "client/tracy_rpmalloc.cpp" #include "client/TracyDxt1.cpp" +#include "client/TracyAlloc.cpp" #if TRACY_HAS_CALLSTACK == 2 || TRACY_HAS_CALLSTACK == 3 || TRACY_HAS_CALLSTACK == 4 || TRACY_HAS_CALLSTACK == 6 # include "libbacktrace/alloc.cpp" diff --git a/client/TracyAlloc.cpp b/client/TracyAlloc.cpp new file mode 100644 index 00000000..eb94dd8d --- /dev/null +++ b/client/TracyAlloc.cpp @@ -0,0 +1,35 @@ +#ifdef TRACY_ENABLE + +#include + +#include "../common/TracyAlloc.hpp" +#include "../common/TracyYield.hpp" + +namespace tracy +{ + +extern std::atomic RpInitDone; +extern std::atomic RpInitLock; + +TRACY_API void InitRpmallocPlumbing() +{ + const auto done = RpInitDone.load( std::memory_order_acquire ); + if( !done ) + { + int expected = 0; + while( !RpInitLock.compare_exchange_weak( expected, 1, std::memory_order_release, std::memory_order_relaxed ) ) { expected = 0; YieldThread(); } + const auto done = RpInitDone.load( std::memory_order_acquire ); + if( !done ) + { + rpmalloc_initialize(); + RpInitDone.store( 1, std::memory_order_release ); + } + RpInitLock.store( 0, std::memory_order_release ); + } + rpmalloc_thread_initialize(); + RpThreadInitDone = true; +} + +} + +#endif diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index 128d42d6..6a722cd1 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -977,6 +977,7 @@ TRACY_API void ShutdownProfiler() # else std::atomic RpInitDone { 0 }; std::atomic RpInitLock { 0 }; +thread_local bool RpThreadInitDone = false; static std::atomic profilerDataLock { 0 }; static std::atomic profilerData { nullptr }; @@ -1097,6 +1098,7 @@ thread_local ThreadHandleWrapper init_order(104) s_threadHandle { detail::GetThr static InitTimeWrapper init_order(101) s_initTime { SetupHwTimer() }; std::atomic init_order(102) RpInitDone( 0 ); std::atomic init_order(102) RpInitLock( 0 ); +thread_local bool RpThreadInitDone = false; moodycamel::ConcurrentQueue init_order(103) s_queue( QueuePrealloc ); std::atomic init_order(104) s_lockCounter( 0 ); std::atomic init_order(104) s_gpuCtxCounter( 0 ); diff --git a/common/TracyAlloc.hpp b/common/TracyAlloc.hpp index 1981c09a..27861c97 100644 --- a/common/TracyAlloc.hpp +++ b/common/TracyAlloc.hpp @@ -4,9 +4,8 @@ #include #ifdef TRACY_ENABLE -# include +# include "TracyApi.h" # include "TracyForceInline.hpp" -# include "TracyYield.hpp" # include "../client/tracy_rpmalloc.hpp" #endif @@ -14,30 +13,12 @@ namespace tracy { #ifdef TRACY_ENABLE -extern std::atomic RpInitDone; -extern std::atomic RpInitLock; - -namespace -{ -static inline void InitRpmallocPlumbing() -{ - int expected = 0; - while( !RpInitLock.compare_exchange_weak( expected, 1, std::memory_order_release, std::memory_order_relaxed ) ) { expected = 0; YieldThread(); } - const auto done = RpInitDone.load( std::memory_order_acquire ); - if( !done ) - { - rpmalloc_initialize(); - RpInitDone.store( 1, std::memory_order_release ); - } - RpInitLock.store( 0, std::memory_order_release ); -} +extern thread_local bool RpThreadInitDone; +TRACY_API void InitRpmallocPlumbing(); static tracy_force_inline void InitRpmalloc() { - const auto done = RpInitDone.load( std::memory_order_acquire ); - if( !done ) InitRpmallocPlumbing(); - rpmalloc_thread_initialize(); -} + if( !RpThreadInitDone ) InitRpmallocPlumbing(); } #endif