Skip to content
Open
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
2 changes: 1 addition & 1 deletion MAINTAINERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +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` |
| `__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`. A state whose receiver is an archetype (see `__facade::__walking_receiver`) is for the walk, or for asking whether connecting can throw: it holds placeholders for its operations (see `__facade::__state_op_t`) and does not connect them, but whether connecting them can throw is still asked of the real operations | `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
80 changes: 61 additions & 19 deletions include/exec/detail/repeat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ namespace experimental::execution::__repeat
using __child_t = __repeat::__child_t<_Sender>;
using __body_rcvr_t = __body_receiver_t<_Algo, _Sender, _Receiver>;
using __bounce_rcvr_t = __facade::__receiver_t<_Algo, __bounce, __state, _Sender, _Receiver>;
using __body_op_t = connect_result_t<__child_t, __body_rcvr_t>;
using __bounce_op_t = connect_result_t<__bounce_sender_t, __bounce_rcvr_t>;
// (A walk's state holds a placeholder for the body's operation: see
// __facade::__state_op_t. The trampoline's operation is the same in every walk.)
using __body_op_t =
__facade::__state_op_t<_Receiver, connect_result_t, __child_t, __body_rcvr_t>;
using __bounce_op_t = connect_result_t<__bounce_sender_t, __bounce_rcvr_t>;

static constexpr bool __nothrow_connect = __nothrow_constructible_from<__child_t, __child_t &>
&& __nothrow_connectable<__child_t, __body_rcvr_t>;
Expand All @@ -118,10 +121,19 @@ namespace experimental::execution::__repeat

// Connects the body of the next iteration, destroying the operation of the last one.
// (This happens in the last one's completion. Operations that complete must not touch
// themselves afterward.)
// themselves afterward.) If the state is for a walk, __body_op_ is a placeholder.
constexpr auto __connect_body() noexcept(__nothrow_connect) -> __body_op_t &
{
return __body_op_.__emplace_from(STDEXEC::connect, __child_t(__child_), __body_rcvr_t{this});
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});
}
}

// Destroys the operations, before the operation completes
Expand All @@ -147,6 +159,41 @@ namespace experimental::execution::__repeat
return true;
}

// The type that __get_state returns: the state, or the type error that it reports.
// (Classes keyed by bools only, so that the debug information, which names a class when
// it names the state's type, does not repeat the sender's and the receiver's types.)
template <bool _Copyable, bool _Connectable>
struct __state_or_error
{
template <class _Algo, class _Sender, class _Receiver>
using __f = __state<_Algo, _Sender, _Receiver>;
};

template <bool _Connectable>
struct __state_or_error<false, _Connectable>
{
template <class _Algo, class _Sender, class _Receiver>
using __f = __mexception<_WHAT_(_INVALID_ARGUMENT_),
_WHERE_(_IN_ALGORITHM_, typename __decay_t<_Sender>::__tag_t),
_WHY_(_THE_INPUT_SENDER_MUST_BE_COPYABLE_),
_WITH_SENDER_<__child_t<_Sender>>>;
};

template <>
struct __state_or_error<true, false>
{
template <class _Algo, class _Sender, class _Receiver>
using __f = __mexception<_WHAT_(_INVALID_ARGUMENT_),
_WHERE_(_IN_ALGORITHM_, typename __decay_t<_Sender>::__tag_t),
_WHY_(_THE_SENDER_CANNOT_BE_CONNECTED_),
_WITH_SENDER_<__child_t<_Sender>>>;
};

template <class _Algo, class _Sender, class _Receiver>
using __state_type_t = __state_or_error<
__std::copy_constructible<__child_t<_Sender>>,
__connectable<_Algo, _Sender, _Receiver>>::template __f<_Algo, _Sender, _Receiver>;

// The base of the repeating algorithms. _Derived is the algorithm, which says what the
// body's value completions mean (with a __complete(__body, ...) handler), and how the
// operation starts (with __start, which calls __start_iteration). Its static member
Expand Down Expand Up @@ -202,29 +249,24 @@ namespace experimental::execution::__repeat
return STDEXEC::schedule(trampoline_scheduler{});
}

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

