From a3d8b5d2257c2ad009d745811f8156deccd5eed0 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sat, 15 Aug 2020 14:59:16 +0200 Subject: [PATCH] Allow running specific tasks on main thread. --- profiler/src/main.cpp | 40 +++++++++++++++++++++++++++++++++------- server/TracyView.cpp | 6 ++++-- server/TracyView.hpp | 8 +++++--- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/profiler/src/main.cpp b/profiler/src/main.cpp index c92b8b33..a3a7aa5d 100644 --- a/profiler/src/main.cpp +++ b/profiler/src/main.cpp @@ -142,6 +142,22 @@ static std::atomic viewShutdown { ViewShutdown::False }; static double animTime = 0; static float dpiScale = 1.f; static ImGuiTextFilter addrFilter, portFilter, progFilter; +static std::thread::id mainThread; +static std::vector> mainThreadTasks; +static std::mutex mainThreadLock; + +void RunOnMainThread( std::function cb ) +{ + if( std::this_thread::get_id() == mainThread ) + { + cb(); + } + else + { + std::lock_guard lock( mainThreadLock ); + mainThreadTasks.emplace_back( cb ); + } +} int main( int argc, char** argv ) { @@ -215,6 +231,8 @@ int main( int argc, char** argv ) } } + mainThread = std::this_thread::get_id(); + // Setup window glfwSetErrorCallback(glfw_error_callback); if( !glfwInit() ) return 1; @@ -305,7 +323,7 @@ int main( int argc, char** argv ) auto f = std::unique_ptr( tracy::FileRead::Open( argv[1] ) ); if( f ) { - view = std::make_unique( *f, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); + view = std::make_unique( RunOnMainThread, *f, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); } } else @@ -331,7 +349,7 @@ int main( int argc, char** argv ) } if( connectTo ) { - view = std::make_unique( connectTo, port, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); + view = std::make_unique( RunOnMainThread, connectTo, port, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); } glfwShowWindow( window ); @@ -350,6 +368,14 @@ int main( int argc, char** argv ) { std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) ); } + std::unique_lock lock( mainThreadLock ); + if( !mainThreadTasks.empty() ) + { + std::vector> tmp; + std::swap( tmp, mainThreadTasks ); + lock.unlock(); + for( auto& cb : tmp ) cb(); + } } if( loadThread.joinable() ) loadThread.join(); @@ -635,11 +661,11 @@ static void DrawContents() { std::string addrPart = std::string( addr, ptr ); uint32_t portPart = atoi( ptr+1 ); - view = std::make_unique( addrPart.c_str(), portPart, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); + view = std::make_unique( RunOnMainThread, addrPart.c_str(), portPart, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); } else { - view = std::make_unique( addr, port, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); + view = std::make_unique( RunOnMainThread, addr, port, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); } } ImGui::SameLine( 0, ImGui::GetFontSize() * 2 ); @@ -657,7 +683,7 @@ static void DrawContents() loadThread = std::thread( [f] { try { - view = std::make_unique( *f, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); + view = std::make_unique( RunOnMainThread, *f, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); } catch( const tracy::UnsupportedVersion& e ) { @@ -779,7 +805,7 @@ static void DrawContents() } if( selected && !loadThread.joinable() ) { - view = std::make_unique( v.second.address.c_str(), v.second.port, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); + view = std::make_unique( RunOnMainThread, v.second.address.c_str(), v.second.port, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); } ImGui::NextColumn(); const auto acttime = ( v.second.activeTime + ( time - v.second.time ) / 1000 ) * 1000000000ll; @@ -919,7 +945,7 @@ static void DrawContents() viewShutdown.store( ViewShutdown::False, std::memory_order_relaxed ); if( reconnect ) { - view = std::make_unique( reconnectAddr.c_str(), reconnectPort, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); + view = std::make_unique( RunOnMainThread, reconnectAddr.c_str(), reconnectPort, fixedWidth, smallFont, bigFont, SetWindowTitleCallback, GetMainWindowNative ); } break; default: diff --git a/server/TracyView.cpp b/server/TracyView.cpp index 8627521a..8276c21f 100644 --- a/server/TracyView.cpp +++ b/server/TracyView.cpp @@ -129,7 +129,7 @@ enum { MinFrameSize = 5 }; static View* s_instance = nullptr; -View::View( const char* addr, int port, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, GetWindowCallback gwcb ) +View::View( void(*cbMainThread)(std::function), const char* addr, int port, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, GetWindowCallback gwcb ) : m_worker( addr, port ) , m_staticView( false ) , m_pause( false ) @@ -142,6 +142,7 @@ View::View( const char* addr, int port, ImFont* fixedWidth, ImFont* smallFont, I , m_stcb( stcb ) , m_gwcb( gwcb ) , m_userData() + , m_cbMainThread( cbMainThread ) { assert( s_instance == nullptr ); s_instance = this; @@ -149,7 +150,7 @@ View::View( const char* addr, int port, ImFont* fixedWidth, ImFont* smallFont, I InitTextEditor( fixedWidth ); } -View::View( FileRead& f, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, GetWindowCallback gwcb ) +View::View( void(*cbMainThread)(std::function), FileRead& f, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, SetTitleCallback stcb, GetWindowCallback gwcb ) : m_worker( f ) , m_filename( f.GetFilename() ) , m_staticView( true ) @@ -161,6 +162,7 @@ View::View( FileRead& f, ImFont* fixedWidth, ImFont* smallFont, ImFont* bigFont, , m_stcb( stcb ) , m_gwcb( gwcb ) , m_userData( m_worker.GetCaptureProgram().c_str(), m_worker.GetCaptureTime() ) + , m_cbMainThread( cbMainThread ) { assert( s_instance == nullptr ); s_instance = this; diff --git a/server/TracyView.hpp b/server/TracyView.hpp index 0011c8eb..3dd2cbf8 100644 --- a/server/TracyView.hpp +++ b/server/TracyView.hpp @@ -75,9 +75,9 @@ public: using SetTitleCallback = void(*)( const char* ); using GetWindowCallback = void*(*)(); - View( ImFont* fixedWidth = nullptr, ImFont* smallFont = nullptr, ImFont* bigFont = nullptr, SetTitleCallback stcb = nullptr, GetWindowCallback gwcb = nullptr ) : View( "127.0.0.1", 8086, fixedWidth, smallFont, bigFont, stcb, gwcb ) {} - View( const char* addr, int port, ImFont* fixedWidth = nullptr, ImFont* smallFont = nullptr, ImFont* bigFont = nullptr, SetTitleCallback stcb = nullptr, GetWindowCallback gwcb = nullptr ); - View( FileRead& f, ImFont* fixedWidth = nullptr, ImFont* smallFont = nullptr, ImFont* bigFont = nullptr, SetTitleCallback stcb = nullptr, GetWindowCallback gwcb = nullptr ); + View( void(*cbMainThread)(std::function), ImFont* fixedWidth = nullptr, ImFont* smallFont = nullptr, ImFont* bigFont = nullptr, SetTitleCallback stcb = nullptr, GetWindowCallback gwcb = nullptr ) : View( cbMainThread, "127.0.0.1", 8086, fixedWidth, smallFont, bigFont, stcb, gwcb ) {} + View( void(*cbMainThread)(std::function), const char* addr, int port, ImFont* fixedWidth = nullptr, ImFont* smallFont = nullptr, ImFont* bigFont = nullptr, SetTitleCallback stcb = nullptr, GetWindowCallback gwcb = nullptr ); + View( void(*cbMainThread)(std::function), FileRead& f, ImFont* fixedWidth = nullptr, ImFont* smallFont = nullptr, ImFont* bigFont = nullptr, SetTitleCallback stcb = nullptr, GetWindowCallback gwcb = nullptr ); ~View(); static bool Draw(); @@ -456,6 +456,8 @@ private: RangeSlim m_setRangePopup; bool m_setRangePopupOpen = false; + void(*m_cbMainThread)(std::function); + struct FindZone { enum : uint64_t { Unselected = std::numeric_limits::max() - 1 }; enum class GroupBy : int { Thread, UserText, ZoneName, Callstack, Parent, NoGrouping };