]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
enhance: Make ProgressBar thread safe
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 30 Oct 2025 17:43:23 +0000 (18:43 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 12 Nov 2025 20:09:55 +0000 (21:09 +0100)
src/ccache/progressbar.cpp
src/ccache/progressbar.hpp
src/ccache/storage/local/localstorage.cpp

index b13e5b8b10e17d0ffa0726b639ece42908bcf184..fe51355437b87a129b40acbb77e15264e6f75d8e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019-2024 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2025 Joel Rosdahl and other contributors
 //
 // See doc/authors.adoc for a complete list of contributors.
 //
@@ -76,6 +76,9 @@ ProgressBar::update(double value)
   DEBUG_ASSERT(value <= 1.0);
 
   int16_t new_value = static_cast<int16_t>(1000 * value);
+
+  std::unique_lock<std::mutex> lock(m_mutex);
+
   if (new_value == m_current_value) {
     return;
   }
index 7ea9d982fcd685ce737c436fb75642bf0697141a..aaca112b2ff3b1fcda457a3136efc14470ab5627 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019-2021 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2025 Joel Rosdahl and other contributors
 //
 // See doc/authors.adoc for a complete list of contributors.
 //
@@ -20,6 +20,7 @@
 
 #include <cstddef>
 #include <cstdint>
+#include <mutex>
 #include <string>
 
 class ProgressBar
@@ -34,6 +35,7 @@ public:
   void update(double value);
 
 private:
+  std::mutex m_mutex;
   const std::string m_header;
   const size_t m_width;
   const bool m_stdout_is_a_terminal;
index d4b1bcdae0586a54122dbe3cc579c91896df86d3..c682c3ab1d06b3801c9d0d2e1c3193f0e257bc6b 100644 (file)
@@ -839,10 +839,10 @@ CompressionStatistics
 LocalStorage::get_compression_statistics(
   const uint32_t threads, const ProgressReceiver& progress_receiver) const
 {
-  std::atomic<uint64_t> content_size{0};
-  std::atomic<uint64_t> actual_size{0};
-  std::atomic<uint64_t> incompressible_size{0};
-  std::atomic<uint32_t> completed_dirs{0};
+  std::atomic<uint64_t> content_size = 0;
+  std::atomic<uint64_t> actual_size = 0;
+  std::atomic<uint64_t> incompressible_size = 0;
+  std::atomic<uint32_t> completed_dirs = 0;
 
   const size_t read_ahead =
     std::max(static_cast<size_t>(10), 2 * static_cast<size_t>(threads));