// The state, or the type error that it reports (see __state_or_error)
template <class _Sender, class _Receiver>
static constexpr auto __get_state(_Sender &&__sndr, _Receiver __rcvr)
noexcept(__repeat::__nothrow_state<_Derived, _Sender, _Receiver>())
-> __state_type<_Sender, _Receiver>
{
using __tag_t = __decay_t<_Sender>::__tag_t;
if constexpr (!__std::copy_constructible<__child_t<_Sender>>)
if constexpr (__connectable<_Derived, _Sender, _Receiver>)
{
return __mexception<_WHAT_(_INVALID_ARGUMENT_),
_WHERE_(_IN_ALGORITHM_, __tag_t),
_WHY_(_THE_INPUT_SENDER_MUST_BE_COPYABLE_),
_WITH_SENDER_<__child_t<_Sender>>>();
}
else if constexpr (!__connectable<_Derived, _Sender, _Receiver>)
{
return __mexception<_WHAT_(_INVALID_ARGUMENT_),
_WHERE_(_IN_ALGORITHM_, __tag_t),
_WHY_(_THE_SENDER_CANNOT_BE_CONNECTED_),
_WITH_SENDER_<__child_t<_Sender>>>();
return __state<_Derived, _Sender, _Receiver>(static_cast<_Sender &&>(__sndr),
static_cast<_Receiver &&>(__rcvr));
}
else
{
return __state<_Derived, _Sender, _Receiver>(static_cast<_Sender &&>(__sndr),
static_cast<_Receiver &&>(__rcvr));
return __state_type<_Sender, _Receiver>();
}
}

Expand Down
22 changes: 16 additions & 6 deletions include/stdexec/__detail/__associate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,24 @@ namespace STDEXEC
using __wrap_sender_t = __associate_data_t::__wrap_sender_t;
using __wrapped_rcvr_t =
__facade::__receiver_t<__algo, __wrapped_op, __state, _Sender, _Receiver>;
using __op_t = connect_result_t<__wrap_sender_t, __wrapped_rcvr_t>;
// (A walk's state holds a placeholder for the operation: see __facade::__state_op_t.)
using __op_t =
__facade::__state_op_t<_Receiver, connect_result_t, __wrap_sender_t, __wrapped_rcvr_t>;

// The wrapped sender's operation is connected only if the association succeeded.
// The wrapped sender's operation is connected only if the association succeeded, and
// the state is not for a walk.
explicit __state(std::pair<__assoc_t, __sender_ref_t> __parts, _Receiver&& __rcvr)
: __rcvr_(static_cast<_Receiver&&>(__rcvr))
, __assoc_(STDEXEC::__allocator_aware_forward(std::move(__parts.first), __rcvr_))
{
if (__assoc_)
if constexpr (!__facade::__walking_receiver<_Receiver>)
{
__op_.__emplace_from(STDEXEC::connect,
std::move(*__parts.second),
__wrapped_rcvr_t{this});
if (__assoc_)
{
__op_.__emplace_from(STDEXEC::connect,
std::move(*__parts.second),
__wrapped_rcvr_t{this});
}
}
}

