1
0
mirror of https://github.com/wolfpld/tracy.git synced 2025-03-20 07:40:02 +08:00

Use custom threading wrapper instead of std::thread.

std::thread may perform memory allocation when a thread is created (it
does so on MSVC). Tracy heap is managed by its own allocator and this
changes prevents accessing application heap.
This commit is contained in:
Bartosz Taudul 2017-10-16 21:13:57 +02:00
parent 2f8d3ff5eb
commit 9f28205548
2 changed files with 8 additions and 5 deletions

View File

@ -23,6 +23,7 @@
#include "TracyAlloc.hpp" #include "TracyAlloc.hpp"
#include "TracyScoped.hpp" #include "TracyScoped.hpp"
#include "TracyProfiler.hpp" #include "TracyProfiler.hpp"
#include "TracyThread.hpp"
#ifdef _DEBUG #ifdef _DEBUG
# define DISABLE_LZ4 # define DISABLE_LZ4
@ -77,6 +78,7 @@ static Profiler init_order(106) s_profiler;
#endif #endif
static Profiler* s_instance = nullptr; static Profiler* s_instance = nullptr;
static Thread* s_thread = nullptr;
Profiler::Profiler() Profiler::Profiler()
: m_mainThread( GetThreadHandle() ) : m_mainThread( GetThreadHandle() )
@ -94,14 +96,16 @@ Profiler::Profiler()
uint32_t cpu; uint32_t cpu;
m_timeBegin = GetTime( cpu ); m_timeBegin = GetTime( cpu );
m_thread = std::thread( [this] { Worker(); } ); s_thread = (Thread*)tracy_malloc( sizeof( Thread ) );
SetThreadName( m_thread, "Tracy Profiler" ); new(s_thread) Thread( LaunchWorker, this );
SetThreadName( s_thread->Handle(), "Tracy Profiler" );
} }
Profiler::~Profiler() Profiler::~Profiler()
{ {
m_shutdown.store( true, std::memory_order_relaxed ); m_shutdown.store( true, std::memory_order_relaxed );
m_thread.join(); s_thread->~Thread();
tracy_free( s_thread );
tracy_free( m_buffer ); tracy_free( m_buffer );
LZ4_freeStream( m_stream ); LZ4_freeStream( m_stream );

View File

@ -5,7 +5,6 @@
#include <chrono> #include <chrono>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <thread>
#include "concurrentqueue.h" #include "concurrentqueue.h"
#include "../common/tracy_lz4.hpp" #include "../common/tracy_lz4.hpp"
@ -163,6 +162,7 @@ public:
static bool ShouldExit(); static bool ShouldExit();
private: private:
static void LaunchWorker( void* ptr ) { ((Profiler*)ptr)->Worker(); }
void Worker(); void Worker();
bool SendData( const char* data, size_t len ); bool SendData( const char* data, size_t len );
@ -180,7 +180,6 @@ private:
int64_t m_timeBegin; int64_t m_timeBegin;
uint64_t m_mainThread; uint64_t m_mainThread;
uint64_t m_epoch; uint64_t m_epoch;
std::thread m_thread;
std::atomic<bool> m_shutdown; std::atomic<bool> m_shutdown;
std::unique_ptr<Socket> m_sock; std::unique_ptr<Socket> m_sock;