From 055dfa8d39137e4a0cd18865624e10be7c2b828c Mon Sep 17 00:00:00 2001 From: Xuehan Xu Date: Thu, 14 Oct 2021 17:41:18 +0800 Subject: [PATCH] crimson/common: extract parallel_for_each into errorator-loop.h otherwise, crimson::errorator::parallel_for_each would be referencing incomplete crimson::parallel_for_each_state type Signed-off-by: Xuehan Xu --- src/crimson/common/errorator-loop.h | 30 ++++++++++++++++++ src/crimson/common/errorator.h | 47 ++++++++++------------------- 2 files changed, 46 insertions(+), 31 deletions(-) diff --git a/src/crimson/common/errorator-loop.h b/src/crimson/common/errorator-loop.h index d2da4775dbc..b17217276e8 100644 --- a/src/crimson/common/errorator-loop.h +++ b/src/crimson/common/errorator-loop.h @@ -57,4 +57,34 @@ public: } }; +template +static inline typename errorator::template future<> +parallel_for_each(Iterator first, Iterator last, Func&& func) noexcept { + parallel_for_each_state* s = nullptr; + // Process all elements, giving each future the following treatment: + // - available, not failed: do nothing + // - available, failed: collect exception in ex + // - not available: collect in s (allocating it if needed) + for (;first != last; ++first) { + auto f = seastar::futurize_invoke(std::forward(func), *first); + if (!f.available() || f.failed()) { + if (!s) { + using itraits = std::iterator_traits; + auto n = (seastar::internal::iterator_range_estimate_vector_capacity( + first, last, typename itraits::iterator_category()) + 1); + s = new parallel_for_each_state(n); + } + s->add_future(std::move(f)); + } + } + // If any futures were not available, hand off to parallel_for_each_state::start(). + // Otherwise we can return a result immediately. + if (s) { + // s->get_future() takes ownership of s (and chains it to one of the futures it contains) + // so this isn't a leak + return s->get_future(); + } + return seastar::make_ready_future<>(); +} + } // namespace crimson diff --git a/src/crimson/common/errorator.h b/src/crimson/common/errorator.h index a3c26a6128c..8472ad00eed 100644 --- a/src/crimson/common/errorator.h +++ b/src/crimson/common/errorator.h @@ -331,6 +331,13 @@ class parallel_for_each_state; template static inline constexpr bool is_error_v = std::is_base_of_v, T>; +template +struct errorator; + +template +static inline typename errorator::template future<> +parallel_for_each(Iterator first, Iterator last, Func&& func) noexcept; + template struct errorator { @@ -894,44 +901,22 @@ public: return make_ready_future<>(); } - template - static inline errorator::future<> - parallel_for_each(Iterator first, Iterator last, Func&& func) noexcept { - parallel_for_each_state* s = nullptr; - // Process all elements, giving each future the following treatment: - // - available, not failed: do nothing - // - available, failed: collect exception in ex - // - not available: collect in s (allocating it if needed) - for (;first != last; ++first) { - auto f = seastar::futurize_invoke(std::forward(func), *first); - if (!f.available() || f.failed()) { - if (!s) { - using itraits = std::iterator_traits; - auto n = (seastar::internal::iterator_range_estimate_vector_capacity( - first, last, typename itraits::iterator_category()) + 1); - s = new parallel_for_each_state(n); - } - s->add_future(std::move(f)); - } - } - // If any futures were not available, hand off to parallel_for_each_state::start(). - // Otherwise we can return a result immediately. - if (s) { - // s->get_future() takes ownership of s (and chains it to one of the futures it contains) - // so this isn't a leak - return s->get_future(); - } - return seastar::make_ready_future<>(); - } - template static inline auto parallel_for_each(Container&& container, Func&& func) noexcept { - return parallel_for_each( + return crimson::parallel_for_each( std::begin(container), std::end(container), std::forward(func)); } + template + static inline errorator::future<> + parallel_for_each(Iterator first, Iterator last, Func&& func) noexcept { + return crimson::parallel_for_each( + first, + last, + std::forward(func)); + } private: template class futurize { -- 2.39.5