diff --git a/profiler/build/win32/Tracy.vcxproj b/profiler/build/win32/Tracy.vcxproj index fe713213..947890aa 100644 --- a/profiler/build/win32/Tracy.vcxproj +++ b/profiler/build/win32/Tracy.vcxproj @@ -202,6 +202,7 @@ + NotSet NotSet @@ -323,6 +324,7 @@ + diff --git a/profiler/build/win32/Tracy.vcxproj.filters b/profiler/build/win32/Tracy.vcxproj.filters index e256df08..06f1982c 100644 --- a/profiler/build/win32/Tracy.vcxproj.filters +++ b/profiler/build/win32/Tracy.vcxproj.filters @@ -336,6 +336,9 @@ src + + src + @@ -683,6 +686,9 @@ src + + src + diff --git a/profiler/src/RunQueue.cpp b/profiler/src/RunQueue.cpp new file mode 100644 index 00000000..42d07c17 --- /dev/null +++ b/profiler/src/RunQueue.cpp @@ -0,0 +1,31 @@ +#include "RunQueue.hpp" + +RunQueue::RunQueue() + : m_mainThread( std::this_thread::get_id() ) +{ +} + +void RunQueue::Queue( std::function cb, bool forceDelay ) +{ + if( !forceDelay && std::this_thread::get_id() == m_mainThread ) + { + cb(); + } + else + { + std::lock_guard lock( m_lock ); + m_queue.emplace_back( cb ); + } +} + +void RunQueue::Run() +{ + std::unique_lock lock( m_lock ); + if( !m_queue.empty() ) + { + std::vector> tmp; + std::swap( tmp, m_queue ); + lock.unlock(); + for( auto& cb : tmp ) cb(); + } +} diff --git a/profiler/src/RunQueue.hpp b/profiler/src/RunQueue.hpp new file mode 100644 index 00000000..a904ba62 --- /dev/null +++ b/profiler/src/RunQueue.hpp @@ -0,0 +1,23 @@ +#ifndef __RUNQUEUE_HPP__ +#define __RUNQUEUE_HPP__ + +#include +#include +#include +#include + +class RunQueue +{ +public: + RunQueue(); + + void Queue( std::function cb, bool forceDelay = false ); + void Run(); + +private: + std::vector> m_queue; + std::mutex m_lock; + std::thread::id m_mainThread; +}; + +#endif diff --git a/profiler/src/main.cpp b/profiler/src/main.cpp index de2a955a..f7d374c5 100644 --- a/profiler/src/main.cpp +++ b/profiler/src/main.cpp @@ -53,6 +53,7 @@ #include "HttpRequest.hpp" #include "NativeWindow.hpp" #include "ResolvService.hpp" +#include "RunQueue.hpp" static void glfw_error_callback(int error, const char* description) { @@ -118,24 +119,14 @@ 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; +static RunQueue mainThreadTasks; static uint32_t updateVersion = 0; static bool showReleaseNotes = false; static std::string releaseNotes; void RunOnMainThread( std::function cb, bool forceDelay = false ) { - if( !forceDelay && std::this_thread::get_id() == mainThread ) - { - cb(); - } - else - { - std::lock_guard lock( mainThreadLock ); - mainThreadTasks.emplace_back( cb ); - } + mainThreadTasks.Queue( cb, forceDelay ); } static void SetupDPIScale( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigFont, ImFont*& cb_smallFont ) @@ -274,8 +265,6 @@ int main( int argc, char** argv ) } } - mainThread = std::this_thread::get_id(); - updateThread = std::thread( [] { HttpRequest( "nereid.pl", "/tracy/version", 8099, [] ( int size, char* data ) { if( size == 4 ) @@ -390,14 +379,7 @@ 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(); - } + mainThreadTasks.Run(); } if( loadThread.joinable() ) loadThread.join();