From: Joel Rosdahl Date: Thu, 10 Nov 2022 15:37:08 +0000 (+0100) Subject: enhance: Allow short-lived lock file to wait for long-lived X-Git-Tag: v4.8~86 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4c8873296434e00015d956e55508b37372ba842e;p=thirdparty%2Fccache.git enhance: Allow short-lived lock file to wait for long-lived --- diff --git a/src/util/LockFile.cpp b/src/util/LockFile.cpp index f90b76262..4848eb64d 100644 --- a/src/util/LockFile.cpp +++ b/src/util/LockFile.cpp @@ -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 +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 -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) diff --git a/src/util/LockFile.hpp b/src/util/LockFile.hpp index 705dd7b87..d5e2f85ae 100644 --- a/src/util/LockFile.hpp +++ b/src/util/LockFile.hpp @@ -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 get_last_lock_update(); virtual bool on_before_break(); - virtual std::optional 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 get_last_lock_update() override; #endif }; @@ -135,12 +136,6 @@ LockFile::on_before_break() return true; } -inline std::optional -LockFile::get_last_lock_update() -{ - return std::nullopt; -} - #endif } // namespace util