forked from OSchip/llvm-project
[coroutines] Add noop_coroutine to <experimental/coroutine>
A recent addition to Coroutines TS (https://wg21.link/p0913) adds a pre-defined coroutine noop_coroutine that does nothing. This patch implements require library types in <experimental/coroutine> Related clang and llvm patches: https://reviews.llvm.org/D45114 https://reviews.llvm.org/D45120 llvm-svn: 329237
This commit is contained in:
parent
f11eb3ebe7
commit
0f87a80795
|
@ -93,28 +93,28 @@ class _LIBCPP_TEMPLATE_VIS coroutine_handle;
|
|||
template <>
|
||||
class _LIBCPP_TEMPLATE_VIS coroutine_handle<void> {
|
||||
public:
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
_LIBCPP_CONSTEXPR coroutine_handle() _NOEXCEPT : __handle_(nullptr) {}
|
||||
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
_LIBCPP_CONSTEXPR coroutine_handle(nullptr_t) _NOEXCEPT : __handle_(nullptr) {}
|
||||
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
coroutine_handle& operator=(nullptr_t) _NOEXCEPT {
|
||||
__handle_ = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
_LIBCPP_CONSTEXPR void* address() const _NOEXCEPT { return __handle_; }
|
||||
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
_LIBCPP_CONSTEXPR explicit operator bool() const _NOEXCEPT { return __handle_; }
|
||||
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
void operator()() { resume(); }
|
||||
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
void resume() {
|
||||
_LIBCPP_ASSERT(__is_suspended(),
|
||||
"resume() can only be called on suspended coroutines");
|
||||
|
@ -123,14 +123,14 @@ public:
|
|||
__builtin_coro_resume(__handle_);
|
||||
}
|
||||
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
void destroy() {
|
||||
_LIBCPP_ASSERT(__is_suspended(),
|
||||
"destroy() can only be called on suspended coroutines");
|
||||
__builtin_coro_destroy(__handle_);
|
||||
}
|
||||
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
bool done() const {
|
||||
_LIBCPP_ASSERT(__is_suspended(),
|
||||
"done() can only be called on suspended coroutines");
|
||||
|
@ -138,7 +138,7 @@ public:
|
|||
}
|
||||
|
||||
public:
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
static coroutine_handle from_address(void* __addr) _NOEXCEPT {
|
||||
coroutine_handle __tmp;
|
||||
__tmp.__handle_ = __addr;
|
||||
|
@ -146,7 +146,7 @@ public:
|
|||
}
|
||||
|
||||
// FIXME: Should from_address(nullptr) be allowed?
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
static coroutine_handle from_address(nullptr_t) _NOEXCEPT {
|
||||
return coroutine_handle(nullptr);
|
||||
}
|
||||
|
@ -169,27 +169,27 @@ private:
|
|||
};
|
||||
|
||||
// 18.11.2.7 comparison operators:
|
||||
inline _LIBCPP_ALWAYS_INLINE
|
||||
inline _LIBCPP_INLINE_VISIBILITY
|
||||
bool operator==(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
|
||||
return __x.address() == __y.address();
|
||||
}
|
||||
inline _LIBCPP_ALWAYS_INLINE
|
||||
inline _LIBCPP_INLINE_VISIBILITY
|
||||
bool operator!=(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
|
||||
return !(__x == __y);
|
||||
}
|
||||
inline _LIBCPP_ALWAYS_INLINE
|
||||
inline _LIBCPP_INLINE_VISIBILITY
|
||||
bool operator<(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
|
||||
return less<void*>()(__x.address(), __y.address());
|
||||
}
|
||||
inline _LIBCPP_ALWAYS_INLINE
|
||||
inline _LIBCPP_INLINE_VISIBILITY
|
||||
bool operator>(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
|
||||
return __y < __x;
|
||||
}
|
||||
inline _LIBCPP_ALWAYS_INLINE
|
||||
inline _LIBCPP_INLINE_VISIBILITY
|
||||
bool operator<=(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
|
||||
return !(__x > __y);
|
||||
}
|
||||
inline _LIBCPP_ALWAYS_INLINE
|
||||
inline _LIBCPP_INLINE_VISIBILITY
|
||||
bool operator>=(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
|
||||
return !(__x < __y);
|
||||
}
|
||||
|
@ -202,8 +202,8 @@ public:
|
|||
// 18.11.2.1 construct/reset
|
||||
using coroutine_handle<>::coroutine_handle;
|
||||
#else
|
||||
_LIBCPP_ALWAYS_INLINE coroutine_handle() _NOEXCEPT : _Base() {}
|
||||
_LIBCPP_ALWAYS_INLINE coroutine_handle(nullptr_t) _NOEXCEPT : _Base(nullptr) {}
|
||||
_LIBCPP_INLINE_VISIBILITY coroutine_handle() _NOEXCEPT : _Base() {}
|
||||
_LIBCPP_INLINE_VISIBILITY coroutine_handle(nullptr_t) _NOEXCEPT : _Base(nullptr) {}
|
||||
#endif
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
coroutine_handle& operator=(nullptr_t) _NOEXCEPT {
|
||||
|
@ -213,12 +213,12 @@ public:
|
|||
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
_Promise& promise() const {
|
||||
return *reinterpret_cast<_Promise*>(
|
||||
return *static_cast<_Promise*>(
|
||||
__builtin_coro_promise(this->__handle_, __alignof(_Promise), false));
|
||||
}
|
||||
|
||||
public:
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
static coroutine_handle from_address(void* __addr) _NOEXCEPT {
|
||||
coroutine_handle __tmp;
|
||||
__tmp.__handle_ = __addr;
|
||||
|
@ -229,7 +229,7 @@ public:
|
|||
// the deleted _Promise* overload doesn't make from_address(nullptr)
|
||||
// ambiguous.
|
||||
// FIXME: should from_address work with nullptr?
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
static coroutine_handle from_address(nullptr_t) _NOEXCEPT {
|
||||
return coroutine_handle(nullptr);
|
||||
}
|
||||
|
@ -248,7 +248,7 @@ public:
|
|||
"pointers to the coroutine's promise type; use 'from_promise' instead");
|
||||
}
|
||||
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
static coroutine_handle from_promise(_Promise& __promise) _NOEXCEPT {
|
||||
typedef typename remove_cv<_Promise>::type _RawPromise;
|
||||
coroutine_handle __tmp;
|
||||
|
@ -259,21 +259,60 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
#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*>(
|
||||
__builtin_coro_promise(this->__handle_, __alignof(_Promise), false));
|
||||
}
|
||||
|
||||
_LIBCPP_CONSTEXPR explicit operator bool() const _NOEXCEPT { return true; }
|
||||
_LIBCPP_CONSTEXPR bool done() const _NOEXCEPT { return false; }
|
||||
|
||||
_LIBCPP_CONSTEXPR void operator()() const _NOEXCEPT {}
|
||||
_LIBCPP_CONSTEXPR void resume() const _NOEXCEPT {}
|
||||
_LIBCPP_CONSTEXPR void destroy() const _NOEXCEPT {}
|
||||
|
||||
private:
|
||||
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 {
|
||||
return {};
|
||||
}
|
||||
#endif // __has_builtin(__builtin_coro_noop)
|
||||
|
||||
struct _LIBCPP_TYPE_VIS suspend_never {
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
bool await_ready() const _NOEXCEPT { return true; }
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
void await_suspend(coroutine_handle<>) const _NOEXCEPT {}
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
void await_resume() const _NOEXCEPT {}
|
||||
};
|
||||
|
||||
struct _LIBCPP_TYPE_VIS suspend_always {
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
bool await_ready() const _NOEXCEPT { return false; }
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
void await_suspend(coroutine_handle<>) const _NOEXCEPT {}
|
||||
_LIBCPP_ALWAYS_INLINE
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
void await_resume() const _NOEXCEPT {}
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,66 @@
|
|||
// -*- C++ -*-
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||
// Source Licenses. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// UNSUPPORTED: c++98, c++03, c++11
|
||||
// XFAIL: clang-5, clang-6
|
||||
// <experimental/coroutine>
|
||||
// struct noop_coroutine_promise;
|
||||
// using noop_coroutine_handle = coroutine_handle<noop_coroutine_promise>;
|
||||
// noop_coroutine_handle noop_coroutine() noexcept;
|
||||
|
||||
#include <experimental/coroutine>
|
||||
#include <cassert>
|
||||
#include <type_traits>
|
||||
|
||||
namespace coro = std::experimental::coroutines_v1;
|
||||
|
||||
static_assert(std::is_same<coro::coroutine_handle<coro::noop_coroutine_promise>, coro::noop_coroutine_handle>::value, "");
|
||||
static_assert(std::is_same<decltype(coro::noop_coroutine()), coro::noop_coroutine_handle>::value, "");
|
||||
|
||||
// template <> struct coroutine_handle<noop_coroutine_promise> : coroutine_handle<>
|
||||
// {
|
||||
// // 18.11.2.7 noop observers
|
||||
// constexpr explicit operator bool() const noexcept;
|
||||
// constexpr bool done() const noexcept;
|
||||
|
||||
// // 18.11.2.8 noop resumption
|
||||
// constexpr void operator()() const noexcept;
|
||||
// constexpr void resume() const noexcept;
|
||||
// constexpr void destroy() const noexcept;
|
||||
|
||||
// // 18.11.2.9 noop promise access
|
||||
// noop_coroutine_promise& promise() const noexcept;
|
||||
|
||||
// // 18.11.2.10 noop address
|
||||
// constexpr void* address() const noexcept;
|
||||
|
||||
int main()
|
||||
{
|
||||
auto h = coro::noop_coroutine();
|
||||
coro::coroutine_handle<> base = h;
|
||||
|
||||
assert(h);
|
||||
assert(base);
|
||||
|
||||
assert(!h.done());
|
||||
assert(!base.done());
|
||||
|
||||
h.resume();
|
||||
h.destroy();
|
||||
h();
|
||||
static_assert(h.done() == false, "");
|
||||
static_assert(h, "");
|
||||
|
||||
h.promise();
|
||||
assert(h.address() == base.address());
|
||||
assert(h.address() != nullptr);
|
||||
assert(coro::coroutine_handle<>::from_address(h.address()) == base);
|
||||
}
|
||||
|
Loading…
Reference in New Issue