]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Use monotonic clock for spin locks in inode cache
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 30 Sep 2025 17:37:07 +0000 (19:37 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 30 Sep 2025 17:37:07 +0000 (19:37 +0200)
src/ccache/inodecache.cpp
src/ccache/inodecache.hpp

index 83083fcf5c256d12bc28452de7979f4d3d480e1d..29d67e6421a2db4833fbfdbb921719b54782b445 100644 (file)
@@ -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<Hash::Digest>() == 20,
               "Increment version number if size of digest is changed.");
@@ -184,7 +184,7 @@ spin_lock(std::atomic<pid_t>& 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<pid_t>& 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<std::chrono::steady_clock>();
     if (now > m_last_fs_space_check + k_fs_space_check_valid_duration) {
       m_last_fs_space_check = now;
 
index bcbb240bf1e77e0935424bd9fd1fa4944de3b32b..79efe78fb261848d283765e53733f75f18064200 100644 (file)
@@ -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 <sys/types.h>
 
+#include <chrono>
 #include <cstdint>
 #include <filesystem>
 #include <functional>
@@ -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<std::chrono::steady_clock> m_last_fs_space_check;
   util::MemoryMap m_map;
 };