]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Delay LongLivedLockFileManager thread start to first register
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 5 Jan 2023 10:38:55 +0000 (11:38 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 15 Jan 2023 20:33:57 +0000 (21:33 +0100)
src/test_lockfile.cpp
src/util/LockFile.cpp
src/util/LongLivedLockFileManager.cpp
src/util/LongLivedLockFileManager.hpp

index 25dc7280807cbaf1ef8cc98143332bae73566ad7..68286c99eba3ada7bbcdc062ea58156283a434d7 100644 (file)
@@ -1,4 +1,4 @@
-// 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.
 //
@@ -49,12 +49,8 @@ main(int argc, char** argv)
     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();
@@ -63,6 +59,9 @@ main(int argc, char** argv)
     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});
index 467c7b2b34ae08bcd70c3b856c20efeaa521082c..43ed32cd2885391e41d81b9b8378be3386ded99f 100644 (file)
@@ -125,6 +125,9 @@ LockFile::make_long_lived(
 {
 #ifndef _WIN32
   m_lock_manager = &lock_manager;
+  if (acquired()) {
+    m_lock_manager->register_alive_file(m_alive_file);
+  }
 #endif
 }
 
index e0bdf1e946088bbed7affaeae3a784ffd88eb0b3..71dc0eb3d66b122e588ab8cea09b0c3e47754f78 100644 (file)
@@ -1,4 +1,4 @@
-// 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.
 //
@@ -32,37 +32,21 @@ namespace util {
 
 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
 }
 
@@ -72,6 +56,9 @@ LongLivedLockFileManager::register_alive_file(
 {
 #ifndef _WIN32
   std::unique_lock<std::mutex> lock(m_mutex);
+  if (!m_thread.joinable()) {
+    start_thread();
+  }
   m_alive_files.insert(path);
 #endif
 }
@@ -86,4 +73,27 @@ LongLivedLockFileManager::deregister_alive_file(
 #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
index 72fdc5b477c4ac5e709a74506f552fcccd2dba75..5b118cec2039d3d7ff309a58cb12105f8c7fa6b4 100644 (file)
@@ -1,4 +1,4 @@
-// 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.
 //
@@ -44,6 +44,8 @@ private:
   std::condition_variable m_stop_condition;
   bool m_stop = false;
   std::set<std::string> m_alive_files;
+
+  void start_thread();
 #endif
 };