1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
//===----------------------------------------------------------------------===//
//
// 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 _LIBCPP___COROUTINE_COROUTINE_HANDLE_H
#define _LIBCPP___COROUTINE_COROUTINE_HANDLE_H
#include <__assert>
#include <__config>
#include <__functional/hash.h>
#include <__memory/addressof.h>
#include <compare>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CXX20_COROUTINES)
_LIBCPP_BEGIN_NAMESPACE_STD
// [coroutine.handle]
template <class _Promise = void>
struct _LIBCPP_TEMPLATE_VIS coroutine_handle;
template <>
struct _LIBCPP_TEMPLATE_VIS coroutine_handle<void> {
public:
// [coroutine.handle.con], construct/reset
_LIBCPP_HIDE_FROM_ABI
constexpr coroutine_handle() noexcept = default;
_LIBCPP_HIDE_FROM_ABI
constexpr coroutine_handle(nullptr_t) noexcept {}
_LIBCPP_HIDE_FROM_ABI
coroutine_handle& operator=(nullptr_t) noexcept {
__handle_ = nullptr;
return *this;
}
// [coroutine.handle.export.import], export/import
_LIBCPP_HIDE_FROM_ABI
constexpr void* address() const noexcept { return __handle_; }
_LIBCPP_HIDE_FROM_ABI
static constexpr coroutine_handle from_address(void* __addr) noexcept {
coroutine_handle __tmp;
__tmp.__handle_ = __addr;
return __tmp;
}
// [coroutine.handle.observers], observers
_LIBCPP_HIDE_FROM_ABI
constexpr explicit operator bool() const noexcept {
return __handle_ != nullptr;
}
_LIBCPP_HIDE_FROM_ABI
bool done() const {
_LIBCPP_ASSERT(__is_suspended(), "done() can be called only on suspended coroutines");
return __builtin_coro_done(__handle_);
}
// [coroutine.handle.resumption], resumption
_LIBCPP_HIDE_FROM_ABI
void operator()() const { resume(); }
_LIBCPP_HIDE_FROM_ABI
void resume() const {
_LIBCPP_ASSERT(__is_suspended(), "resume() can be called only on suspended coroutines");
_LIBCPP_ASSERT(!done(), "resume() has undefined behavior when the coroutine is done");
__builtin_coro_resume(__handle_);
}
_LIBCPP_HIDE_FROM_ABI
void destroy() const {
_LIBCPP_ASSERT(__is_suspended(), "destroy() can be called only on suspended coroutines");
__builtin_coro_destroy(__handle_);
}
private:
bool __is_suspended() const {
// FIXME actually implement a check for if the coro is suspended.
return __handle_ != nullptr;
}
void* __handle_ = nullptr;
};
// [coroutine.handle.compare]
inline _LIBCPP_HIDE_FROM_ABI
constexpr bool operator==(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
return __x.address() == __y.address();
}
inline _LIBCPP_HIDE_FROM_ABI
constexpr strong_ordering operator<=>(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
return compare_three_way()(__x.address(), __y.address());
}
template <class _Promise>
struct _LIBCPP_TEMPLATE_VIS coroutine_handle {
public:
// [coroutine.handle.con], construct/reset
_LIBCPP_HIDE_FROM_ABI
constexpr coroutine_handle() noexcept = default;
_LIBCPP_HIDE_FROM_ABI
constexpr coroutine_handle(nullptr_t) noexcept {}
_LIBCPP_HIDE_FROM_ABI
static coroutine_handle from_promise(_Promise& __promise) {
using _RawPromise = typename remove_cv<_Promise>::type;
coroutine_handle __tmp;
__tmp.__handle_ =
__builtin_coro_promise(_VSTD::addressof(const_cast<_RawPromise&>(__promise)), alignof(_Promise), true);
return __tmp;
}
_LIBCPP_HIDE_FROM_ABI
coroutine_handle& operator=(nullptr_t) noexcept {
__handle_ = nullptr;
return *this;
}
// [coroutine.handle.export.import], export/import
_LIBCPP_HIDE_FROM_ABI
constexpr void* address() const noexcept { return __handle_; }
_LIBCPP_HIDE_FROM_ABI
static constexpr coroutine_handle from_address(void* __addr) noexcept {
coroutine_handle __tmp;
__tmp.__handle_ = __addr;
return __tmp;
}
// [coroutine.handle.conv], conversion
_LIBCPP_HIDE_FROM_ABI
constexpr operator coroutine_handle<>() const noexcept {
return coroutine_handle<>::from_address(address());
}
// [coroutine.handle.observers], observers
_LIBCPP_HIDE_FROM_ABI
constexpr explicit operator bool() const noexcept {
return __handle_ != nullptr;
}
_LIBCPP_HIDE_FROM_ABI
bool done() const {
_LIBCPP_ASSERT(__is_suspended(), "done() can be called only on suspended coroutines");
return __builtin_coro_done(__handle_);
}
// [coroutine.handle.resumption], resumption
_LIBCPP_HIDE_FROM_ABI
void operator()() const { resume(); }
_LIBCPP_HIDE_FROM_ABI
void resume() const {
_LIBCPP_ASSERT(__is_suspended(), "resume() can be called only on suspended coroutines");
_LIBCPP_ASSERT(!done(), "resume() has undefined behavior when the coroutine is done");
__builtin_coro_resume(__handle_);
}
_LIBCPP_HIDE_FROM_ABI
void destroy() const {
_LIBCPP_ASSERT(__is_suspended(), "destroy() can be called only on suspended coroutines");
__builtin_coro_destroy(__handle_);
}
// [coroutine.handle.promise], promise access
_LIBCPP_HIDE_FROM_ABI
_Promise& promise() const {
return *static_cast<_Promise*>(__builtin_coro_promise(this->__handle_, alignof(_Promise), false));
}
private:
bool __is_suspended() const {
// FIXME actually implement a check for if the coro is suspended.
return __handle_ != nullptr;
}
void* __handle_ = nullptr;
};
// [coroutine.handle.hash]
template <class _Tp>
struct hash<coroutine_handle<_Tp>> {
_LIBCPP_HIDE_FROM_ABI
size_t operator()(const coroutine_handle<_Tp>& __v) const noexcept { return hash<void*>()(__v.address()); }
};
_LIBCPP_END_NAMESPACE_STD
#endif // __LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_CXX20_COROUTINES)
#endif // _LIBCPP___COROUTINE_COROUTINE_HANDLE_H
|