From: Joel Rosdahl Date: Thu, 5 Jan 2023 10:38:55 +0000 (+0100) Subject: enhance: Delay LongLivedLockFileManager thread start to first register X-Git-Tag: v4.8~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=35a387e15924d5982d2a812759fb476a547b619a;p=thirdparty%2Fccache.git enhance: Delay LongLivedLockFileManager thread start to first register --- diff --git a/src/test_lockfile.cpp b/src/test_lockfile.cpp index 25dc72808..68286c99e 100644 --- a/src/test_lockfile.cpp +++ b/src/test_lockfile.cpp @@ -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 lock_manager; + util::LongLivedLockFileManager lock_manager; util::LockFile lock(path); - if (long_lived) { - lock_manager = std::make_unique(); - 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}); diff --git a/src/util/LockFile.cpp b/src/util/LockFile.cpp index 467c7b2b3..43ed32cd2 100644 --- a/src/util/LockFile.cpp +++ b/src/util/LockFile.cpp @@ -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 } diff --git a/src/util/LongLivedLockFileManager.cpp b/src/util/LongLivedLockFileManager.cpp index e0bdf1e94..71dc0eb3d 100644 --- a/src/util/LongLivedLockFileManager.cpp +++ b/src/util/LongLivedLockFileManager.cpp @@ -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 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 lock(m_mutex); - m_stop = true; + if (m_thread.joinable()) { + LOG_RAW("Stopping keep-alive thread"); + { + std::unique_lock 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 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 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 diff --git a/src/util/LongLivedLockFileManager.hpp b/src/util/LongLivedLockFileManager.hpp index 72fdc5b47..5b118cec2 100644 --- a/src/util/LongLivedLockFileManager.hpp +++ b/src/util/LongLivedLockFileManager.hpp @@ -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 m_alive_files; + + void start_thread(); #endif };