Use early exit/continue to simplify code and reduce indentation, move increase/decrease query count into functions

This commit is contained in:
Dennis Luxen 2015-01-22 12:35:46 +01:00
parent a8db29399f
commit c881aa7b32
2 changed files with 61 additions and 42 deletions

View File

@ -100,12 +100,49 @@ void OSRM_impl::RegisterPlugin(BasePlugin *plugin)
int OSRM_impl::RunQuery(RouteParameters &route_parameters, JSON::Object &json_result) 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) 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<boost::interprocess::named_mutex> 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 // lock update pending
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> pending_lock( boost::interprocess::scoped_lock<boost::interprocess::named_mutex> pending_lock(
barrier->pending_update_mutex); barrier->pending_update_mutex);
@ -124,30 +161,7 @@ int OSRM_impl::RunQuery(RouteParameters &route_parameters, JSON::Object &json_re
->CheckAndReloadFacade(); ->CheckAndReloadFacade();
} }
iter->second->HandleRequest(route_parameters, json_result);
if (barrier)
{
// lock query
boost::interprocess::scoped_lock<boost::interprocess::named_mutex> 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;
}
// proxy code for compilation firewall // proxy code for compilation firewall
OSRM::OSRM(libosrm_config &lib_config) OSRM::OSRM(libosrm_config &lib_config)
: OSRM_pimpl_(osrm::make_unique<OSRM_impl>(lib_config)) : OSRM_pimpl_(osrm::make_unique<OSRM_impl>(lib_config))
{ {

View File

@ -62,6 +62,11 @@ class OSRM_impl
std::unique_ptr<SharedBarriers> barrier; std::unique_ptr<SharedBarriers> barrier;
// base class pointer to the objects // base class pointer to the objects
BaseDataFacade<QueryEdge::EdgeData> *query_data_facade; BaseDataFacade<QueryEdge::EdgeData> *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 #endif // OSRM_IMPL_H