From c881aa7b32954a45017c747f2a170ed91f4fb35d Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Thu, 22 Jan 2015 12:35:46 +0100 Subject: [PATCH] Use early exit/continue to simplify code and reduce indentation, move increase/decrease query count into functions --- Library/OSRM_impl.cpp | 98 ++++++++++++++++++++++++------------------- Library/OSRM_impl.h | 5 +++ 2 files changed, 61 insertions(+), 42 deletions(-) diff --git a/Library/OSRM_impl.cpp b/Library/OSRM_impl.cpp index b03cc5e2d..aa8eb3d1e 100644 --- a/Library/OSRM_impl.cpp +++ b/Library/OSRM_impl.cpp @@ -100,54 +100,68 @@ void OSRM_impl::RegisterPlugin(BasePlugin *plugin) int OSRM_impl::RunQuery(RouteParameters &route_parameters, JSON::Object &json_result) { - const PluginMap::const_iterator &iter = plugin_map.find(route_parameters.service); + const auto &plugin_iterator = plugin_map.find(route_parameters.service); - if (plugin_map.end() != iter) + if (plugin_map.end() == plugin_iterator) { - if (barrier) - { - // lock update pending - boost::interprocess::scoped_lock pending_lock( - barrier->pending_update_mutex); - - // lock query - boost::interprocess::scoped_lock query_lock( - barrier->query_mutex); - - // unlock update pending - pending_lock.unlock(); - - // increment query count - ++(barrier->number_of_queries); - - (static_cast *>(query_data_facade)) - ->CheckAndReloadFacade(); - } - - iter->second->HandleRequest(route_parameters, json_result); - if (barrier) - { - // lock query - boost::interprocess::scoped_lock query_lock( - barrier->query_mutex); - - // decrement query count - --(barrier->number_of_queries); - BOOST_ASSERT_MSG(0 <= barrier->number_of_queries, "invalid number of queries"); - - // notify all processes that were waiting for this condition - if (0 == barrier->number_of_queries) - { - barrier->no_running_queries_condition.notify_all(); - } - } - return 200; + return 400; } - return 400; + + increase_concurrent_query_count(); + plugin_iterator->second->HandleRequest(route_parameters, json_result); + decrease_concurrent_query_count(); + return 200; +} + +// decrease number of concurrent queries +void OSRM_impl::decrease_concurrent_query_count() +{ + if (!barrier) + { + return; + } + // lock query + boost::interprocess::scoped_lock query_lock( + barrier->query_mutex); + + // decrement query count + --(barrier->number_of_queries); + BOOST_ASSERT_MSG(0 <= barrier->number_of_queries, "invalid number of queries"); + + // notify all processes that were waiting for this condition + if (0 == barrier->number_of_queries) + { + barrier->no_running_queries_condition.notify_all(); + } +} + +// increase number of concurrent queries +void OSRM_impl::increase_concurrent_query_count() +{ + if (!barrier) + { + return; + } + + // lock update pending + boost::interprocess::scoped_lock pending_lock( + barrier->pending_update_mutex); + + // lock query + boost::interprocess::scoped_lock query_lock( + barrier->query_mutex); + + // unlock update pending + pending_lock.unlock(); + + // increment query count + ++(barrier->number_of_queries); + + (static_cast *>(query_data_facade)) + ->CheckAndReloadFacade(); } // proxy code for compilation firewall - OSRM::OSRM(libosrm_config &lib_config) : OSRM_pimpl_(osrm::make_unique(lib_config)) { diff --git a/Library/OSRM_impl.h b/Library/OSRM_impl.h index 625fc19ca..4568f47d3 100644 --- a/Library/OSRM_impl.h +++ b/Library/OSRM_impl.h @@ -62,6 +62,11 @@ class OSRM_impl std::unique_ptr barrier; // base class pointer to the objects BaseDataFacade *query_data_facade; + + // decrease number of concurrent queries + void decrease_concurrent_query_count(); + // increase number of concurrent queries + void increase_concurrent_query_count(); }; #endif // OSRM_IMPL_H