125{
126 if (lock_file_path.empty())
127 return;
130 const auto now = std::chrono::steady_clock::now();
132 const auto deadline = now + timeout;
133 bool taken = false;
134 for (;;) {
135 try {
136 if ((taken =
m_slot->file_lock->try_lock()))
137 break;
138 } catch (const std::exception &e) {
139 defer(*
m_slot,
"Cannot lock " + lock_file_path +
": " + e.what());
140 break;
141 }
142 if (std::chrono::steady_clock::now() >= deadline) {
143 defer(*
m_slot,
"Another instance has held " + lock_file_path +
" for over " + std::to_string(timeout.count()) +
" ms");
144 break;
145 }
146 std::this_thread::sleep_for(std::chrono::milliseconds(5));
147 }
148 if (! taken)
149 m_slot->file_lock.reset();
150 }
151
154}
static void defer(Slot &slot, const std::string &reason)
Definition InstanceLock.cpp:94
bool m_locked
Definition InstanceLock.hpp:57
Slot * m_slot
Definition InstanceLock.hpp:55
std::unique_lock< std::recursive_mutex > m_slot_guard
Definition InstanceLock.hpp:56
static Slot & slot_for(const std::string &lock_file_path)
Definition InstanceLock.cpp:80
static bool open_lock_file(Slot &slot, const std::string &lock_file_path)
Definition InstanceLock.cpp:102