Only lock the virtual address space when shared memory was requested

In addition, some improvements:

- unlock only when locking succeeded
- scoped exception safe RAII locker

Reference:

- https://github.com/Project-OSRM/osrm-backend/issues/1698#issuecomment-144003177
This commit is contained in:
Daniel J. Hofmann 2015-10-09 17:03:37 +02:00 committed by Patrick Niklaus
parent 74ac283c52
commit e75be68466

View File

@ -91,11 +91,23 @@ int main(int argc, const char *argv[])
}
#ifdef __linux__
const int lock_flags = MCL_CURRENT | MCL_FUTURE;
if (-1 == mlockall(lock_flags))
struct MemoryLocker final
{
SimpleLogger().Write(logWARNING) << argv[0] << " could not be locked to RAM";
explicit MemoryLocker(bool shouldLock_) : shouldLock(shouldLock_)
{
if (-1 == mlockall(MCL_CURRENT | MCL_FUTURE))
{
couldLock = false;
SimpleLogger().Write(logWARNING) << "memory could not be locked to RAM";
}
}
~MemoryLocker()
{
if (couldLock)
(void)munlockall();
}
bool shouldLock = false, couldLock = false;
} memoryLocker(lib_config.use_shared_memory);
#endif
SimpleLogger().Write() << "starting up engines, " << OSRM_VERSION;
@ -177,9 +189,6 @@ int main(int argc, const char *argv[])
SimpleLogger().Write(logWARNING) << "exception: " << e.what();
return 1;
}
#ifdef __linux__
munlockall();
#endif
return 0;
}