From b15c3ce73b68f014c5deaafef46631878d483078 Mon Sep 17 00:00:00 2001 From: Cra3z <3324654761@qq.com> Date: Sat, 14 Nov 2026 13:45:40 +0800 Subject: [PATCH] Add support for Windows thread pool and GCD queue --- CMakeLists.txt | 6 + examples/CMakeLists.txt | 1 + .../execution/detail/thread_pool_backend.hpp | 198 +++++++++++++----- src/beman/execution/CMakeLists.txt | 1 + tests/beman/execution/CMakeLists.txt | 1 + .../exec-parallel-scheduler.test.cpp | 48 +++++ 6 files changed, 201 insertions(+), 54 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7afed45c..ea6fc67e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,6 +100,12 @@ option( ON ) +option( + BEMAN_EXECUTION_WITH_GCD_SUPPORT + "Enable support for libdispatch (Grand Central Dispatch). Default: OFF. Values: { ON, OFF }." + OFF +) + add_subdirectory(src/beman/execution) if(NOT BEMAN_USE_MODULES) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 12bd5624..58d27b9d 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -50,6 +50,7 @@ foreach(EXAMPLE ${EXAMPLES}) set(EXAMPLE_TARGET ${PROJECT_NAME}.${EXAMPLE}) add_executable(${EXAMPLE_TARGET}) target_sources(${EXAMPLE_TARGET} PRIVATE ${EXAMPLE}.cpp) + target_compile_definitions(${EXAMPLE_TARGET} PRIVATE NOMINMAX) if(BEMAN_USE_MODULES) target_compile_definitions(${EXAMPLE_TARGET} PUBLIC BEMAN_HAS_MODULES) target_link_libraries(${EXAMPLE_TARGET} PRIVATE beman::execution) diff --git a/include/beman/execution/detail/thread_pool_backend.hpp b/include/beman/execution/detail/thread_pool_backend.hpp index ac425353..2c82c520 100644 --- a/include/beman/execution/detail/thread_pool_backend.hpp +++ b/include/beman/execution/detail/thread_pool_backend.hpp @@ -6,6 +6,13 @@ #include #include +#if BEMAN_EXECUTION_WITH_GCD_SUPPORT && __has_include() +#include +#endif +#if defined(_WIN32) && __has_include() +#include +#include +#endif #ifdef BEMAN_HAS_IMPORT_STD import std; #else @@ -18,6 +25,7 @@ import std; #include #include #include +#include #include #include #endif @@ -48,7 +56,7 @@ class thread_pool_backend_base auto operator=(task_base&&) -> task_base& = delete; - virtual auto exec() noexcept -> void = 0; + virtual auto exec(int ec = 0) noexcept -> void = 0; task_base* next; }; @@ -57,10 +65,14 @@ class thread_pool_backend_base explicit schedule_task(::beman::execution::parallel_scheduler_replacement::receiver_proxy& p) noexcept : proxy(p) {} - auto exec() noexcept -> void override { + auto exec(int ec) noexcept -> void override { auto& proxy_ref = proxy; ::std::destroy_at(this); - proxy_ref.set_value(); + if (ec) [[unlikely]] { + proxy_ref.set_error(::std::make_exception_ptr(::std::system_error(ec, ::std::system_category()))); + } else { + proxy_ref.set_value(); + } } ::beman::execution::parallel_scheduler_replacement::receiver_proxy& proxy; @@ -71,18 +83,32 @@ class thread_pool_backend_base struct single_bulk_task : task_base { single_bulk_task(::beman::execution::parallel_scheduler_replacement::bulk_item_receiver_proxy& p, - ::std::size_t shape) noexcept - : proxy(p), shape(shape) {} - - auto exec() noexcept -> void override { - proxy.execute(0uz, shape); + ::std::size_t shape, + bool one_by_one) noexcept + : proxy(p), shape(shape), one_by_one(one_by_one) {} + + auto exec(int ec) noexcept -> void override { + if (ec == 0) [[likely]] { + if (one_by_one) { + for (::std::size_t i = 0; i < shape; ++i) { + proxy.execute(i, i + 1uz); + } + } else { + proxy.execute(0uz, shape); + } + } auto& proxy_ref = proxy; ::std::destroy_at(this); - proxy_ref.set_value(); + if (ec) [[unlikely]] { + proxy_ref.set_error(::std::make_exception_ptr(::std::system_error(ec, ::std::system_category()))); + } else { + proxy_ref.set_value(); + } } ::beman::execution::parallel_scheduler_replacement::bulk_item_receiver_proxy& proxy; ::std::size_t shape; + bool one_by_one; }; // `single_bulk_task` is small enough to fit directly into the pre-allocated storage provided by @@ -97,6 +123,7 @@ class thread_pool_backend_base batched_bulk_task* head; ::std::size_t chunk_count; bool one_by_one; + int ec = 0; ::std::size_t ref_count; }; @@ -106,25 +133,36 @@ class thread_pool_backend_base ::std::size_t j) noexcept : cookie(cookie), proxy(proxy), i(i), j(j) {} - auto exec() noexcept -> void override { - if (cookie->one_by_one) { - for (::std::size_t k = i; k < j; ++k) { - proxy.execute(k, k + 1uz); + auto exec(int ec) noexcept -> void override { + if (ec) [[unlikely]] { + if (next == nullptr) { + cookie->ec = ec; } } else { - proxy.execute(i, j); + if (cookie->one_by_one) { + for (::std::size_t k = i; k < j; ++k) { + proxy.execute(k, k + 1uz); + } + } else { + proxy.execute(i, j); + } } ::std::atomic_ref<::std::size_t> ref_count{cookie->ref_count}; if (ref_count.fetch_sub(1uz, ::std::memory_order_acq_rel) == 1uz) { auto head = cookie->head; const auto chunk_count = cookie->chunk_count; + auto error = ::std::error_code{cookie->ec, ::std::system_category()}; auto& proxy_ref = proxy; ::std::destroy_at(cookie); ::std::destroy_n(head, chunk_count); ::operator delete( head, chunk_count * sizeof(batched_bulk_task), ::std::align_val_t{alignof(batched_bulk_task)}); - proxy_ref.set_value(); + if (error) [[unlikely]] { + proxy_ref.set_error(::std::make_exception_ptr(::std::system_error{error})); + } else { + proxy_ref.set_value(); + } } } @@ -152,16 +190,9 @@ class thread_pool_backend_base auto operator=(thread_pool_backend_base&&) -> thread_pool_backend_base& = delete; - auto shutdown() noexcept -> void { - ::std::unique_lock guard{mtx}; - shutdown_requested = true; - guard.unlock(); - cv.notify_all(); - } - auto schedule(::beman::execution::parallel_scheduler_replacement::receiver_proxy& proxy, ::std::span<::std::byte> storage) noexcept -> void override { - push_back(::std::construct_at(reinterpret_cast(storage.data()), proxy)); // nothrow! + submit(::std::construct_at(reinterpret_cast(storage.data()), proxy)); // nothrow! } auto schedule_bulk_chunked(::std::size_t shape, @@ -178,10 +209,11 @@ class thread_pool_backend_base protected: [[nodiscard]] static auto num_threads() noexcept -> ::std::size_t { - static const ::std::size_t count = ::std::max(1u, ::std::thread::hardware_concurrency()); + static const ::std::size_t count = (::std::max)(1u, ::std::thread::hardware_concurrency()); return count; } + private: auto schedule_bulk(::std::size_t shape, bool one_by_one, ::beman::execution::parallel_scheduler_replacement::bulk_item_receiver_proxy& proxy, @@ -191,11 +223,13 @@ class thread_pool_backend_base return; } - const ::std::size_t chunk_length = (shape + num_threads() - 1uz) / num_threads(); - const ::std::size_t chunk_count = (shape + chunk_length - 1uz) / chunk_length; + const ::std::size_t threads = num_threads(); + const ::std::size_t chunk_length = shape / threads + (shape % threads != 0uz); + const ::std::size_t chunk_count = shape / chunk_length + (shape % chunk_length != 0uz); try { if (chunk_count == 1uz) { - push_back(::std::construct_at(reinterpret_cast(storage.data()), proxy, shape)); + submit(::std::construct_at( + reinterpret_cast(storage.data()), proxy, shape, one_by_one)); } else { auto head = static_cast(::operator new( chunk_count * sizeof(batched_bulk_task), ::std::align_val_t{alignof(batched_bulk_task)})); @@ -206,7 +240,7 @@ class thread_pool_backend_base batched_bulk_task* prev = nullptr; for (::std::size_t i = 0; i < chunk_count; ++i) { const ::std::size_t begin = i * chunk_length; - const ::std::size_t end = ::std::min(begin + chunk_length, shape); + const ::std::size_t end = begin + (::std::min)(chunk_length, shape - begin); auto task = ::std::construct_at(head + i, cookie, proxy, begin, end); if (prev) { prev->next = task; @@ -214,14 +248,43 @@ class thread_pool_backend_base prev = task; } // NOLINTEND(*-reinterpret-cast, *-pointer-arithmetic-on-polymorphic-object, *-ctr56-cpp) - push_back(head, chunk_count); + submit(head, chunk_count); } } catch (...) { proxy.set_error(::std::current_exception()); } } - auto push_back(task_base* t, ::std::size_t n = 1uz) noexcept -> void { + virtual auto submit(task_base* t, ::std::size_t n = 1uz) noexcept -> void = 0; +}; + +struct simple_thread_pool_backend : ::beman::execution::detail::thread_pool_backend_base { + explicit simple_thread_pool_backend(::std::in_place_t) : workers(::new ::std::thread[num_threads()]) {} + + simple_thread_pool_backend() : simple_thread_pool_backend(::std::in_place) { + for (auto& worker : ::std::span(workers.get(), num_threads())) { + worker = ::std::thread(&simple_thread_pool_backend::run, this); + } + } + + ~simple_thread_pool_backend() override { + shutdown(); + for (auto& worker : ::std::span(workers.get(), num_threads())) { + if (worker.joinable()) { + worker.join(); + } + } + } + + private: + auto shutdown() noexcept -> void { + ::std::unique_lock guard{mtx}; + shutdown_requested = true; + guard.unlock(); + cv.notify_all(); + } + + auto submit(task_base* t, ::std::size_t n) noexcept -> void override { ::std::unique_lock guard{mtx}; for (::std::size_t i = 0; i < n; ++i) { if (auto prev_back = ::std::exchange(back, t)) { @@ -249,41 +312,68 @@ class thread_pool_backend_base return front ? ::std::exchange(front, front->next) : nullptr; } - protected: - bool shutdown_requested = false; - ::std::mutex mtx; - ::std::condition_variable cv; - task_base* front = nullptr; - task_base* back = nullptr; -}; - -struct thread_pool_backend : ::beman::execution::detail::thread_pool_backend_base { - explicit thread_pool_backend(::std::in_place_t) : workers(::new ::std::thread[num_threads()]) {} - - thread_pool_backend() : thread_pool_backend(::std::in_place) { - for (auto& worker : ::std::span(workers.get(), num_threads())) { - worker = ::std::thread(&thread_pool_backend::run, this); + auto run() noexcept -> void { + while (auto task = pop_front()) { + task->exec(); } } - ~thread_pool_backend() override { - shutdown(); - for (auto& worker : ::std::span(workers.get(), num_threads())) { - if (worker.joinable()) { - worker.join(); - } + bool shutdown_requested = false; + ::std::mutex mtx; + ::std::condition_variable cv; + task_base* front = nullptr; + task_base* back = nullptr; + ::std::unique_ptr<::std::thread[]> workers; +}; + +#if BEMAN_EXECUTION_WITH_GCD_SUPPORT && __has_include() +struct gcd_thread_pool_backend : ::beman::execution::detail::thread_pool_backend_base { + private: + auto submit(task_base* t, ::std::size_t n) noexcept -> void override { + auto queue = ::dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); + for (::std::size_t i = 0; i < n; ++i) { + auto* next = t->next; + ::dispatch_async_f(queue, t, &run); + t = next; } + assert(t == nullptr); } + static auto run(void* ctx) noexcept -> void { static_cast(ctx)->exec(); } +}; +#endif + +#if defined(_WIN32) && __has_include() +struct win_thread_pool_backend : ::beman::execution::detail::thread_pool_backend_base { private: - auto run() noexcept -> void { - while (auto task = pop_front()) { - task->exec(); + auto submit(task_base* t, ::std::size_t n) noexcept -> void override { + int ec = 0; + for (::std::size_t i = 0; i < n; ++i) { + auto* next = t->next; + if (ec) [[unlikely]] { + t->exec(ec); + } else if (!::TrySubmitThreadpoolCallback(&run, t, nullptr)) { + ec = static_cast(::GetLastError()); + t->exec(ec); + } + t = next; } + assert(t == nullptr); } - ::std::unique_ptr<::std::thread[]> workers; + static auto CALLBACK run(::PTP_CALLBACK_INSTANCE, void* ctx) noexcept -> void { + static_cast(ctx)->exec(); + } }; +#endif + +#if BEMAN_EXECUTION_WITH_GCD_SUPPORT && __has_include() +using thread_pool_backend = gcd_thread_pool_backend; +#elif defined(_WIN32) && __has_include() +using thread_pool_backend = win_thread_pool_backend; +#else +using thread_pool_backend = simple_thread_pool_backend; +#endif } // namespace beman::execution::detail diff --git a/src/beman/execution/CMakeLists.txt b/src/beman/execution/CMakeLists.txt index cb77cab7..de4f5e8e 100644 --- a/src/beman/execution/CMakeLists.txt +++ b/src/beman/execution/CMakeLists.txt @@ -251,6 +251,7 @@ target_compile_definitions( ${BEMAN_EXECUTION_TARGET_NAME} INTERFACE BEMAN_EXECUTION_WITH_DEFAULT_PARALLEL_SCHEDULER_BACKEND=$ + BEMAN_EXECUTION_WITH_GCD_SUPPORT=$ ) if(BEMAN_USE_MODULES) diff --git a/tests/beman/execution/CMakeLists.txt b/tests/beman/execution/CMakeLists.txt index 753c211f..9b2b7b92 100644 --- a/tests/beman/execution/CMakeLists.txt +++ b/tests/beman/execution/CMakeLists.txt @@ -142,6 +142,7 @@ foreach(test ${execution_tests}) else() target_link_libraries(${TEST_EXE} PRIVATE beman::execution_headers) endif() + target_compile_definitions(${TEST_EXE} PRIVATE NOMINMAX) add_test(NAME ${TEST_EXE} COMMAND $) endforeach() diff --git a/tests/beman/execution/exec-parallel-scheduler.test.cpp b/tests/beman/execution/exec-parallel-scheduler.test.cpp index c7daa1b6..e0a02180 100644 --- a/tests/beman/execution/exec-parallel-scheduler.test.cpp +++ b/tests/beman/execution/exec-parallel-scheduler.test.cpp @@ -11,6 +11,7 @@ import std; #include #include #include +#include #include #include #include @@ -49,6 +50,52 @@ struct bulk_proxy : replaceability::bulk_item_receiver_proxy { auto execute(::std::size_t, ::std::size_t) noexcept -> void override {} }; +struct inline_pool_backend : test_std::detail::thread_pool_backend_base { + static auto run_single(replaceability::bulk_item_receiver_proxy& proxy, std::size_t shape, bool one_by_one) + -> void { + alignas(single_bulk_task) std::byte storage[sizeof(single_bulk_task)]; + std::construct_at(reinterpret_cast(storage), proxy, shape, one_by_one)->exec(0); + } + + private: + auto submit(task_base* task, std::size_t count) noexcept -> void override { + for (std::size_t i = 0; i < count; ++i) { + auto* next = task->next; + task->exec(); + task = next; + } + } +}; + +auto test_thread_pool_bulk_boundaries() -> void { + struct tracking_proxy : replaceability::bulk_item_receiver_proxy { + std::size_t end = 0; + bool completed = false; + bool unit_ranges = false; + + auto execute(std::size_t begin, std::size_t next) noexcept -> void override { + ASSERT(begin == end); + ASSERT(next > begin); + ASSERT(!unit_ranges || next - begin == 1); + end = next; + } + auto set_value() noexcept -> void override { completed = true; } + auto set_error(std::exception_ptr) noexcept -> void override { ASSERT(false); } + auto set_stopped() noexcept -> void override { ASSERT(false); } + }; + + tracking_proxy single; + single.unit_ranges = true; + inline_pool_backend::run_single(single, 4, true); + ASSERT(single.completed && single.end == 4); + + tracking_proxy large; + inline_pool_backend pool; + alignas(std::max_align_t) std::byte storage[256]; + pool.schedule_bulk_chunked((std::numeric_limits::max)(), large, storage); + ASSERT(large.completed && large.end == (std::numeric_limits::max)()); +} + struct backend : replaceability::parallel_scheduler_backend { auto schedule(replaceability::receiver_proxy&, ::std::span<::std::byte>) noexcept -> void override {} auto schedule_bulk_chunked(::std::size_t, @@ -259,6 +306,7 @@ auto query_parallel_scheduler_backend() -> std::shared_ptr