Extract main thread run queue.

This commit is contained in:
Bartosz Taudul 2022-07-25 20:17:13 +02:00
parent f0f00512b0
commit 208755ad53
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
5 changed files with 66 additions and 22 deletions

View File

@ -202,6 +202,7 @@
<ClCompile Include="..\..\src\main.cpp" />
<ClCompile Include="..\..\src\NativeWindow.cpp" />
<ClCompile Include="..\..\src\ResolvService.cpp" />
<ClCompile Include="..\..\src\RunQueue.cpp" />
<ClCompile Include="..\..\src\winmain.cpp">
<EnableEnhancedInstructionSet Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotSet</EnableEnhancedInstructionSet>
<EnableEnhancedInstructionSet Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotSet</EnableEnhancedInstructionSet>
@ -323,6 +324,7 @@
<ClInclude Include="..\..\src\imgui\imgui_impl_opengl3_loader.h" />
<ClInclude Include="..\..\src\NativeWindow.hpp" />
<ClInclude Include="..\..\src\ResolvService.hpp" />
<ClInclude Include="..\..\src\RunQueue.hpp" />
<ClInclude Include="..\..\src\stb_image.h" />
</ItemGroup>
<ItemGroup>

View File

@ -336,6 +336,9 @@
<ClCompile Include="..\..\src\Fonts.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\RunQueue.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\server\TracyEvent.hpp">
@ -683,6 +686,9 @@
<ClInclude Include="..\..\src\Fonts.hpp">
<Filter>src</Filter>
</ClInclude>
<ClInclude Include="..\..\src\RunQueue.hpp">
<Filter>src</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Natvis Include="DebugVis.natvis" />

31
profiler/src/RunQueue.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "RunQueue.hpp"
RunQueue::RunQueue()
: m_mainThread( std::this_thread::get_id() )
{
}
void RunQueue::Queue( std::function<void()> cb, bool forceDelay )
{
if( !forceDelay && std::this_thread::get_id() == m_mainThread )
{
cb();
}
else
{
std::lock_guard<std::mutex> lock( m_lock );
m_queue.emplace_back( cb );
}
}
void RunQueue::Run()
{
std::unique_lock<std::mutex> lock( m_lock );
if( !m_queue.empty() )
{
std::vector<std::function<void()>> tmp;
std::swap( tmp, m_queue );
lock.unlock();
for( auto& cb : tmp ) cb();
}
}

23
profiler/src/RunQueue.hpp Normal file
View File

@ -0,0 +1,23 @@
#ifndef __RUNQUEUE_HPP__
#define __RUNQUEUE_HPP__
#include <functional>
#include <mutex>
#include <thread>
#include <vector>
class RunQueue
{
public:
RunQueue();
void Queue( std::function<void()> cb, bool forceDelay = false );
void Run();
private:
std::vector<std::function<void()>> m_queue;
std::mutex m_lock;
std::thread::id m_mainThread;
};
#endif

View File

@ -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 { ViewShutdown::False };
static double animTime = 0;
static float dpiScale = 1.f;
static ImGuiTextFilter addrFilter, portFilter, progFilter;
static std::thread::id mainThread;
static std::vector<std::function<void()>> 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<void()> cb, bool forceDelay = false )
{
if( !forceDelay && std::this_thread::get_id() == mainThread )
{
cb();
}
else
{
std::lock_guard<std::mutex> 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<std::mutex> lock( mainThreadLock );
if( !mainThreadTasks.empty() )
{
std::vector<std::function<void()>> tmp;
std::swap( tmp, mainThreadTasks );
lock.unlock();
for( auto& cb : tmp ) cb();
}
mainThreadTasks.Run();
}
if( loadThread.joinable() ) loadThread.join();