#ifndef OSRM_RETRY_TIMED_LOCK_HPP #define OSRM_RETRY_TIMED_LOCK_HPP #include #include #include #include class RetryLock { public: RetryLock(int timeout_seconds, const char *name) : timeout_seconds(timeout_seconds), name(name), mutex(std::make_unique( boost::interprocess::open_or_create, name)), internal_lock(*mutex, boost::interprocess::defer_lock) { } bool TryLock() { if (timeout_seconds >= 0) { return internal_lock.timed_lock(boost::posix_time::microsec_clock::universal_time() + boost::posix_time::seconds(timeout_seconds)); } else { internal_lock.lock(); return true; } } void ForceLock() { mutex.reset(); boost::interprocess::named_mutex::remove(name); mutex = std::make_unique( boost::interprocess::open_or_create, name); } private: int timeout_seconds; const char *name; std::unique_ptr mutex; boost::interprocess::scoped_lock internal_lock; }; #endif