@@ -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,
0 commit comments