From: Joel Rosdahl Date: Tue, 30 Sep 2025 17:37:07 +0000 (+0200) Subject: fix: Use monotonic clock for spin locks in inode cache X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=12dbf2f39530d892835474145d8c033f7605a27f;p=thirdparty%2Fccache.git fix: Use monotonic clock for spin locks in inode cache --- diff --git a/src/ccache/inodecache.cpp b/src/ccache/inodecache.cpp index 83083fcf..29d67e64 100644 --- a/src/ccache/inodecache.cpp +++ b/src/ccache/inodecache.cpp @@ -82,7 +82,7 @@ const uint32_t k_num_buckets = 32 * 1024; const uint32_t k_num_entries = 4; // Maximum time the spin lock loop will try before giving up. -const auto k_max_lock_duration = util::Duration(5); +const std::chrono::seconds k_max_lock_duration{5}; // The memory-mapped file may reside on a filesystem with compression. Memory // accesses to the file risk crashing if such a filesystem gets full, so stop @@ -90,7 +90,7 @@ const auto k_max_lock_duration = util::Duration(5); const uint64_t k_min_fs_mib_left = 100; // 100 MiB // How long a filesystem space check is valid before we make a new one. -const util::Duration k_fs_space_check_valid_duration(1); +const std::chrono::seconds k_fs_space_check_valid_duration{1}; static_assert(std::tuple_size() == 20, "Increment version number if size of digest is changed."); @@ -184,7 +184,7 @@ spin_lock(std::atomic& owner_pid, const pid_t self_pid) pid_t prev_pid = 0; pid_t lock_pid = 0; bool reset_timer = false; - util::TimePoint lock_time; + auto lock_time = std::chrono::steady_clock::now(); while (true) { for (int i = 0; i < 10000; ++i) { lock_pid = owner_pid.load(std::memory_order_relaxed); @@ -204,9 +204,10 @@ spin_lock(std::atomic& owner_pid, const pid_t self_pid) } // If everything is OK, we should never hit this. if (reset_timer) { - lock_time = util::TimePoint::now(); + lock_time = std::chrono::steady_clock::now(); reset_timer = false; - } else if (util::TimePoint::now() - lock_time > k_max_lock_duration) { + } else if (std::chrono::steady_clock::now() - lock_time + > k_max_lock_duration) { return false; } } @@ -447,7 +448,7 @@ InodeCache::initialize() } if (m_fd) { - auto now = util::TimePoint::now(); + auto now = std::chrono::time_point(); if (now > m_last_fs_space_check + k_fs_space_check_valid_duration) { m_last_fs_space_check = now; diff --git a/src/ccache/inodecache.hpp b/src/ccache/inodecache.hpp index bcbb240b..79efe78f 100644 --- a/src/ccache/inodecache.hpp +++ b/src/ccache/inodecache.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2024 Joel Rosdahl and other contributors +// Copyright (C) 2020-2025 Joel Rosdahl and other contributors // // See doc/authors.adoc for a complete list of contributors. // @@ -27,6 +27,7 @@ #include +#include #include #include #include @@ -143,6 +144,6 @@ private: struct SharedRegion* m_sr = nullptr; bool m_failed = false; const pid_t m_self_pid; - util::TimePoint m_last_fs_space_check; + std::chrono::time_point m_last_fs_space_check; util::MemoryMap m_map; };