Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async-object #1339

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
52 changes: 52 additions & 0 deletions include/exec/__detail/__decl_receiver.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2024 Kirk Shoop
* Copyright (c) 2023 NVIDIA Corporation
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include "../../stdexec/__detail/__execution_fwd.hpp"
#include "../../stdexec/__detail/__receivers.hpp"

#include "../../stdexec/concepts.hpp"

namespace exec {

// disable spurious warning in clang
// https://github.com/llvm/llvm-project/issues/61566
STDEXEC_PRAGMA_PUSH()
STDEXEC_PRAGMA_IGNORE_GNU("-Wundefined-internal")

// fake receiver used to calculate whether inner connect is nothrow
template<class _Env>
struct __decl_receiver {
using __t = __decl_receiver;
using __id = __decl_receiver;

using receiver_concept = stdexec::receiver_t;

template <class... _An>
void set_value_t(_An&&... __an) && noexcept;

template <class _Error>
void set_error_t(_Error&& __err) && noexcept;

void set_stopped_t() && noexcept;

_Env get_env_t() const& noexcept;
};

STDEXEC_PRAGMA_POP()

} // namespace exec
36 changes: 36 additions & 0 deletions include/exec/__detail/__tuple_index_pack.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2024 Kirk Shoop
* Copyright (c) 2023 NVIDIA Corporation
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include "../../stdexec/concepts.hpp"
#include "../../stdexec/__detail/__tuple.hpp"

#include <type_traits>
#include <tuple>
#include <utility>

namespace exec {

struct __tuple_index_pack_t {
template<typename Fn, typename T, typename... Tn>
auto operator()(Fn&& fn, T&& t, Tn&&... tn) const {
return fn(std::make_index_sequence<std::tuple_size<std::remove_cvref_t<T>>::value>(), std::forward<T&&>(t), std::forward<Tn&&>(tn)...);
}
};
constexpr inline static __tuple_index_pack_t __tuple_index_pack;

} // namespace exec
53 changes: 53 additions & 0 deletions include/exec/__detail/__tuple_reverse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2024 Kirk Shoop
* Copyright (c) 2023 NVIDIA Corporation
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include "../../stdexec/concepts.hpp"
#include "../../stdexec/__detail/__tuple.hpp"

#include <type_traits>
#include <tuple>
#include <utility>

namespace exec {

template<template<class...> class R, class I, class... Tn>
struct __apply_reverse_impl;

template<template<class...> class R, std::size_t... In, class... Tn>
struct __apply_reverse_impl<R, std::index_sequence<In...>, Tn...> {
using tn_t = std::tuple<Tn...>;
using type = R<typename std::tuple_element<sizeof...(In) - 1 - In, tn_t>::type...>;
};

template<template<class...> class R, class... Tn>
using __apply_reverse = typename __apply_reverse_impl<R, std::make_index_sequence<sizeof...(Tn)>, Tn...>::type;

struct __tuple_reverse_t {
template<typename T, size_t... I>
static auto reverse(T&& t, std::index_sequence<I...>) {
return std::make_tuple(std::get<sizeof...(I) - 1 - I>(std::forward<T>(t))...);
}

template<typename T>
auto operator()(T&& t) const {
return __tuple_reverse_t::reverse(std::forward<T>(t), std::make_index_sequence<std::tuple_size<T>::value>());
}
};
constexpr inline static __tuple_reverse_t __tuple_reverse;

} // namespace exec
127 changes: 127 additions & 0 deletions include/exec/async_object.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (c) 2024 Kirk Shoop
* Copyright (c) 2023 NVIDIA Corporation
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include "../stdexec/execution.hpp"
#include "../stdexec/concepts.hpp"

#include "__detail/__manual_lifetime.hpp"

