codekingpro's picture
Add files using upload-large-folder tool
501e3f2 verified
Raw
History Blame Contribute Delete
10.4 kB
// -*- C++ -*-
//===----------------------------- coroutine -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCUDACXX_EXPERIMENTAL_COROUTINE
#define _LIBCUDACXX_EXPERIMENTAL_COROUTINE
/**
experimental/coroutine synopsis
// C++next
namespace std {
namespace experimental {
inline namespace coroutines_v1 {
// 18.11.1 coroutine traits
template <typename R, typename... ArgTypes>
class coroutine_traits;
// 18.11.2 coroutine handle
template <typename Promise = void>
class coroutine_handle;
// 18.11.2.7 comparison operators:
bool operator==(coroutine_handle<> x, coroutine_handle<> y) noexcept;
bool operator!=(coroutine_handle<> x, coroutine_handle<> y) noexcept;
bool operator<(coroutine_handle<> x, coroutine_handle<> y) noexcept;
bool operator<=(coroutine_handle<> x, coroutine_handle<> y) noexcept;
bool operator>=(coroutine_handle<> x, coroutine_handle<> y) noexcept;
bool operator>(coroutine_handle<> x, coroutine_handle<> y) noexcept;
// 18.11.3 trivial awaitables
struct suspend_never;
struct suspend_always;
// 18.11.2.8 hash support:
template <class T> struct hash;
template <class P> struct hash<coroutine_handle<P>>;
} // namespace coroutines_v1
} // namespace experimental
} // namespace std
*/
#include <experimental/__config>
#include <new>
#include <type_traits>
#include <functional>
#include <memory> // for hash<T*>
#include <cstddef>
#include <__debug>
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header
#ifdef _LIBCUDACXX_HAS_NO_COROUTINES
# if defined(_LIBCUDACXX_WARNING)
_LIBCUDACXX_WARNING("<experimental/coroutine> cannot be used with this compiler")
# else
# warning <experimental/coroutine> cannot be used with this compiler
# endif
#endif
#ifndef _LIBCUDACXX_HAS_NO_COROUTINES
_LIBCUDACXX_BEGIN_NAMESPACE_EXPERIMENTAL_COROUTINES
template <class _Tp, class = void>
struct __coroutine_traits_sfinae {};
template <class _Tp>
struct __coroutine_traits_sfinae<
_Tp, __void_t<typename _Tp::promise_type>>
{
using promise_type = typename _Tp::promise_type;
};
template <typename _Ret, typename... _Args>
struct coroutine_traits
: public __coroutine_traits_sfinae<_Ret>
{
};
template <typename _Promise = void>
class _LIBCUDACXX_TEMPLATE_VIS coroutine_handle;
template <>
class _LIBCUDACXX_TEMPLATE_VIS coroutine_handle<void> {
public:
_LIBCUDACXX_INLINE_VISIBILITY
constexpr coroutine_handle() noexcept : __handle_(nullptr) {}
_LIBCUDACXX_INLINE_VISIBILITY
constexpr coroutine_handle(nullptr_t) noexcept : __handle_(nullptr) {}
_LIBCUDACXX_INLINE_VISIBILITY
coroutine_handle& operator=(nullptr_t) noexcept {
__handle_ = nullptr;
return *this;
}
_LIBCUDACXX_INLINE_VISIBILITY
constexpr void* address() const noexcept { return __handle_; }
_LIBCUDACXX_INLINE_VISIBILITY
constexpr explicit operator bool() const noexcept { return __handle_; }
_LIBCUDACXX_INLINE_VISIBILITY
void operator()() { resume(); }
_LIBCUDACXX_INLINE_VISIBILITY
void resume() {
_LIBCUDACXX_ASSERT(__is_suspended(),
"resume() can only be called on suspended coroutines");
_LIBCUDACXX_ASSERT(!done(),
"resume() has undefined behavior when the coroutine is done");
__builtin_coro_resume(__handle_);
}
_LIBCUDACXX_INLINE_VISIBILITY
void destroy() {
_LIBCUDACXX_ASSERT(__is_suspended(),
"destroy() can only be called on suspended coroutines");
__builtin_coro_destroy(__handle_);
}
_LIBCUDACXX_INLINE_VISIBILITY
bool done() const {
_LIBCUDACXX_ASSERT(__is_suspended(),
"done() can only be called on suspended coroutines");
return __builtin_coro_done(__handle_);
}
public:
_LIBCUDACXX_INLINE_VISIBILITY
static coroutine_handle from_address(void* __addr) noexcept {
coroutine_handle __tmp;
__tmp.__handle_ = __addr;
return __tmp;
}
// FIXME: Should from_address(nullptr) be allowed?
_LIBCUDACXX_INLINE_VISIBILITY
static coroutine_handle from_address(nullptr_t) noexcept {
return coroutine_handle(nullptr);
}
template <class _Tp, bool _CallIsValid = false>
static coroutine_handle from_address(_Tp*) {
static_assert(_CallIsValid,
"coroutine_handle<void>::from_address cannot be called with "
"non-void pointers");
}
private:
bool __is_suspended() const noexcept {
// FIXME actually implement a check for if the coro is suspended.
return __handle_;
}
template <class _PromiseT> friend class coroutine_handle;
void* __handle_;
};
// 18.11.2.7 comparison operators:
inline _LIBCUDACXX_INLINE_VISIBILITY
bool operator==(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
return __x.address() == __y.address();
}
inline _LIBCUDACXX_INLINE_VISIBILITY
bool operator!=(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
return !(__x == __y);
}
inline _LIBCUDACXX_INLINE_VISIBILITY
bool operator<(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
return less<void*>()(__x.address(), __y.address());
}
inline _LIBCUDACXX_INLINE_VISIBILITY
bool operator>(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
return __y < __x;
}
inline _LIBCUDACXX_INLINE_VISIBILITY
bool operator<=(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
return !(__x > __y);
}
inline _LIBCUDACXX_INLINE_VISIBILITY
bool operator>=(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
return !(__x < __y);
}
template <typename _Promise>
class _LIBCUDACXX_TEMPLATE_VIS coroutine_handle : public coroutine_handle<> {
using _Base = coroutine_handle<>;
public:
// 18.11.2.1 construct/reset
using coroutine_handle<>::coroutine_handle;
_LIBCUDACXX_INLINE_VISIBILITY
coroutine_handle& operator=(nullptr_t) noexcept {
_Base::operator=(nullptr);
return *this;
}
_LIBCUDACXX_INLINE_VISIBILITY
_Promise& promise() const {
return *static_cast<_Promise*>(
__builtin_coro_promise(this->__handle_, _LIBCUDACXX_ALIGNOF(_Promise), false));
}
public:
_LIBCUDACXX_INLINE_VISIBILITY
static coroutine_handle from_address(void* __addr) noexcept {
coroutine_handle __tmp;
__tmp.__handle_ = __addr;
return __tmp;
}
// NOTE: this overload isn't required by the standard but is needed so
// the deleted _Promise* overload doesn't make from_address(nullptr)
// ambiguous.
// FIXME: should from_address work with nullptr?
_LIBCUDACXX_INLINE_VISIBILITY
static coroutine_handle from_address(nullptr_t) noexcept {
return coroutine_handle(nullptr);
}
template <class _Tp, bool _CallIsValid = false>
static coroutine_handle from_address(_Tp*) {
static_assert(_CallIsValid,
"coroutine_handle<promise_type>::from_address cannot be called with "
"non-void pointers");
}
template <bool _CallIsValid = false>
static coroutine_handle from_address(_Promise*) {
static_assert(_CallIsValid,
"coroutine_handle<promise_type>::from_address cannot be used with "
"pointers to the coroutine's promise type; use 'from_promise' instead");
}
_LIBCUDACXX_INLINE_VISIBILITY
static coroutine_handle from_promise(_Promise& __promise) noexcept {
typedef __remove_cv_t<_Promise> _RawPromise;
coroutine_handle __tmp;
__tmp.__handle_ = __builtin_coro_promise(
_CUDA_VSTD::addressof(const_cast<_RawPromise&>(__promise)),
_LIBCUDACXX_ALIGNOF(_Promise), true);
return __tmp;
}
};
#if __has_builtin(__builtin_coro_noop)
struct noop_coroutine_promise {};
template <>
class _LIBCUDACXX_TEMPLATE_VIS coroutine_handle<noop_coroutine_promise>
: public coroutine_handle<> {
using _Base = coroutine_handle<>;
using _Promise = noop_coroutine_promise;
public:
_LIBCUDACXX_INLINE_VISIBILITY
_Promise& promise() const {
return *static_cast<_Promise*>(
__builtin_coro_promise(this->__handle_, _LIBCUDACXX_ALIGNOF(_Promise), false));
}
constexpr explicit operator bool() const noexcept { return true; }
constexpr bool done() const noexcept { return false; }
_LIBCUDACXX_CONSTEXPR_AFTER_CXX17 void operator()() const noexcept {}
_LIBCUDACXX_CONSTEXPR_AFTER_CXX17 void resume() const noexcept {}
_LIBCUDACXX_CONSTEXPR_AFTER_CXX17 void destroy() const noexcept {}
private:
_LIBCUDACXX_INLINE_VISIBILITY
friend coroutine_handle<noop_coroutine_promise> noop_coroutine() noexcept;
_LIBCUDACXX_INLINE_VISIBILITY coroutine_handle() noexcept {
this->__handle_ = __builtin_coro_noop();
}
};
using noop_coroutine_handle = coroutine_handle<noop_coroutine_promise>;
inline _LIBCUDACXX_INLINE_VISIBILITY
noop_coroutine_handle noop_coroutine() noexcept {
return noop_coroutine_handle();
}
#endif // __has_builtin(__builtin_coro_noop)
struct suspend_never {
_LIBCUDACXX_INLINE_VISIBILITY
bool await_ready() const noexcept { return true; }
_LIBCUDACXX_INLINE_VISIBILITY
void await_suspend(coroutine_handle<>) const noexcept {}
_LIBCUDACXX_INLINE_VISIBILITY
void await_resume() const noexcept {}
};
struct suspend_always {
_LIBCUDACXX_INLINE_VISIBILITY
bool await_ready() const noexcept { return false; }
_LIBCUDACXX_INLINE_VISIBILITY
void await_suspend(coroutine_handle<>) const noexcept {}
_LIBCUDACXX_INLINE_VISIBILITY
void await_resume() const noexcept {}
};
_LIBCUDACXX_END_NAMESPACE_EXPERIMENTAL_COROUTINES
_LIBCUDACXX_BEGIN_NAMESPACE_STD
template <class _Tp>
struct hash<_CUDA_VSTD_CORO::coroutine_handle<_Tp> > {
using __arg_type = _CUDA_VSTD_CORO::coroutine_handle<_Tp>;
_LIBCUDACXX_INLINE_VISIBILITY
size_t operator()(__arg_type const& __v) const noexcept
{return hash<void*>()(__v.address());}
};
_LIBCUDACXX_END_NAMESPACE_STD
#endif // !defined(_LIBCUDACXX_HAS_NO_COROUTINES)
#endif /* _LIBCUDACXX_EXPERIMENTAL_COROUTINE */