Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MAINTAINERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ algorithms need only `__complete`, and perhaps `__start`.
| `__start(state, children)` | starts every child with `children.__start_all()` | the algorithm completes from `start`, starts only some of its children, or starts a nested operation | `just`, `let_value`, `repeat_n` |
| `__get_state(sndr, rcvr)` | a `__basic_state` that holds the receiver and the sender's data | the state holds more than the data (nested operations, saved results, a stop source), or the algorithm rejects its inputs by returning an `__mexception` | `when_all`, `continues_on`, `let_value` |
| `__get_state(sndr, rcvr, __subscribing_t)` | the same as the above | the state of a subscribed (sequence) sender differs from that of a connected one | |
| `__state_type<Sender, Receiver, Subscribing...>` (opt-in) | the type that `__get_state` returns | the state holds nested operations. The walk then gets the state's type without asking whether making the state can throw, which would instantiate the operations below it. It must name the type that `__get_state` returns, or its `__mexception` | `let_value`, `__sequence`, `exec::ignore_all_values` |
| `__connects_children` | `true` | the algorithm connects a child itself, in its state: to connect it later, to start it from a nested operation, or to destroy its operation early | `__sequence`, `starts_on`, `let_value` |
| `__connect(key, state, sndr, rcvr)` | `STDEXEC::connect(sndr, rcvr)` | a child or nested operation must be connected to a different kind of receiver, as when a domain adapts a child whose operation completes from a kernel | nvexec |
| `__receiver_base` | an empty struct | a domain's algorithm needs the facade's receivers to have a base class, so that its senders complete them in the way they expect | nvexec |
Expand Down
101 changes: 90 additions & 11 deletions include/exec/fork_join.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,26 +155,59 @@ namespace experimental::execution
STDEXEC::__facade::__nth_child_of_t<Sndr, 0>,
STDEXEC::__fwd_env_t<Env>...>;

// The operation of a sender that cannot be connected to the receiver it is given (in the
// walk, where the receiver is an archetype, when the closures' senders depend on the
// environment). The walk asks the sender for its completions, and reports its error.
// The operation of a sender that cannot be connected to the receiver it is given. (The
// walk asks the sender for its completions, and reports its error.)
struct _no_op
{};

template <bool Connectable>
// The type of an operation that the state holds: the operation, or _no_op, or, if the
// state is for a walk (its receiver is an archetype), a placeholder, since the walk needs
// only the state's type. Naming the operation there would instantiate the operations
// below it, connected to archetypes. (A class keyed by bools only, so that the debug
// information, which names the class when it names the operation's type, does not repeat
// the sender's and the receiver's types.)
template <bool Walking, bool Connectable>
struct _op_of
{
template <class Sender, class Receiver>
using __f = STDEXEC::connect_result_t<Sender, Receiver>;
};

template <bool Connectable>
struct _op_of<true, Connectable>
{
template <class, class>
using __f = STDEXEC::__facade::__walk_op;
};

template <>
struct _op_of<false>
struct _op_of<false, false>
{
template <class, class>
using __f = _no_op;
};

// Can the operations be connected? A walk does not ask: its state holds placeholders.
template <bool Walking>
struct _connectable_of
{
template <class Child, class ChildRcvr, class Fork, class ForkRcvr>
static consteval auto __f() noexcept -> bool
{
return STDEXEC::sender_to<Child, ChildRcvr> && STDEXEC::sender_to<Fork, ForkRcvr>;
}
};

template <>
struct _connectable_of<true>
{
template <class, class, class, class>
static consteval auto __f() noexcept -> bool
{
return true;
}
};

