Add task dispatcher to server.

This commit is contained in:
Bartosz Taudul 2019-09-20 22:58:12 +02:00
parent e1e5d6bd47
commit 6f5a23a198
8 changed files with 145 additions and 0 deletions

View File

@ -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" />

View File

@ -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>

View File

@ -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" />

View File

@ -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" />

View 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();
}
}
}

View 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

View File

@ -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" />

View File

@ -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>