Adding background thread to platform.

This commit is contained in:
Vladimir Byko-Ianko 2018-05-21 17:57:04 +03:00 committed by mpimenov
parent 0d8ebf81da
commit 031471b0c6
2 changed files with 16 additions and 5 deletions

View file

@ -270,19 +270,22 @@ unsigned Platform::CpuCores() const
void Platform::ShutdownThreads()
{
ASSERT(m_networkThread && m_fileThread, ());
ASSERT(m_networkThread && m_fileThread && m_backgroundThread, ());
m_networkThread->ShutdownAndJoin();
m_fileThread->ShutdownAndJoin();
m_backgroundThread->ShutdownAndJoin();
m_networkThread.reset();
m_fileThread.reset();
m_backgroundThread.reset();
}
void Platform::RunThreads()
{
ASSERT(!m_networkThread && !m_fileThread, ());
ASSERT(!m_networkThread && !m_fileThread && !m_backgroundThread, ());
m_networkThread = make_unique<base::WorkerThread>();
m_fileThread = make_unique<base::WorkerThread>();
m_backgroundThread = make_unique<base::WorkerThread>();
}
string DebugPrint(Platform::EError err)

View file

@ -84,7 +84,8 @@ public:
{
File,
Network,
Gui
Gui,
Background,
};
using TFilesWithType = std::vector<std::pair<std::string, EFileType>>;
@ -125,6 +126,7 @@ protected:
std::unique_ptr<base::WorkerThread> m_networkThread;
std::unique_ptr<base::WorkerThread> m_fileThread;
std::unique_ptr<base::WorkerThread> m_backgroundThread;
public:
Platform();
@ -282,7 +284,7 @@ public:
template <typename Task>
void RunTask(Thread thread, Task && task)
{
ASSERT(m_networkThread && m_fileThread, ());
ASSERT(m_networkThread && m_fileThread && m_backgroundThread, ());
switch (thread)
{
case Thread::File:
@ -294,13 +296,16 @@ public:
case Thread::Gui:
RunOnGuiThread(std::forward<Task>(task));
break;
case Thread::Background:
m_backgroundThread->Push(std::forward<Task>(task));
break;
}
}
template <typename Task>
void RunDelayedTask(Thread thread, base::WorkerThread::Duration const & delay, Task && task)
{
ASSERT(m_networkThread && m_fileThread, ());
ASSERT(m_networkThread && m_fileThread && m_backgroundThread, ());
switch (thread)
{
case Thread::File:
@ -312,6 +317,9 @@ public:
case Thread::Gui:
CHECK(false, ("Delayed tasks for gui thread are not supported yet"));
break;
case Thread::Background:
m_backgroundThread->PushDelayed(delay, std::forward<Task>(task));
break;
}
}