]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Allow short-lived lock file to wait for long-lived
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 10 Nov 2022 15:37:08 +0000 (16:37 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 13 Dec 2022 19:36:55 +0000 (20:36 +0100)
src/util/LockFile.cpp
src/util/LockFile.hpp

index f90b762624ed371cf57e9ff076bf8e7f8fc78418..4848eb64d9c675ec23ca2bcf5321f27a2b69f46b 100644 (file)
@@ -73,7 +73,11 @@ private:
 namespace util {
 
 LockFile::LockFile(const std::string& path)
-  : m_lock_file(path + ".lock"),
+  :
+#ifndef _WIN32
+    m_alive_file(path + ".alive"),
+#endif
+    m_lock_file(path + ".lock"),
 #ifndef _WIN32
     m_acquired(false)
 #else
@@ -274,6 +278,16 @@ LockFile::do_acquire(const bool blocking)
   }
 }
 
+std::optional<util::TimePoint>
+LockFile::get_last_lock_update()
+{
+  if (const auto stat = Stat::stat(m_alive_file); stat) {
+    return stat.mtime();
+  } else {
+    return std::nullopt;
+  }
+}
+
 #else // !_WIN32
 
 void*
@@ -337,12 +351,7 @@ ShortLivedLockFile::ShortLivedLockFile(const std::string& path) : LockFile(path)
 {
 }
 
-LongLivedLockFile::LongLivedLockFile(const std::string& path)
-  : LockFile(path)
-#ifndef _WIN32
-    ,
-    m_alive_file(path + ".alive")
-#endif
+LongLivedLockFile::LongLivedLockFile(const std::string& path) : LockFile(path)
 {
 }
 
@@ -395,16 +404,6 @@ LongLivedLockFile::on_before_break()
   return Util::unlink_tmp(m_alive_file);
 }
 
-std::optional<util::TimePoint>
-LongLivedLockFile::get_last_lock_update()
-{
-  if (const auto stat = Stat::stat(m_alive_file); stat) {
-    return stat.mtime();
-  } else {
-    return std::nullopt;
-  }
-}
-
 #endif
 
 LockFileGuard::LockFileGuard(LockFile& lock_file, Mode mode)
index 705dd7b87a6d2f2026a019cc8db62fff25015f19..d5e2f85ae5f223236d2dc5f0717d9ad372b9d5aa 100644 (file)
@@ -48,6 +48,9 @@ public:
 
 protected:
   LockFile(const std::string& path);
+#ifndef _WIN32
+  std::string m_alive_file;
+#endif
 
 private:
   std::string m_lock_file;
@@ -62,8 +65,8 @@ private:
   virtual void on_before_release();
 #ifndef _WIN32
   bool do_acquire(bool blocking);
+  std::optional<util::TimePoint> get_last_lock_update();
   virtual bool on_before_break();
-  virtual std::optional<util::TimePoint> get_last_lock_update();
 #else
   void* do_acquire(bool blocking);
 #endif
@@ -90,7 +93,6 @@ public:
 
 private:
 #ifndef _WIN32
-  std::string m_alive_file;
   std::thread m_keep_alive_thread;
   std::mutex m_stop_keep_alive_mutex;
   bool m_stop_keep_alive = false;
@@ -99,7 +101,6 @@ private:
   void on_after_acquire() override;
   void on_before_release() override;
   bool on_before_break() override;
-  std::optional<util::TimePoint> get_last_lock_update() override;
 #endif
 };
 
@@ -135,12 +136,6 @@ LockFile::on_before_break()
   return true;
 }
 
-inline std::optional<util::TimePoint>
-LockFile::get_last_lock_update()
-{
-  return std::nullopt;
-}
-
 #endif
 
 } // namespace util