|
| 1 | +// include/beman/execution/detail/affine_on.hpp -*-C++-*- |
| 2 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | + |
| 4 | +#ifndef INCLUDED_INCLUDE_BEMAN_EXECUTION_DETAIL_AFFINE_ON |
| 5 | +#define INCLUDED_INCLUDE_BEMAN_EXECUTION_DETAIL_AFFINE_ON |
| 6 | + |
| 7 | +#include <beman/execution/detail/env.hpp> |
| 8 | +#include <beman/execution/detail/forward_like.hpp> |
| 9 | +#include <beman/execution/detail/fwd_env.hpp> |
| 10 | +#include <beman/execution/detail/get_domain_early.hpp> |
| 11 | +#include <beman/execution/detail/get_scheduler.hpp> |
| 12 | +#include <beman/execution/detail/get_stop_token.hpp> |
| 13 | +#include <beman/execution/detail/join_env.hpp> |
| 14 | +#include <beman/execution/detail/make_sender.hpp> |
| 15 | +#include <beman/execution/detail/never_stop_token.hpp> |
| 16 | +#include <beman/execution/detail/prop.hpp> |
| 17 | +#include <beman/execution/detail/schedule_from.hpp> |
| 18 | +#include <beman/execution/detail/scheduler.hpp> |
| 19 | +#include <beman/execution/detail/sender.hpp> |
| 20 | +#include <beman/execution/detail/sender_adaptor.hpp> |
| 21 | +#include <beman/execution/detail/sender_adaptor_closure.hpp> |
| 22 | +#include <beman/execution/detail/sender_for.hpp> |
| 23 | +#include <beman/execution/detail/sender_has_affine_on.hpp> |
| 24 | +#include <beman/execution/detail/tag_of_t.hpp> |
| 25 | +#include <beman/execution/detail/transform_sender.hpp> |
| 26 | +#include <beman/execution/detail/write_env.hpp> |
| 27 | + |
| 28 | +#include <concepts> |
| 29 | +#include <type_traits> |
| 30 | + |
| 31 | +// ---------------------------------------------------------------------------- |
| 32 | + |
| 33 | +namespace beman::execution::detail { |
| 34 | + |
| 35 | +/** |
| 36 | + * @brief The affine_on_t struct is a sender adaptor closure that transforms a sender |
| 37 | + * to complete on the scheduler obtained from the receiver's environment. |
| 38 | + * |
| 39 | + * This adaptor implements scheduler affinity to adapt a sender to complete on the |
| 40 | + * scheduler obtained the receiver's environment. The get_scheduler query is used |
| 41 | + * to obtain the scheduler on which the sender gets started. |
| 42 | + */ |
| 43 | +struct affine_on_t : ::beman::execution::sender_adaptor_closure<affine_on_t> { |
| 44 | + /** |
| 45 | + * @brief Adapt a sender with affine_on. |
| 46 | + * |
| 47 | + * @tparam Sender The deduced type of the sender to be transformed. |
| 48 | + * @param sender The sender to be transformed. |
| 49 | + * @return An adapted sender to complete on the scheduler it was started on. |
| 50 | + */ |
| 51 | + template <::beman::execution::sender Sender> |
| 52 | + auto operator()(Sender&& sender) const { |
| 53 | + return ::beman::execution::detail::transform_sender( |
| 54 | + ::beman::execution::detail::get_domain_early(sender), |
| 55 | + ::beman::execution::detail::make_sender( |
| 56 | + *this, ::beman::execution::env<>{}, ::std::forward<Sender>(sender))); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @brief Overload for creating a sender adaptor from affine_on. |
| 61 | + * |
| 62 | + * @return A sender adaptor for the affine_on_t. |
| 63 | + */ |
| 64 | + auto operator()() const { return ::beman::execution::detail::sender_adaptor{*this}; } |
| 65 | + |
| 66 | + /** |
| 67 | + * @brief affine_on is implemented by transforming it into a use of schedule_from. |
| 68 | + * |
| 69 | + * The constraints ensure that the environment provides a scheduler which is |
| 70 | + * infallible and, thus, can be used to guarantee completion on the correct |
| 71 | + * scheduler. |
| 72 | + * |
| 73 | + * The implementation first tries to see if the child sender's tag has a custom |
| 74 | + * affine_on implementation. If it does, that is used. Otherwise, the default |
| 75 | + * implementation gets a scheduler from the environment and uses schedule_from |
| 76 | + * to adapt the sender to complete on that scheduler. |
| 77 | + * |
| 78 | + * @tparam Sender The type of the sender to be transformed. |
| 79 | + * @tparam Env The type of the environment providing the scheduler. |
| 80 | + * @param sender The sender to be transformed. |
| 81 | + * @param env The environment providing the scheduler. |
| 82 | + * @return A transformed sender that is affined to the scheduler. |
| 83 | + */ |
| 84 | + template <::beman::execution::sender Sender, typename Env> |
| 85 | + requires ::beman::execution::detail::sender_for<Sender, affine_on_t> && requires(const Env& env) { |
| 86 | + { ::beman::execution::get_scheduler(env) } -> ::beman::execution::scheduler; |
| 87 | + { ::beman::execution::schedule(::beman::execution::get_scheduler(env)) } -> ::beman::execution::sender; |
| 88 | + { |
| 89 | + ::beman::execution::get_completion_signatures( |
| 90 | + ::beman::execution::schedule(::beman::execution::get_scheduler(env)), |
| 91 | + ::beman::execution::detail::join_env( |
| 92 | + ::beman::execution::env{::beman::execution::prop{::beman::execution::get_stop_token, |
| 93 | + ::beman::execution::never_stop_token{}}}, |
| 94 | + env)) |
| 95 | + } -> ::std::same_as<::beman::execution::completion_signatures<::beman::execution::set_value_t()>>; |
| 96 | + } |
| 97 | + static auto transform_sender(Sender&& sender, const Env& env) { |
| 98 | + [[maybe_unused]] auto& [tag, data, child] = sender; |
| 99 | + using child_tag_t = ::beman::execution::tag_of_t<::std::remove_cvref_t<decltype(child)>>; |
| 100 | + |
| 101 | +#if 0 |
| 102 | + if constexpr (requires(const child_tag_t& t) { |
| 103 | + { |
| 104 | + t.affine_on(::beman::execution::detail::forward_like<Sender>(child), env) |
| 105 | + } -> ::beman::execution::sender; |
| 106 | + }) |
| 107 | +#else |
| 108 | + if constexpr (::beman::execution::detail::nested_sender_has_affine_on<Sender, Env>) |
| 109 | +#endif |
| 110 | + { |
| 111 | + return child_tag_t{}.affine_on(::beman::execution::detail::forward_like<Sender>(child), env); |
| 112 | + } else { |
| 113 | + return ::beman::execution::write_env( |
| 114 | + ::beman::execution::schedule_from( |
| 115 | + ::beman::execution::get_scheduler(env), |
| 116 | + ::beman::execution::write_env(::beman::execution::detail::forward_like<Sender>(child), env)), |
| 117 | + ::beman::execution::detail::join_env( |
| 118 | + ::beman::execution::env{::beman::execution::prop{::beman::execution::get_stop_token, |
| 119 | + ::beman::execution::never_stop_token{}}}, |
| 120 | + env)); |
| 121 | + } |
| 122 | + } |
| 123 | +}; |
| 124 | + |
| 125 | +} // namespace beman::execution::detail |
| 126 | + |
| 127 | +namespace beman::execution { |
| 128 | +/** |
| 129 | + * @brief affine_on is a CPO, used to adapt a sender to complete on the scheduler |
| 130 | + * it got started on which is derived from get_scheduler on the receiver's environment. |
| 131 | + */ |
| 132 | +using beman::execution::detail::affine_on_t; |
| 133 | +inline constexpr affine_on_t affine_on{}; |
| 134 | +} // namespace beman::execution |
| 135 | + |
| 136 | +// ---------------------------------------------------------------------------- |
| 137 | + |
| 138 | +#endif |
0 commit comments