From 3c0ce01954570d4a4e3b147d082ac17dd1f4536c Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Wed, 27 Sep 2017 01:03:29 +0200 Subject: [PATCH] Simplify access to queue producer token. Note that calibration loop needs separate token, as the thread_local instance is created after the profiler (and its calibration loop). --- client/TracyProfiler.cpp | 53 +++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/client/TracyProfiler.cpp b/client/TracyProfiler.cpp index b92fa5aa..efe670c6 100755 --- a/client/TracyProfiler.cpp +++ b/client/TracyProfiler.cpp @@ -28,12 +28,7 @@ namespace tracy enum { QueuePrealloc = 256 * 1024 }; static moodycamel::ConcurrentQueue s_queue( QueueItemSize * QueuePrealloc ); - -static moodycamel::ProducerToken& GetToken() -{ - static thread_local moodycamel::ProducerToken token( s_queue ); - return token; -} +static thread_local moodycamel::ProducerToken s_token( s_queue ); static std::atomic s_id( 0 ); @@ -48,6 +43,26 @@ static Profiler s_profiler; static Profiler* s_instance = nullptr; +static inline uint64_t ZoneBeginImpl( moodycamel::ProducerToken& token, QueueZoneBegin&& data ) +{ + auto id = GetNewId(); + QueueItem item; + item.hdr.type = QueueType::ZoneBegin; + item.hdr.id = id; + item.zoneBegin = std::move( data ); + s_queue.enqueue( token, std::move( item ) ); + return id; +} + +static inline void ZoneEndImpl( moodycamel::ProducerToken& token, uint64_t id, QueueZoneEnd&& data ) +{ + QueueItem item; + item.hdr.type = QueueType::ZoneEnd; + item.hdr.id = id; + item.zoneEnd = std::move( data ); + s_queue.enqueue( token, std::move( item ) ); +} + Profiler::Profiler() : m_mainThread( GetThreadHandle() ) , m_shutdown( false ) @@ -80,22 +95,12 @@ Profiler::~Profiler() uint64_t Profiler::ZoneBegin( QueueZoneBegin&& data ) { - auto id = GetNewId(); - QueueItem item; - item.hdr.type = QueueType::ZoneBegin; - item.hdr.id = id; - item.zoneBegin = std::move( data ); - s_queue.enqueue( GetToken(), std::move( item ) ); - return id; + return ZoneBeginImpl( s_token, std::move( data ) ); } void Profiler::ZoneEnd( uint64_t id, QueueZoneEnd&& data ) { - QueueItem item; - item.hdr.type = QueueType::ZoneEnd; - item.hdr.id = id; - item.zoneEnd = std::move( data ); - s_queue.enqueue( GetToken(), std::move( item ) ); + ZoneEndImpl( s_token, id, std::move( data ) ); } void Profiler::FrameMark() @@ -103,7 +108,7 @@ void Profiler::FrameMark() QueueItem item; item.hdr.type = QueueType::FrameMark; item.hdr.id = (uint64_t)GetTime(); - s_queue.enqueue( GetToken(), std::move( item ) ); + s_queue.enqueue( s_token, std::move( item ) ); } bool Profiler::ShouldExit() @@ -226,7 +231,7 @@ void Profiler::SendSourceLocation( uint64_t ptr ) item.srcloc.function = (uint64_t)srcloc->function; item.srcloc.line = srcloc->line; item.srcloc.color = srcloc->color; - s_queue.enqueue( GetToken(), std::move( item ) ); + s_queue.enqueue( s_token, std::move( item ) ); } bool Profiler::HandleServerQuery() @@ -305,10 +310,13 @@ void Profiler::CalibrateDelay() enum { Iterations = 50000 }; enum { Events = Iterations * 2 }; // start + end static_assert( Events * 2 < QueuePrealloc, "Delay calibration loop will allocate memory in queue" ); + + moodycamel::ProducerToken ptoken( s_queue ); for( int i=0; i