read_only if shmem size is 0, read_write otherwise

This commit is contained in:
Michael Krasnyk 2016-12-25 16:53:10 +01:00 committed by Patrick Niklaus
parent fe2beb6f68
commit 774b8688ca
2 changed files with 7 additions and 10 deletions

View File

@ -52,12 +52,9 @@ class SharedMemory
template <typename IdentifierT> template <typename IdentifierT>
SharedMemory(const boost::filesystem::path &lock_file, SharedMemory(const boost::filesystem::path &lock_file,
const IdentifierT id, const IdentifierT id,
const uint64_t size = 0, const uint64_t size = 0)
bool read_write = false)
: key(lock_file.string().c_str(), id) : key(lock_file.string().c_str(), id)
{ {
const auto access =
read_write ? boost::interprocess::read_write : boost::interprocess::read_only;
// open only // open only
if (0 == size) if (0 == size)
{ {
@ -65,7 +62,7 @@ class SharedMemory
util::Log(logDEBUG) << "opening " << shm.get_shmid() << " from id " << id; util::Log(logDEBUG) << "opening " << shm.get_shmid() << " from id " << id;
region = boost::interprocess::mapped_region(shm, access); region = boost::interprocess::mapped_region(shm, boost::interprocess::read_only);
} }
// open or create // open or create
else else
@ -83,7 +80,7 @@ class SharedMemory
} }
} }
#endif #endif
region = boost::interprocess::mapped_region(shm, access); region = boost::interprocess::mapped_region(shm, boost::interprocess::read_write);
} }
} }
@ -232,7 +229,7 @@ class SharedMemory
template <typename IdentifierT, typename LockFileT = OSRMLockFile> template <typename IdentifierT, typename LockFileT = OSRMLockFile>
std::unique_ptr<SharedMemory> std::unique_ptr<SharedMemory>
makeSharedMemory(const IdentifierT &id, const uint64_t size = 0, bool read_write = false) makeSharedMemory(const IdentifierT &id, const uint64_t size = 0)
{ {
try try
{ {
@ -248,7 +245,7 @@ makeSharedMemory(const IdentifierT &id, const uint64_t size = 0, bool read_write
boost::filesystem::ofstream ofs(lock_file()); boost::filesystem::ofstream ofs(lock_file());
} }
} }
return std::make_unique<SharedMemory>(lock_file(), id, size, read_write); return std::make_unique<SharedMemory>(lock_file(), id, size);
} }
catch (const boost::interprocess::interprocess_exception &e) catch (const boost::interprocess::interprocess_exception &e)
{ {

View File

@ -175,14 +175,14 @@ Storage::ReturnCode Storage::Run(int max_wait)
// Allocate shared memory block // Allocate shared memory block
auto regions_size = sizeof(layout) + layout.GetSizeOfLayout(); auto regions_size = sizeof(layout) + layout.GetSizeOfLayout();
util::Log() << "allocating shared memory of " << regions_size << " bytes"; util::Log() << "allocating shared memory of " << regions_size << " bytes";
auto shared_memory = makeSharedMemory(data_region, regions_size, true); auto shared_memory = makeSharedMemory(data_region, regions_size);
// Copy memory layout to shared memory and populate data // Copy memory layout to shared memory and populate data
char *shared_memory_ptr = static_cast<char *>(shared_memory->Ptr()); char *shared_memory_ptr = static_cast<char *>(shared_memory->Ptr());
memcpy(shared_memory_ptr, &layout, sizeof(layout)); memcpy(shared_memory_ptr, &layout, sizeof(layout));
PopulateData(layout, shared_memory_ptr + sizeof(layout)); PopulateData(layout, shared_memory_ptr + sizeof(layout));
auto data_type_memory = makeSharedMemory(CURRENT_REGION, sizeof(SharedDataTimestamp), true); auto data_type_memory = makeSharedMemory(CURRENT_REGION, sizeof(SharedDataTimestamp));
SharedDataTimestamp *data_timestamp_ptr = SharedDataTimestamp *data_timestamp_ptr =
static_cast<SharedDataTimestamp *>(data_type_memory->Ptr()); static_cast<SharedDataTimestamp *>(data_type_memory->Ptr());