mirror of
https://github.com/wolfpld/tracy.git
synced 2024-11-25 15:34:36 +00:00
Add task dispatcher to server.
This commit is contained in:
parent
e1e5d6bd47
commit
6f5a23a198
@ -135,6 +135,7 @@
|
||||
<ClCompile Include="..\..\..\common\tracy_lz4hc.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyMemory.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyPrint.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyTaskDispatch.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyThreadCompress.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyWorker.cpp" />
|
||||
<ClCompile Include="..\..\src\capture.cpp" />
|
||||
@ -160,6 +161,7 @@
|
||||
<ClInclude Include="..\..\..\server\TracyPopcnt.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyPrint.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracySlab.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyTaskDispatch.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyThreadCompress.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyVector.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyWorker.hpp" />
|
||||
|
@ -42,6 +42,9 @@
|
||||
<ClCompile Include="..\..\..\server\TracyThreadCompress.cpp">
|
||||
<Filter>server</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\..\server\TracyTaskDispatch.cpp">
|
||||
<Filter>server</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\..\common\tracy_lz4.hpp">
|
||||
@ -116,5 +119,8 @@
|
||||
<ClInclude Include="..\..\..\server\TracyThreadCompress.hpp">
|
||||
<Filter>server</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\..\server\TracyTaskDispatch.hpp">
|
||||
<Filter>server</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -116,6 +116,7 @@
|
||||
<ClCompile Include="..\..\..\server\TracyMemory.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyPrint.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyStorage.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyTaskDispatch.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyTexture.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyThreadCompress.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyUserData.cpp" />
|
||||
@ -172,6 +173,7 @@
|
||||
<ClInclude Include="..\..\..\server\TracySlab.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyStorage.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyStringDiscovery.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyTaskDispatch.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyTexture.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyThreadCompress.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyUserData.hpp" />
|
||||
|
@ -108,6 +108,9 @@
|
||||
<ClCompile Include="..\..\..\server\TracyThreadCompress.cpp">
|
||||
<Filter>server</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\..\server\TracyTaskDispatch.cpp">
|
||||
<Filter>server</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\..\common\tracy_lz4.hpp">
|
||||
@ -290,6 +293,9 @@
|
||||
<ClInclude Include="..\..\..\server\TracyViewData.hpp">
|
||||
<Filter>server</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\..\server\TracyTaskDispatch.hpp">
|
||||
<Filter>server</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Natvis Include="DebugVis.natvis" />
|
||||
|
82
server/TracyTaskDispatch.cpp
Normal file
82
server/TracyTaskDispatch.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "TracyTaskDispatch.hpp"
|
||||
|
||||
namespace tracy
|
||||
{
|
||||
|
||||
TaskDispatch::TaskDispatch( size_t workers )
|
||||
: m_exit( false )
|
||||
, m_jobs( 0 )
|
||||
{
|
||||
assert( workers >= 1 );
|
||||
|
||||
m_workers.reserve( workers );
|
||||
for( size_t i=0; i<workers; i++ )
|
||||
{
|
||||
m_workers.emplace_back( std::thread( [this]{ Worker(); } ) );
|
||||
}
|
||||
}
|
||||
|
||||
TaskDispatch::~TaskDispatch()
|
||||
{
|
||||
m_exit.store( true, std::memory_order_release );
|
||||
m_queueLock.lock();
|
||||
m_cvWork.notify_all();
|
||||
m_queueLock.unlock();
|
||||
|
||||
for( auto& worker : m_workers )
|
||||
{
|
||||
worker.join();
|
||||
}
|
||||
}
|
||||
|
||||
void TaskDispatch::Queue( const std::function<void(void)>& f )
|
||||
{
|
||||
std::lock_guard<std::mutex> lock( m_queueLock );
|
||||
m_queue.emplace_back( f );
|
||||
m_cvWork.notify_one();
|
||||
}
|
||||
|
||||
void TaskDispatch::Queue( std::function<void(void)>&& f )
|
||||
{
|
||||
std::lock_guard<std::mutex> lock( m_queueLock );
|
||||
m_queue.emplace_back( std::move( f ) );
|
||||
m_cvWork.notify_one();
|
||||
}
|
||||
|
||||
void TaskDispatch::Sync()
|
||||
{
|
||||
std::unique_lock<std::mutex> lock( m_queueLock );
|
||||
while( !m_queue.empty() )
|
||||
{
|
||||
auto f = m_queue.back();
|
||||
m_queue.pop_back();
|
||||
lock.unlock();
|
||||
f();
|
||||
lock.lock();
|
||||
}
|
||||
m_cvJobs.wait( lock, [this]{ return m_jobs == 0; } );
|
||||
}
|
||||
|
||||
void TaskDispatch::Worker()
|
||||
{
|
||||
for(;;)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock( m_queueLock );
|
||||
m_cvWork.wait( lock, [this]{ return !m_queue.empty() || m_exit.load( std::memory_order_acquire ); } );
|
||||
if( m_exit.load( std::memory_order_acquire ) ) return;
|
||||
auto f = m_queue.back();
|
||||
m_queue.pop_back();
|
||||
m_jobs++;
|
||||
lock.unlock();
|
||||
f();
|
||||
lock.lock();
|
||||
m_jobs--;
|
||||
if( m_jobs == 0 && m_queue.empty() ) m_cvJobs.notify_one();
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
39
server/TracyTaskDispatch.hpp
Normal file
39
server/TracyTaskDispatch.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
#ifndef __TRACYTASKDISPATCH_HPP__
|
||||
#define __TRACYTASKDISPATCH_HPP__
|
||||
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace tracy
|
||||
{
|
||||
|
||||
class TaskDispatch
|
||||
{
|
||||
public:
|
||||
TaskDispatch( size_t workers );
|
||||
~TaskDispatch();
|
||||
|
||||
void Queue( const std::function<void(void)>& f );
|
||||
void Queue( std::function<void(void)>&& f );
|
||||
|
||||
void Sync();
|
||||
|
||||
private:
|
||||
void Worker();
|
||||
|
||||
std::vector<std::function<void(void)>> m_queue;
|
||||
std::mutex m_queueLock;
|
||||
std::condition_variable m_cvWork, m_cvJobs;
|
||||
std::atomic<bool> m_exit;
|
||||
size_t m_jobs;
|
||||
|
||||
std::vector<std::thread> m_workers;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
@ -134,6 +134,7 @@
|
||||
<ClCompile Include="..\..\..\common\tracy_lz4.cpp" />
|
||||
<ClCompile Include="..\..\..\common\tracy_lz4hc.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyMemory.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyTaskDispatch.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyThreadCompress.cpp" />
|
||||
<ClCompile Include="..\..\..\server\TracyWorker.cpp" />
|
||||
<ClCompile Include="..\..\src\update.cpp" />
|
||||
@ -157,6 +158,7 @@
|
||||
<ClInclude Include="..\..\..\server\TracyMemory.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyPopcnt.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracySlab.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyTaskDispatch.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyThreadCompress.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyVector.hpp" />
|
||||
<ClInclude Include="..\..\..\server\TracyWorker.hpp" />
|
||||
|
@ -36,6 +36,9 @@
|
||||
<ClCompile Include="..\..\..\server\TracyThreadCompress.cpp">
|
||||
<Filter>server</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\..\server\TracyTaskDispatch.cpp">
|
||||
<Filter>server</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\..\common\tracy_lz4.hpp">
|
||||
@ -104,5 +107,8 @@
|
||||
<ClInclude Include="..\..\..\server\TracyThreadCompress.hpp">
|
||||
<Filter>server</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\..\server\TracyTaskDispatch.hpp">
|
||||
<Filter>server</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user