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