From 19a457ab7dbbfee8eed09e5cffbadfb938b89daf Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Thu, 17 Oct 2013 18:11:53 +0200 Subject: [PATCH] implement update barrier --- Library/OSRM.cpp | 26 ++++++++++++++++++++++++-- Library/OSRM.h | 9 +++++++++ Server/DataStructures/SharedBarriers.h | 18 ++++++++++++++++++ datastore.cpp | 1 + 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 Server/DataStructures/SharedBarriers.h diff --git a/Library/OSRM.cpp b/Library/OSRM.cpp index 46d0a7fe0..601691cba 100644 --- a/Library/OSRM.cpp +++ b/Library/OSRM.cpp @@ -26,21 +26,36 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "OSRM.h" -#include -OSRM::OSRM( const ServerPaths & server_paths, const bool use_shared_memory ) { +OSRM::OSRM( const ServerPaths & server_paths, const bool use_shared_memory ) + : + shm( + boost::interprocess::open_or_create, + "SharedBarriers", + boost::interprocess::read_write + ), + use_shared_memory(use_shared_memory) +{ if( !use_shared_memory ) { SimpleLogger().Write() << "loading data into internal memory"; query_data_facade = new InternalDataFacade( server_paths ); } else { + region = boost::interprocess::mapped_region( + shm, //What to map + boost::interprocess::read_write //Map it as read-write + ); + shm.truncate( sizeof(SharedBarriers) ); + barrier = static_cast( region.get_address() ); + SimpleLogger().Write() << "loading data from shared memory"; query_data_facade = new SharedDataFacade( server_paths ); } + //The following plugins handle all requests. RegisterPlugin( new HelloWorldPlugin() @@ -88,6 +103,13 @@ void OSRM::RunQuery(RouteParameters & route_parameters, http::Reply & reply) { if(plugin_map.end() != iter) { reply.status = http::Reply::ok; + if( use_shared_memory && barrier->update_ongoing ) { + //wait until we get the mutex and free it immediately + //TODO: increment semaphore of querying processes + boost::interprocess::scoped_lock< + boost::interprocess::interprocess_mutex + > lock(barrier->update_mutex); + } iter->second->HandleRequest(route_parameters, reply ); } else { reply = http::Reply::stockReply(http::Reply::badRequest); diff --git a/Library/OSRM.h b/Library/OSRM.h index 6951f69a6..4b24fbad1 100644 --- a/Library/OSRM.h +++ b/Library/OSRM.h @@ -45,9 +45,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "../Util/ProgramOptions.h" #include "../Util/SimpleLogger.h" #include "../Server/BasicDatastructures.h" +#include "../Server/DataStructures/SharedBarriers.h" #include #include +#include +#include +#include +#include #include #include #include @@ -57,7 +62,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. class OSRM : boost::noncopyable { private: typedef boost::unordered_map PluginMap; + boost::interprocess::shared_memory_object shm; + boost::interprocess::mapped_region region; + SharedBarriers * barrier; + bool use_shared_memory; public: OSRM( const ServerPaths & paths, diff --git a/Server/DataStructures/SharedBarriers.h b/Server/DataStructures/SharedBarriers.h new file mode 100644 index 000000000..4f43403e2 --- /dev/null +++ b/Server/DataStructures/SharedBarriers.h @@ -0,0 +1,18 @@ +#include +#include + +struct SharedBarriers { + + SharedBarriers () : update_ongoing(false), number_of_queries(0) { } + + // Mutex to protect access to the boolean variable + boost::interprocess::interprocess_mutex update_mutex; + boost::interprocess::interprocess_mutex query_mutex; + + // Condition that no update is running + boost::interprocess::interprocess_condition update_finished_condition; + + // Is there any message? + bool update_ongoing; + int number_of_queries; +}; diff --git a/datastore.cpp b/datastore.cpp index db08496c8..a97edf18c 100644 --- a/datastore.cpp +++ b/datastore.cpp @@ -33,6 +33,7 @@ or see http://www.gnu.org/licenses/agpl.txt. #include #include +#include #include #include