Expand Down Expand Up @@ -244,6 +250,10 @@ namespace STDEXEC
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((!std::is_lvalue_reference_v<_Sender>
Expand Down
40 changes: 33 additions & 7 deletions include/stdexec/__detail/__finally.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,11 @@ namespace STDEXEC
__facade::__receiver_t<__algo, __facade::__child<0>, __state, _Sender, _Receiver>;
using __final_rcvr_t =
__facade::__receiver_t<__algo, __facade::__child<1>, __state, _Sender, _Receiver>;
using __initial_op_t = connect_result_t<__initial_t, __initial_rcvr_t>;
using __final_op_t = connect_result_t<__final_t, __final_rcvr_t>;
// (A walk's state holds placeholders for the operations: see __facade::__state_op_t.)
using __initial_op_t =
__facade::__state_op_t<_Receiver, connect_result_t, __initial_t, __initial_rcvr_t>;
using __final_op_t =
__facade::__state_op_t<_Receiver, connect_result_t, __final_t, __final_rcvr_t>;

static constexpr bool __saves_results =
__facade::__completions_of_t<__algo, __facade::__child<1>, _Sender, _Receiver>::__count(
Expand All @@ -131,12 +134,14 @@ 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_(STDEXEC::connect(__facade::__get_child<1>(static_cast<_Sender&&>(__sndr)),
__final_rcvr_t{this}))
, __final_op_(__state::__connect_final(static_cast<_Sender&&>(__sndr), this))
{
__initial_op_.__emplace_from(STDEXEC::connect,
__facade::__get_child<0>(static_cast<_Sender&&>(__sndr)),
__initial_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});
}
}

STDEXEC_IMMOVABLE(__state);
Expand All @@ -146,6 +151,23 @@ 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,
Expand Down Expand Up @@ -187,6 +209,10 @@ namespace STDEXEC
__hide_scheduler{STDEXEC::get_env(__state.__rcvr_)});
}

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

template <class _Sender, class _Receiver>
STDEXEC_ATTRIBUTE(host, device)
static constexpr auto __get_state(_Sender&& __sndr, _Receiver __rcvr)
Expand Down
39 changes: 35 additions & 4 deletions include/stdexec/__detail/__starts_on.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ namespace STDEXEC

struct __algo;

// The type of the child's operation: the one that the state attaches, 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 a bool 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>
struct __child_op_of
{
template <class _Sender, class _State, class _Child, class _Rcvr, class... _Subscribing>
using __f =
__facade::__child_op_t<__algo, _Sender, 0, _State, _Child, _Rcvr, _Subscribing...>;
};

template <>
struct __child_op_of<true>
{
template <class _Sender, class _State, class _Child, class _Rcvr, class... _Subscribing>
using __f = __facade::__walk_op;
};

// The state of a starts_on. If the starts_on is subscribed (_Subscribing is
// __facade::__subscribing_t), so is its child, when that is a sequence sender (see
// __facade::__attaches_child).
Expand All @@ -63,8 +84,9 @@ namespace STDEXEC
__facade::__receiver_t<__algo, __sched_op, __state, _Sender, _Receiver>;
using __child_rcvr_t =
__facade::__receiver_t<__algo, __facade::__child<0>, __state, _Sender, _Receiver>;
using __child_op_t = __facade::
__child_op_t<__algo, _Sender, 0, __state, __child_t, __child_rcvr_t, _Subscribing...>;
using __child_op_of_t = __child_op_of<__facade::__walking_receiver<_Receiver>>;
using __child_op_t =
__child_op_of_t::template __f<_Sender, __state, __child_t, __child_rcvr_t, _Subscribing...>;

static constexpr bool __nothrow_v = __nothrow_callable<schedule_t, __scheduler_t const &>
&& __nothrow_connectable<__sched_sndr_t, __sched_rcvr_t>
Expand Down Expand Up @@ -94,11 +116,16 @@ namespace STDEXEC
__child_op_t __child_op_;

private:
// Connects the child, or attaches it (see __facade::__attaches_child)
// Connects the child, or attaches it (see __facade::__attaches_child), unless the state
// is for a walk: then __child_op_ is a placeholder.
STDEXEC_ATTRIBUTE(always_inline)
constexpr auto __attach_child(__child_t&& __child) noexcept(__nothrow_v) -> __child_op_t
{
if constexpr (__facade::__attaches_child<__algo, _Sender, 0>)
if constexpr (__facade::__walking_receiver<_Receiver>)
{
return {};
}
else if constexpr (__facade::__attaches_child<__algo, _Sender, 0>)
{
using __attach_t =
__facade::__attach_child_t<__algo, _Sender, 0, __child_rcvr_t, _Subscribing...>;
Expand Down Expand Up @@ -181,6 +208,10 @@ namespace STDEXEC
__facade::__get_data(__sndr)};
}

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

