Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions public/client/TracyProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,13 +909,15 @@ static Profiler* s_instance = nullptr;
static Thread* s_thread;
#ifndef TRACY_NO_FRAME_IMAGE
static Thread* s_compressThread;
std::atomic<bool> s_compressThreadGone { false };
#endif
#ifdef TRACY_HAS_CALLSTACK
static Thread* s_symbolThread;
std::atomic<bool> s_symbolThreadGone { false };
#endif
#ifdef TRACY_HAS_SYSTEM_TRACING
static std::atomic<Thread*> s_sysTraceThread(nullptr);
std::atomic<bool> s_sysTraceThreadGone { false };
#endif

#if defined __linux__ && !defined TRACY_NO_CRASH_HANDLER
Expand Down Expand Up @@ -1175,6 +1177,7 @@ static void StartSystemTracing( int64_t& samplingPeriod )
new( sysTraceThread ) Thread( SysTraceWorker, nullptr );
Thread* prev = s_sysTraceThread.exchange( sysTraceThread );
assert( prev == nullptr );
s_sysTraceThreadGone.store( false );
std::this_thread::sleep_for( std::chrono::milliseconds( 1 ) );
}
}
Expand All @@ -1185,6 +1188,7 @@ static void StopSystemTracing()
if( sysTraceThread )
{
SysTraceStop();
while( s_sysTraceThreadGone.load() == false ) { YieldThread(); }
sysTraceThread->~Thread();
tracy_free( sysTraceThread );
}
Expand Down Expand Up @@ -1617,11 +1621,13 @@ void Profiler::SpawnWorkerThreads()
#ifndef TRACY_NO_FRAME_IMAGE
s_compressThread = (Thread*)tracy_malloc( sizeof( Thread ) );
new(s_compressThread) Thread( LaunchCompressWorker, this );
s_compressThreadGone.store( false );
#endif

#ifdef TRACY_HAS_CALLSTACK
s_symbolThread = (Thread*)tracy_malloc( sizeof( Thread ) );
new(s_symbolThread) Thread( LaunchSymbolWorker, this );
s_symbolThreadGone.store( false );
#endif

#if defined _WIN32 && !defined TRACY_WIN32_NO_DESKTOP && !defined TRACY_NO_CRASH_HANDLER
Expand Down Expand Up @@ -1649,15 +1655,18 @@ Profiler::~Profiler()
#endif

#ifdef TRACY_HAS_CALLSTACK
while( s_symbolThreadGone.load() == false ) { YieldThread(); }
s_symbolThread->~Thread();
tracy_free( s_symbolThread );
#endif

#ifndef TRACY_NO_FRAME_IMAGE
while( s_compressThreadGone.load() == false ) { YieldThread(); }
s_compressThread->~Thread();
tracy_free( s_compressThread );
#endif

while( m_shutdownFinished.load() == false ) { YieldThread(); }
s_thread->~Thread();
tracy_free( s_thread );

Expand Down Expand Up @@ -2296,6 +2305,7 @@ void Profiler::CompressWorker()

if( shouldExit )
{
s_compressThreadGone.store( true, std::memory_order_release );
return;
}
}
Expand Down
1 change: 1 addition & 0 deletions public/client/TracySysTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,7 @@ void SysTraceWorker( void* ptr )

for( int i=0; i<numBuffers; i++ ) ringArray[i].~RingBuffer();
tracy_free_fast( ringArray );
s_sysTraceThreadGone.store( true, std::memory_order_release );
}

void SysTraceGetExternalName( uint64_t thread, const char*& threadName, const char*& name )
Expand Down
7 changes: 6 additions & 1 deletion public/client/TracyThread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#if defined _WIN32
# include <windows.h>
# include <thread>
# include <chrono>
#else
# include <pthread.h>
#endif
Expand Down Expand Up @@ -43,7 +45,10 @@ class Thread

~Thread()
{
WaitForSingleObject( m_hnd, INFINITE );
// Here we can't use WaitForSingleObject( m_hnd, INFINITE ); because during DLL unload
// this destructor may run inside DllMain. Waiting on a thread in DllMain is forbidden
// and can lead to a "loader lock" deadlock.
std::this_thread::sleep_for( std::chrono::milliseconds( 20 ) );
CloseHandle( m_hnd );
}

Expand Down
Loading