-// Copyright (C) 2022 Joel Rosdahl and other contributors
+// Copyright (C) 2022-2023 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
return 1;
}
- std::unique_ptr<util::LongLivedLockFileManager> lock_manager;
+ util::LongLivedLockFileManager lock_manager;
util::LockFile lock(path);
- if (long_lived) {
- lock_manager = std::make_unique<util::LongLivedLockFileManager>();
- lock.make_long_lived(*lock_manager);
- }
if (blocking) {
PRINT_RAW(stdout, "Acquiring\n");
lock.acquire();
lock.try_acquire();
}
if (lock.acquired()) {
+ if (long_lived) {
+ lock.make_long_lived(lock_manager);
+ }
PRINT_RAW(stdout, "Acquired\n");
PRINT(stdout, "Sleeping {} second{}\n", *seconds, *seconds == 1 ? "" : "s");
std::this_thread::sleep_for(std::chrono::seconds{*seconds});
-// Copyright (C) 2022 Joel Rosdahl and other contributors
+// Copyright (C) 2022-2023 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
LongLivedLockFileManager::LongLivedLockFileManager()
{
-#ifndef _WIN32
- LOG_RAW("Starting keep-alive thread");
- m_thread = std::thread([&] {
- auto awake_time = std::chrono::steady_clock::now();
- while (true) {
- std::unique_lock<std::mutex> lock(m_mutex);
- m_stop_condition.wait_until(lock, awake_time, [this] { return m_stop; });
- if (m_stop) {
- return;
- }
- for (const auto& alive_file : m_alive_files) {
- util::set_timestamps(alive_file);
- }
- awake_time += k_keep_alive_interval;
- }
- });
- LOG_RAW("Started keep-alive thread");
-#endif
}
LongLivedLockFileManager::~LongLivedLockFileManager()
{
#ifndef _WIN32
- LOG_RAW("Stopping keep-alive thread");
- {
- std::unique_lock<std::mutex> lock(m_mutex);
- m_stop = true;
+ if (m_thread.joinable()) {
+ LOG_RAW("Stopping keep-alive thread");
+ {
+ std::unique_lock<std::mutex> lock(m_mutex);
+ m_stop = true;
+ }
+ m_stop_condition.notify_one();
+ m_thread.join();
+ LOG_RAW("Stopped keep-alive thread");
}
- m_stop_condition.notify_one();
- m_thread.join();
- LOG_RAW("Stopped keep-alive thread");
#endif
}
{
#ifndef _WIN32
std::unique_lock<std::mutex> lock(m_mutex);
+ if (!m_thread.joinable()) {
+ start_thread();
+ }
m_alive_files.insert(path);
#endif
}
#endif
}
+#ifndef _WIN32
+void
+LongLivedLockFileManager::start_thread()
+{
+ LOG_RAW("Starting keep-alive thread");
+ m_thread = std::thread([&] {
+ auto awake_time = std::chrono::steady_clock::now();
+ while (true) {
+ std::unique_lock<std::mutex> lock(m_mutex);
+ m_stop_condition.wait_until(lock, awake_time, [this] { return m_stop; });
+ if (m_stop) {
+ return;
+ }
+ for (const auto& alive_file : m_alive_files) {
+ util::set_timestamps(alive_file);
+ }
+ awake_time += k_keep_alive_interval;
+ }
+ });
+ LOG_RAW("Started keep-alive thread");
+}
+#endif
+
} // namespace util