namespace exec {

struct async_construct_t {
template<class _O, class _Stg, class... _An>
auto operator()(_O&& __o, _Stg& __stg, _An&&... __an) const
noexcept(noexcept(((_O&&)__o).async_construct(__stg, ((_An&&)__an)...)))
-> decltype(((_O&&)__o).async_construct(__stg, ((_An&&)__an)...)) {
using __construct = decltype(((_O&&)__o).async_construct(__stg, ((_An&&)__an)...));
static_assert(!stdexec::same_as<__construct, void>, "async_construct must not return void");
static_assert(stdexec::__single_value_sender<__construct>, "async_construct must return a sender with a single set_value overload");
static_assert(stdexec::sender_of<__construct, stdexec::set_value_t(typename std::remove_cvref_t<_O>::handle)>, "async_construct must return a sender that completes with set_value(handle)");
return ((_O&&)__o).async_construct(__stg, ((_An&&)__an)...);
}
template<class _O, class _Stg, class... _An>
auto operator()(_O&& __o, _Stg& __stg, _An&&... __an) const
noexcept(noexcept(((_O&&)__o).async_construct(__stg, ((_An&&)__an)...)))
-> std::enable_if_t<
stdexec::same_as<decltype(((_O&&)__o).async_construct(__stg, ((_An&&)__an)...)), void>,
decltype(((_O&&)__o).async_construct(__stg, ((_An&&)__an)...))> = delete;
};
constexpr inline static async_construct_t async_construct{};

template<class _O, class... _An>
using async_construct_result_t = stdexec::__call_result_t<async_construct_t, const std::remove_cvref_t<_O>&, typename std::remove_cvref_t<_O>::storage&, _An...>;

struct async_destruct_t {
template<class _O, class _Stg>
auto operator()(_O&& __o, _Stg& __stg) const
noexcept
-> std::enable_if_t<
!stdexec::same_as<decltype(((_O&&)__o).async_destruct(__stg)), void>,
decltype(((_O&&)__o).async_destruct(__stg))> {
static_assert(noexcept(((_O&&)__o).async_destruct(__stg)), "async_destruct must be noexcept");
using __destruct = decltype(((_O&&)__o).async_destruct(__stg));
static_assert(!stdexec::same_as<__destruct, void>, "async_destruct must not return void");
static_assert(stdexec::__single_value_sender<__destruct>, "async_destruct must return a sender with a single set_value overload");
static_assert(stdexec::sender_of<__destruct, stdexec::set_value_t()>, "async_destruct must return a sender that completes with set_value()");
static_assert(stdexec::__nofail_sender<__destruct>, "async_destruct must return a sender that has no set_error(..) completions");
return ((_O&&)__o).async_destruct(__stg);
}
template<class _O, class _Stg>
auto operator()(_O&& __o, _Stg& __stg) const
noexcept
-> std::enable_if_t<
stdexec::same_as<decltype(((_O&&)__o).async_destruct(__stg)), void>,
decltype(((_O&&)__o).async_destruct(__stg))> = delete;
};
constexpr inline static async_destruct_t async_destruct{};

template<class _O>
using async_destruct_result_t = stdexec::__call_result_t<async_destruct_t, const std::remove_cvref_t<_O>&, typename std::remove_cvref_t<_O>::storage&>;

namespace __async_object {

template<class _T>
concept __immovable_object =
!std::is_move_constructible_v<_T> &&
!std::is_copy_constructible_v<_T> &&
!std::is_move_assignable_v<_T> &&
!std::is_copy_assignable_v<_T>;

template<class _T>
concept __object =
!std::is_default_constructible_v<_T> &&
__immovable_object<_T>;

template<class _T>
concept __storage =
std::is_nothrow_default_constructible_v<_T> &&
__immovable_object<_T>;

template<class _S>
concept __async_destruct_result_valid =
stdexec::__single_value_sender<_S> &&
stdexec::sender_of<_S, stdexec::set_value_t()>;

} // namespace __async_object

template<class _T>
concept async_object =
requires (){
typename _T::object;
typename _T::handle;
typename _T::storage;
} &&
std::is_move_constructible_v<_T> &&
std::is_nothrow_move_constructible_v<typename _T::handle> &&
__async_object::__object<typename _T::object> &&
__async_object::__storage<typename _T::storage> &&
requires (const _T& __t_clv, typename _T::storage& __s_lv){
{ async_destruct_t{}(__t_clv, __s_lv) }
-> stdexec::__nofail_sender;
} &&
__async_object::__async_destruct_result_valid<async_destruct_result_t<_T>>;

template<class _T, class... _An>
concept async_object_constructible_from =
async_object<_T> &&
requires (const _T& __t_clv, typename _T::storage& __s_lv, _An... __an){
{ async_construct_t{}(__t_clv, __s_lv, __an...) }
-> stdexec::sender_of<stdexec::set_value_t(typename _T::handle)>;
};

} // namespace exec
Loading