Skip to content

Commit d2140a6

Browse files
Address review feedback: deduplicate adaptation traits, protect destructors, and add try_query tests
Signed-off-by: Shivansh Singh <singhshivansh023@gmail.com>
1 parent bfe9bc7 commit d2140a6

3 files changed

Lines changed: 574 additions & 63 deletions

File tree

‎libs/core/executors/include/hpx/executors/parallel_scheduler.hpp‎

Lines changed: 55 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,38 @@ namespace hpx::execution::experimental {
5454
// negligible.
5555
namespace detail {
5656

57+
// P3804R2 Section 3.3: Stop token adaptation traits shared by
58+
// operation_state and virtual_parallel_bulk_op.
59+
template <typename Receiver>
60+
struct stop_token_adaptation_traits
61+
{
62+
using receiver_env_t = env_of_t<std::decay_t<Receiver> const&>;
63+
using native_stop_token_t = stop_token_of_t<receiver_env_t>;
64+
65+
static constexpr bool native_is_inplace =
66+
std::is_same_v<native_stop_token_t, inplace_stop_token>;
67+
68+
struct forward_stop_request
69+
{
70+
inplace_stop_source& source_;
71+
void operator()() noexcept
72+
{
73+
source_.request_stop();
74+
}
75+
};
76+
77+
struct empty_stop_callback
78+
{
79+
};
80+
81+
// clang-format off
82+
using stop_callback_t = std::conditional_t<native_is_inplace,
83+
empty_stop_callback,
84+
stop_callback_for_t<native_stop_token_t,
85+
forward_stop_request>>;
86+
// clang-format on
87+
};
88+
5789
// Virtual base for type-erased bulk operation states.
5890
HPX_CXX_CORE_EXPORT struct base_parallel_bulk_op
5991
{
@@ -96,31 +128,14 @@ namespace hpx::execution::experimental {
96128
// ---- Stop token adaptation (parity with operation_state) ----
97129
// P3804R2 Section 3.3: The bulk path must provide the same
98130
// inplace_stop_token adaptation as the schedule path.
99-
using receiver_env_t = env_of_t<std::decay_t<Receiver> const&>;
100-
using native_stop_token_t = stop_token_of_t<receiver_env_t>;
101-
131+
using traits_t = stop_token_adaptation_traits<Receiver>;
132+
using native_stop_token_t = typename traits_t::native_stop_token_t;
102133
static constexpr bool native_is_inplace =
103-
std::is_same_v<native_stop_token_t, inplace_stop_token>;
104-
105-
struct forward_stop_request
106-
{
107-
inplace_stop_source& source_;
108-
void operator()() noexcept
109-
{
110-
source_.request_stop();
111-
}
112-
};
113-
114-
struct empty_stop_callback
115-
{
116-
};
117-
118-
// clang-format off
119-
using stop_callback_t = std::conditional_t<native_is_inplace,
120-
empty_stop_callback,
121-
typename native_stop_token_t::template callback_type<
122-
forward_stop_request>>;
123-
// clang-format on
134+
traits_t::native_is_inplace;
135+
using forward_stop_request =
136+
typename traits_t::forward_stop_request;
137+
using empty_stop_callback = typename traits_t::empty_stop_callback;
138+
using stop_callback_t = typename traits_t::stop_callback_t;
124139

125140
public:
126141
std::shared_ptr<parallel_scheduler_backend> backend_;
@@ -277,8 +292,8 @@ namespace hpx::execution::experimental {
277292
{
278293
// Return the adapted token from our
279294
// inplace_stop_source.
280-
::new (result_out) inplace_stop_token(
281-
op_.stop_source_.get_token());
295+
::new (result_out)
296+
inplace_stop_token(op_.stop_source_.get_token());
282297
}
283298
return true;
284299
}
@@ -767,40 +782,14 @@ namespace hpx::execution::experimental {
767782
struct operation_state
768783
{
769784
private:
770-
// Compute the receiver's native stop token type.
771-
using receiver_env_t = env_of_t<std::decay_t<Receiver> const&>;
772-
using native_stop_token_t = stop_token_of_t<receiver_env_t>;
773-
774-
// P3804R2: Is the native stop token already inplace?
785+
using traits_t = detail::stop_token_adaptation_traits<Receiver>;
786+
using native_stop_token_t = typename traits_t::native_stop_token_t;
775787
static constexpr bool native_is_inplace =
776-
std::is_same_v<native_stop_token_t, inplace_stop_token>;
777-
778-
// Stop callback functor that forwards stop requests from
779-
// the receiver's arbitrary stop token to our
780-
// inplace_stop_source.
781-
struct forward_stop_request
782-
{
783-
inplace_stop_source& source_;
784-
void operator()() noexcept
785-
{
786-
source_.request_stop();
787-
}
788-
};
789-
790-
// The stop callback type, only instantiated when adaptation
791-
// is needed (native token is not inplace).
792-
// When native_is_inplace is true, this is a lightweight
793-
// empty struct (zero overhead).
794-
struct empty_stop_callback
795-
{
796-
};
797-
798-
// clang-format off
799-
using stop_callback_t = std::conditional_t<native_is_inplace,
800-
empty_stop_callback,
801-
typename native_stop_token_t::template callback_type<
802-
forward_stop_request>>;
803-
// clang-format on
788+
traits_t::native_is_inplace;
789+
using forward_stop_request =
790+
typename traits_t::forward_stop_request;
791+
using empty_stop_callback = typename traits_t::empty_stop_callback;
792+
using stop_callback_t = typename traits_t::stop_callback_t;
804793

805794
public:
806795
// Concrete receiver_proxy that adapts the actual Receiver
@@ -900,8 +889,8 @@ namespace hpx::execution::experimental {
900889
// callback registered in start() ensures that
901890
// stop requests propagate from the receiver's
902891
// arbitrary token to this source.
903-
::new (result_out) inplace_stop_token(
904-
op_.stop_source_.get_token());
892+
::new (result_out)
893+
inplace_stop_token(op_.stop_source_.get_token());
905894
}
906895
return true;
907896
}
@@ -965,8 +954,11 @@ namespace hpx::execution::experimental {
965954
// (the proxy returns the native token directly).
966955
if constexpr (!native_is_inplace)
967956
{
968-
stop_callback_.emplace(
969-
stop_token, forward_stop_request{stop_source_});
957+
if (stop_token.stop_possible())
958+
{
959+
stop_callback_.emplace(
960+
stop_token, forward_stop_request{stop_source_});
961+
}
970962
}
971963

972964
// Delegate to the backend via the member proxy,

‎libs/core/executors/include/hpx/executors/parallel_scheduler_backend.hpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <new>
2626
#include <optional>
2727
#include <span>
28+
#include <type_traits>
2829
#include <typeinfo>
2930
#include <utility>
3031

@@ -80,6 +81,12 @@ namespace hpx::execution::experimental {
8081
template <typename P, typename Query>
8182
std::optional<P> try_query(Query /* q */) const noexcept
8283
{
84+
static_assert(std::is_object_v<P> && !std::is_array_v<P> &&
85+
std::is_same_v<P, std::remove_cv_t<P>>,
86+
"P must be a cv-unqualified, non-array object type");
87+
static_assert(std::is_nothrow_move_constructible_v<P>,
88+
"P must be nothrow move constructible");
89+
8390
alignas(P) std::byte storage[sizeof(P)];
8491
if (query_env_impl(
8592
typeid(Query), typeid(P), static_cast<void*>(&storage)))
@@ -98,6 +105,10 @@ namespace hpx::execution::experimental {
98105
HPX_CXX_CORE_EXPORT struct parallel_scheduler_bulk_item_receiver_proxy
99106
: parallel_scheduler_receiver_proxy
100107
{
108+
protected:
109+
~parallel_scheduler_bulk_item_receiver_proxy() = default;
110+
111+
public:
101112
virtual void execute(std::size_t begin, std::size_t end) noexcept = 0;
102113
};
103114

0 commit comments

Comments
 (0)