Only keep reader lock on shared memory during queries.

This commit is contained in:
Patrick Niklaus
2016-10-12 01:09:20 +02:00
committed by Patrick Niklaus
parent c69545c47a
commit 847f530c8e
7 changed files with 174 additions and 301 deletions
+23
View File
@@ -183,6 +183,29 @@ struct SharedDataTimestamp
unsigned timestamp;
};
inline std::string regionToString(const SharedDataType region)
{
switch (region)
{
case CURRENT_REGIONS:
return "CURRENT_REGIONS";
case LAYOUT_1:
return "LAYOUT_1";
case DATA_1:
return "DATA_1";
case LAYOUT_2:
return "LAYOUT_2";
case DATA_2:
return "DATA_2";
case LAYOUT_NONE:
return "LAYOUT_NONE";
case DATA_NONE:
return "DATA_NONE";
default:
return "INVALID_REGION";
}
}
static_assert(sizeof(block_id_to_name) / sizeof(*block_id_to_name) == SharedDataLayout::NUM_BLOCKS,
"Number of blocks needs to match the number of Block names.");
}
+29 -144
View File
@@ -42,40 +42,6 @@ struct OSRMLockFile
#ifndef _WIN32
class SharedMemory
{
// Remove shared memory on destruction
class shm_remove
{
private:
int m_shmid;
public:
shm_remove() : m_shmid(INT_MAX) {}
shm_remove(int shmid) : m_shmid(shmid) {}
shm_remove(shm_remove &&other) : m_shmid(std::move(other.m_shmid)) {}
shm_remove(const shm_remove &) = delete;
shm_remove &operator=(const shm_remove &) = delete;
shm_remove &operator=(const shm_remove &&other)
{
m_shmid = other.m_shmid;
return *this;
}
~shm_remove()
{
if (m_shmid != INT_MAX)
{
util::SimpleLogger().Write(logDEBUG) << "automatic memory deallocation of "
<< m_shmid;
if (!boost::interprocess::xsi_shared_memory::remove(m_shmid))
{
util::SimpleLogger().Write(logDEBUG) << "could not deallocate id " << m_shmid;
}
}
}
};
public:
void *Ptr() const { return region.get_address(); }
@@ -86,28 +52,24 @@ class SharedMemory
SharedMemory(const boost::filesystem::path &lock_file,
const IdentifierT id,
const uint64_t size = 0,
bool read_write = false,
bool remove_prev = true,
bool owner = true)
bool read_write = false)
: key(lock_file.string().c_str(), id)
{
const auto access =
read_write ? boost::interprocess::read_write : boost::interprocess::read_only;
// open only
if (0 == size)
{ // read_only
{
shm = boost::interprocess::xsi_shared_memory(boost::interprocess::open_only, key);
util::SimpleLogger().Write(logDEBUG) << "opening " << shm.get_shmid() << " from id "
<< id;
region = boost::interprocess::mapped_region(
shm,
(read_write ? boost::interprocess::read_write : boost::interprocess::read_only));
region = boost::interprocess::mapped_region(shm, access);
}
// open or create
else
{ // writeable pointer
// remove previously allocated mem
if (remove_prev)
{
Remove(key);
}
{
shm = boost::interprocess::xsi_shared_memory(
boost::interprocess::open_or_create, key, size);
util::SimpleLogger().Write(logDEBUG) << "opening/creating " << shm.get_shmid()
@@ -121,12 +83,7 @@ class SharedMemory
}
}
#endif
region = boost::interprocess::mapped_region(shm, boost::interprocess::read_write);
if (owner)
{
remover = shm_remove{shm.get_shmid()};
}
region = boost::interprocess::mapped_region(shm, access);
}
}
@@ -161,36 +118,28 @@ class SharedMemory
{
boost::interprocess::xsi_shared_memory shm(boost::interprocess::open_only, key);
}
catch (...)
{
result = false;
}
return result;
}
static bool Remove(const boost::interprocess::xsi_key &key)
{
bool ret = false;
try
{
boost::interprocess::xsi_shared_memory xsi(boost::interprocess::open_only, key);
util::SimpleLogger().Write(logDEBUG) << "deallocating prev memory " << xsi.get_shmid();
ret = boost::interprocess::xsi_shared_memory::remove(xsi.get_shmid());
}
catch (const boost::interprocess::interprocess_exception &e)
{
if (e.get_error_code() != boost::interprocess::not_found_error)
{
throw;
}
result = false;
}
return ret;
return result;
}
static bool Remove(const boost::interprocess::xsi_key &key)
{
boost::interprocess::xsi_shared_memory xsi(boost::interprocess::open_only, key);
util::SimpleLogger().Write(logDEBUG) << "deallocating prev memory " << xsi.get_shmid();
return boost::interprocess::xsi_shared_memory::remove(xsi.get_shmid());
}
boost::interprocess::xsi_key key;
boost::interprocess::xsi_shared_memory shm;
boost::interprocess::mapped_region region;
shm_remove remover;
};
#else
// Windows - specific code
@@ -198,31 +147,6 @@ class SharedMemory
{
SharedMemory(const SharedMemory &) = delete;
SharedMemory &operator=(const SharedMemory &) = delete;
// Remove shared memory on destruction
class shm_remove
{
private:
char *m_shmid;
public:
shm_remove() : m_shmid(nullptr) {}
shm_remove(char *id) : m_shmid(id) {}
shm_remove(const shm_remove &) = delete;
shm_remove &operator=(const shm_remove &) = delete;
~shm_remove()
{
if (m_shmid != nullptr)
{
util::SimpleLogger().Write(logDEBUG) << "automatic memory deallocation";
if (!boost::interprocess::shared_memory_object::remove(m_shmid))
{
util::SimpleLogger().Write(logDEBUG) << "could not deallocate id " << m_shmid;
}
}
}
};
public:
void *Ptr() const { return region.get_address(); }
@@ -230,36 +154,25 @@ class SharedMemory
SharedMemory(const boost::filesystem::path &lock_file,
const int id,
const uint64_t size = 0,
bool read_write = false,
bool remove_prev = true,
bool owner = false)
bool read_write = false)
{
sprintf(key, "%s.%d", "osrm.lock", id);
auto access = read_write ? boost::interprocess::read_write : boost::interprocess::read_only;
if (0 == size)
{ // read_only
shm = boost::interprocess::shared_memory_object(
boost::interprocess::open_only,
key,
read_write ? boost::interprocess::read_write : boost::interprocess::read_only);
region = boost::interprocess::mapped_region(
shm, read_write ? boost::interprocess::read_write : boost::interprocess::read_only);
region = boost::interprocess::mapped_region(shm, access);
}
else
{ // writeable pointer
// remove previously allocated mem
if (remove_prev)
{
Remove(key);
}
shm = boost::interprocess::shared_memory_object(
boost::interprocess::open_or_create, key, boost::interprocess::read_write);
shm.truncate(size);
region = boost::interprocess::mapped_region(shm, boost::interprocess::read_write);
region = boost::interprocess::mapped_region(shm, access);
if (owner)
{
remover = shm_remover{key};
}
util::SimpleLogger().Write(logDEBUG) << "writeable memory allocated " << size
<< " bytes";
}
@@ -308,35 +221,19 @@ class SharedMemory
static bool Remove(char *key)
{
bool ret = false;
try
{
util::SimpleLogger().Write(logDEBUG) << "deallocating prev memory for key " << key;
ret = boost::interprocess::shared_memory_object::remove(key);
}
catch (const boost::interprocess::interprocess_exception &e)
{
if (e.get_error_code() != boost::interprocess::not_found_error)
{
throw;
}
}
return ret;
util::SimpleLogger().Write(logDEBUG) << "deallocating prev memory for key " << key;
return boost::interprocess::shared_memory_object::remove(key);
}
char key[500];
boost::interprocess::shared_memory_object shm;
boost::interprocess::mapped_region region;
shm_remove remover;
};
#endif
template <typename IdentifierT, typename LockFileT = OSRMLockFile>
std::unique_ptr<SharedMemory> makeSharedMemory(const IdentifierT &id,
const uint64_t size = 0,
bool read_write = false,
bool remove_prev = true,
bool owner = false)
std::unique_ptr<SharedMemory>
makeSharedMemory(const IdentifierT &id, const uint64_t size = 0, bool read_write = false)
{
try
{
@@ -352,8 +249,7 @@ std::unique_ptr<SharedMemory> makeSharedMemory(const IdentifierT &id,
boost::filesystem::ofstream ofs(lock_file());
}
}
return std::make_unique<SharedMemory>(
lock_file(), id, size, read_write, remove_prev, owner);
return std::make_unique<SharedMemory>(lock_file(), id, size, read_write);
}
catch (const boost::interprocess::interprocess_exception &e)
{
@@ -362,17 +258,6 @@ std::unique_ptr<SharedMemory> makeSharedMemory(const IdentifierT &id,
throw util::exception(e.what());
}
}
template <typename IdentifierT>
std::unique_ptr<SharedMemory> makeSharedMemoryView(const IdentifierT &id)
{
return makeSharedMemory(id, 0, false, false, false);
}
template <typename IdentifierT>
std::unique_ptr<SharedMemory> makeOwnedSharedMemoryView(const IdentifierT &id)
{
return makeSharedMemory(id, 0, false, false, true);
}
}
}