diff --git a/include/beman/execution/detail/parallel_scheduler.hpp b/include/beman/execution/detail/parallel_scheduler.hpp index 5bf0bf31..a2630039 100644 --- a/include/beman/execution/detail/parallel_scheduler.hpp +++ b/include/beman/execution/detail/parallel_scheduler.hpp @@ -33,6 +33,7 @@ import beman.execution.detail.inplace_stop_source; import beman.execution.detail.operation_state; import beman.execution.detail.psched_bulk_sender; import beman.execution.detail.parallel_scheduler_replacement; +import beman.execution.detail.rcvr_proxy_base; import beman.execution.detail.receiver; import beman.execution.detail.scheduler_tag; import beman.execution.detail.sender; @@ -58,6 +59,7 @@ import beman.execution.detail.stop_token_of_t; #include #include #include +#include #include #include #include @@ -163,27 +165,16 @@ class parallel_scheduler::sender { }; template - struct rcvr_proxy : ::beman::execution::parallel_scheduler_replacement::receiver_proxy { - using stop_token_t = ::beman::execution::stop_token_of_t<::beman::execution::env_of_t>; + struct rcvr_proxy : ::beman::execution::detail::rcvr_proxy_base { + using base = ::beman::execution::detail::rcvr_proxy_base; - explicit rcvr_proxy(Rcvr rcvr) noexcept - : sp_(::beman::execution::get_stop_token(::beman::execution::get_env(rcvr))), rcvr_(::std::move(rcvr)) {} + explicit rcvr_proxy(Rcvr rcvr) noexcept : base(::std::move(rcvr)) {} auto set_value() noexcept -> void final { ::beman::execution::set_value(::std::move(this->rcvr_)); } auto set_error(::std::exception_ptr error) noexcept -> void final { ::beman::execution::set_error(::std::move(this->rcvr_), ::std::move(error)); } - - auto set_stopped() noexcept -> void final { ::beman::execution::set_stopped(::std::move(this->rcvr_)); } - - private: - auto query_stop_token() const noexcept -> ::std::optional<::beman::execution::inplace_stop_token> final { - return sp_.get_token(); - } - - [[BEMAN_EXECUTION_NO_UNIQUE_ADDRESS]] ::beman::execution::detail::stop_propagator sp_; - Rcvr rcvr_; }; template <::beman::execution::receiver Rcvr> diff --git a/include/beman/execution/detail/psched_bulk_sender.hpp b/include/beman/execution/detail/psched_bulk_sender.hpp index 25a0f7de..37bdfcab 100644 --- a/include/beman/execution/detail/psched_bulk_sender.hpp +++ b/include/beman/execution/detail/psched_bulk_sender.hpp @@ -27,6 +27,7 @@ import beman.execution.detail.completion_signatures; import beman.execution.detail.decayed_tuple; import beman.execution.detail.env_of_t; import beman.execution.detail.execution_policy; +import beman.execution.detail.fwd_env; import beman.execution.detail.get_completion_signatures; import beman.execution.detail.get_env; import beman.execution.detail.get_stop_token; @@ -36,6 +37,7 @@ import beman.execution.detail.meta.prepend; import beman.execution.detail.meta.unique; import beman.execution.detail.operation_state; import beman.execution.detail.parallel_scheduler_replacement; +import beman.execution.detail.rcvr_proxy_base; import beman.execution.detail.receiver; import beman.execution.detail.remap_attrs; import beman.execution.detail.sender; @@ -54,6 +56,7 @@ import beman.execution.detail.value_types_of_t; #include #include #include +#include #include #include #include @@ -63,6 +66,7 @@ import beman.execution.detail.value_types_of_t; #include #include #include +#include #include #include #include @@ -89,20 +93,18 @@ struct psched_bulk_sender { static constexpr bool is_parallel_policy = ::std::same_as || ::std::same_as; template - struct rcvr_proxy : ::beman::execution::parallel_scheduler_replacement::bulk_item_receiver_proxy { + struct rcvr_proxy : ::beman::execution::detail::rcvr_proxy_base { + using base = ::beman::execution::detail::rcvr_proxy_base; using result_type = ::beman::execution::detail::meta:: prepend<::std::monostate, ::beman::execution::value_types_of_t>>; using stop_token_t = ::beman::execution::stop_token_of_t<::beman::execution::env_of_t>; - rcvr_proxy(::std::shared_ptr backend, Rcvr rcvr, Policy policy, Shape shape, Fn fn) noexcept - : backend(::std::move(backend)), - sp(::beman::execution::get_stop_token(::beman::execution::get_env(rcvr))), - rcvr(::std::move(rcvr)), - policy(::std::move(policy)), - shape(::std::move(shape)), - fn(::std::move(fn)) {} + rcvr_proxy(::std::shared_ptr backend, Rcvr rcvr, Shape shape, Fn fn) noexcept + : base(::std::move(rcvr)), backend(::std::move(backend)), shape(::std::move(shape)), fn(::std::move(fn)) {} - auto get_env() const noexcept { return ::beman::execution::get_env(rcvr); } + auto get_env() const noexcept { + return ::beman::execution::detail::fwd_env(::beman::execution::get_env(this->rcvr_)); + } auto execute(::std::size_t begin, ::std::size_t end) noexcept -> void final { assert(begin < end); @@ -144,7 +146,7 @@ struct psched_bulk_sender { return; } const auto call_set_value = [this](Args&&... args) { - ::beman::execution::set_value(::std::move(rcvr), ::std::forward(args)...); + ::beman::execution::set_value(::std::move(this->rcvr_), ::std::forward(args)...); }; BEMAN_EXECUTION_TRY { ::std::visit( @@ -162,29 +164,19 @@ struct psched_bulk_sender { template auto set_error(E e) noexcept -> void { - ::beman::execution::set_error(::std::move(rcvr), ::std::move(e)); + ::beman::execution::set_error(::std::move(this->rcvr_), ::std::move(e)); } auto set_error(::std::exception_ptr e) noexcept -> void final { this->set_error<>(::std::move(e)); } - auto set_stopped() noexcept -> void final { ::beman::execution::set_stopped(::std::move(rcvr)); } - - private: - auto query_stop_token() const noexcept -> ::std::optional<::beman::execution::inplace_stop_token> final { - return sp.get_token(); - } - public: alignas(psched_storage_alignment)::std::byte storage[psched_storage_size]; - ::std::shared_ptr backend; - result_type result; - [[BEMAN_EXECUTION_NO_UNIQUE_ADDRESS]] ::beman::execution::detail::stop_propagator sp; - Rcvr rcvr; - Policy policy; - Shape shape; - Fn fn; - ::std::atomic has_exception{false}; - ::std::exception_ptr exception; + ::std::shared_ptr backend; + result_type result; + Shape shape; + Fn fn; + ::std::atomic has_exception{false}; + ::std::exception_ptr exception; }; template @@ -227,8 +219,8 @@ struct psched_bulk_sender { }; using sub_state_t = ::beman::execution::connect_result_t; - state(::std::shared_ptr backend, Child child, Rcvr rcvr, Policy policy, Shape shape, Fn fn) - : proxy(::std::move(backend), ::std::move(rcvr), ::std::move(policy), ::std::move(shape), ::std::move(fn)), + state(::std::shared_ptr backend, Child child, Rcvr rcvr, const Policy&, Shape shape, Fn fn) + : proxy(::std::move(backend), ::std::move(rcvr), ::std::move(shape), ::std::move(fn)), sub_state(::beman::execution::connect(::std::move(child), receiver_ref{&proxy})) {} auto start() & noexcept -> void { ::beman::execution::start(sub_state); } diff --git a/include/beman/execution/detail/rcvr_proxy_base.hpp b/include/beman/execution/detail/rcvr_proxy_base.hpp new file mode 100644 index 00000000..0058c91f --- /dev/null +++ b/include/beman/execution/detail/rcvr_proxy_base.hpp @@ -0,0 +1,89 @@ +// include/beman/execution/detail/rcvr_proxy_base.hpp -*-C++-*- +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef INCLUDED_BEMAN_EXECUTION_DETAIL_RCVR_PROXY_BASE +#define INCLUDED_BEMAN_EXECUTION_DETAIL_RCVR_PROXY_BASE + +#include +#ifdef BEMAN_HAS_IMPORT_STD +import std; +#else +#include +#include +#include +#endif +#ifdef BEMAN_HAS_MODULES +import beman.execution.detail.env_of_t; +import beman.execution.detail.get_env; +import beman.execution.detail.get_stop_token; +import beman.execution.detail.inplace_stop_source; +import beman.execution.detail.parallel_scheduler_replacement; +import beman.execution.detail.stop_propagator; +import beman.execution.detail.stop_token_of_t; +import beman.execution.detail.set_stopped; +import beman.execution.detail.unreachable; +import beman.execution.detail.unstoppable_token; +#else +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +// ---------------------------------------------------------------------------- + +namespace beman::execution::detail { +template +using select_rcvr_proxy_base = + ::std::conditional_t; + +template +struct rcvr_proxy_base : select_rcvr_proxy_base { + using stop_token_t = ::beman::execution::stop_token_of_t<::beman::execution::env_of_t>; + + explicit rcvr_proxy_base(Rcvr rcvr) noexcept + : sp_(::beman::execution::get_stop_token(::beman::execution::get_env(rcvr))), rcvr_(::std::move(rcvr)) {} + + auto set_stopped() noexcept -> void final { ::beman::execution::set_stopped(::std::move(this->rcvr_)); } + + private: + auto query_stop_token() const noexcept -> ::std::optional<::beman::execution::inplace_stop_token> final { + return sp_.get_token(); + } + + protected: + ::beman::execution::detail::stop_propagator sp_; + Rcvr rcvr_; +}; + +template + requires ::beman::execution::unstoppable_token< + ::beman::execution::stop_token_of_t<::beman::execution::env_of_t>> +struct rcvr_proxy_base : select_rcvr_proxy_base { + using stop_token_t = ::beman::execution::stop_token_of_t<::beman::execution::env_of_t>; + + explicit rcvr_proxy_base(Rcvr rcvr) noexcept : rcvr_(::std::move(rcvr)) {} + + auto set_stopped() noexcept -> void final { ::beman::execution::detail::unreachable(); } + + private: + auto query_stop_token() const noexcept -> ::std::optional<::beman::execution::inplace_stop_token> final { + return ::std::nullopt; + } + + protected: + Rcvr rcvr_; +}; +} // namespace beman::execution::detail + +// ---------------------------------------------------------------------------- + +#endif // INCLUDED_BEMAN_EXECUTION_DETAIL_RCVR_PROXY_BASE diff --git a/include/beman/execution/detail/stop_combiner.hpp b/include/beman/execution/detail/stop_combiner.hpp new file mode 100644 index 00000000..e4ce33bb --- /dev/null +++ b/include/beman/execution/detail/stop_combiner.hpp @@ -0,0 +1,104 @@ +// include/beman/execution/detail/stop_combiner.hpp -*-C++-*- +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#ifndef INCLUDED_BEMAN_EXECUTION_DETAIL_STOP_COMBINER +#define INCLUDED_BEMAN_EXECUTION_DETAIL_STOP_COMBINER + +#include +#ifdef BEMAN_HAS_IMPORT_STD +import std; +#else +#include +#include +#include +#include +#include +#endif +#ifdef BEMAN_HAS_MODULES +import beman.execution.detail.stoppable_token; +import beman.execution.detail.stop_callback_for_t; +#else +#include +#include +#endif + +// ---------------------------------------------------------------------------- + +namespace beman::execution::detail { +template +class stop_callback_once { + public: + stop_callback_once(Fn& fn, ::std::atomic& flag) noexcept : fn_(::std::addressof(fn)), flag_(&flag) {} + + auto operator()() const noexcept -> void { + if (flag_->exchange(false, ::std::memory_order_relaxed)) { + ::std::invoke(*fn_); + } + } + + private: + Fn* fn_; + ::std::atomic* flag_; +}; + +template +class stop_combiner { + static_assert(::beman::execution::stoppable_token && ::beman::execution::stoppable_token); + + public: + template + class callback_type { + private: + using fn_once_t = ::beman::execution::detail::stop_callback_once; + + public: + template + callback_type(const stop_combiner& combiner, Initializer&& init) noexcept( + ::std::is_nothrow_constructible_v && + ::std::is_nothrow_constructible_v<::beman::execution::stop_callback_for_t, + First, + fn_once_t> && + ::std::is_nothrow_constructible_v<::beman::execution::stop_callback_for_t, + Second, + fn_once_t>) + : fn_(::std::forward(init)), + first_cb_(combiner.first_, fn_once_t{fn_, flag_}), + second_cb_(combiner.second_, fn_once_t{fn_, flag_}) {} + + callback_type(const callback_type&) = delete; + + ~callback_type() = default; + + auto operator=(const callback_type&) -> callback_type& = delete; + + private: + Callback fn_; + ::std::atomic flag_{true}; + ::beman::execution::stop_callback_for_t first_cb_; + ::beman::execution::stop_callback_for_t second_cb_; + }; + + public: + stop_combiner(First first, Second second) noexcept(::std::is_nothrow_move_constructible_v && + ::std::is_nothrow_move_constructible_v) + : first_(::std::move(first)), second_(::std::move(second)) {} + + [[nodiscard]] auto stop_possible() const noexcept -> bool { + return first_.stop_possible() || second_.stop_possible(); + } + + [[nodiscard]] auto stop_requested() const noexcept -> bool { + return first_.stop_requested() || second_.stop_requested(); + } + + friend auto operator==(const stop_combiner& lhs, const stop_combiner& rhs) -> bool = default; + + private: + [[BEMAN_EXECUTION_NO_UNIQUE_ADDRESS]] First first_; + [[BEMAN_EXECUTION_NO_UNIQUE_ADDRESS]] Second second_; +}; +} // namespace beman::execution::detail + +// ---------------------------------------------------------------------------- + +#endif // INCLUDED_BEMAN_EXECUTION_DETAIL_STOP_COMBINER diff --git a/include/beman/execution/detail/stop_propagator.hpp b/include/beman/execution/detail/stop_propagator.hpp index 3c092da3..1f91ad7d 100644 --- a/include/beman/execution/detail/stop_propagator.hpp +++ b/include/beman/execution/detail/stop_propagator.hpp @@ -9,7 +9,6 @@ import std; #else #include -#include #include #include #endif @@ -30,45 +29,49 @@ import beman.execution.detail.unstoppable_token; // ---------------------------------------------------------------------------- namespace beman::execution::detail { -template <::beman::execution::stoppable_token StopToken> +template class stop_propagator { - public: - explicit stop_propagator(StopToken stop_token) noexcept - : stop_callback_(::std::move(stop_token), ::std::bind_front(&stop_propagator::propagate, this)) {} - - auto get_token() const noexcept -> ::std::optional<::beman::execution::inplace_stop_token> { - return stop_source_.get_token(); - } + static_assert(::beman::execution::stoppable_token && + ::beman::execution::detail::stoppable_source); private: - auto propagate() noexcept -> void { stop_source_.request_stop(); } + auto propagate() noexcept(noexcept(::std::declval().request_stop())) -> void { + stop_source_.request_stop(); + } - private: - using stop_cb_t = decltype(::std::bind_front(&stop_propagator::propagate, ::std::declval())); + using propagate_fn = decltype(::std::bind_front(&stop_propagator::propagate, ::std::declval())); + using stop_callback_t = ::beman::execution::stop_callback_for_t; - ::beman::execution::inplace_stop_source stop_source_; - ::beman::execution::stop_callback_for_t stop_callback_; -}; - -template <> -class stop_propagator<::beman::execution::inplace_stop_token> { public: - explicit stop_propagator(::beman::execution::inplace_stop_token stop_token) noexcept : stop_token_(stop_token) {} + template + requires ::std::constructible_from + explicit stop_propagator(StopToken stop_token, Args&&... args) noexcept( + ::std::is_nothrow_constructible_v && + ::std::is_nothrow_constructible_v) + : stop_source_(::std::forward(args)...), + stop_callback_(::std::move(stop_token), ::std::bind_front(&stop_propagator::propagate, this)) {} - auto get_token() const noexcept -> ::std::optional<::beman::execution::inplace_stop_token> { return stop_token_; } + auto get_token() const noexcept { return stop_source_.get_token(); } private: - ::beman::execution::inplace_stop_token stop_token_; + StopSource stop_source_; + [[BEMAN_EXECUTION_NO_UNIQUE_ADDRESS]] stop_callback_t stop_callback_; }; -template <::beman::execution::unstoppable_token StopToken> -class stop_propagator { +template + requires ::std::same_as<::std::decay_t().get_token())>, StopToken> +class stop_propagator { + static_assert(::beman::execution::stoppable_token && + ::beman::execution::detail::stoppable_source); + public: - explicit stop_propagator(StopToken) noexcept {} + explicit stop_propagator(StopToken stop_token) noexcept(::std::is_nothrow_move_constructible_v) + : stop_token_(::std::move(stop_token)) {} - static auto get_token() noexcept -> ::std::optional<::beman::execution::inplace_stop_token> { - return ::std::nullopt; - } + auto get_token() const noexcept -> StopToken { return stop_token_; } + + private: + [[BEMAN_EXECUTION_NO_UNIQUE_ADDRESS]] StopToken stop_token_; }; } // namespace beman::execution::detail diff --git a/include/beman/execution/detail/stop_when.hpp b/include/beman/execution/detail/stop_when.hpp index 64510ed0..239f5b21 100644 --- a/include/beman/execution/detail/stop_when.hpp +++ b/include/beman/execution/detail/stop_when.hpp @@ -14,34 +14,40 @@ import std; #endif #ifdef BEMAN_HAS_MODULES import beman.execution.detail.connect; +import beman.execution.detail.fwd_env; import beman.execution.detail.get_completion_signatures; import beman.execution.detail.get_env; import beman.execution.detail.get_stop_token; -import beman.execution.detail.inplace_stop_source; +import beman.execution.detail.join_env; import beman.execution.detail.operation_state; +import beman.execution.detail.prop; import beman.execution.detail.receiver; import beman.execution.detail.sender; +import beman.execution.detail.sender_in; import beman.execution.detail.set_error; import beman.execution.detail.set_stopped; import beman.execution.detail.set_value; import beman.execution.detail.start; -import beman.execution.detail.stop_callback_for_t; +import beman.execution.detail.stop_combiner; import beman.execution.detail.stoppable_token; import beman.execution.detail.unstoppable_token; #else #include +#include #include #include #include -#include +#include #include +#include #include #include +#include #include #include #include #include -#include +#include #include #include #endif @@ -69,90 +75,74 @@ struct beman::execution::detail::stop_when_t::sender { std::remove_cvref_t sndr; template <::beman::execution::receiver Rcvr> - struct base_state { - using rcvr_t = ::std::remove_cvref_t; - rcvr_t rcvr; - ::beman::execution::inplace_stop_source source{}; - base_state(Rcvr&& r) : rcvr(::std::forward(r)) {} - virtual ~base_state() noexcept = default; - virtual auto reset() & noexcept -> void = 0; - }; - template <::beman::execution::receiver Rcvr> - struct state : base_state { + struct state { using operation_state_concept = ::beman::execution::operation_state_tag; - using rcvr_t = base_state::rcvr_t; + using rcvr_t = ::std::remove_cvref_t; using token1_t = ::std::remove_cvref_t; using token2_t = decltype(::beman::execution::get_stop_token(::beman::execution::get_env(::std::declval()))); - struct cb_t { - ::beman::execution::inplace_stop_source& source; - auto operator()() const noexcept { this->source.request_stop(); } - }; - struct env { - base_state* st; - auto query(const ::beman::execution::get_stop_token_t&) const noexcept { - return this->st->source.get_token(); - } - template - requires requires(const Q& q, A&&... a, const rcvr_t& r) { - q(::beman::execution::get_env(r), ::std::forward(a)...); - } - auto query(const Q& q, A&&... a) const noexcept { - return q(::beman::execution::get_env(this->st->rcvr), ::std::forward(a)...); - } + struct data_t { + token1_t tok; + rcvr_t rcvr; }; struct receiver { using receiver_concept = ::beman::execution::receiver_tag; - base_state* st; - auto get_env() const noexcept -> env { return env{this->st}; } + auto get_env() const noexcept { + auto rcvr_env = ::beman::execution::get_env(data->rcvr); + ::beman::execution::detail::stop_combiner token{data->tok, + ::beman::execution::get_stop_token(rcvr_env)}; + return ::beman::execution::detail::join_env( + ::beman::execution::prop{::beman::execution::get_stop_token, ::std::move(token)}, + ::std::move(rcvr_env)); + } template auto set_value(A&&... a) const noexcept -> void { - this->st->reset(); - ::beman::execution::set_value(::std::move(this->st->rcvr), ::std::forward(a)...); + ::beman::execution::set_value(::std::move(this->data->rcvr), ::std::forward(a)...); } template auto set_error(E&& e) const noexcept -> void { - this->st->reset(); - ::beman::execution::set_error(::std::move(this->st->rcvr), ::std::forward(e)); + ::beman::execution::set_error(::std::move(this->data->rcvr), ::std::forward(e)); } auto set_stopped() const noexcept -> void { - this->st->reset(); - ::beman::execution::set_stopped(::std::move(this->st->rcvr)); + ::beman::execution::set_stopped(::std::move(this->data->rcvr)); } + + data_t* data; }; + using inner_state_t = decltype(::beman::execution::connect(::std::declval(), ::std::declval())); - token1_t tok; - std::optional<::beman::execution::stop_callback_for_t> cb1; - std::optional<::beman::execution::stop_callback_for_t> cb2; - inner_state_t inner_state; - template <::beman::execution::sender S, ::beman::execution::stoppable_token T, ::beman::execution::receiver R> state(S&& s, T&& t, R&& r) - : base_state{::std::forward(r)}, - tok(::std::forward(t)), - inner_state(::beman::execution::connect(::std::forward(s), receiver{this})) {} - - auto start() & noexcept { - this->cb1.emplace(this->tok, cb_t{this->source}); - this->cb2.emplace(::beman::execution::get_stop_token(::beman::execution::get_env(this->rcvr)), - cb_t{this->source}); - ::beman::execution::start(this->inner_state); - } - auto reset() & noexcept -> void override { - this->cb1.reset(); - this->cb2.reset(); - } + : data(::std::forward(t), ::std::forward(r)), + inner_state(::beman::execution::connect(::std::forward(s), receiver{&data})) {} + + state(const state&) = delete; + + ~state() = default; + + auto operator=(const state&) -> state& = delete; + + auto start() & noexcept { ::beman::execution::start(this->inner_state); } + + data_t data; + inner_state_t inner_state; }; template - static consteval auto get_completion_signatures() noexcept { - return ::beman::execution::get_completion_signatures, E...>(); + requires ::beman::execution::sender_in<::std::remove_cvref_t, E...> + static consteval auto get_completion_signatures() { + return ::beman::execution::get_completion_signatures<::std::remove_cvref_t, E...>(); + } + + auto get_env() const noexcept { + return ::beman::execution::detail::fwd_env(::beman::execution::get_env(this->sndr)); } + template <::beman::execution::receiver Rcvr> auto connect(Rcvr&& rcvr) && -> state { return state{std::move(this->sndr), ::std::move(this->tok), ::std::forward(rcvr)}; diff --git a/include/beman/execution/detail/task.hpp b/include/beman/execution/detail/task.hpp index 1c401ff6..852940cd 100644 --- a/include/beman/execution/detail/task.hpp +++ b/include/beman/execution/detail/task.hpp @@ -56,6 +56,7 @@ import beman.execution.detail.simple_allocator; import beman.execution.detail.stoppable_source; import beman.execution.detail.stoppable_token; import beman.execution.detail.stop_callback_for_t; +import beman.execution.detail.stop_propagator; import beman.execution.detail.stop_token_of_t; import beman.execution.detail.sub_visit; import beman.execution.detail.task_scheduler; @@ -97,6 +98,7 @@ import beman.execution.detail.value_signature; #include #include #include +#include #include #include #include @@ -461,39 +463,6 @@ struct env_holder { Environment environment; }; -template -class propagator { - public: - template - requires ::std::constructible_from - explicit propagator(StopToken stop_token, Args&&... args) - : stop_source_(::std::forward(args)...), - stop_callback_(::std::move(stop_token), ::std::bind_front(&propagator::propagate, this)) {} - - auto get_token() const noexcept { return stop_source_.get_token(); } - - private: - auto propagate() -> void { stop_source_.request_stop(); } - - private: - using stop_cb_t = decltype(::std::bind_front(&propagator::propagate, ::std::declval())); - - StopSource stop_source_; - ::beman::execution::stop_callback_for_t stop_callback_; -}; - -template - requires ::std::same_as<::std::decay_t().get_token())>, StopToken> -class propagator { - public: - explicit propagator(StopToken stop_token) noexcept : stop_token_(::std::move(stop_token)) {} - - auto get_token() const noexcept -> StopToken { return stop_token_; } - - private: - StopToken stop_token_; -}; - template auto make_sched(const Env& env) noexcept -> Sched { if constexpr (requires { @@ -564,7 +533,7 @@ struct state : state_base { using stop_source_type = typename state_base::stop_source_type; using stop_token_type = typename state_base::stop_token_type; using upstream_stop_token_type = ::beman::execution::stop_token_of_t<::beman::execution::env_of_t>; - using propagator_type = ::beman::execution::detail::task::propagator; + using propagator_type = ::beman::execution::detail::stop_propagator; template state(Receiver r, H h, const Env& env) noexcept @@ -623,7 +592,7 @@ class awaiter : public state_base { using stop_source_type = typename state_base::stop_source_type; using stop_token_type = typename state_base::stop_token_type; using upstream_stop_token_type = ::beman::execution::stop_token_of_t<::beman::execution::env_of_t>; - using propagator_type = ::beman::execution::detail::task::propagator; + using propagator_type = ::beman::execution::detail::stop_propagator; explicit awaiter(::std::coroutine_handle parent, handle h, const auto& env) noexcept : parent(parent), handle(::std::move(h)), holder(env), scheduler(), propagator() {} diff --git a/include/beman/execution/detail/task_scheduler.hpp b/include/beman/execution/detail/task_scheduler.hpp index fb39e5c7..a28896bd 100644 --- a/include/beman/execution/detail/task_scheduler.hpp +++ b/include/beman/execution/detail/task_scheduler.hpp @@ -41,6 +41,7 @@ import beman.execution.detail.psched_bulk_sender; import beman.execution.detail.parallel_scheduler_replacement; import beman.execution.detail.prop; import beman.execution.detail.query_with_default; +import beman.execution.detail.rcvr_proxy_base; import beman.execution.detail.receiver; import beman.execution.detail.schedule; import beman.execution.detail.schedule_result_t; @@ -82,6 +83,7 @@ import beman.execution.detail.unstoppable_token; #include #include #include +#include #include #include #include @@ -431,31 +433,14 @@ class task_scheduler::sender { }; template - struct rcvr_proxy : ::beman::execution::parallel_scheduler_replacement::receiver_proxy { - using stop_token_t = ::beman::execution::stop_token_of_t<::beman::execution::env_of_t>; + struct rcvr_proxy : ::beman::execution::detail::rcvr_proxy_base { + using base = ::beman::execution::detail::rcvr_proxy_base; - explicit rcvr_proxy(Rcvr rcvr) noexcept - : sp_(::beman::execution::get_stop_token(::beman::execution::get_env(rcvr))), rcvr_(::std::move(rcvr)) {} + explicit rcvr_proxy(Rcvr rcvr) noexcept : base(::std::move(rcvr)) {} auto set_value() noexcept -> void final { ::beman::execution::set_value(::std::move(this->rcvr_)); } auto set_error(::std::exception_ptr) noexcept -> void final { ::beman::execution::detail::unreachable(); } - - auto set_stopped() noexcept -> void final { - if constexpr (::beman::execution::unstoppable_token) { - ::beman::execution::detail::unreachable(); - } else { - ::beman::execution::set_stopped(::std::move(this->rcvr_)); - } - } - - private: - auto query_stop_token() const noexcept -> ::std::optional<::beman::execution::inplace_stop_token> final { - return sp_.get_token(); - } - - [[BEMAN_EXECUTION_NO_UNIQUE_ADDRESS]] ::beman::execution::detail::stop_propagator sp_; - Rcvr rcvr_; }; template <::beman::execution::receiver Rcvr> diff --git a/include/beman/execution/detail/type_id.hpp b/include/beman/execution/detail/type_id.hpp index f4490f7c..dd1e9dc1 100644 --- a/include/beman/execution/detail/type_id.hpp +++ b/include/beman/execution/detail/type_id.hpp @@ -31,7 +31,7 @@ consteval auto type_id_impl() -> type_info { auto start = func_sig.find(prefix); if (start != ::std::string_view::npos) { start += prefix.size(); - auto end = func_sig.find(']', start); + auto end = func_sig.rfind(']'); if (end != ::std::string_view::npos) { return {func_sig.substr(start, end - start)}; } @@ -55,7 +55,10 @@ consteval auto type_id_impl() -> type_info { auto start = func_sig.find(prefix); if (start != ::std::string_view::npos) { start += prefix.size(); - auto end = func_sig.find_first_of(";]", start); + auto end = func_sig.find(';', start); + if (end == ::std::string_view::npos) { + end = func_sig.rfind(']'); + } if (end != ::std::string_view::npos) { return {func_sig.substr(start, end - start)}; } diff --git a/src/beman/execution/CMakeLists.txt b/src/beman/execution/CMakeLists.txt index 92a1ddec..4b4ddd4c 100644 --- a/src/beman/execution/CMakeLists.txt +++ b/src/beman/execution/CMakeLists.txt @@ -166,6 +166,7 @@ target_sources( ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/prop.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/query_with_default.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/queryable.hpp + ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/rcvr_proxy_base.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/read_env.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/receiver.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/receiver_of.hpp @@ -204,6 +205,7 @@ target_sources( ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/starts_on.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/state_type.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/stop_callback_for_t.hpp + ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/stop_combiner.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/stop_propagator.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/stop_source.hpp ${PROJECT_SOURCE_DIR}/include/beman/execution/detail/stop_token_of_t.hpp @@ -405,6 +407,7 @@ if(BEMAN_USE_MODULES) psched_bulk_sender.cppm query_with_default.cppm queryable.cppm + rcvr_proxy_base.cppm read_env.cppm receiver_of.cppm receiver.cppm @@ -444,6 +447,7 @@ if(BEMAN_USE_MODULES) state_type.cppm stoppable_callback_for.cppm stop_callback_for_t.cppm + stop_combiner.cppm stop_propagator.cppm stop_source.cppm stop_token_of_t.cppm diff --git a/src/beman/execution/rcvr_proxy_base.cppm b/src/beman/execution/rcvr_proxy_base.cppm new file mode 100644 index 00000000..4489ab13 --- /dev/null +++ b/src/beman/execution/rcvr_proxy_base.cppm @@ -0,0 +1,11 @@ +module; +// src/beman/execution/rcvr_proxy_base.cppm -*-C++-*- +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include + +export module beman.execution.detail.rcvr_proxy_base; + +namespace beman::execution::detail { +export using beman::execution::detail::rcvr_proxy_base; +} // namespace beman::execution::detail diff --git a/src/beman/execution/stop_combiner.cppm b/src/beman/execution/stop_combiner.cppm new file mode 100644 index 00000000..73dff990 --- /dev/null +++ b/src/beman/execution/stop_combiner.cppm @@ -0,0 +1,11 @@ +module; +// src/beman/execution/stop_combiner.cppm -*-C++-*- +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include + +export module beman.execution.detail.stop_combiner; + +namespace beman::execution::detail { +export using beman::execution::detail::stop_combiner; +} // namespace beman::execution::detail diff --git a/tests/beman/execution/exec-task.test.cpp b/tests/beman/execution/exec-task.test.cpp index 001b0314..f399185f 100644 --- a/tests/beman/execution/exec-task.test.cpp +++ b/tests/beman/execution/exec-task.test.cpp @@ -26,7 +26,8 @@ import std; import beman.execution; import beman.execution.detail.simple_allocator; #else -#include +#include +#include #endif #include @@ -367,13 +368,29 @@ auto test_task_allocator_arg_unrelated_to_allocator_type() -> void { } auto test_task_propagates_stop_token() -> void { - test_std::inplace_stop_source source; - ASSERT(source.request_stop()); + { + test_std::inplace_stop_source source; + ASSERT(source.request_stop()); - bool value{}; - auto operation = test_std::connect(stop_token_task(), stop_token_receiver{source.get_token(), &value}); - test_std::start(operation); - ASSERT(value); + bool value{}; + auto operation = test_std::connect(stop_token_task(), stop_token_receiver{source.get_token(), &value}); + test_std::start(operation); + ASSERT(value); + } + { + test_std::stop_source source; + ASSERT(source.request_stop()); + + auto [value] = + test_std::sync_wait(stop_token_task() | + test_std::write_env(test_std::prop{test_std::get_stop_token, source.get_token()})) + .value(); + ASSERT(value); + } + { + auto [value] = test_std::sync_wait(stop_token_task()).value(); + ASSERT(!value); + } } auto test_lifetimes() -> void { diff --git a/tests/beman/execution/include/test/sender_env.hpp b/tests/beman/execution/include/test/sender_env.hpp index de4e7492..89485f57 100644 --- a/tests/beman/execution/include/test/sender_env.hpp +++ b/tests/beman/execution/include/test/sender_env.hpp @@ -57,8 +57,8 @@ struct sender_env { return {}; } template - auto connect(Receiver&&) && noexcept { - return test_std::connect(test_std::just(), std::forward(Receiver{})); + auto connect(Receiver&& rcvr) && noexcept { + return test_std::connect(test_std::just(), std::forward(rcvr)); } };