Review fixes

This commit is contained in:
Sergey Yershov 2016-03-09 13:18:40 +03:00
parent becab05072
commit 3ddb131b04
4 changed files with 29 additions and 16 deletions

View file

@ -1,8 +1,11 @@
#include "deferred_task.hpp"
DeferredTask::DeferredTask(TDuration duration) : m_duration(duration)
namespace my
{
m_thread = thread([this] {
DeferredTask::DeferredTask(TDuration const & duration) : m_duration(duration)
{
m_thread = thread([this]
{
unique_lock<mutex> l(m_mutex);
while (!m_terminate)
{
@ -15,11 +18,15 @@ DeferredTask::DeferredTask(TDuration duration) : m_duration(duration)
if (m_cv.wait_for(l, m_duration) != cv_status::timeout)
continue;
auto local_fn = move(m_fn);
auto fn = move(m_fn);
m_fn = nullptr;
l.unlock();
local_fn();
l.lock();
if (fn)
{
l.unlock();
fn();
l.lock();
}
}
});
}
@ -42,3 +49,4 @@ void DeferredTask::Drop()
}
m_cv.notify_one();
}
} // namespace my

View file

@ -1,12 +1,13 @@
#pragma once
#include "std/thread.hpp"
#include "std/chrono.hpp"
#include "std/mutex.hpp"
#include "std/condition_variable.hpp"
#include "std/function.hpp"
#include "std/mutex.hpp"
#include "std/thread.hpp"
namespace my
{
class DeferredTask
{
using TDuration = duration<double>;
@ -18,12 +19,12 @@ class DeferredTask
bool m_terminate = false;
public:
DeferredTask(TDuration duration);
DeferredTask(TDuration const & duration);
~DeferredTask();
void Drop();
template<typename TFn>
template <typename TFn>
void RestartWith(TFn const && fn)
{
{
@ -33,3 +34,4 @@ public:
m_cv.notify_one();
}
};
} // namespace my

View file

@ -288,7 +288,7 @@ Framework::Framework()
, m_bmManager(*this)
, m_fixedSearchResults(0)
, m_lastReportedCountry(kInvalidCountryId)
, m_watchdogCountryUpdate(seconds(2))
, m_deferredCountryUpdate(seconds(1))
{
// Restore map style before classificator loading
int mapStyle = MapStyleLight;
@ -946,15 +946,19 @@ void Framework::ClearAllCaches()
void Framework::OnCheckUpdateCurrentCountry(m2::PointF const & pt, int zoomLevel)
{
if (zoomLevel <= scales::GetUpperWorldScale())
m_watchdogCountryUpdate.Drop();
m_deferredCountryUpdate.Drop();
else
m_watchdogCountryUpdate.RestartWith([this, pt, zoomLevel]{
m_deferredCountryUpdate.RestartWith([this, pt, zoomLevel]
{
OnUpdateCurrentCountry(pt, zoomLevel);
});
}
void Framework::OnUpdateCurrentCountry(m2::PointF const & pt, int zoomLevel)
{
if (zoomLevel <= scales::GetUpperWorldScale())
return;
storage::TCountryId newCountryId = m_infoGetter->GetRegionCountryId(m2::PointD(pt));
if (newCountryId == m_lastReportedCountry)

View file

@ -42,7 +42,6 @@
#include "std/list.hpp"
#include "std/shared_ptr.hpp"
#include "std/target_os.hpp"
#include "std/thread.hpp"
#include "std/unique_ptr.hpp"
#include "std/vector.hpp"
#include "std/weak_ptr.hpp"
@ -128,7 +127,7 @@ protected:
double m_startForegroundTime;
storage::Storage m_storage;
DeferredTask m_watchdogCountryUpdate;
my::DeferredTask m_deferredCountryUpdate;
location::TMyPositionModeChanged m_myPositionListener;