From 5110d55f1748ac1b7da952667e2870d300c10cfe Mon Sep 17 00:00:00 2001 From: Sherief Farouk Date: Sun, 28 Oct 2018 18:55:55 -0700 Subject: [PATCH] Fix for using Tracy with multithreaded NT loader in Windows 10 RS5 (Issue #26) [Take 2]. --- client/TracyProfiler.cpp | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index 07973756..71facb47 100644 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -56,7 +56,7 @@ #if defined _MSC_VER || defined __CYGWIN__ # include extern "C" typedef LONG (WINAPI *t_RtlGetVersion)( PRTL_OSVERSIONINFOW ); -# define TRACY_USE_INITONCE (1) +# define TRACY_USE_INITONCE #else # include # include @@ -72,18 +72,6 @@ extern "C" typedef LONG (WINAPI *t_RtlGetVersion)( PRTL_OSVERSIONINFOW ); namespace tracy { -struct RPMallocInit -{ - RPMallocInit() - { - #if defined TRACY_USE_INITONCE - rpmalloc_thread_initialize(); - #else - rpmalloc_initialize(); - #endif //if defined TRACY_USE_INITONCE - } -}; - #if defined TRACY_USE_INITONCE namespace { @@ -95,17 +83,35 @@ namespace rpmalloc_initialize(); return TRUE; } + + INIT_ONCE InitOnce = INIT_ONCE_STATIC_INIT; } #endif //if defined TRACY_USE_INITONCE +struct RPMallocInit +{ + RPMallocInit() + { +#if defined TRACY_USE_INITONCE + InitOnceExecuteOnce(&InitOnce, InitOnceCallback, nullptr, nullptr); + //We must call rpmalloc_thread_initialize() explicitly here since the InitOnceCallback might + //not be called on this thread if another thread has executed it earlier. + rpmalloc_thread_initialize(); +#else + rpmalloc_initialize(); +#endif //if defined TRACY_USE_INITONCE + } +}; + + + struct RPMallocThreadInit { RPMallocThreadInit() { - #if defined TRACY_USE_INITONCE - static INIT_ONCE InitOnce = INIT_ONCE_STATIC_INIT; +#if defined TRACY_USE_INITONCE InitOnceExecuteOnce(&InitOnce, InitOnceCallback, nullptr, nullptr); - #endif //if defined TRACY_USE_INITONCE +#endif //if defined TRACY_USE_INITONCE rpmalloc_thread_initialize(); } };