diff --git a/include/exec/detail/repeat.hpp b/include/exec/detail/repeat.hpp index 2b5ad34dc..105550a51 100644 --- a/include/exec/detail/repeat.hpp +++ b/include/exec/detail/repeat.hpp @@ -126,16 +126,10 @@ namespace experimental::execution::__repeat // themselves afterward.) If the state is for a walk, __body_op_ is a placeholder. constexpr auto __connect_body() noexcept(__nothrow_connect) -> __body_op_t & { - if constexpr (__facade::__walking_receiver<_Receiver>) - { - return __body_op_.emplace(); - } - else - { - return __body_op_.__emplace_from(STDEXEC::connect, - __child_t(__child_), - __body_rcvr_t{this}); - } + return __facade::__emplace_state_op<_Receiver>(__body_op_, + STDEXEC::connect, + __child_t(__child_), + __body_rcvr_t{this}); } // Destroys the operations, before the operation completes diff --git a/include/stdexec/__detail/__associate.hpp b/include/stdexec/__detail/__associate.hpp index c0c5417c1..6e6b3893e 100644 --- a/include/stdexec/__detail/__associate.hpp +++ b/include/stdexec/__detail/__associate.hpp @@ -216,14 +216,12 @@ namespace STDEXEC : __rcvr_(static_cast<_Receiver&&>(__rcvr)) , __assoc_(STDEXEC::__allocator_aware_forward(std::move(__parts.first), __rcvr_)) { - if constexpr (!__facade::__walking_receiver<_Receiver>) + if (__assoc_) { - if (__assoc_) - { - __op_.__emplace_from(STDEXEC::connect, - std::move(*__parts.second), - __wrapped_rcvr_t{this}); - } + __facade::__emplace_state_op<_Receiver>(__op_, + STDEXEC::connect, + std::move(*__parts.second), + __wrapped_rcvr_t{this}); } } diff --git a/include/stdexec/__detail/__continues_on.hpp b/include/stdexec/__detail/__continues_on.hpp index cc7bf05d0..a964fc988 100644 --- a/include/stdexec/__detail/__continues_on.hpp +++ b/include/stdexec/__detail/__continues_on.hpp @@ -159,18 +159,19 @@ namespace STDEXEC STDEXEC_IMMOVABLE(__state); - _Receiver __rcvr_; - __env2_t const __env2_; - __storage_t __storage_{}; - connect_result_t<__sched_sndr_t, __sched_rcvr_t> __op_; + _Receiver __rcvr_; + __env2_t const __env2_; + __storage_t __storage_{}; + __facade::__state_op_t<_Receiver, connect_result_t, __sched_sndr_t, __sched_rcvr_t> __op_; private: template constexpr explicit __state(__scheduler_t __sch, _Child const & __child, _Receiver&& __rcvr) : __rcvr_(static_cast<_Receiver&&>(__rcvr)) , __env2_(__trnsfr::__mk_env2(__child, STDEXEC::get_env(__rcvr_))) - , __op_(STDEXEC::connect(STDEXEC::schedule(static_cast<__scheduler_t const &>(__sch)), - __sched_rcvr_t{this})) + , __op_(__facade::__make_state_op<_Receiver>(STDEXEC::connect, + STDEXEC::schedule(__sch), + __sched_rcvr_t{this})) {} }; diff --git a/include/stdexec/__detail/__counting_scopes.hpp b/include/stdexec/__detail/__counting_scopes.hpp index c9052c25d..148013cac 100644 --- a/include/stdexec/__detail/__counting_scopes.hpp +++ b/include/stdexec/__detail/__counting_scopes.hpp @@ -139,14 +139,18 @@ namespace STDEXEC using __sched_sender_t = __call_result_t>>; using __sched_rcvr_t = __facade::__receiver_t<__join_algo, __sched_op, __join_state, _Sender, _Rcvr>; - using __op_t = connect_result_t<__sched_sender_t, __sched_rcvr_t>; + // (A walk's state holds a placeholder for the operation: see __facade::__state_op_t.) + using __op_t = + __facade::__state_op_t<_Rcvr, connect_result_t, __sched_sender_t, __sched_rcvr_t>; explicit __join_state(__base_scope* __scope, _Rcvr __rcvr) noexcept(__nothrow_connectable<__sched_sender_t, __sched_rcvr_t>) : __join_state_base(__scope, &__join_state::__notify) , __rcvr_(std::move(__rcvr)) - , __op_(STDEXEC::connect(schedule(get_start_scheduler(STDEXEC::get_env(__rcvr_))), - __sched_rcvr_t{this})) + , __op_(__facade::__make_state_op<_Rcvr>(STDEXEC::connect, + schedule( + get_start_scheduler(STDEXEC::get_env(__rcvr_))), + __sched_rcvr_t{this})) {} __join_state(__join_state&&) = delete; diff --git a/include/stdexec/__detail/__finally.hpp b/include/stdexec/__detail/__finally.hpp index 2d3cda9fb..312d74edb 100644 --- a/include/stdexec/__detail/__finally.hpp +++ b/include/stdexec/__detail/__finally.hpp @@ -145,14 +145,16 @@ namespace STDEXEC && __nothrow_connectable<__initial_t, __initial_rcvr_t>) : __rcvr_(static_cast<_Receiver&&>(__rcvr)) , __env2_(__trnsfr::__mk_env2(__facade::__get_child<0>(__sndr), STDEXEC::get_env(__rcvr_))) - , __final_op_(__state::__connect_final(static_cast<_Sender&&>(__sndr), this)) + , __final_op_(__facade::__make_state_op<_Receiver>(STDEXEC::connect, + __facade::__get_child<1>( + static_cast<_Sender&&>(__sndr)), + __final_rcvr_t{this})) { - if constexpr (!__facade::__walking_receiver<_Receiver>) - { - __initial_op_.__emplace_from(STDEXEC::connect, - __facade::__get_child<0>(static_cast<_Sender&&>(__sndr)), - __initial_rcvr_t{this}); - } + __facade::__emplace_state_op<_Receiver>(__initial_op_, + STDEXEC::connect, + __facade::__get_child<0>( + static_cast<_Sender&&>(__sndr)), + __initial_rcvr_t{this}); } STDEXEC_IMMOVABLE(__state); @@ -162,23 +164,6 @@ namespace STDEXEC __storage_t __storage_{}; __final_op_t __final_op_; __optional<__initial_op_t> __initial_op_{}; - - private: - // Connects the final sender, unless the state is for a walk: then __final_op_ is a - // placeholder. - static constexpr auto __connect_final(_Sender&& __sndr, __state* __self) - noexcept(__nothrow_connectable<__final_t, __final_rcvr_t>) -> __final_op_t - { - if constexpr (__facade::__walking_receiver<_Receiver>) - { - return {}; - } - else - { - return STDEXEC::connect(__facade::__get_child<1>(static_cast<_Sender&&>(__sndr)), - __final_rcvr_t{__self}); - } - } }; // __finally_(initial, final): when the initial sender completes, its results are saved, diff --git a/include/stdexec/__detail/__just.hpp b/include/stdexec/__detail/__just.hpp index 5ffd567fb..d1b068746 100644 --- a/include/stdexec/__detail/__just.hpp +++ b/include/stdexec/__detail/__just.hpp @@ -60,10 +60,9 @@ namespace STDEXEC // Not static_cast(__state.__data_): nvcc's cudafe++ // drops the && when it hands the code to the host compiler, which then copies the // data instead of moving it. - return STDEXEC::__apply( - [&](_Ts&&... __vs) noexcept - { return __facade::__send(__state.__rcvr_, _SetTag(), static_cast<_Ts&&>(__vs)...); }, - static_cast<_State&&>(__state).__data_); + return __facade::__send_tuple(__state.__rcvr_, + _SetTag(), + static_cast<_State&&>(__state).__data_); } }; } // namespace __just diff --git a/include/stdexec/__detail/__let.hpp b/include/stdexec/__detail/__let.hpp index b63a1a05d..81e19d69c 100644 --- a/include/stdexec/__detail/__let.hpp +++ b/include/stdexec/__detail/__let.hpp @@ -236,12 +236,11 @@ namespace STDEXEC , __env2_(__mk_secondary_env_t<__set_tag_t>()(__facade::__get_child<0>(__sndr), STDEXEC::get_env(__rcvr_))) { - if constexpr (!__walking()) - { - __ops_.__emplace_from(STDEXEC::connect, - __facade::__get_child<0>(static_cast<_Sender&&>(__sndr)), - __child_rcvr_t{this}); - } + __facade::__emplace_state_op<_Receiver>(__ops_, + STDEXEC::connect, + __facade::__get_child<0>( + static_cast<_Sender&&>(__sndr)), + __child_rcvr_t{this}); } STDEXEC_IMMOVABLE(__state); diff --git a/include/stdexec/__detail/__sender_facade.hpp b/include/stdexec/__detail/__sender_facade.hpp index e5ca7c967..89efd12f8 100644 --- a/include/stdexec/__detail/__sender_facade.hpp +++ b/include/stdexec/__detail/__sender_facade.hpp @@ -196,9 +196,9 @@ namespace STDEXEC //////////////////////////////////////////////////////////////////////////////////////// // 2. Completing. __send is the only way an algorithm completes: it sends a completion to // the algorithm's receiver, and returns the token that records the completion. - // __start_nested is the only way it starts a child or nested operation. (Call these - // with qualified names, so that argument-dependent lookup does not look in the - // receiver's namespaces.) + // (__send_tuple sends the elements of a tuple.) __start_nested is the only way it starts + // a child or nested operation. (Call these with qualified names, so that + // argument-dependent lookup does not look in the receiver's namespaces.) STDEXEC_MODULE_EXPORT_AUTHORING template STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) @@ -218,6 +218,29 @@ namespace STDEXEC return {}; } + // Sends its arguments as the arguments of a completion (for __send_tuple) + struct __send_fn + { + template + STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) + constexpr auto operator()(_Receiver &__rcvr, _Tag, _As &&...__as) const noexcept // + -> __effects<_Tag(_As...)> + { + return __facade::__send(__rcvr, _Tag(), static_cast<_As &&>(__as)...); + } + }; + + // Sends the elements of a tuple (an STDEXEC::__tuple) as the arguments of a completion, + // as __send(__rcvr, _Tag(), __elements...) would + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(nodiscard, always_inline, host, device) + constexpr auto __send_tuple(_Receiver &__rcvr, _Tag, _Tuple &&__tupl) noexcept + -> __apply_result_t<__send_fn, _Tuple, _Receiver &, _Tag> + { + return STDEXEC::__apply(__send_fn(), static_cast<_Tuple &&>(__tupl), __rcvr, _Tag()); + } + //////////////////////////////////////////////////////////////////////////////////////// // 3. Combinators for control flow whose paths complete differently STDEXEC_MODULE_EXPORT_AUTHORING @@ -730,6 +753,86 @@ namespace STDEXEC template class _Fn, class... _Args> using __state_op_t = __op_or_walk_op<__walking_receiver<_Rcvr>>::template __f<_Fn, _Args...>; + template + struct __state_op_maker + { + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __make(_Fn &&__fn, _Args &&...__args) + noexcept(__nothrow_callable<_Fn, _Args...>) -> __call_result_t<_Fn, _Args...> + { + return static_cast<_Fn &&>(__fn)(static_cast<_Args &&>(__args)...); + } + + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __emplace(_Storage &__storage, _Fn &&__fn, _Args &&...__args) + noexcept(__nothrow_callable<_Fn, _Args...>) -> decltype(auto) + { + return __storage.template __emplace_from<_Idx...>(static_cast<_Fn &&>(__fn), + static_cast<_Args &&>(__args)...); + } + }; + + template <> + struct __state_op_maker + { + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __make(_Fn &&, _Args &&...) noexcept -> __walk_op + { + return {}; + } + + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + static constexpr auto __emplace(_Storage &__storage, _Fn &&, _Args &&...) noexcept + -> decltype(auto) + { + return __storage.template __emplace_from<_Idx...>([]() noexcept { return __walk_op(); }); + } + }; + + // Makes an operation for a member of an algorithm's state whose type is + // __state_op_t<_Rcvr, ...>, as __fn(__args...) does (__fn is usually STDEXEC::connect), + // unless the state is for a walk: then it returns the placeholder without calling __fn, + // so the operation's type is never computed. (The arguments are still written, but a + // walk's state is never constructed, so they are never evaluated.) + // + // , __op_(__facade::__make_state_op<_Receiver>(STDEXEC::connect, + // STDEXEC::schedule(__sch), + // __sched_rcvr_t{this})) + // + // __emplace_state_op does the same for a member that holds the operation in an + // __optional or a __variant (with __emplace_from), and returns a reference to it. To + // emplace a __variant's alternative by its index, give the index after _Rcvr. + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr auto __make_state_op(_Fn &&__fn, _Args &&...__args) noexcept(noexcept( + __state_op_maker<__walking_receiver<_Rcvr>>::__make(static_cast<_Fn &&>(__fn), + static_cast<_Args &&>(__args)...))) + -> decltype(auto) + { + return __state_op_maker<__walking_receiver<_Rcvr>>::__make(static_cast<_Fn &&>(__fn), + static_cast<_Args &&>(__args)...); + } + + STDEXEC_MODULE_EXPORT_AUTHORING + template + STDEXEC_ATTRIBUTE(always_inline, host, device) + constexpr auto __emplace_state_op(_Storage &__storage, _Fn &&__fn, _Args &&...__args) + noexcept(noexcept(__state_op_maker<__walking_receiver<_Rcvr>>::template __emplace<_Idx...>( + __storage, + static_cast<_Fn &&>(__fn), + static_cast<_Args &&>(__args)...))) -> decltype(auto) + { + return __state_op_maker<__walking_receiver<_Rcvr>>::template __emplace<_Idx...>( + __storage, + static_cast<_Fn &&>(__fn), + static_cast<_Args &&>(__args)...); + } + // The type of the operations that a state holds, as _Lazy::__t (a class that names it, so // that it is not instantiated unless used), or _Walk if the state is for a walk. template diff --git a/include/stdexec/__detail/__sequence.hpp b/include/stdexec/__detail/__sequence.hpp index 719aeddfc..b0cdf96ee 100644 --- a/include/stdexec/__detail/__sequence.hpp +++ b/include/stdexec/__detail/__sequence.hpp @@ -256,20 +256,24 @@ namespace STDEXEC , __env2s_(__later_t::__mk_env2s(__sndr, STDEXEC::get_env(__rcvr_))) , __sndrs_(__later_t::__take_senders(static_cast<_Sender &&>(__sndr))) { - if constexpr (__nbr != 0 && !__walking()) + if constexpr (__nbr != 0) { if constexpr (__attaches<0>()) { - __ops_.__emplace_from(__attach_t<0>(), - *this, - __facade::__get_child<0>(static_cast<_Sender &&>(__sndr)), - __receiver_t<0>{this}); + __facade::__emplace_state_op<_Receiver>(__ops_, + __attach_t<0>(), + *this, + __facade::__get_child<0>( + static_cast<_Sender &&>(__sndr)), + __receiver_t<0>{this}); } else { - __ops_.__emplace_from(STDEXEC::connect, - __facade::__get_child<0>(static_cast<_Sender &&>(__sndr)), - __receiver_t<0>{this}); + __facade::__emplace_state_op<_Receiver>(__ops_, + STDEXEC::connect, + __facade::__get_child<0>( + static_cast<_Sender &&>(__sndr)), + __receiver_t<0>{this}); } } } diff --git a/include/stdexec/__detail/__starts_on.hpp b/include/stdexec/__detail/__starts_on.hpp index 0d03a1e0c..b39c929dc 100644 --- a/include/stdexec/__detail/__starts_on.hpp +++ b/include/stdexec/__detail/__starts_on.hpp @@ -101,38 +101,40 @@ namespace STDEXEC constexpr explicit __state(_Sender&& __sndr, _Receiver&& __rcvr) noexcept(__nothrow_v) : __rcvr_(static_cast<_Receiver&&>(__rcvr)) , __env2_(STDEXEC::__mk_sch_env(__facade::__get_data(__sndr), STDEXEC::get_env(__rcvr_))) - , __sched_op_(STDEXEC::connect(STDEXEC::schedule(static_cast<__scheduler_t const &>( - __facade::__get_data(__sndr))), - __sched_rcvr_t{this})) + , __sched_op_(__facade::__make_state_op<_Receiver>( + STDEXEC::connect, + STDEXEC::schedule(static_cast<__scheduler_t const &>(__facade::__get_data(__sndr))), + __sched_rcvr_t{this})) , __child_op_(__attach_child(__facade::__get_child<0>(static_cast<_Sender&&>(__sndr)))) {} STDEXEC_IMMOVABLE(__state); - _Receiver __rcvr_; - __env2_t const __env2_; - connect_result_t<__sched_sndr_t, __sched_rcvr_t> __sched_op_; - __child_op_t __child_op_; + _Receiver __rcvr_; + __env2_t const __env2_; + __facade::__state_op_t<_Receiver, connect_result_t, __sched_sndr_t, __sched_rcvr_t> + __sched_op_; + __child_op_t __child_op_; private: - // Connects the child, or attaches it (see __facade::__attaches_child), unless the state - // is for a walk: then __child_op_ is a placeholder. + // Connects the child, or attaches it (see __facade::__attaches_child) STDEXEC_ATTRIBUTE(always_inline) constexpr auto __attach_child(__child_t&& __child) noexcept(__nothrow_v) -> __child_op_t { - if constexpr (__facade::__walking_receiver<_Receiver>) - { - return {}; - } - else if constexpr (__facade::__attaches_child<__algo, _Sender, 0>) + if constexpr (__facade::__attaches_child<__algo, _Sender, 0>) { using __attach_t = __facade::__attach_child_t<__algo, _Sender, 0, __child_rcvr_t, _Subscribing...>; - return __attach_t()(*this, static_cast<__child_t&&>(__child), __child_rcvr_t{this}); + return __facade::__make_state_op<_Receiver>(__attach_t(), + *this, + static_cast<__child_t&&>(__child), + __child_rcvr_t{this}); } else { - return STDEXEC::connect(static_cast<__child_t&&>(__child), __child_rcvr_t{this}); + return __facade::__make_state_op<_Receiver>(STDEXEC::connect, + static_cast<__child_t&&>(__child), + __child_rcvr_t{this}); } } }; diff --git a/test/stdexec/detail/test_facade.cpp b/test/stdexec/detail/test_facade.cpp index ccadaceb2..c6d523698 100644 --- a/test/stdexec/detail/test_facade.cpp +++ b/test/stdexec/detail/test_facade.cpp @@ -341,13 +341,20 @@ namespace explicit t_schedule_state(Sender &&sndr, Receiver &&rcvr) : __rcvr_(static_cast(rcvr)) - , op_(ex::connect(ex::schedule(ex::__facade::__get_data(sndr)), sched_rcvr_t{this})) + , op_(ex::__facade::__make_state_op(ex::connect, + ex::schedule(static_cast( + ex::__facade::__get_data(sndr))), + sched_rcvr_t{this})) {} t_schedule_state(t_schedule_state &&) = delete; - Receiver __rcvr_; - ex::connect_result_t op_; + // A walk's state holds a placeholder for the operation (see __facade::__state_op_t). + using op_t = + ex::__facade::__state_op_t; + + Receiver __rcvr_; + op_t op_; }; // The completions of the schedule operation are passed on, by the default __complete.