Hold file and network threads as unique_ptrs

This commit is contained in:
VladiMihaylenko 2018-02-21 11:19:00 +03:00 committed by Roman Kuznetsov
parent ef9cf2faba
commit a94b181f2f
7 changed files with 64 additions and 32 deletions

View file

@ -484,7 +484,7 @@ Framework::Framework(FrameworkParams const & params)
Framework::~Framework()
{
GetPlatform().ShutdownThreads();
m_threadRunner.reset();
osm::Editor & editor = osm::Editor::Instance();

View file

@ -145,6 +145,10 @@ class Framework : public SearchAPI::Delegate, public RoutingManager::Delegate
} m_fixedPos;
#endif
private:
// Must be first member in Framework.
unique_ptr<Platform::ThreadRunner> m_threadRunner = make_unique<Platform::ThreadRunner>();
protected:
using TDrapeFunction = function<void (df::DrapeEngine *)>;

View file

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

View file

@ -30,9 +30,21 @@ namespace platform
class LocalCountryFile;
}
class Platform;
extern Platform & GetPlatform();
class Platform
{
public:
friend struct ThreadRunner;
struct ThreadRunner
{
ThreadRunner() { GetPlatform().RunThreads(); }
~ThreadRunner() { GetPlatform().ShutdownThreads(); }
};
enum EError
{
ERR_OK = 0,
@ -111,8 +123,8 @@ protected:
unique_ptr<base::TaskLoop> m_guiThread;
base::WorkerThread m_networkThread;
base::WorkerThread m_fileThread;
unique_ptr<base::WorkerThread> m_networkThread;
unique_ptr<base::WorkerThread> m_fileThread;
public:
Platform();
@ -263,50 +275,51 @@ public:
template <typename Task>
void RunTask(Thread thread, Task && task)
{
ASSERT(m_networkThread && m_fileThread, ());
switch (thread)
{
case Thread::File:
m_fileThread.Push(forward<Task>(task));
break;
case Thread::Network:
m_networkThread.Push(forward<Task>(task));
break;
case Thread::Gui:
RunOnGuiThread(forward<Task>(task));
break;
case Thread::File:
m_fileThread->Push(forward<Task>(task));
break;
case Thread::Network:
m_networkThread->Push(forward<Task>(task));
break;
case Thread::Gui:
RunOnGuiThread(forward<Task>(task));
break;
}
}
template <typename Task>
void RunDelayedTask(Thread thread, base::WorkerThread::Duration const & delay, Task && task)
{
ASSERT(m_networkThread && m_fileThread, ());
switch (thread)
{
case Thread::File:
m_fileThread.PushDelayed(delay, forward<Task>(task));
break;
case Thread::Network:
m_networkThread.PushDelayed(delay, forward<Task>(task));
break;
case Thread::Gui:
CHECK(false, ("Delayed tasks for gui thread are not supported yet"));
break;
case Thread::File:
m_fileThread->PushDelayed(delay, forward<Task>(task));
break;
case Thread::Network:
m_networkThread->PushDelayed(delay, forward<Task>(task));
break;
case Thread::Gui:
CHECK(false, ("Delayed tasks for gui thread are not supported yet"));
break;
}
}
void ShutdownThreads();
// Use this method for testing purposes only.
void SetGuiThread(unique_ptr<base::TaskLoop> guiThread);
private:
void RunThreads();
void ShutdownThreads();
void RunOnGuiThread(base::TaskLoop::Task && task);
void RunOnGuiThread(base::TaskLoop::Task const & task);
void GetSystemFontNames(FilesList & res) const;
};
extern Platform & GetPlatform();
string DebugPrint(Platform::EError err);
string DebugPrint(Platform::ChargingStatus status);

View file

@ -108,7 +108,6 @@ Storage::Storage(string const & pathToCountriesFile /* = COUNTRIES_FILE */,
SetLocale(languages::GetCurrentTwine());
LoadCountriesFile(pathToCountriesFile, m_dataDir);
CalMaxMwmSizeBytes();
LoadServerListForSession();
}
Storage::Storage(string const & referenceCountriesTxtJsonForTesting,
@ -122,7 +121,6 @@ Storage::Storage(string const & referenceCountriesTxtJsonForTesting,
LoadCountriesFromBuffer(referenceCountriesTxtJsonForTesting, m_countries, m_affiliations);
CHECK_LESS_OR_EQUAL(0, m_currentVersion, ("Can't load test countries file"));
CalMaxMwmSizeBytes();
LoadServerListForTesting();
}
void Storage::Init(TUpdateCallback const & didDownload, TDeleteCallback const & willDelete)
@ -642,10 +640,15 @@ void Storage::DownloadNextFile(QueuedCountry const & country)
return;
}
if (m_sessionServerList)
if (m_sessionServerList || !m_downloadingUrlsForTesting.empty())
{
DoDownload();
}
else
{
LoadServerListForSession();
SetDeferDownloading();
}
}
void Storage::DeleteFromDownloader(TCountryId const & countryId)
@ -786,7 +789,7 @@ void Storage::ReportProgressForHierarchy(TCountryId const & countryId,
void Storage::DoDownload()
{
ASSERT_THREAD_CHECKER(m_threadChecker, ());
CHECK(m_sessionServerList, ());
CHECK(m_sessionServerList || !m_downloadingUrlsForTesting.empty(), ());
// Queue can be empty because countries were deleted from queue.
if (m_queue.empty())

View file

@ -72,6 +72,7 @@ UNIT_TEST(SmallMwms_ReDownloadExistedMWMIgnored_Test)
UNIT_TEST(SmallMwms_InterruptDownloadResumeDownload_Test)
{
auto runner = make_unique<Platform::ThreadRunner>();
WritableDirChanger writableDirChanger(kMapTestDir);
// Start download but interrupt it

View file

@ -61,7 +61,7 @@ bool DownloadFile(string const & url,
string GetCountriesTxtWebUrl(string const version)
{
return kTestWebServer + "/direct/" + version + "/" + kCountriesTxtFile;
return kTestWebServer + "direct/" + version + "/" + kCountriesTxtFile;
}
string GetCountriesTxtFilePath()