// (The state of a subscribed starts_on takes __facade::__subscribing_t.)
template <class _Sender, class _Receiver, class... _Subscribing>
STDEXEC_ATTRIBUTE(host, device)
Expand Down
34 changes: 34 additions & 0 deletions test/stdexec/algos/adaptors/test_associate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import std;
#else
# include <concepts>
# include <exception>
# include <memory>
# include <stdexcept>
#endif

namespace ex = STDEXEC;
Expand Down Expand Up @@ -305,6 +307,38 @@ namespace
(void) snd;
}

#if !STDEXEC_NO_STDCPP_EXCEPTIONS()
struct throws_on_connect
{
using sender_concept = ex::sender_tag;
using completion_signatures = ex::completion_signatures<ex::set_value_t()>;

template <class Receiver>
auto connect(Receiver) const -> ex::connect_result_t<decltype(ex::just()), Receiver>
{
throw std::logic_error("connect");
}
};

TEST_CASE("associate - connecting can throw if connecting the wrapped sender can",
"[adaptors][associate]")
{
// (The function is noexcept, so the exception can come only from connecting.)
auto fn = []() noexcept
{
return ex::associate(throws_on_connect{}, null_token{});
};
using Sender = decltype(fn());
STATIC_REQUIRE(!ex::__nothrow_connectable<Sender, ex::__receiver_archetype<ex::env<>>>);

// So an algorithm that connects it when another sender completes reports the exception.
using Let = decltype(ex::let_value(ex::just(), fn));
STATIC_REQUIRE(ex::__mapply<ex::__mcontains<ex::set_error_t(std::exception_ptr)>,
ex::completion_signatures_of_t<Let, ex::env<>>>::value);
CHECK_THROWS_AS(ex::sync_wait(ex::let_value(ex::just(), fn)), std::logic_error);
}
#endif // !STDEXEC_NO_STDCPP_EXCEPTIONS()

// TODO: check the pass-through nature of __sync_attrs
// TODO: check the pass-through stop request behaviour
// TODO: confirm timing of destruction of opstate relative to release of
Expand Down
35 changes: 35 additions & 0 deletions test/stdexec/algos/adaptors/test_finally.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,41 @@ namespace
CHECK_THROWS_AS(sync_wait(std::move(s)), int);
CHECK(called);
}

struct throws_on_connect
{
using sender_concept = sender_tag;
using completion_signatures = STDEXEC::completion_signatures<set_value_t()>;

template <class Receiver>
auto connect(Receiver) const -> connect_result_t<decltype(just()), Receiver>
{
throw 42;
}
};

TEST_CASE("finally - connecting can throw if connecting either sender can", "[adaptors][finally]")
{
// (The functions are noexcept, so the exception can come only from connecting.)
auto initial_throws = []() noexcept
{
return exec::finally(throws_on_connect{}, just());
};
auto final_throws = []() noexcept
{
return exec::finally(just(), throws_on_connect{});
};
using Rcvr = __receiver_archetype<ex::env<>>;
STATIC_REQUIRE(!__nothrow_connectable<decltype(initial_throws()), Rcvr>);
STATIC_REQUIRE(!__nothrow_connectable<decltype(final_throws()), Rcvr>);

// So an algorithm that connects it when another sender completes reports the exception.
using Let = decltype(let_value(just(), final_throws));
STATIC_REQUIRE(__mapply<__mcontains<set_error_t(std::exception_ptr)>,
completion_signatures_of_t<Let, ex::env<>>>::value);
CHECK_THROWS_AS(sync_wait(let_value(just(), initial_throws)), int);
CHECK_THROWS_AS(sync_wait(let_value(just(), final_throws)), int);
}
#endif // !STDEXEC_NO_STDCPP_EXCEPTIONS()

TEST_CASE("finally completes where the final action completes", "[adaptors][finally]")
Expand Down
Loading
Loading