Header check: wait for chunked work with std::latch DoWorkChunked posts a known number of chunks to the worker pool, so a std::latch initialized with the chunk count replaces the AtomicRefCount, mutex, and condition variable that tracked completion. This removes the extra reference held while posting and the lock held across the decrement, both of which only existed to keep the completion state alive until the last notification finished. std::latch::count_down only touches the latch address after the final decrement, so destroying the latch as soon as wait() returns is safe. All supported toolchains build GN as C++23 with a macOS minimum of 14, where std::latch is available. Follows up on a suggestion by msta@ on my earlier CL 26341. gn_unittests and the simple integration test pass, no measurable performance impact. Change-Id: I355fcb0c57716d3a66e79f3268886714c2aeef54 Reviewed-on: https://gn-review.googlesource.com/c/gn/+/26640 Commit-Queue: Philipp Wollermann <philwo@google.com> Reviewed-by: Takuto Ikuta <tikuta@google.com> Reviewed-by: Matt Stark <msta@google.com>
diff --git a/src/gn/header_checker.cc b/src/gn/header_checker.cc index 69c2c95..6d2c279 100644 --- a/src/gn/header_checker.cc +++ b/src/gn/header_checker.cc
@@ -5,11 +5,10 @@ #include "gn/header_checker.h" #include <algorithm> -#include <condition_variable> +#include <latch> #include <mutex> #include <span> -#include "base/atomic_ref_count.h" #include "base/containers/queue.h" #include "base/files/file_util.h" #include "base/strings/string_util.h" @@ -175,38 +174,22 @@ const std::vector<T>& items, size_t chunk_size, const Work& work) { - base::AtomicRefCount task_count; - std::mutex task_count_lock; - std::condition_variable task_count_cv; - auto finish_task = [&]() { - // Hold the lock across the decrement so the waiter cannot return and - // destroy the completion state before the notification is done. - std::unique_lock<std::mutex> lock(task_count_lock); - if (!task_count.Decrement()) { - // Signal |task_count_cv| when |task_count| becomes zero. - task_count_cv.notify_one(); - } - }; + if (items.empty()) + return; - // Hold one extra reference while posting so the count can't reach zero - // before all chunks are posted. - task_count.Increment(); std::span<const T> all(items); + + size_t num_chunks = (all.size() + chunk_size - 1) / chunk_size; + std::latch chunks_done(num_chunks); for (size_t begin = 0; begin < all.size(); begin += chunk_size) { std::span<const T> chunk = all.subspan(begin, std::min(chunk_size, all.size() - begin)); - task_count.Increment(); - pool.PostTask([&work, &finish_task, chunk]() { + pool.PostTask([&work, &chunks_done, chunk]() { work(chunk); - finish_task(); + chunks_done.count_down(); }); } - finish_task(); - - // Wait for all tasks posted by this function to complete. - std::unique_lock<std::mutex> lock(task_count_lock); - while (!task_count.IsZero()) - task_count_cv.wait(lock); + chunks_done.wait(); } } // namespace