From fc2fb1b77c77b108798c11edbfbe737365bbd5ff Mon Sep 17 00:00:00 2001 From: Shivansh Singh Date: Fri, 23 Oct 2026 20:13:14 +0530 Subject: [PATCH 1/3] Execution: Fix stop callback lifetimes and implement bulk token adaptation Signed-off-by: Shivansh Singh --- .../hpx/execution/algorithms/run_loop.hpp | 2 +- .../tests/regressions/CMakeLists.txt | 2 +- .../regressions/run_loop_execute_7762.cpp | 70 --- .../hpx/executors/parallel_scheduler.hpp | 320 +++++++++-- .../executors/parallel_scheduler_backend.hpp | 68 ++- .../tests/unit/parallel_scheduler.cpp | 519 +++++++++++++++++- 6 files changed, 841 insertions(+), 140 deletions(-) delete mode 100644 libs/core/execution/tests/regressions/run_loop_execute_7762.cpp diff --git a/libs/core/execution/include/hpx/execution/algorithms/run_loop.hpp b/libs/core/execution/include/hpx/execution/algorithms/run_loop.hpp index 5c726c88cd50..d10c794c42d2 100644 --- a/libs/core/execution/include/hpx/execution/algorithms/run_loop.hpp +++ b/libs/core/execution/include/hpx/execution/algorithms/run_loop.hpp @@ -127,7 +127,7 @@ namespace hpx::execution::experimental { Receiver_&& receiver) noexcept(noexcept(std:: is_nothrow_constructible_v, Receiver_>)) - : run_loop_opstate_base(tail, &execute) + : run_loop_opstate_base(tail) , loop(loop) , receiver(HPX_FORWARD(Receiver_, receiver)) { diff --git a/libs/core/execution/tests/regressions/CMakeLists.txt b/libs/core/execution/tests/regressions/CMakeLists.txt index 0d664bd2f4d3..ebb620e668a7 100644 --- a/libs/core/execution/tests/regressions/CMakeLists.txt +++ b/libs/core/execution/tests/regressions/CMakeLists.txt @@ -5,7 +5,7 @@ # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) set(tests annotated_minmax_2593 chunk_size_4118 executor_parameters_num_chunks - fibonacci_sr is_executor_1691 run_loop_execute_7762 + is_executor_1691 fibonacci_sr ) if(HPX_WITH_DATAPAR) diff --git a/libs/core/execution/tests/regressions/run_loop_execute_7762.cpp b/libs/core/execution/tests/regressions/run_loop_execute_7762.cpp deleted file mode 100644 index 059e368ab7b2..000000000000 --- a/libs/core/execution/tests/regressions/run_loop_execute_7762.cpp +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright (c) 2026 Hartmut Kaiser -// Copyright (c) 2026 Shivansh Singh -// -// SPDX-License-Identifier: BSL-1.0 -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#include -#include -#include -#include - -#include -#include - -namespace ex = hpx::execution::experimental; - -struct test_receiver -{ - using receiver_concept = ex::receiver_t; - - ex::run_loop& loop; - std::atomic& executed; - - template - void set_error(E&&) && noexcept - { - HPX_TEST(false); - loop.finish(); - } - - void set_stopped() && noexcept - { - HPX_TEST(false); - loop.finish(); - } - - void set_value() && noexcept - { - executed.store(true); - loop.finish(); - } -}; - -int hpx_main() -{ - ex::run_loop loop; - auto sched = loop.get_scheduler(); - - std::atomic executed{false}; - - auto sender = ex::schedule(sched); - auto op_state = - ex::connect(std::move(sender), test_receiver{loop, executed}); - ex::start(op_state); - - loop.run(); - - HPX_TEST(executed.load()); - - return hpx::local::finalize(); -} - -int main(int argc, char* argv[]) -{ - HPX_TEST_EQ_MSG(hpx::local::init(hpx_main, argc, argv), 0, - "HPX main exited with non-zero status"); - - return hpx::util::report_errors(); -} diff --git a/libs/core/executors/include/hpx/executors/parallel_scheduler.hpp b/libs/core/executors/include/hpx/executors/parallel_scheduler.hpp index 8030da67bf4a..34dafd0dad87 100644 --- a/libs/core/executors/include/hpx/executors/parallel_scheduler.hpp +++ b/libs/core/executors/include/hpx/executors/parallel_scheduler.hpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -91,6 +92,55 @@ namespace hpx::execution::experimental { // negligible. namespace detail { + // P3804R2 Section 3.3: Stop token adaptation traits shared by + // operation_state and virtual_parallel_bulk_op. + template + struct stop_token_adaptation_traits + { + using receiver_env_t = env_of_t const&>; + using native_stop_token_t = stop_token_of_t; + + static constexpr bool native_is_inplace = + std::is_same_v; + + struct forward_stop_request + { + inplace_stop_source& source_; + void operator()() noexcept + { + source_.request_stop(); + } + }; + + struct empty_stop_callback + { + }; + + // clang-format off + using stop_callback_t = std::conditional_t>; + // clang-format on + }; + + // Helper base that pulls stop_token_adaptation_traits aliases + // into a single place so both operation_state and + // virtual_parallel_bulk_op can inherit them without duplication. + template + struct stop_token_adaptation_base + { + protected: + using traits_t = stop_token_adaptation_traits; + using native_stop_token_t = typename traits_t::native_stop_token_t; + static constexpr bool native_is_inplace = + traits_t::native_is_inplace; + using forward_stop_request = + typename traits_t::forward_stop_request; + using empty_stop_callback = typename traits_t::empty_stop_callback; + using stop_callback_t = typename traits_t::stop_callback_t; + }; + // Virtual base for type-erased bulk operation states. HPX_CXX_CORE_EXPORT struct base_parallel_bulk_op { @@ -127,8 +177,27 @@ namespace hpx::execution::experimental { // calls backend->schedule_bulk_chunked() or schedule_bulk_unchunked(). HPX_CXX_CORE_EXPORT template - struct virtual_parallel_bulk_op final : base_parallel_bulk_op + struct virtual_parallel_bulk_op final + : base_parallel_bulk_op + , stop_token_adaptation_base { + private: + // ---- Stop token adaptation (parity with operation_state) ---- + // P3804R2 Section 3.3: The bulk path must provide the same + // inplace_stop_token adaptation as the schedule path. + // Aliases inherited from stop_token_adaptation_base. + using typename stop_token_adaptation_base::traits_t; + using typename stop_token_adaptation_base< + Receiver>::native_stop_token_t; + using stop_token_adaptation_base::native_is_inplace; + using typename stop_token_adaptation_base< + Receiver>::forward_stop_request; + using typename stop_token_adaptation_base< + Receiver>::empty_stop_callback; + using + typename stop_token_adaptation_base::stop_callback_t; + + public: std::shared_ptr backend_; std::size_t count_; // Count passed to backend (1 for seq, shape for par) @@ -137,6 +206,13 @@ namespace hpx::execution::experimental { F f_; std::decay_t receiver_; + // P3804R2 Section 3.3: inplace_stop_source for adaptation. + inplace_stop_source stop_source_; + + // P3804R2 Section 3.3: Stop callback bridge (registered in + // do_bulk when adaptation is needed). + std::optional stop_callback_; + // Pre-allocated storage passed to the backend as scratch space. alignas(parallel_scheduler_storage_alignment) std::byte storage_[parallel_scheduler_storage_size]; @@ -207,8 +283,13 @@ namespace hpx::execution::experimental { } } + // Lifetime fix: reset the stop callback BEFORE invoking + // the completion signal on the receiver. This ensures + // the stop source cannot be destroyed while the callback + // still references it. void set_value() noexcept override { + op_.stop_callback_.reset(); std::apply( [&](auto&&... vals) { hpx::execution::experimental::set_value( @@ -219,35 +300,63 @@ namespace hpx::execution::experimental { void set_error(std::exception_ptr ep) noexcept override { + op_.stop_callback_.reset(); hpx::execution::experimental::set_error( HPX_MOVE(op_.receiver_), HPX_MOVE(ep)); } void set_stopped() noexcept override { + op_.stop_callback_.reset(); hpx::execution::experimental::set_stopped( HPX_MOVE(op_.receiver_)); } bool stop_requested() const noexcept override { - return get_stop_token(get_env(op_.receiver_)) - .stop_requested(); + // Use the adapted source when adaptation is active, + // matching operation_state behavior. + if constexpr (native_is_inplace) + { + return get_stop_token(get_env(op_.receiver_)) + .stop_requested(); + } + else + { + return op_.stop_source_.stop_requested(); + } } protected: - void query_env(std::type_info const& query_type, + // P3804R2 Section 3.1 / 3.3: Answer get_stop_token_t + // queries. Returns the adapted inplace_stop_token + // regardless of the native token type. + bool query_env_impl(std::type_info const& query_type, std::type_info const& result_type, - void* result) const noexcept override + void* result_out) const noexcept override { - using token_type = std::decay_t; - if (query_type == typeid(get_stop_token_t) && - result_type == typeid(token_type)) + if (query_type != + typeid(hpx::execution::experimental:: + get_stop_token_t) || + result_type != typeid(inplace_stop_token)) + { + return false; + } + + if constexpr (native_is_inplace) { - static_cast*>(result) - ->emplace(get_stop_token(get_env(op_.receiver_))); + // Forward the native inplace token directly. + ::new (result_out) inplace_stop_token( + get_stop_token(get_env(op_.receiver_))); } + else + { + // Return the adapted token from our + // inplace_stop_source. + ::new (result_out) + inplace_stop_token(op_.stop_source_.get_token()); + } + return true; } }; @@ -381,11 +490,33 @@ namespace hpx::execution::experimental { // completes. Constructs the proxy via placement new into the // inline buffer (no heap allocation) then dispatches to the // backend. + // + // P3804R2 Section 3.3: Register the stop callback bridge + // before dispatching to the backend, matching operation_state. template void do_bulk(Vs&&... vs) noexcept { hpx::detail::try_catch_exception_ptr( [&]() { + // P2079R10 4.1: if stop_token is stopped, complete + // with set_stopped as soon as is practical. + auto stop_token = get_stop_token(get_env(receiver_)); + if (stop_token.stop_requested()) + { + set_stopped(HPX_MOVE(receiver_)); + return; + } + + // P3804R2 Section 3.3: Register the stop callback + if constexpr (!native_is_inplace) + { + if (stop_token.stop_possible()) + { + stop_callback_.emplace(stop_token, + forward_stop_request{stop_source_}); + } + } + new (proxy_buf_) proxy_t(*this, HPX_FORWARD(Vs, vs)...); proxy_active_ = true; @@ -402,6 +533,7 @@ namespace hpx::execution::experimental { } }, [&](std::exception_ptr ep) { + stop_callback_.reset(); hpx::execution::experimental::set_error( HPX_MOVE(receiver_), HPX_MOVE(ep)); }); @@ -717,71 +849,161 @@ namespace hpx::execution::experimental { // P2079R10: operation_state owns the receiver and manages the // frontend/backend boundary. On start(), it checks the stop token // and then delegates to the backend. + // + // P3804R2 Section 3.3: Stop token adaptation. + // The backend always queries for inplace_stop_token via + // try_query(get_stop_token_t{}). + // If the receiver's native stop token is already inplace_stop_token, + // we pass it through directly. Otherwise we maintain an + // inplace_stop_source inside the operation_state and register a + // stop callback that bridges the receiver's arbitrary stop token + // to our internal inplace_stop_source. template - struct operation_state + struct operation_state : detail::stop_token_adaptation_base { + private: + // Aliases inherited from stop_token_adaptation_base. + using + typename detail::stop_token_adaptation_base::traits_t; + using typename detail::stop_token_adaptation_base< + Receiver>::native_stop_token_t; + using detail::stop_token_adaptation_base< + Receiver>::native_is_inplace; + using typename detail::stop_token_adaptation_base< + Receiver>::forward_stop_request; + using typename detail::stop_token_adaptation_base< + Receiver>::empty_stop_callback; + using typename detail::stop_token_adaptation_base< + Receiver>::stop_callback_t; + + public: // Concrete receiver_proxy that adapts the actual Receiver // to the type-erased proxy interface. + // + // P3804R2 Section 3.3: Overrides query_env_impl to provide + // the adapted inplace_stop_token to the backend. struct concrete_receiver_proxy final : parallel_scheduler_receiver_proxy { - std::decay_t& receiver_; + // Back-pointer to the owning operation_state so we + // can access the inplace_stop_source for adaptation. + operation_state& op_; - explicit concrete_receiver_proxy( - std::decay_t& rcvr) noexcept - : receiver_(rcvr) + explicit concrete_receiver_proxy(operation_state& op) noexcept + : op_(op) { } + // Lifetime fix: reset the stop callback BEFORE + // invoking the completion signal on the receiver. + // The real danger is the stop source getting + // destroyed while the callback still references it. void set_value() noexcept override { + op_.stop_callback_.reset(); hpx::execution::experimental::set_value( - HPX_MOVE(receiver_)); + HPX_MOVE(op_.receiver_)); } void set_error(std::exception_ptr ep) noexcept override { + op_.stop_callback_.reset(); hpx::execution::experimental::set_error( - HPX_MOVE(receiver_), HPX_MOVE(ep)); + HPX_MOVE(op_.receiver_), HPX_MOVE(ep)); } void set_stopped() noexcept override { + op_.stop_callback_.reset(); hpx::execution::experimental::set_stopped( - HPX_MOVE(receiver_)); + HPX_MOVE(op_.receiver_)); } // P2079R10 4.2: allow backends to poll for cancellation. // Forwards the stop token state of the actual receiver. bool stop_requested() const noexcept override { - return get_stop_token(get_env(receiver_)).stop_requested(); + // When we have an adapted source, check it directly + // (it is updated by the stop callback bridge). + // Otherwise, query the native token directly. + if constexpr (native_is_inplace) + { + return get_stop_token(get_env(op_.receiver_)) + .stop_requested(); + } + else + { + return op_.stop_source_.stop_requested(); + } } protected: - void query_env(std::type_info const& query_type, + // P3804R2 Section 3.1 / 3.3: Answer environment + // queries from the backend. + // + // Currently supports: + // get_stop_token_t -> inplace_stop_token + // + // When the native token is already inplace, we + // forward it directly. Otherwise, we return the + // token from our adapted inplace_stop_source. + bool query_env_impl(std::type_info const& query_type, std::type_info const& result_type, - void* result) const noexcept override + void* result_out) const noexcept override { - using token_type = std::decay_t; - if (query_type == typeid(get_stop_token_t) && - result_type == typeid(token_type)) + // Only handle get_stop_token_t -> inplace_stop_token + if (query_type != + typeid(hpx::execution::experimental:: + get_stop_token_t) || + result_type != typeid(inplace_stop_token)) + { + return false; + } + + if constexpr (native_is_inplace) { - static_cast*>(result) - ->emplace(get_stop_token(get_env(receiver_))); + // No adaptation needed: forward the native + // inplace token directly. + ::new (result_out) inplace_stop_token( + get_stop_token(get_env(op_.receiver_))); } + else + { + // P3804R2 Section 3.3: Return the token from + // our adapted inplace_stop_source. The stop + // callback registered in start() ensures that + // stop requests propagate from the receiver's + // arbitrary token to this source. + ::new (result_out) + inplace_stop_token(op_.stop_source_.get_token()); + } + return true; } }; HPX_NO_UNIQUE_ADDRESS std::decay_t receiver_; std::shared_ptr backend_; + + // P3804R2 Section 3.3: inplace_stop_source for adaptation. + // When the native token is already inplace_stop_token, the + // stop_source_ is still present but unused (the proxy + // returns the native token directly). The cost is minimal + // (one atomic bool in inplace_stop_source). + inplace_stop_source stop_source_; + // The proxy must be a member (not a local) because the // backend's schedule() posts work asynchronously. The // operation_state outlives the completion per the // sender/receiver protocol. concrete_receiver_proxy proxy_; + // P3804R2 Section 3.3: Stop callback that bridges the + // receiver's arbitrary stop token to our inplace_stop_source. + // For native inplace tokens, this is an empty struct. + // Must be declared AFTER stop_source_ so it is constructed + // after the source exists and destroyed before it. + std::optional stop_callback_; + // P2079R10 4.2: pre-allocated storage for the backend. alignas(parallel_scheduler_storage_alignment) std::byte storage_[parallel_scheduler_storage_size]; @@ -791,7 +1013,7 @@ namespace hpx::execution::experimental { std::shared_ptr backend) : receiver_(HPX_FORWARD(Receiver_, receiver)) , backend_(HPX_MOVE(backend)) - , proxy_(receiver_) + , proxy_(*this) { } @@ -802,18 +1024,36 @@ namespace hpx::execution::experimental { void start() noexcept { - // P2079R10 4.1: if stop_token is stopped, complete - // with set_stopped as soon as is practical. - auto stop_token = get_stop_token(get_env(receiver_)); - if (stop_token.stop_requested()) - { - set_stopped(HPX_MOVE(receiver_)); - return; - } + hpx::detail::try_catch_exception_ptr( + [&]() { + // P2079R10 4.1: if stop_token is stopped, complete + // with set_stopped as soon as is practical. + auto stop_token = get_stop_token(get_env(receiver_)); + if (stop_token.stop_requested()) + { + set_stopped(HPX_MOVE(receiver_)); + return; + } + + // P3804R2 Section 3.3: Register the stop callback + if constexpr (!native_is_inplace) + { + if (stop_token.stop_possible()) + { + stop_callback_.emplace(stop_token, + forward_stop_request{stop_source_}); + } + } - // Delegate to the backend via the member proxy, - // passing pre-allocated storage per P2079R10 / P3927R2. - backend_->schedule(proxy_, std::span(storage_)); + // Delegate to the backend via the member proxy + backend_->schedule( + proxy_, std::span(storage_)); + }, + [&](std::exception_ptr ep) { + stop_callback_.reset(); + hpx::execution::experimental::set_error( + HPX_MOVE(receiver_), HPX_MOVE(ep)); + }); } }; diff --git a/libs/core/executors/include/hpx/executors/parallel_scheduler_backend.hpp b/libs/core/executors/include/hpx/executors/parallel_scheduler_backend.hpp index 9a3f1f453884..b8381e2f45b2 100644 --- a/libs/core/executors/include/hpx/executors/parallel_scheduler_backend.hpp +++ b/libs/core/executors/include/hpx/executors/parallel_scheduler_backend.hpp @@ -23,10 +23,12 @@ #include #include #include +#include #include #include #include #include +#include namespace hpx::execution::experimental { @@ -45,42 +47,58 @@ namespace hpx::execution::experimental { // P2079R10 / P3804R2 receiver_proxy: type-erased completion interface. // The backend calls these to signal completion back to the frontend. - // try_query() exposes supported receiver environment properties to a - // replacement backend. P2079R10 requires get_stop_token_t with an - // inplace_stop_token result to be supported. HPX's in_place_stop_token is - // supported as well. // + // P3804R2 Section 3.2: No virtual destructor -- objects are never + // destroyed polymorphically. The frontend knows the concrete type and + // destroys it directly. + // + // P3804R2 Section 3.1: try_query

(Query) const noexcept provides a + // generic, type-safe way for backends to query the receiver's + // environment (e.g. stop tokens, allocators). Internally it routes + // through the protected virtual query_env_impl() which the frontend's + // concrete proxy overrides. + // + // P3804R2 Section 3.3: When the backend queries for an + // inplace_stop_token, the frontend proxy adapts whatever stop token + // the actual receiver provides. See concrete_receiver_proxy in + // parallel_scheduler.hpp. HPX_CXX_CORE_EXPORT struct parallel_scheduler_receiver_proxy { - virtual ~parallel_scheduler_receiver_proxy() = default; + protected: + ~parallel_scheduler_receiver_proxy() = default; + // P3804R2: Type-erased virtual bridge for environment queries + virtual bool query_env_impl(std::type_info const& query_type, + std::type_info const& result_type, + void* result_out) const noexcept = 0; + + public: virtual void set_value() noexcept = 0; virtual void set_error(std::exception_ptr) noexcept = 0; virtual void set_stopped() noexcept = 0; + virtual bool stop_requested() const noexcept = 0; + // P3804R2 Section 3.1: Const-qualified try_query template - [[nodiscard]] std::optional

try_query(Query) const noexcept + [[nodiscard]] std::optional

try_query(Query /* q */) const noexcept { static_assert(std::is_object_v

&& !std::is_array_v

&& std::is_same_v>, - "P must be a cv-unqualified non-array object type"); - - std::optional

result; - query_env(typeid(std::remove_cvref_t), typeid(P), &result); - return result; - } - - // P2079R10 4.2 / P3804R2: backends can poll this to check if work should stop. - // Returns true if the associated stop token has been signalled. - // const-qualified per P3804R2 (aligns with try_query being const). - virtual bool stop_requested() const noexcept - { - return false; + "P must be a cv-unqualified, non-array object type"); + static_assert(std::is_nothrow_move_constructible_v

, + "P must be nothrow move constructible"); + + alignas(P) std::byte storage[sizeof(P)]; + if (query_env_impl( + typeid(Query), typeid(P), static_cast(&storage))) + { + P* ptr = std::launder(reinterpret_cast(&storage)); + std::optional

result(std::move(*ptr)); + ptr->~P(); + return result; + } + return std::nullopt; } - - protected: - virtual void query_env(std::type_info const& query_type, - std::type_info const& result_type, void* result) const noexcept = 0; }; // P2079R10 bulk_item_receiver_proxy: extends receiver_proxy with @@ -88,6 +106,10 @@ namespace hpx::execution::experimental { HPX_CXX_CORE_EXPORT struct parallel_scheduler_bulk_item_receiver_proxy : parallel_scheduler_receiver_proxy { + protected: + ~parallel_scheduler_bulk_item_receiver_proxy() = default; + + public: virtual void execute(std::size_t begin, std::size_t end) noexcept = 0; }; diff --git a/libs/core/executors/tests/unit/parallel_scheduler.cpp b/libs/core/executors/tests/unit/parallel_scheduler.cpp index 5cb33b4cbf5c..9689d6054a89 100644 --- a/libs/core/executors/tests/unit/parallel_scheduler.cpp +++ b/libs/core/executors/tests/unit/parallel_scheduler.cpp @@ -113,8 +113,9 @@ namespace { ex::parallel_scheduler_receiver_proxy& proxy) noexcept { source_.request_stop(); - using stop_token_type = decltype(source_.get_token()); - auto token = proxy.try_query(ex::get_stop_token); + // P3804R2: Query for the adapted inplace_stop_token, not the native type + auto token = + proxy.try_query(ex::get_stop_token); found_token_ = token.has_value() && token->stop_requested(); found_unsupported_ = proxy.try_query(ex::get_stop_token).has_value(); @@ -169,13 +170,55 @@ namespace { } // namespace +// Helper receiver with a non-inplace stop token (hpx::stop_token) for +// testing P3804R2 Section 3.3 stop token adaptation across proxy boundaries. +struct non_inplace_stop_receiver +{ + using receiver_concept = ex::receiver_t; + hpx::stop_token stop_token_; + bool* set_value_called_ = nullptr; + + struct env + { + hpx::stop_token token_; + + hpx::stop_token query(ex::get_stop_token_t) const noexcept + { + return token_; + } + }; + + env get_env() const noexcept + { + return env{stop_token_}; + } + + template + void set_value(Ts&&...) && noexcept + { + if (set_value_called_) + { + *set_value_called_ = true; + } + } + + void set_error(std::exception_ptr) && noexcept + { + HPX_TEST(false); + } + + void set_stopped() && noexcept {} +}; + int hpx_main(int, char*[]) { // Type and Concept Tests static_assert( - std::has_virtual_destructor_v); - static_assert(std::has_virtual_destructor_v< - ex::parallel_scheduler_bulk_item_receiver_proxy>); + !std::has_virtual_destructor_v, + "P3804R2: Proxies must not have virtual destructors"); + static_assert(!std::has_virtual_destructor_v< + ex::parallel_scheduler_bulk_item_receiver_proxy>, + "P3804R2: Proxies must not have virtual destructors"); // parallel_scheduler models scheduler concept { @@ -1282,6 +1325,472 @@ int hpx_main(int, char*[]) } } + // ======================================================================== + // P3804R2 TRY_QUERY AND STOP TOKEN ADAPTATION TESTS + // ======================================================================== + + // P3804R2 Section 3.1: try_query(get_stop_token_t{}) + // returns a valid token through the proxy. + { + bool got_token = false; + bool token_not_stopped = false; + + struct try_query_backend final : ex::parallel_scheduler_backend + { + bool& got_token_; + bool& token_not_stopped_; + + try_query_backend(bool& g, bool& ns) + : got_token_(g) + , token_not_stopped_(ns) + { + } + + void schedule(ex::parallel_scheduler_receiver_proxy& proxy, + std::span) noexcept override + { + // P3804R2: Backend queries the proxy for an + // inplace_stop_token. + auto tok = proxy.try_query( + ex::get_stop_token_t{}); + got_token_ = tok.has_value(); + if (tok) + { + token_not_stopped_ = !tok->stop_requested(); + } + proxy.set_value(); + } + + void schedule_bulk_chunked(std::size_t, + ex::parallel_scheduler_bulk_item_receiver_proxy& p, + std::span) noexcept override + { + p.set_value(); + } + void schedule_bulk_unchunked(std::size_t, + ex::parallel_scheduler_bulk_item_receiver_proxy& p, + std::span) noexcept override + { + p.set_value(); + } + bool equal_to( + ex::parallel_scheduler_backend const& o) const noexcept override + { + return this == &o; + } + }; + + auto b = + std::make_shared(got_token, token_not_stopped); + auto orig = ex::query_parallel_scheduler_backend(); + ex::set_parallel_scheduler_backend(b); + auto sched = ex::get_parallel_scheduler(); + ex::sync_wait(ex::schedule(sched)); + + // The proxy should have returned a valid inplace_stop_token + HPX_TEST(got_token); + // And it should not have been stopped (no stop request in flight) + HPX_TEST(token_not_stopped); + + ex::set_parallel_scheduler_backend(orig); + } + + // P3804R2 Section 3.1: try_query with an unsupported query type + // returns std::nullopt. + { + bool query_returned_nullopt = false; + + struct unsupported_query_backend final : ex::parallel_scheduler_backend + { + bool& result_; + explicit unsupported_query_backend(bool& r) + : result_(r) + { + } + + void schedule(ex::parallel_scheduler_receiver_proxy& proxy, + std::span) noexcept override + { + // Query for an unsupported type (get_allocator_t -> int) + auto val = proxy.try_query(ex::get_allocator_t{}); + result_ = !val.has_value(); + proxy.set_value(); + } + + void schedule_bulk_chunked(std::size_t, + ex::parallel_scheduler_bulk_item_receiver_proxy& p, + std::span) noexcept override + { + p.set_value(); + } + void schedule_bulk_unchunked(std::size_t, + ex::parallel_scheduler_bulk_item_receiver_proxy& p, + std::span) noexcept override + { + p.set_value(); + } + bool equal_to( + ex::parallel_scheduler_backend const& o) const noexcept override + { + return this == &o; + } + }; + + auto b = + std::make_shared(query_returned_nullopt); + auto orig = ex::query_parallel_scheduler_backend(); + ex::set_parallel_scheduler_backend(b); + auto sched = ex::get_parallel_scheduler(); + ex::sync_wait(ex::schedule(sched)); + + HPX_TEST(query_returned_nullopt); + ex::set_parallel_scheduler_backend(orig); + } + + // P3804R2 Section 3.3: Schedule path -- Receiver with a non-inplace stop + // token (e.g. hpx::stop_token) requests a stop, and the proxy correctly + // reports stop_requested() == true across the boundary. + { + hpx::stop_source stop_src; + bool proxy_saw_initial_false = false; + bool proxy_saw_stop_requested = false; + bool proxy_tok_saw_stop = false; + bool receiver_completed = false; + + struct non_inplace_stop_backend final : ex::parallel_scheduler_backend + { + hpx::stop_source& stop_src_; + bool& saw_initial_false_; + bool& saw_stop_requested_; + bool& tok_saw_stop_; + + non_inplace_stop_backend(hpx::stop_source& src, bool& init_f, + bool& saw_stop, bool& tok_stop) + : stop_src_(src) + , saw_initial_false_(init_f) + , saw_stop_requested_(saw_stop) + , tok_saw_stop_(tok_stop) + { + } + + void schedule(ex::parallel_scheduler_receiver_proxy& proxy, + std::span) noexcept override + { + // Prior to stop request, proxy should report false + saw_initial_false_ = !proxy.stop_requested(); + + // Non-inplace receiver requests stop + stop_src_.request_stop(); + + // Proxy must now report stop_requested() == true + saw_stop_requested_ = proxy.stop_requested(); + + // Querying adapted inplace_stop_token must also reflect stop + auto tok = proxy.try_query( + ex::get_stop_token_t{}); + if (tok) + { + tok_saw_stop_ = tok->stop_requested(); + } + + proxy.set_value(); + } + + void schedule_bulk_chunked(std::size_t, + ex::parallel_scheduler_bulk_item_receiver_proxy& p, + std::span) noexcept override + { + p.set_value(); + } + void schedule_bulk_unchunked(std::size_t, + ex::parallel_scheduler_bulk_item_receiver_proxy& p, + std::span) noexcept override + { + p.set_value(); + } + bool equal_to( + ex::parallel_scheduler_backend const& o) const noexcept override + { + return this == &o; + } + }; + + auto b = std::make_shared(stop_src, + proxy_saw_initial_false, proxy_saw_stop_requested, + proxy_tok_saw_stop); + auto orig = ex::query_parallel_scheduler_backend(); + ex::set_parallel_scheduler_backend(b); + auto sched = ex::get_parallel_scheduler(); + + auto op = ex::connect(ex::schedule(sched), + non_inplace_stop_receiver{ + stop_src.get_token(), &receiver_completed}); + ex::start(op); + + HPX_TEST(proxy_saw_initial_false); + HPX_TEST(proxy_saw_stop_requested); + HPX_TEST(proxy_tok_saw_stop); + HPX_TEST(receiver_completed); + + ex::set_parallel_scheduler_backend(orig); + } + + // P3804R2 Section 3.1 & 3.3: try_query(get_stop_token_t{}) + // successfully returns a token on the bulk path. + { + bool bulk_got_token = false; + bool bulk_token_not_stopped = false; + + struct bulk_query_backend final : ex::parallel_scheduler_backend + { + bool& got_token_; + bool& token_not_stopped_; + + bulk_query_backend(bool& g, bool& ns) + : got_token_(g) + , token_not_stopped_(ns) + { + } + + void schedule(ex::parallel_scheduler_receiver_proxy& p, + std::span) noexcept override + { + p.set_value(); + } + + void schedule_bulk_chunked(std::size_t count, + ex::parallel_scheduler_bulk_item_receiver_proxy& p, + std::span) noexcept override + { + auto tok = + p.try_query(ex::get_stop_token_t{}); + got_token_ = tok.has_value(); + if (tok) + { + token_not_stopped_ = !tok->stop_requested(); + } + if (count > 0) + { + p.execute(0, count); + } + p.set_value(); + } + + void schedule_bulk_unchunked(std::size_t count, + ex::parallel_scheduler_bulk_item_receiver_proxy& p, + std::span) noexcept override + { + auto tok = + p.try_query(ex::get_stop_token_t{}); + got_token_ = tok.has_value(); + if (tok) + { + token_not_stopped_ = !tok->stop_requested(); + } + for (std::size_t i = 0; i < count; ++i) + { + p.execute(i, i + 1); + } + p.set_value(); + } + + bool equal_to( + ex::parallel_scheduler_backend const& o) const noexcept override + { + return this == &o; + } + }; + + auto b = std::make_shared( + bulk_got_token, bulk_token_not_stopped); + auto orig = ex::query_parallel_scheduler_backend(); + ex::set_parallel_scheduler_backend(b); + auto sched = ex::get_parallel_scheduler(); + + std::vector results(5, 0); + auto snd = ex::schedule(sched) | + ex::bulk_unchunked( + ex::par, 5, [&results](std::size_t i) { results[i] = 1; }); + ex::sync_wait(std::move(snd)); + + HPX_TEST(bulk_got_token); + HPX_TEST(bulk_token_not_stopped); + for (int i = 0; i < 5; ++i) + { + HPX_TEST_EQ(results[i], 1); + } + + ex::set_parallel_scheduler_backend(orig); + } + + // P3804R2 Section 3.1: Bulk path -- Unsupported query returns std::nullopt. + { + bool query_returned_nullopt = false; + + struct bulk_unsupported_query_backend final + : ex::parallel_scheduler_backend + { + bool& result_; + explicit bulk_unsupported_query_backend(bool& r) + : result_(r) + { + } + + void schedule(ex::parallel_scheduler_receiver_proxy& proxy, + std::span) noexcept override + { + proxy.set_value(); + } + + void schedule_bulk_chunked(std::size_t count, + ex::parallel_scheduler_bulk_item_receiver_proxy& p, + std::span) noexcept override + { + auto val = p.try_query(ex::get_allocator_t{}); + result_ = !val.has_value(); + if (count > 0) + { + p.execute(0, count); + } + p.set_value(); + } + + void schedule_bulk_unchunked(std::size_t count, + ex::parallel_scheduler_bulk_item_receiver_proxy& p, + std::span) noexcept override + { + auto val = p.try_query(ex::get_allocator_t{}); + result_ = !val.has_value(); + for (std::size_t i = 0; i < count; ++i) + { + p.execute(i, i + 1); + } + p.set_value(); + } + + bool equal_to( + ex::parallel_scheduler_backend const& o) const noexcept override + { + return this == &o; + } + }; + + auto b = std::make_shared( + query_returned_nullopt); + auto orig = ex::query_parallel_scheduler_backend(); + ex::set_parallel_scheduler_backend(b); + auto sched = ex::get_parallel_scheduler(); + + auto snd = ex::schedule(sched) | + ex::bulk_chunked(ex::par, 5, [](std::size_t, std::size_t) {}); + ex::sync_wait(std::move(snd)); + + HPX_TEST(query_returned_nullopt); + ex::set_parallel_scheduler_backend(orig); + } + + // P3804R2 Section 3.3: Bulk path -- Receiver with a non-inplace stop token + // (e.g. hpx::stop_token) requests a stop, and the bulk proxy correctly + // reports stop_requested() == true across the boundary. + { + hpx::stop_source stop_src; + bool proxy_saw_initial_false = false; + bool proxy_saw_stop_requested = false; + bool proxy_tok_saw_stop = false; + bool receiver_completed = false; + + struct non_inplace_stop_bulk_backend final + : ex::parallel_scheduler_backend + { + hpx::stop_source& stop_src_; + bool& saw_initial_false_; + bool& saw_stop_requested_; + bool& tok_saw_stop_; + + non_inplace_stop_bulk_backend(hpx::stop_source& src, bool& init_f, + bool& saw_stop, bool& tok_stop) + : stop_src_(src) + , saw_initial_false_(init_f) + , saw_stop_requested_(saw_stop) + , tok_saw_stop_(tok_stop) + { + } + + void schedule(ex::parallel_scheduler_receiver_proxy& p, + std::span) noexcept override + { + p.set_value(); + } + + void schedule_bulk_chunked(std::size_t count, + ex::parallel_scheduler_bulk_item_receiver_proxy& p, + std::span) noexcept override + { + saw_initial_false_ = !p.stop_requested(); + stop_src_.request_stop(); + saw_stop_requested_ = p.stop_requested(); + auto tok = + p.try_query(ex::get_stop_token_t{}); + if (tok) + { + tok_saw_stop_ = tok->stop_requested(); + } + if (count > 0) + { + p.execute(0, count); + } + p.set_value(); + } + + void schedule_bulk_unchunked(std::size_t count, + ex::parallel_scheduler_bulk_item_receiver_proxy& p, + std::span) noexcept override + { + saw_initial_false_ = !p.stop_requested(); + stop_src_.request_stop(); + saw_stop_requested_ = p.stop_requested(); + auto tok = + p.try_query(ex::get_stop_token_t{}); + if (tok) + { + tok_saw_stop_ = tok->stop_requested(); + } + for (std::size_t i = 0; i < count; ++i) + { + p.execute(i, i + 1); + } + p.set_value(); + } + + bool equal_to( + ex::parallel_scheduler_backend const& o) const noexcept override + { + return this == &o; + } + }; + + auto b = std::make_shared(stop_src, + proxy_saw_initial_false, proxy_saw_stop_requested, + proxy_tok_saw_stop); + auto orig = ex::query_parallel_scheduler_backend(); + ex::set_parallel_scheduler_backend(b); + auto sched = ex::get_parallel_scheduler(); + + auto snd = ex::schedule(sched) | + ex::bulk_chunked(ex::par, 5, [](std::size_t, std::size_t) {}); + auto op = ex::connect(std::move(snd), + non_inplace_stop_receiver{ + stop_src.get_token(), &receiver_completed}); + ex::start(op); + + HPX_TEST(proxy_saw_initial_false); + HPX_TEST(proxy_saw_stop_requested); + HPX_TEST(proxy_tok_saw_stop); + HPX_TEST(receiver_completed); + + ex::set_parallel_scheduler_backend(orig); + } + return hpx::local::finalize(); } From 25cdb28c87f249d6f7da9269361db46cfc13e4d9 Mon Sep 17 00:00:00 2001 From: Shivansh Singh Date: Thu, 12 Nov 2026 21:53:41 +0530 Subject: [PATCH 2/3] Executors: Fix query_env_impl override signature and token storage in receiver proxy Signed-off-by: Shivansh Singh --- .../include/hpx/executors/task_scheduler.hpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/libs/core/executors/include/hpx/executors/task_scheduler.hpp b/libs/core/executors/include/hpx/executors/task_scheduler.hpp index 6438c8341f36..9bf386800fe6 100644 --- a/libs/core/executors/include/hpx/executors/task_scheduler.hpp +++ b/libs/core/executors/include/hpx/executors/task_scheduler.hpp @@ -573,7 +573,7 @@ namespace hpx::execution::experimental { } protected: - void query_env(std::type_info const& query_type, + bool query_env_impl(std::type_info const& query_type, std::type_info const& result_type, void* result) const noexcept override { @@ -585,12 +585,15 @@ namespace hpx::execution::experimental { get_stop_token_t) && result_type == typeid(token_type)) { - static_cast*>(result) - ->emplace( - hpx::execution::experimental::get_stop_token( - hpx::execution::experimental::get_env( - receiver_))); + auto* target = static_cast(result); + ::new (target) + token_type(hpx::execution::experimental:: + get_stop_token( + hpx::execution::experimental::get_env( + receiver_))); + return true; } + return false; } }; From 28d10d80db9d8d4cf11f55a78d24a722ee084959 Mon Sep 17 00:00:00 2001 From: Shivansh Singh Date: Thu, 12 Nov 2026 21:58:50 +0530 Subject: [PATCH 3/3] Format: Fix clang-format check for placement new in query_env_impl Signed-off-by: Shivansh Singh --- .../executors/include/hpx/executors/task_scheduler.hpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/libs/core/executors/include/hpx/executors/task_scheduler.hpp b/libs/core/executors/include/hpx/executors/task_scheduler.hpp index 9bf386800fe6..f1862b659bab 100644 --- a/libs/core/executors/include/hpx/executors/task_scheduler.hpp +++ b/libs/core/executors/include/hpx/executors/task_scheduler.hpp @@ -586,11 +586,10 @@ namespace hpx::execution::experimental { result_type == typeid(token_type)) { auto* target = static_cast(result); - ::new (target) - token_type(hpx::execution::experimental:: - get_stop_token( - hpx::execution::experimental::get_env( - receiver_))); + ::new (target) token_type( + hpx::execution::experimental::get_stop_token( + hpx::execution::experimental::get_env( + receiver_))); return true; } return false;