template <class Sndr, class Rcvr>
struct _state
{
Expand All @@ -196,10 +229,13 @@ namespace experimental::execution
static constexpr bool _child_stays_put =
STDEXEC::__trnsfr::__child_stays_put_v<_child_t, STDEXEC::env_of_t<Rcvr>>;

static constexpr bool _connectable = STDEXEC::sender_to<_child_t, _child_rcvr_t>
&& STDEXEC::sender_to<_when_all_sndr_t, _fork_rcvr_t>;
using _child_op_t = _op_of<_connectable>::template __f<_child_t, _child_rcvr_t>;
using _fork_op_t = _op_of<_connectable>::template __f<_when_all_sndr_t, _fork_rcvr_t>;
// (A walk's state holds placeholders: see _op_of.)
using _connectable_of_t = _connectable_of<STDEXEC::__facade::__walking_receiver<Rcvr>>;
static constexpr bool _connectable =
_connectable_of_t::template __f<_child_t, _child_rcvr_t, _when_all_sndr_t, _fork_rcvr_t>();
using _op_of_t = _op_of<STDEXEC::__facade::__walking_receiver<Rcvr>, _connectable>;
using _child_op_t = _op_of_t::template __f<_child_t, _child_rcvr_t>;
using _fork_op_t = _op_of_t::template __f<_when_all_sndr_t, _fork_rcvr_t>;

static constexpr bool _nothrow_v =
STDEXEC::__nothrow_applicable<_mk_when_all_fn, _closures_t, _cache_sndr_t>
Expand All @@ -215,7 +251,7 @@ namespace experimental::execution
STDEXEC::get_env(__rcvr_)))
, _fork_op_(_state::_connect_fork(static_cast<Sndr&&>(sndr), this))
{
if constexpr (_connectable)
if constexpr (_connectable && !STDEXEC::__facade::__walking_receiver<Rcvr>)
{
_child_op_.__emplace_from(STDEXEC::connect,
STDEXEC::__facade::__get_child<0>(static_cast<Sndr&&>(sndr)),
Expand All @@ -236,7 +272,7 @@ namespace experimental::execution
static constexpr auto
_connect_fork(Sndr&& sndr, _state* self) noexcept(_nothrow_v) -> _fork_op_t
{
if constexpr (_connectable)
if constexpr (_connectable && !STDEXEC::__facade::__walking_receiver<Rcvr>)
{
return STDEXEC::connect(STDEXEC::__apply(_mk_when_all_fn{},
STDEXEC::__facade::__get_data(
Expand All @@ -251,6 +287,42 @@ namespace experimental::execution
}
};

// The type that __get_state returns: the state, or the type error that it reports.
// (Classes keyed by bools only, as _op_of is.)
template <bool DecayCopyable>
struct _state_if_copyable
{
template <class Sndr, class Rcvr>
using __f = _state<Sndr, Rcvr>;
};

template <>
struct _state_if_copyable<false>
{
template <class Sndr, class Rcvr>
using __f = STDEXEC::__mexception<STDEXEC::_WHAT_(
STDEXEC::_PREDECESSOR_RESULTS_ARE_NOT_DECAY_COPYABLE_),
STDEXEC::_IN_ALGORITHM_(fork_join_t)>;
};

template <bool HasDomain>
struct _state_if_valid
{
template <class Sndr, class Rcvr>
using __f = _state_if_copyable<STDEXEC::__decay_copyable_results_t<
typename _state<Sndr, Rcvr>::_child_completions_t>::value>::template __f<Sndr, Rcvr>;
};

template <>
struct _state_if_valid<false>
{
template <class Sndr, class Rcvr>
using __f =
STDEXEC::__mexception<INVALID_ARGUMENTS_TO_FORK_JOIN,
STDEXEC::_WITH_SENDERS_<STDEXEC::__facade::__nth_child_of_t<Sndr, 0>>,
STDEXEC::_WITH_ENVIRONMENT_(STDEXEC::env_of_t<Rcvr>)>;
};

// fork_join(sndr, closures...): when the child completes, its results are cached, its
// operation is destroyed, and when_all(closures(cache)...) is started, where cache is a
// sender of the cached results (by const reference). Its completions are the operation's.
Expand Down Expand Up @@ -366,6 +438,13 @@ namespace experimental::execution
return true;
}

// (See __facade::__state_type_t.)
template <class Sndr, class Rcvr>
using __state_type =
_state_if_valid<STDEXEC::__minvocable_q<_child_domain_t,
Sndr,
STDEXEC::env_of_t<Rcvr>>>::template __f<Sndr, Rcvr>;

template <class Sndr, class Rcvr>
STDEXEC_ATTRIBUTE(host, device)
static constexpr auto __get_state(Sndr&& sndr, Rcvr rcvr)
Expand Down
14 changes: 6 additions & 8 deletions include/exec/sequence/ignore_all_values.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ namespace experimental::execution
return {};
}

// (See __facade::__state_type_t.)
template <class _Sender, class _Receiver>
using __state_type = __state<_Sender, _Receiver>;

template <class _Sender, class _Receiver>
static constexpr auto __get_state(_Sender&& __sndr, _Receiver __rcvr)
noexcept(__nothrow_constructible_from<__state<_Sender, _Receiver>, _Sender, _Receiver>)
Expand Down Expand Up @@ -306,12 +310,6 @@ namespace experimental::execution
// saved result. (The receiver's type does not name the operation's sender, as the state's
// does: the types of the items' operations name it, and they would grow with the size of
// the sender, as would the debug info that names them.)
template <class _Child, class _Rcvr>
struct __nothrow_subscribe_t
{
static constexpr bool value = __nothrow_subscribable<_Child, _Rcvr>;
};

template <class _Receiver, class _Variant>
struct __state_base : __result_type<_Variant>
{
Expand All @@ -332,8 +330,8 @@ namespace experimental::execution
using __base_t = __state_base<_Receiver, __result_variant_t<__child_t, env_of_t<_Receiver>>>;
using __seq_rcvr_t = __ignore_all_values::__receiver<__base_t>;

explicit __state(_Sender&& __sndr, _Receiver&& __rcvr) noexcept(
__facade::__nothrow_state_v<_Receiver, __nothrow_subscribe_t<__child_t, __seq_rcvr_t>>)
explicit __state(_Sender&& __sndr, _Receiver&& __rcvr)
noexcept(__nothrow_subscribable<__child_t, __seq_rcvr_t>)
: __base_t{{}, static_cast<_Receiver&&>(__rcvr)}
, __op_{__subscribe_child(static_cast<_Sender&&>(__sndr), this)}
{}
Expand Down
42 changes: 33 additions & 9 deletions include/stdexec/__detail/__let.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,20 @@ namespace STDEXEC
struct __types<__mlist<_Ts...>>
{
using __tuple_t = __tuple<_Ts...>;
using __op_t = __result_op_of<__minvoke_or_q<__invoke_result_t, void, __fn_t, _Ts&...>,
__receiver2_t<_Ts...>,
_Ts...>::__t;
};

// The operation of the sender that the function returns when it is called with the
// results in _List. (Kept apart from __types, which the results' storage names, so that
// naming the storage does not name the operations: see __ops_lazy.)
template <class _List>
struct __result_op_for;

template <class... _Ts>
struct __result_op_for<__mlist<_Ts...>>
{
using __t = __result_op_of<__minvoke_or_q<__invoke_result_t, void, __fn_t, _Ts&...>,
__receiver2_t<_Ts...>,
_Ts...>::__t;
};

// The child is connected with the state. The operation of the sender that the
Expand All @@ -182,8 +193,13 @@ namespace STDEXEC
struct __variants
{
using __args_t = __variant<typename __types<_Lists>::__tuple_t...>;
using __ops_t = __variant<connect_result_t<__child_t, __child_rcvr_t>,
typename __types<_Lists>::__op_t...>;
};

template <class... _Lists>
struct __ops_variant
{
using __t = __variant<connect_result_t<__child_t, __child_rcvr_t>,
typename __result_op_for<_Lists>::__t...>;
};

using __variants_t = __mapply_q<__variants, __args_lists_t>;
Expand All @@ -192,11 +208,15 @@ namespace STDEXEC
// and of the senders that the function returns: they would be connected to archetypes.)
struct __ops_lazy
{
using __t = __variants_t::__ops_t;
using __t = __mapply_q<__ops_variant, __args_lists_t>::__t;
};
using __ops_t =
__facade::__lazy_state_ops_t<_Receiver, __ops_lazy, __variant<__facade::__walk_op>>;
static constexpr bool __walking = __facade::__walking_v<_Receiver>;

static consteval auto __walking() noexcept -> bool
{
return __facade::__walking_receiver<_Receiver>;
}

static constexpr bool __nothrow_v = __nothrow_decay_copyable<__data_of<_Sender>>
&& __nothrow_connectable<__child_t, __child_rcvr_t>;
Expand All @@ -209,7 +229,7 @@ namespace STDEXEC
, __env2_(__mk_secondary_env_t<__set_tag_t>()(__facade::__get_child<0>(__sndr),
STDEXEC::get_env(__rcvr_)))
{
if constexpr (!__walking)
if constexpr (!__walking())
{
__ops_.__emplace_from(STDEXEC::connect,
__facade::__get_child<0>(static_cast<_Sender&&>(__sndr)),
Expand Down Expand Up @@ -287,6 +307,10 @@ namespace STDEXEC

using __facade::__defaults::__nested_sender;

// (See __facade::__state_type_t.)
template <class _Sender, class _Receiver>
using __state_type = __state<_LetTag, _Sender, _Receiver>;

template <class _Sender, class _Receiver>
STDEXEC_ATTRIBUTE(host, device)
static constexpr auto __get_state(_Sender&& __sndr, _Receiver __rcvr)
Expand Down Expand Up @@ -361,7 +385,7 @@ namespace STDEXEC
using __started_t = __facade::__effects<__facade::__starts<__key_t>>;

// In a walk, only the effects matter (and the state holds no operations to make).
if constexpr (_State::__walking)
if constexpr (_State::__walking())
{
if constexpr (__nothrow_v)
return __started_t();
Expand Down
68 changes: 43 additions & 25 deletions include/stdexec/__detail/__sender_facade.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,41 @@ namespace STDEXEC
struct __subscribing_t
{};

// Does the algorithm name the type of its state with an alias, __state_type?
template <class _Algo, class _Sender, class _Receiver, class... _Subscribing>
concept __declares_state_type = requires {
typename _Algo::template __state_type<_Sender, _Receiver, _Subscribing...>;
};

// (A class keyed only by whether the algorithm declares __state_type, so that the debug
// information, which names the class when it names the state's type by this alias, does
// not repeat the types of the sender and the receiver.)
template <bool _Declared>
struct __state_type_of
{
template <class _Algo, class _Sender, class _Receiver, class... _Subscribing>
using __f = decltype(_Algo::__get_state(__declval<_Sender>(),
__declval<_Receiver>(),
_Subscribing()...));
};

template <>
struct __state_type_of<true>
{
template <class _Algo, class _Sender, class _Receiver, class... _Subscribing>
using __f = _Algo::template __state_type<_Sender, _Receiver, _Subscribing...>;
};

// The type of the algorithm's state: the one it declares with __state_type, or else the
// type that __get_state returns. Naming the type of a call needs the function's exception
// specification ([except.spec]), and its body if its return type is deduced. An algorithm
// whose state holds operations declares __state_type, so that the walk, which needs only
// the type, does not ask whether making the state can throw: that would name the
// operations below the state.
template <class _Algo, class _Sender, class _Receiver, class... _Subscribing>
using __state_type_t =
decltype(_Algo::__get_state(__declval<_Sender>(), __declval<_Receiver>(), _Subscribing()...));
__state_type_of<__declares_state_type<_Algo, _Sender, _Receiver, _Subscribing...>>::
template __f<_Algo, _Sender, _Receiver, _Subscribing...>;

// Is the child keyed by __child<_Idx> subscribed? It is if it is the item source of the
// sender, of type _Sender, and the sender is subscribed.
Expand Down Expand Up @@ -745,15 +777,17 @@ namespace STDEXEC
using __f = __walk_op;
};

// Is the state's receiver an archetype, as it is in a walk? Then the state's operations
// are never made or started (the state is for a walk, or for asking whether connecting
// can throw), so it holds placeholders for them. (Whether making the state can throw is
// still asked of the real operations.)
STDEXEC_MODULE_EXPORT_AUTHORING
template <class _Rcvr, template <class...> class _Fn, class... _Args>
using __state_op_t = __op_or_walk_op<
__std::derived_from<_Rcvr, __receiver_archetype_base>>::template __f<_Fn, _Args...>;
template <class _Rcvr>
concept __walking_receiver = __std::derived_from<_Rcvr, __receiver_archetype_base>;

// Is the state's receiver an archetype, as it is in a walk?
STDEXEC_MODULE_EXPORT_AUTHORING
template <class _Rcvr>
inline constexpr bool __walking_v = __std::derived_from<_Rcvr, __receiver_archetype_base>;
template <class _Rcvr, template <class...> class _Fn, class... _Args>
using __state_op_t = __op_or_walk_op<__walking_receiver<_Rcvr>>::template __f<_Fn, _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.
Expand All @@ -773,24 +807,8 @@ namespace STDEXEC

STDEXEC_MODULE_EXPORT_AUTHORING
template <class _Rcvr, class _Lazy, class _Walk>
using __lazy_state_ops_t = __lazy_state_ops<__walking_v<_Rcvr>>::template __f<_Lazy, _Walk>;

// Is a state's value of noexcept(...) that of the trait _Trait (a class with a static
// member `value`)? Not for a walk, whose state is never constructed: it is true, without
// instantiating the trait (which would name the operations below the state). Clang
// instantiates the exception specification of __get_state to get the state's type, and a
// concept-id in a disjunction is satisfied even when the other operand decides it, so
// the trait is what makes this lazy.
template <bool _Walking, class _Trait>
inline constexpr bool __nothrow_unless_walking_v = _Trait::value;

template <class _Trait>
inline constexpr bool __nothrow_unless_walking_v<true, _Trait> = true;

STDEXEC_MODULE_EXPORT_AUTHORING
template <class _Rcvr, class _Trait>
inline constexpr bool __nothrow_state_v =
__nothrow_unless_walking_v<__std::derived_from<_Rcvr, __receiver_archetype_base>, _Trait>;
using __lazy_state_ops_t =
__lazy_state_ops<__walking_receiver<_Rcvr>>::template __f<_Lazy, _Walk>;

struct __walk_attach_t
{
Expand Down
Loading
Loading