From b905a52ef6dee5c2d2c4b69389bacdb77492b2a8 Mon Sep 17 00:00:00 2001 From: Constantin Shalnev Date: Thu, 16 Jul 2015 14:57:17 +0300 Subject: [PATCH] Improved locks --- routing/async_router.cpp | 71 ++++++++++++++++++++-------------------- routing/async_router.hpp | 2 +- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/routing/async_router.cpp b/routing/async_router.cpp index 658fa95042..7abc5d8fdd 100644 --- a/routing/async_router.cpp +++ b/routing/async_router.cpp @@ -58,6 +58,8 @@ AsyncRouter::AsyncRouter(unique_ptr && router, unique_ptr guard(m_paramsMutex); - UNUSED_VALUE(guard); + lock_guard paramsGuard(m_paramsMutex); + UNUSED_VALUE(paramsGuard); m_startPoint = startPoint; m_startDirection = direction; @@ -83,10 +84,11 @@ void AsyncRouter::CalculateRoute(m2::PointD const & startPoint, m2::PointD const void AsyncRouter::ClearState() { - ASSERT(m_router, ()); m_router->Cancel(); - lock_guard guard(m_routeMutex); + lock_guard routingGuard(m_routingMutex); + UNUSED_VALUE(routingGuard); + m_router->ClearState(); } @@ -140,13 +142,15 @@ void AsyncRouter::CalculateRouteImpl(TReadyCallback const & callback) Route route(m_router->GetName()); IRouter::ResultCode code; - lock_guard guard(m_routeMutex); + lock_guard routingGuard(m_routingMutex); + UNUSED_VALUE(routingGuard); m_isReadyThread.clear(); m2::PointD startPoint, finalPoint, startDirection; { - lock_guard params(m_paramsMutex); + lock_guard paramsGuard(m_paramsMutex); + UNUSED_VALUE(paramsGuard); startPoint = m_startPoint; finalPoint = m_finalPoint; @@ -156,6 +160,8 @@ void AsyncRouter::CalculateRouteImpl(TReadyCallback const & callback) } my::Timer timer; + double elapsedSec = 0.0; + try { LOG(LDEBUG, ("Calculating the route from", startPoint, "to", finalPoint, "startDirection", startDirection)); @@ -166,47 +172,42 @@ void AsyncRouter::CalculateRouteImpl(TReadyCallback const & callback) // Run basic request. code = m_router->CalculateRoute(startPoint, startDirection, finalPoint, route); - double const elapsedSec = timer.ElapsedSeconds(); - - LogCode(code, elapsedSec); - - SendStatistics(startPoint, startDirection, finalPoint, code, route, elapsedSec); + elapsedSec = timer.ElapsedSeconds(); // routing time } catch (RootException const & e) { - LOG(LERROR, ("Exception happened while calculating route:", e.Msg())); code = IRouter::InternalError; - + LOG(LERROR, ("Exception happened while calculating route:", e.Msg())); SendStatistics(startPoint, startDirection, finalPoint, e.Msg()); - } - - if (code == IRouter::NoError) GetPlatform().RunOnGuiThread(bind(callback, route, code)); - - // Check online response if we have. - vector absent; - if (m_absentFetcher) - m_absentFetcher->GetAbsentCountries(absent); - if (absent.empty()) - { - if (code != IRouter::NoError) - GetPlatform().RunOnGuiThread(bind(callback, route, code)); return; } - for (string const & country : absent) - route.AddAbsentCountry(country); - if (code != IRouter::NoError) + bool const needFetchAbsent = (code != IRouter::Cancelled); + + // Check online response if we have. + vector absent; + if (m_absentFetcher && needFetchAbsent) { - GetPlatform().RunOnGuiThread(bind(callback, route, code)); + m_absentFetcher->GetAbsentCountries(absent); + for (string const & country : absent) + route.AddAbsentCountry(country); } - else + + if (!absent.empty()) { - double const elapsedSec = timer.ElapsedSeconds(); - LogCode(IRouter::NeedMoreMaps, elapsedSec); - SendStatistics(startPoint, startDirection, finalPoint, IRouter::NeedMoreMaps, route, elapsedSec); - GetPlatform().RunOnGuiThread(bind(callback, route, IRouter::NeedMoreMaps)); + elapsedSec = timer.ElapsedSeconds(); // routing time + absents fetch time + + if (code == IRouter::NoError) + { + // Route has been found, but could be found a better route if were download absent files + code = IRouter::NeedMoreMaps; + } } + + LogCode(code, elapsedSec); + SendStatistics(startPoint, startDirection, finalPoint, code, route, elapsedSec); + GetPlatform().RunOnGuiThread(bind(callback, route, code)); } void AsyncRouter::SendStatistics(m2::PointD const & startPoint, m2::PointD const & startDirection, diff --git a/routing/async_router.hpp b/routing/async_router.hpp index 5524a01a8e..dda471887f 100644 --- a/routing/async_router.hpp +++ b/routing/async_router.hpp @@ -60,7 +60,7 @@ private: void LogCode(IRouter::ResultCode code, double const elapsedSec); mutex m_paramsMutex; - mutex m_routeMutex; + mutex m_routingMutex; atomic_flag m_isReadyThread; m2::PointD m_startPoint;