mirror of
https://github.com/wolfpld/tracy.git
synced 2024-11-14 04:01:48 +00:00
Extract main thread run queue.
This commit is contained in:
parent
f0f00512b0
commit
208755ad53
@ -202,6 +202,7 @@
|
|||||||
<ClCompile Include="..\..\src\main.cpp" />
|
<ClCompile Include="..\..\src\main.cpp" />
|
||||||
<ClCompile Include="..\..\src\NativeWindow.cpp" />
|
<ClCompile Include="..\..\src\NativeWindow.cpp" />
|
||||||
<ClCompile Include="..\..\src\ResolvService.cpp" />
|
<ClCompile Include="..\..\src\ResolvService.cpp" />
|
||||||
|
<ClCompile Include="..\..\src\RunQueue.cpp" />
|
||||||
<ClCompile Include="..\..\src\winmain.cpp">
|
<ClCompile Include="..\..\src\winmain.cpp">
|
||||||
<EnableEnhancedInstructionSet Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotSet</EnableEnhancedInstructionSet>
|
<EnableEnhancedInstructionSet Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotSet</EnableEnhancedInstructionSet>
|
||||||
<EnableEnhancedInstructionSet Condition="'$(Configuration)|$(Platform)'=='Release|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\imgui\imgui_impl_opengl3_loader.h" />
|
||||||
<ClInclude Include="..\..\src\NativeWindow.hpp" />
|
<ClInclude Include="..\..\src\NativeWindow.hpp" />
|
||||||
<ClInclude Include="..\..\src\ResolvService.hpp" />
|
<ClInclude Include="..\..\src\ResolvService.hpp" />
|
||||||
|
<ClInclude Include="..\..\src\RunQueue.hpp" />
|
||||||
<ClInclude Include="..\..\src\stb_image.h" />
|
<ClInclude Include="..\..\src\stb_image.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
@ -336,6 +336,9 @@
|
|||||||
<ClCompile Include="..\..\src\Fonts.cpp">
|
<ClCompile Include="..\..\src\Fonts.cpp">
|
||||||
<Filter>src</Filter>
|
<Filter>src</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\src\RunQueue.cpp">
|
||||||
|
<Filter>src</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\..\..\server\TracyEvent.hpp">
|
<ClInclude Include="..\..\..\server\TracyEvent.hpp">
|
||||||
@ -683,6 +686,9 @@
|
|||||||
<ClInclude Include="..\..\src\Fonts.hpp">
|
<ClInclude Include="..\..\src\Fonts.hpp">
|
||||||
<Filter>src</Filter>
|
<Filter>src</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\src\RunQueue.hpp">
|
||||||
|
<Filter>src</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Natvis Include="DebugVis.natvis" />
|
<Natvis Include="DebugVis.natvis" />
|
||||||
|
31
profiler/src/RunQueue.cpp
Normal file
31
profiler/src/RunQueue.cpp
Normal 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
23
profiler/src/RunQueue.hpp
Normal 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
|
@ -53,6 +53,7 @@
|
|||||||
#include "HttpRequest.hpp"
|
#include "HttpRequest.hpp"
|
||||||
#include "NativeWindow.hpp"
|
#include "NativeWindow.hpp"
|
||||||
#include "ResolvService.hpp"
|
#include "ResolvService.hpp"
|
||||||
|
#include "RunQueue.hpp"
|
||||||
|
|
||||||
static void glfw_error_callback(int error, const char* description)
|
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 double animTime = 0;
|
||||||
static float dpiScale = 1.f;
|
static float dpiScale = 1.f;
|
||||||
static ImGuiTextFilter addrFilter, portFilter, progFilter;
|
static ImGuiTextFilter addrFilter, portFilter, progFilter;
|
||||||
static std::thread::id mainThread;
|
static RunQueue mainThreadTasks;
|
||||||
static std::vector<std::function<void()>> mainThreadTasks;
|
|
||||||
static std::mutex mainThreadLock;
|
|
||||||
static uint32_t updateVersion = 0;
|
static uint32_t updateVersion = 0;
|
||||||
static bool showReleaseNotes = false;
|
static bool showReleaseNotes = false;
|
||||||
static std::string releaseNotes;
|
static std::string releaseNotes;
|
||||||
|
|
||||||
void RunOnMainThread( std::function<void()> cb, bool forceDelay = false )
|
void RunOnMainThread( std::function<void()> cb, bool forceDelay = false )
|
||||||
{
|
{
|
||||||
if( !forceDelay && std::this_thread::get_id() == mainThread )
|
mainThreadTasks.Queue( cb, forceDelay );
|
||||||
{
|
|
||||||
cb();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock( mainThreadLock );
|
|
||||||
mainThreadTasks.emplace_back( cb );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SetupDPIScale( float scale, ImFont*& cb_fixedWidth, ImFont*& cb_bigFont, ImFont*& cb_smallFont )
|
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( [] {
|
updateThread = std::thread( [] {
|
||||||
HttpRequest( "nereid.pl", "/tracy/version", 8099, [] ( int size, char* data ) {
|
HttpRequest( "nereid.pl", "/tracy/version", 8099, [] ( int size, char* data ) {
|
||||||
if( size == 4 )
|
if( size == 4 )
|
||||||
@ -390,14 +379,7 @@ int main( int argc, char** argv )
|
|||||||
{
|
{
|
||||||
std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) );
|
std::this_thread::sleep_for( std::chrono::milliseconds( 50 ) );
|
||||||
}
|
}
|
||||||
std::unique_lock<std::mutex> lock( mainThreadLock );
|
mainThreadTasks.Run();
|
||||||
if( !mainThreadTasks.empty() )
|
|
||||||
{
|
|
||||||
std::vector<std::function<void()>> tmp;
|
|
||||||
std::swap( tmp, mainThreadTasks );
|
|
||||||
lock.unlock();
|
|
||||||
for( auto& cb : tmp ) cb();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if( loadThread.joinable() ) loadThread.join();
|
if( loadThread.joinable() ) loadThread.join();
|
||||||
|
Loading…
Reference in New Issue
Block a user