| 1 | #include "../common/TracyAlloc.hpp" |
| 2 | |
| 3 | #ifdef TRACY_USE_RPMALLOC |
| 4 | |
| 5 | #include <atomic> |
| 6 | |
| 7 | #include "../common/TracyForceInline.hpp" |
| 8 | #include "../common/TracyYield.hpp" |
| 9 | |
| 10 | namespace tracy |
| 11 | { |
| 12 | |
| 13 | extern thread_local bool RpThreadInitDone; |
| 14 | extern std::atomic<int> RpInitDone; |
| 15 | extern std::atomic<int> RpInitLock; |
| 16 | |
| 17 | tracy_no_inline static void InitRpmallocPlumbing() |
| 18 | { |
| 19 | const auto done = RpInitDone.load( std::memory_order_acquire ); |
| 20 | if( !done ) |
| 21 | { |
| 22 | int expected = 0; |
| 23 | while( !RpInitLock.compare_exchange_weak( expected, 1, std::memory_order_release, std::memory_order_relaxed ) ) { expected = 0; YieldThread(); } |
| 24 | const auto done = RpInitDone.load( std::memory_order_acquire ); |
| 25 | if( !done ) |
| 26 | { |
| 27 | rpmalloc_initialize(); |
| 28 | RpInitDone.store( 1, std::memory_order_release ); |
| 29 | } |
| 30 | RpInitLock.store( 0, std::memory_order_release ); |
| 31 | } |
| 32 | rpmalloc_thread_initialize(); |
| 33 | RpThreadInitDone = true; |
| 34 | } |
| 35 | |
| 36 | TRACY_API void InitRpmalloc() |
| 37 | { |
| 38 | if( !RpThreadInitDone ) InitRpmallocPlumbing(); |
| 39 | } |
| 40 | |
| 41 | } |
| 42 | |
| 43 | #endif |