2017-05-25 12:36:24 +08:00
|
|
|
// -*- C++ -*-
|
2021-11-18 05:25:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-05-25 12:36:24 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-05-25 12:36:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_EXPERIMENTAL_COROUTINE
|
|
|
|
#define _LIBCPP_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:
|
2017-05-26 11:02:54 +08:00
|
|
|
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;
|
2017-05-25 12:36:24 +08:00
|
|
|
// 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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2022-01-07 22:45:05 +08:00
|
|
|
#include <__debug>
|
|
|
|
#include <cstddef>
|
2017-05-25 12:36:24 +08:00
|
|
|
#include <experimental/__config>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory> // for hash<T*>
|
2022-01-07 22:45:05 +08:00
|
|
|
#include <new>
|
|
|
|
#include <type_traits>
|
2017-05-25 12:36:24 +08:00
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
|
|
|
#pragma GCC system_header
|
|
|
|
#endif
|
|
|
|
|
2021-11-16 14:05:34 +08:00
|
|
|
#ifdef _LIBCPP_HAS_NO_EXPERIMENTAL_COROUTINES
|
2017-05-25 12:36:24 +08:00
|
|
|
# if defined(_LIBCPP_WARNING)
|
|
|
|
_LIBCPP_WARNING("<experimental/coroutine> cannot be used with this compiler")
|
|
|
|
# else
|
|
|
|
# warning <experimental/coroutine> cannot be used with this compiler
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2021-11-16 14:05:34 +08:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXPERIMENTAL_COROUTINES
|
2017-05-26 09:52:59 +08:00
|
|
|
|
2017-05-25 12:36:24 +08:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_COROUTINES
|
|
|
|
|
|
|
|
template <class _Tp, class = void>
|
|
|
|
struct __coroutine_traits_sfinae {};
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
struct __coroutine_traits_sfinae<
|
|
|
|
_Tp, typename __void_t<typename _Tp::promise_type>::type>
|
|
|
|
{
|
|
|
|
using promise_type = typename _Tp::promise_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename _Ret, typename... _Args>
|
2019-04-17 12:31:46 +08:00
|
|
|
struct coroutine_traits
|
2017-05-25 12:36:24 +08:00
|
|
|
: public __coroutine_traits_sfinae<_Ret>
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2017-05-26 07:39:29 +08:00
|
|
|
template <typename _Promise = void>
|
2017-05-25 12:36:24 +08:00
|
|
|
class _LIBCPP_TEMPLATE_VIS coroutine_handle;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class _LIBCPP_TEMPLATE_VIS coroutine_handle<void> {
|
|
|
|
public:
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
_LIBCPP_CONSTEXPR coroutine_handle() _NOEXCEPT : __handle_(nullptr) {}
|
2017-05-25 12:36:24 +08:00
|
|
|
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
_LIBCPP_CONSTEXPR coroutine_handle(nullptr_t) _NOEXCEPT : __handle_(nullptr) {}
|
2017-05-25 12:36:24 +08:00
|
|
|
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
coroutine_handle& operator=(nullptr_t) _NOEXCEPT {
|
2017-05-25 12:36:24 +08:00
|
|
|
__handle_ = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
_LIBCPP_CONSTEXPR void* address() const _NOEXCEPT { return __handle_; }
|
2017-05-25 12:36:24 +08:00
|
|
|
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
_LIBCPP_CONSTEXPR explicit operator bool() const _NOEXCEPT { return __handle_; }
|
2017-05-25 12:36:24 +08:00
|
|
|
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 03:04:55 +08:00
|
|
|
void operator()() { resume(); }
|
2017-05-25 12:36:24 +08:00
|
|
|
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 03:04:55 +08:00
|
|
|
void resume() {
|
2017-05-25 12:36:24 +08:00
|
|
|
_LIBCPP_ASSERT(__is_suspended(),
|
|
|
|
"resume() can only be called on suspended coroutines");
|
|
|
|
_LIBCPP_ASSERT(!done(),
|
|
|
|
"resume() has undefined behavior when the coroutine is done");
|
|
|
|
__builtin_coro_resume(__handle_);
|
|
|
|
}
|
|
|
|
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 03:04:55 +08:00
|
|
|
void destroy() {
|
2017-05-25 12:36:24 +08:00
|
|
|
_LIBCPP_ASSERT(__is_suspended(),
|
|
|
|
"destroy() can only be called on suspended coroutines");
|
|
|
|
__builtin_coro_destroy(__handle_);
|
|
|
|
}
|
|
|
|
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-25 12:36:24 +08:00
|
|
|
bool done() const {
|
|
|
|
_LIBCPP_ASSERT(__is_suspended(),
|
|
|
|
"done() can only be called on suspended coroutines");
|
|
|
|
return __builtin_coro_done(__handle_);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
static coroutine_handle from_address(void* __addr) _NOEXCEPT {
|
2017-05-25 12:36:24 +08:00
|
|
|
coroutine_handle __tmp;
|
|
|
|
__tmp.__handle_ = __addr;
|
|
|
|
return __tmp;
|
|
|
|
}
|
|
|
|
|
2017-05-30 03:24:25 +08:00
|
|
|
// FIXME: Should from_address(nullptr) be allowed?
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-30 03:24:25 +08:00
|
|
|
static coroutine_handle from_address(nullptr_t) _NOEXCEPT {
|
2017-05-30 03:46:16 +08:00
|
|
|
return coroutine_handle(nullptr);
|
2017-05-30 03:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2017-05-25 12:36:24 +08:00
|
|
|
private:
|
2017-05-26 11:02:54 +08:00
|
|
|
bool __is_suspended() const _NOEXCEPT {
|
2017-05-25 12:36:24 +08:00
|
|
|
// 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:
|
2018-04-05 06:18:03 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
bool operator==(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
|
2017-05-25 12:36:24 +08:00
|
|
|
return __x.address() == __y.address();
|
|
|
|
}
|
2018-04-05 06:18:03 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
bool operator!=(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
|
2017-05-25 12:36:24 +08:00
|
|
|
return !(__x == __y);
|
|
|
|
}
|
2018-04-05 06:18:03 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
bool operator<(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
|
2017-05-25 12:36:24 +08:00
|
|
|
return less<void*>()(__x.address(), __y.address());
|
|
|
|
}
|
2018-04-05 06:18:03 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
bool operator>(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
|
2017-05-25 12:36:24 +08:00
|
|
|
return __y < __x;
|
|
|
|
}
|
2018-04-05 06:18:03 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
bool operator<=(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
|
2017-05-25 12:36:24 +08:00
|
|
|
return !(__x > __y);
|
|
|
|
}
|
2018-04-05 06:18:03 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
bool operator>=(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
|
2017-05-25 12:36:24 +08:00
|
|
|
return !(__x < __y);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename _Promise>
|
|
|
|
class _LIBCPP_TEMPLATE_VIS coroutine_handle : public coroutine_handle<> {
|
|
|
|
using _Base = coroutine_handle<>;
|
|
|
|
public:
|
2017-05-26 11:02:54 +08:00
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
2017-05-25 12:36:24 +08:00
|
|
|
// 18.11.2.1 construct/reset
|
|
|
|
using coroutine_handle<>::coroutine_handle;
|
2017-05-26 11:02:54 +08:00
|
|
|
#else
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY coroutine_handle() _NOEXCEPT : _Base() {}
|
|
|
|
_LIBCPP_INLINE_VISIBILITY coroutine_handle(nullptr_t) _NOEXCEPT : _Base(nullptr) {}
|
2017-05-26 11:02:54 +08:00
|
|
|
#endif
|
2017-05-25 12:36:24 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
coroutine_handle& operator=(nullptr_t) _NOEXCEPT {
|
2017-05-25 12:36:24 +08:00
|
|
|
_Base::operator=(nullptr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 02:52:34 +08:00
|
|
|
_Promise& promise() const {
|
2018-04-05 06:18:03 +08:00
|
|
|
return *static_cast<_Promise*>(
|
2019-01-16 09:51:12 +08:00
|
|
|
__builtin_coro_promise(this->__handle_, _LIBCPP_ALIGNOF(_Promise), false));
|
2017-05-25 12:36:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
static coroutine_handle from_address(void* __addr) _NOEXCEPT {
|
2017-05-25 12:36:24 +08:00
|
|
|
coroutine_handle __tmp;
|
|
|
|
__tmp.__handle_ = __addr;
|
|
|
|
return __tmp;
|
|
|
|
}
|
|
|
|
|
2017-05-29 14:42:01 +08:00
|
|
|
// 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?
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-29 14:42:01 +08:00
|
|
|
static coroutine_handle from_address(nullptr_t) _NOEXCEPT {
|
2017-05-30 03:46:16 +08:00
|
|
|
return coroutine_handle(nullptr);
|
2017-05-29 14:42:01 +08:00
|
|
|
}
|
|
|
|
|
2017-05-30 03:24:25 +08:00
|
|
|
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");
|
|
|
|
}
|
2017-05-29 13:00:24 +08:00
|
|
|
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
static coroutine_handle from_promise(_Promise& __promise) _NOEXCEPT {
|
2017-06-16 08:36:17 +08:00
|
|
|
typedef typename remove_cv<_Promise>::type _RawPromise;
|
2017-05-25 12:36:24 +08:00
|
|
|
coroutine_handle __tmp;
|
2017-06-16 08:36:17 +08:00
|
|
|
__tmp.__handle_ = __builtin_coro_promise(
|
|
|
|
_VSTD::addressof(const_cast<_RawPromise&>(__promise)),
|
2019-01-16 09:51:12 +08:00
|
|
|
_LIBCPP_ALIGNOF(_Promise), true);
|
2017-05-25 12:36:24 +08:00
|
|
|
return __tmp;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-05 06:18:03 +08:00
|
|
|
#if __has_builtin(__builtin_coro_noop)
|
|
|
|
struct noop_coroutine_promise {};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class _LIBCPP_TEMPLATE_VIS coroutine_handle<noop_coroutine_promise>
|
|
|
|
: public coroutine_handle<> {
|
|
|
|
using _Base = coroutine_handle<>;
|
|
|
|
using _Promise = noop_coroutine_promise;
|
|
|
|
public:
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
_Promise& promise() const {
|
|
|
|
return *static_cast<_Promise*>(
|
2019-01-16 09:51:12 +08:00
|
|
|
__builtin_coro_promise(this->__handle_, _LIBCPP_ALIGNOF(_Promise), false));
|
2018-04-05 06:18:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_CONSTEXPR explicit operator bool() const _NOEXCEPT { return true; }
|
|
|
|
_LIBCPP_CONSTEXPR bool done() const _NOEXCEPT { return false; }
|
|
|
|
|
2018-04-05 08:18:37 +08:00
|
|
|
_LIBCPP_CONSTEXPR_AFTER_CXX17 void operator()() const _NOEXCEPT {}
|
|
|
|
_LIBCPP_CONSTEXPR_AFTER_CXX17 void resume() const _NOEXCEPT {}
|
|
|
|
_LIBCPP_CONSTEXPR_AFTER_CXX17 void destroy() const _NOEXCEPT {}
|
2018-04-05 06:18:03 +08:00
|
|
|
|
|
|
|
private:
|
2018-07-11 01:38:30 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2018-04-05 06:18:03 +08:00
|
|
|
friend coroutine_handle<noop_coroutine_promise> noop_coroutine() _NOEXCEPT;
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY coroutine_handle() _NOEXCEPT {
|
|
|
|
this->__handle_ = __builtin_coro_noop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using noop_coroutine_handle = coroutine_handle<noop_coroutine_promise>;
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
|
|
|
noop_coroutine_handle noop_coroutine() _NOEXCEPT {
|
2018-04-05 06:51:57 +08:00
|
|
|
return noop_coroutine_handle();
|
2018-04-05 06:18:03 +08:00
|
|
|
}
|
|
|
|
#endif // __has_builtin(__builtin_coro_noop)
|
|
|
|
|
2019-04-17 12:31:46 +08:00
|
|
|
struct suspend_never {
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
bool await_ready() const _NOEXCEPT { return true; }
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
void await_suspend(coroutine_handle<>) const _NOEXCEPT {}
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
void await_resume() const _NOEXCEPT {}
|
2017-05-25 12:36:24 +08:00
|
|
|
};
|
|
|
|
|
2019-04-17 12:31:46 +08:00
|
|
|
struct suspend_always {
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
bool await_ready() const _NOEXCEPT { return false; }
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
void await_suspend(coroutine_handle<>) const _NOEXCEPT {}
|
2018-04-05 06:18:03 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
void await_resume() const _NOEXCEPT {}
|
2017-05-25 12:36:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_EXPERIMENTAL_COROUTINES
|
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
struct hash<_VSTD_CORO::coroutine_handle<_Tp> > {
|
|
|
|
using __arg_type = _VSTD_CORO::coroutine_handle<_Tp>;
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-05-26 11:02:54 +08:00
|
|
|
size_t operator()(__arg_type const& __v) const _NOEXCEPT
|
|
|
|
{return hash<void*>()(__v.address());}
|
2017-05-25 12:36:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2021-11-16 14:05:34 +08:00
|
|
|
#endif // !defined(_LIBCPP_HAS_NO_EXPERIMENTAL_COROUTINES)
|
2017-05-26 09:52:59 +08:00
|
|
|
|
2017-05-25 12:36:24 +08:00
|
|
|
#endif /* _LIBCPP_EXPERIMENTAL_COROUTINE */
|