Skip to content

Commit

Permalink
replace some uses of std::variant with stdexec::__variant
Browse files Browse the repository at this point in the history
  • Loading branch information
ericniebler committed May 27, 2024
1 parent af93d4b commit e142c7c
Show file tree
Hide file tree
Showing 22 changed files with 315 additions and 271 deletions.
2 changes: 1 addition & 1 deletion include/exec/async_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ namespace exec {
auto& __state = *__state_;
try {
std::unique_lock __guard{__state.__mutex_};
using _Tuple = __decayed_tuple<_Tag, _As...>;
using _Tuple = __decayed_std_tuple<_Tag, _As...>;
__state.__data_.template emplace<_Tuple>(_Tag(), static_cast<_As&&>(__as)...);
return true;
} catch (...) {
Expand Down
5 changes: 3 additions & 2 deletions include/exec/create.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ namespace exec {
template <__completion_signature... _Sigs>
struct __create_t {
template <class _Fun, class... _Args>
requires move_constructible<_Fun> && constructible_from<__decayed_tuple<_Args...>, _Args...>
requires move_constructible<_Fun>
&& constructible_from<__decayed_std_tuple<_Args...>, _Args...>
auto operator()(_Fun __fun, _Args&&... __args) const
-> __t<__sender<_Fun, __id<__decayed_tuple<_Args...>>, _Sigs...>> {
-> __t<__sender<_Fun, __id<__decayed_std_tuple<_Args...>>, _Sigs...>> {
return {static_cast<_Fun&&>(__fun), {static_cast<_Args&&>(__args)...}};
}
};
Expand Down
7 changes: 4 additions & 3 deletions include/exec/finally.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ namespace exec {
using namespace stdexec;

template <class _Sigs>
using __result_variant = __for_each_completion_signature<_Sigs, __decayed_tuple, __variant>;
using __result_variant =
__for_each_completion_signature<_Sigs, __decayed_std_tuple, __std_variant>;

template <class _ResultType, class _ReceiverId>
struct __final_operation_base {
Expand Down Expand Up @@ -212,10 +213,10 @@ namespace exec {
using __id = __operation_state;

template <class... _Args>
requires constructible_from<__result_variant<__signatures>, __decayed_tuple<_Args...>>
requires constructible_from<__result_variant<__signatures>, __decayed_std_tuple<_Args...>>
void __store_result_and_start_next_op(_Args&&... __args) {
this->__result_.__construct(
std::in_place_type<__decayed_tuple<_Args...>>, static_cast<_Args&&>(__args)...);
std::in_place_type<__decayed_std_tuple<_Args...>>, static_cast<_Args&&>(__args)...);
STDEXEC_ASSERT(__op_.index() == 0);
_FinalSender& __final = std::get_if<0>(&__op_)->__sndr_;
__final_op_t& __final_op = __op_.template emplace<1>(__conv{[&] {
Expand Down
2 changes: 1 addition & 1 deletion include/exec/into_tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace exec {

template <class _Sender, class _Env>
using __try_result_tuple_t = //
__value_types_of_t<_Sender, _Env, __q<__decayed_tuple>, __q<__msingle>>;
__value_types_of_t<_Sender, _Env, __q<__decayed_std_tuple>, __q<__msingle>>;

template <class _Sender, class _Env>
using __result_tuple_t = //
Expand Down
8 changes: 4 additions & 4 deletions include/exec/libdispatch_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace exec {
using bulk_non_throwing = //
stdexec::__mbool<
stdexec::__nothrow_callable<Fun, Shape, Args &...>
&& noexcept(stdexec::__decayed_tuple<Args...>(std::declval<Args>()...))>;
&& noexcept(stdexec::__decayed_std_tuple<Args...>(std::declval<Args>()...))>;

template <class CvrefSenderId, class ReceiverId, class Shape, class Fun, bool MayThrow>
struct bulk_receiver {
Expand Down Expand Up @@ -389,8 +389,8 @@ namespace exec {
stdexec::__value_types_of_t<
CvrefSender,
stdexec::env_of_t<Receiver>,
stdexec::__q<stdexec::__decayed_tuple>,
stdexec::__q<stdexec::__variant>>;
stdexec::__q<stdexec::__decayed_std_tuple>,
stdexec::__q<stdexec::__std_variant>>;

variant_t data_;
Receiver rcvr_;
Expand Down Expand Up @@ -442,7 +442,7 @@ namespace exec {

template <class... As>
void set_value(As &&...as) noexcept {
using tuple_t = stdexec::__decayed_tuple<As...>;
using tuple_t = stdexec::__decayed_std_tuple<As...>;

if constexpr (MayThrow) {
try {
Expand Down
15 changes: 9 additions & 6 deletions include/exec/sequence/ignore_all_values.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace exec {
void __emplace(_Args&&... __args) noexcept {
int __expected = 0;
if (__emplaced_.compare_exchange_strong(__expected, 1, std::memory_order_relaxed)) {
__result_.template emplace<__decayed_tuple<_Args...>>(static_cast<_Args&&>(__args)...);
__result_.template emplace<__decayed_std_tuple<_Args...>>(static_cast<_Args&&>(__args)...);
__emplaced_.store(2, std::memory_order_release);
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ namespace exec {
template <class _Error>
requires __variant_emplaceable<
_ResultVariant,
__decayed_tuple<set_error_t, _Error>,
__decayed_std_tuple<set_error_t, _Error>,
set_error_t,
_Error>
&& __callable<stdexec::set_stopped_t, _ItemReceiver>
Expand All @@ -106,7 +106,10 @@ namespace exec {
}

void set_stopped() noexcept
requires __variant_emplaceable<_ResultVariant, __decayed_tuple<set_stopped_t>, set_stopped_t>
requires __variant_emplaceable<
_ResultVariant,
__decayed_std_tuple<set_stopped_t>,
set_stopped_t>
&& __callable<set_stopped_t, _ItemReceiver>
{
// stop without error
Expand Down Expand Up @@ -214,16 +217,16 @@ namespace exec {

template <class _Tag>
using __result_tuple_fn = //
__mcompose_q<__types_ref, __mbind_front_q<__decayed_tuple, _Tag>::template __f>;
__mcompose_q<__types_ref, __mbind_front_q<__decayed_std_tuple, _Tag>::template __f>;

template <class _Sigs>
using __result_variant_ = //
__transform_completion_signatures<
_Sigs,
__mconst<__types_ref<>>::__f,
__mcompose_q<__types_ref, __mbind_front_q<__decayed_tuple, set_error_t>::__f>::__f,
__mcompose_q<__types_ref, __mbind_front_q<__decayed_std_tuple, set_error_t>::__f>::__f,
__types_ref<std::tuple<set_stopped_t>>,
__mappend_into_q<__nullable_variant_fn::__f>::__f>;
__mappend_into_q<__nullable_std_variant>::__f>;

template <class _Sender, class _Env>
using __result_variant_t =
Expand Down
12 changes: 8 additions & 4 deletions include/exec/static_thread_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ namespace exec {

template <class... Args>
struct __bulk_non_throwing {
using __t = __decayed_tuple<Args...>;
using __t = __decayed_std_tuple<Args...>;
static constexpr bool __v = noexcept(__t(std::declval<Args>()...));
};
#endif
Expand All @@ -203,7 +203,7 @@ namespace exec {
#if STDEXEC_MSVC()
__bulk_non_throwing<Args...>::__v
#else
noexcept(__decayed_tuple<Args...>(std::declval<Args>()...))
noexcept(__decayed_std_tuple<Args...>(std::declval<Args>()...))
#endif
// there's no need to advertise completion with `exception_ptr`
>;
Expand Down Expand Up @@ -1173,7 +1173,11 @@ namespace exec {
};

using variant_t = //
__value_types_of_t<CvrefSender, env_of_t<Receiver>, __q<__decayed_tuple>, __q<__variant>>;
__value_types_of_t<
CvrefSender,
env_of_t<Receiver>,
__q<__decayed_std_tuple>,
__q<__std_variant>>;

variant_t data_;
static_thread_pool_& pool_;
Expand Down Expand Up @@ -1225,7 +1229,7 @@ namespace exec {

template <class... As>
void set_value(As&&... as) noexcept {
using tuple_t = __decayed_tuple<As...>;
using tuple_t = __decayed_std_tuple<As...>;

shared_state& state = shared_state_;

Expand Down
101 changes: 50 additions & 51 deletions include/exec/when_any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ namespace exec {
template <class... E>
using __as_error = set_error_t (*)(E...);

template <class... _CvrefSenders, class _Env>
auto __completions_fn(_Env&&) //
-> __concat_completion_signatures<
// Here we convert all set_value(Args...) to set_value(__decay_t<Args>...). Note, we keep all
// error types as they are and unconditionally add set_stopped(). The indirection through the
// __completions_fn is to avoid a pack expansion bug in nvc++.
template <class _Env, class... _CvrefSenders>
using __completions_t = //
__concat_completion_signatures<
__eptr_completion_if_t<__all_value_args_nothrow_decay_copyable<_Env, _CvrefSenders...>>,
completion_signatures<set_stopped_t()>,
__transform_completion_signatures<
Expand All @@ -68,33 +71,21 @@ namespace exec {
set_stopped_t (*)(),
__completion_signature_ptrs>...>;

// Here we convert all set_value(Args...) to set_value(__decay_t<Args>...). Note, we keep all
// error types as they are and unconditionally add set_stopped(). The indirection through the
// __completions_fn is to avoid a pack expansion bug in nvc++.
template <class _Env, class... _CvrefSenders>
using __completions_t = //
decltype(__completions_fn<_CvrefSenders...>(__declval<_Env>()));

template <class _Env, class... _CvrefSenders>
using __result_type_t = //
__for_each_completion_signature<
__completions_t<_Env, _CvrefSenders...>,
__decayed_tuple,
__munique<__q<std::variant>>::__f>;

template <class _Variant, class... _Ts>
concept __nothrow_result_constructible_from =
__nothrow_constructible_from<__decayed_tuple<_Ts...>, _Ts...>
&& __nothrow_constructible_from<_Variant, __decayed_tuple<_Ts...>>;
__uniqued_variant_for>;

template <class _Receiver>
auto __make_visitor_fn(_Receiver& __rcvr) noexcept {
return [&__rcvr]<class _Tuple>(_Tuple&& __result) noexcept {
std::apply(
[&__rcvr]<class _Tag, class... _As>(_Tag, _As&&... __args) noexcept {
_Tag{}(static_cast<_Receiver&&>(__rcvr), static_cast<_As&&>(__args)...);
__result.apply(
[&__rcvr]<class... _As>(auto __tag, _As&... __args) noexcept {
__tag(static_cast<_Receiver&&>(__rcvr), static_cast<_As&&>(__args)...);
},
static_cast<_Tuple&&>(__result));
__result);
};
}

Expand All @@ -117,21 +108,26 @@ namespace exec {
std::atomic<std::size_t> __count_{};

_Receiver __rcvr_;
std::optional<_ResultVariant> __result_{};
_ResultVariant __result_{};

STDEXEC_PRAGMA_PUSH()
STDEXEC_PRAGMA_IGNORE_GNU("-Wmissing-braces")

template <class _Tag, class... _Args>
void notify(_Tag, _Args&&... __args) noexcept {
using __result_t = __decayed_tuple<_Tag, _Args...>;
bool __expect = false;
if (__emplaced_.compare_exchange_strong(
__expect, true, std::memory_order_relaxed, std::memory_order_relaxed)) {
// This emplacement can happen only once
if constexpr (__nothrow_result_constructible_from<_ResultVariant, _Tag, _Args...>) {
__result_.emplace(std::tuple{_Tag{}, static_cast<_Args&&>(__args)...});
if constexpr ((__nothrow_decay_copyable<_Args> && ...)) {
__result_.template emplace<__result_t>(_Tag{}, static_cast<_Args&&>(__args)...);
} else {
try {
__result_.emplace(std::tuple{_Tag{}, static_cast<_Args&&>(__args)...});
__result_.template emplace<__result_t>(_Tag{}, static_cast<_Args&&>(__args)...);
} catch (...) {
__result_.emplace(std::tuple{set_error_t{}, std::current_exception()});
using __error_t = __tuple_for<set_error_t, std::exception_ptr>;
__result_.template emplace<__error_t>(set_error_t{}, std::current_exception());
}
}
// stop pending operations
Expand All @@ -146,11 +142,13 @@ namespace exec {
stdexec::set_stopped(static_cast<_Receiver&&>(__rcvr_));
return;
}
STDEXEC_ASSERT(__result_.has_value());
std::visit(
__when_any::__make_visitor_fn(__rcvr_), static_cast<_ResultVariant&&>(*__result_));
STDEXEC_ASSERT(!__result_.is_valueless());
__result_.visit(
__when_any::__make_visitor_fn(__rcvr_), static_cast<_ResultVariant&&>(__result_));
}
}

STDEXEC_PRAGMA_POP()
};

template <class _Receiver, class _ResultVariant>
Expand Down Expand Up @@ -201,13 +199,19 @@ namespace exec {
&& (__nothrow_connectable<__cvref_t<_CvrefSenderIds>, __receiver_t> && ...);

class __t : __op_base_t {
using __opstate_tuple =
__tuple_for<connect_result_t<stdexec::__cvref_t<_CvrefSenderIds>, __receiver_t>...>;
public:
template <class _SenderTuple>
__t(_SenderTuple&& __senders, _Receiver&& __rcvr) noexcept(__nothrow_construct)
: __t{
static_cast<_SenderTuple&&>(__senders),
static_cast<_Receiver&&>(__rcvr),
std::index_sequence_for<_CvrefSenderIds...>{}} {
: __op_base_t{static_cast<_Receiver&&>(__rcvr), sizeof...(_CvrefSenderIds)}
, __ops_{__senders.apply(
[this]<class... _Senders>(_Senders&&... __sndrs) noexcept(__nothrow_construct)
-> __opstate_tuple {
return __opstate_tuple{
{stdexec::connect(static_cast<_Senders&&>(__sndrs), __receiver_t{this})}...};
},
static_cast<_SenderTuple&&>(__senders))} {
}

void start() & noexcept {
Expand All @@ -216,23 +220,12 @@ namespace exec {
if (this->__stop_source_.stop_requested()) {
stdexec::set_stopped(static_cast<_Receiver&&>(this->__rcvr_));
} else {
std::apply([](auto&... __ops) { (stdexec::start(__ops), ...); }, __ops_);
__ops_.apply([](auto&... __ops) { (stdexec::start(__ops), ...); }, __ops_);
}
}

private:
template <class _SenderTuple, std::size_t... _Is>
__t(_SenderTuple&& __senders, _Receiver&& __rcvr, std::index_sequence<_Is...>) //
noexcept(__nothrow_construct)
: __op_base_t{static_cast<_Receiver&&>(__rcvr), sizeof...(_CvrefSenderIds)}
, __ops_{__conv{[&__senders, this] {
return stdexec::connect(
std::get<_Is>(static_cast<_SenderTuple&&>(__senders)),
__receiver_t{static_cast<__op_base_t*>(this)});
}}...} {
}

std::tuple<connect_result_t<stdexec::__cvref_t<_CvrefSenderIds>, __receiver_t>...> __ops_;
__opstate_tuple __ops_;
};
};

Expand All @@ -248,34 +241,40 @@ namespace exec {
template <class _Self, class _Receiver>
using __op_t = stdexec::__t<__op<__id<_Receiver>, __copy_cvref_t<_Self, _SenderIds>...>>;

template <class _Self, class _Env>
using __completions_t = //
__when_any::__completions_t<_Env, __copy_cvref_t<_Self, stdexec::__t<_SenderIds>>...>;

class __t {
public:
using __id = __sender;
using sender_concept = stdexec::sender_t;
using __senders_tuple = __tuple_for<stdexec::__t<_SenderIds>...>;

template <__not_decays_to<__t>... _Senders>
explicit(sizeof...(_Senders) == 1)
__t(_Senders&&... __senders) noexcept((__nothrow_decay_copyable<_Senders> && ...))
: __senders_(static_cast<_Senders&&>(__senders)...) {
: __senders_{{static_cast<_Senders&&>(__senders)}...} {
}

template <__decays_to<__t> _Self, receiver _Receiver>
STDEXEC_MEMFN_DECL(
auto connect)(this _Self&& __self, _Receiver __rcvr) //
noexcept(__nothrow_constructible_from<__op_t<_Self, _Receiver>, _Self, _Receiver>)
-> __op_t<_Self, _Receiver> {
// noexcept(__nothrow_constructible_from<
// __op_t<_Self, _Receiver>, __copy_cvref_t<_Self, __senders_tuple>, _Receiver>)
-> __op_t<_Self, _Receiver> {
return __op_t<_Self, _Receiver>{
static_cast<_Self&&>(__self).__senders_, static_cast<_Receiver&&>(__rcvr)};
}

template <__decays_to<__t> _Self, class _Env>
static auto get_completion_signatures(_Self&&, _Env&&) noexcept
-> __completions_t<_Env, __copy_cvref_t<_Self, stdexec::__t<_SenderIds>>...> {
static auto
get_completion_signatures(_Self&&, _Env&&) noexcept -> __completions_t<_Self, _Env> {
return {};
}

private:
std::tuple<stdexec::__t<_SenderIds>...> __senders_;
__senders_tuple __senders_;
};
};

Expand Down
4 changes: 2 additions & 2 deletions include/nvexec/stream/let_xxx.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ namespace nvexec::STDEXEC_STREAM_DETAIL_NS {

using __op_state_variant_t = //
__minvoke<
__transform<__uncurry<__op_state_for<_Receiver, _Fun>>, __nullable_variant_fn>,
__transform<__uncurry<__op_state_for<_Receiver, _Fun>>, __qq<__nullable_std_variant>>,
_Tuples...>;

__operation<_SenderId, _ReceiverId, _Fun, _Let>* __op_state_;
Expand All @@ -169,7 +169,7 @@ namespace nvexec::STDEXEC_STREAM_DETAIL_NS {
_Let,
stdexec::__t<_SenderId>,
stream_env<env_of_t<stdexec::__t<_ReceiverId>>>,
__q<__decayed_tuple>,
__q<__decayed_std_tuple>,
__munique<__mbind_front_q<__receiver_, _SenderId, _ReceiverId, _Fun, _Let>>>>;

template <class _SenderId, class _ReceiverId, class _Fun, class _Let>
Expand Down
Loading

0 comments on commit e142c7c

Please sign in to comment.