diff --git a/routing/routing_session.cpp b/routing/routing_session.cpp index c52c7ef93f..9848fce56a 100644 --- a/routing/routing_session.cpp +++ b/routing/routing_session.cpp @@ -770,17 +770,8 @@ void RoutingSession::OnTrafficInfoRemoved(MwmSet::MwmId const & mwmId) }); } -shared_ptr RoutingSession::GetTrafficInfo(MwmSet::MwmId const & mwmId) const -{ - CHECK_THREAD_CHECKER(m_threadChecker, ()); - return TrafficCache::GetTrafficInfo(mwmId); -} - void RoutingSession::CopyTraffic(std::map> & trafficColoring) const { - // @TODO(bykoianko) Should be called form gui thread before CalculateRoute() to prepare - // traffic jams for routing. -// CHECK_THREAD_CHECKER(m_threadChecker, ()); TrafficCache::CopyTraffic(trafficColoring); } diff --git a/routing/routing_session.hpp b/routing/routing_session.hpp index 486b207556..2a8ca78be5 100644 --- a/routing/routing_session.hpp +++ b/routing/routing_session.hpp @@ -170,13 +170,14 @@ public: // RoutingObserver overrides: void OnTrafficInfoClear() override; - /// \note. This method may be called from any thread. + /// \note. This method may be called from any thread because it touches class data on gui thread. void OnTrafficInfoAdded(traffic::TrafficInfo && info) override; - /// \note. This method may be called from any thread. + /// \note. This method may be called from any thread because it touches class data on gui thread. void OnTrafficInfoRemoved(MwmSet::MwmId const & mwmId) override; // TrafficCache overrides: - shared_ptr GetTrafficInfo(MwmSet::MwmId const & mwmId) const override; + /// \note. This method may be called from any thread because it touches only data + /// protected by mutex in TrafficCache class. void CopyTraffic(std::map> & trafficColoring) const override; private: diff --git a/traffic/traffic_cache.cpp b/traffic/traffic_cache.cpp index 0819fd207e..6c1b2cab06 100644 --- a/traffic/traffic_cache.cpp +++ b/traffic/traffic_cache.cpp @@ -6,25 +6,26 @@ using namespace std; void TrafficCache::Set(MwmSet::MwmId const & mwmId, shared_ptr coloring) { + lock_guard guard(mutex); m_trafficColoring[mwmId] = coloring; } -void TrafficCache::Remove(MwmSet::MwmId const & mwmId) { m_trafficColoring.erase(mwmId); } - -shared_ptr TrafficCache::GetTrafficInfo(MwmSet::MwmId const & mwmId) const +void TrafficCache::Remove(MwmSet::MwmId const & mwmId) { - auto it = m_trafficColoring.find(mwmId); - - if (it == m_trafficColoring.cend()) - return shared_ptr(); - return it->second; + lock_guard guard(mutex); + m_trafficColoring.erase(mwmId); } void TrafficCache::CopyTraffic( map> & trafficColoring) const { + lock_guard guard(mutex); trafficColoring = m_trafficColoring; } -void TrafficCache::Clear() { m_trafficColoring.clear(); } +void TrafficCache::Clear() +{ + lock_guard guard(mutex); + m_trafficColoring.clear(); +} } // namespace traffic diff --git a/traffic/traffic_cache.hpp b/traffic/traffic_cache.hpp index 815d701a71..33cac28938 100644 --- a/traffic/traffic_cache.hpp +++ b/traffic/traffic_cache.hpp @@ -6,6 +6,7 @@ #include #include +#include namespace traffic { @@ -15,8 +16,6 @@ public: TrafficCache() : m_trafficColoring() {} virtual ~TrafficCache() = default; - virtual shared_ptr GetTrafficInfo( - MwmSet::MwmId const & mwmId) const; virtual void CopyTraffic( std::map> & trafficColoring) const; @@ -27,6 +26,7 @@ protected: void Clear(); private: + std::mutex m_mutex; std::map> m_trafficColoring; }; } // namespace traffic