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