1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___COROUTINE_COROUTINE_HANDLE_H
10 #define _LIBCPP___COROUTINE_COROUTINE_HANDLE_H
14 #include <__functional/hash.h>
15 #include <__memory/addressof.h>
16 #include <__type_traits/remove_cv.h>
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 # pragma GCC system_header
24 #if _LIBCPP_STD_VER >= 20
26 _LIBCPP_BEGIN_NAMESPACE_STD
29 template <class _Promise
= void>
30 struct _LIBCPP_TEMPLATE_VIS coroutine_handle
;
33 struct _LIBCPP_TEMPLATE_VIS coroutine_handle
<void> {
35 // [coroutine.handle.con], construct/reset
36 constexpr coroutine_handle() noexcept
= default;
39 constexpr coroutine_handle(nullptr_t
) noexcept
{}
42 coroutine_handle
& operator=(nullptr_t
) noexcept
{
47 // [coroutine.handle.export.import], export/import
49 constexpr void* address() const noexcept
{ return __handle_
; }
52 static constexpr coroutine_handle
from_address(void* __addr
) noexcept
{
53 coroutine_handle __tmp
;
54 __tmp
.__handle_
= __addr
;
58 // [coroutine.handle.observers], observers
60 constexpr explicit operator bool() const noexcept
{
61 return __handle_
!= nullptr;
66 _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "done() can be called only on suspended coroutines");
67 return __builtin_coro_done(__handle_
);
70 // [coroutine.handle.resumption], resumption
72 void operator()() const { resume(); }
76 _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "resume() can be called only on suspended coroutines");
77 _LIBCPP_ASSERT_UNCATEGORIZED(!done(), "resume() has undefined behavior when the coroutine is done");
78 __builtin_coro_resume(__handle_
);
82 void destroy() const {
83 _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "destroy() can be called only on suspended coroutines");
84 __builtin_coro_destroy(__handle_
);
88 _LIBCPP_HIDE_FROM_ABI
bool __is_suspended() const {
89 // FIXME actually implement a check for if the coro is suspended.
90 return __handle_
!= nullptr;
93 void* __handle_
= nullptr;
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();
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());
106 template <class _Promise
>
107 struct _LIBCPP_TEMPLATE_VIS coroutine_handle
{
109 // [coroutine.handle.con], construct/reset
110 constexpr coroutine_handle() noexcept
= default;
112 _LIBCPP_HIDE_FROM_ABI
113 constexpr coroutine_handle(nullptr_t
) noexcept
{}
115 _LIBCPP_HIDE_FROM_ABI
116 static coroutine_handle
from_promise(_Promise
& __promise
) {
117 using _RawPromise
= __remove_cv_t
<_Promise
>;
118 coroutine_handle __tmp
;
120 __builtin_coro_promise(_VSTD::addressof(const_cast<_RawPromise
&>(__promise
)), alignof(_Promise
), true);
124 _LIBCPP_HIDE_FROM_ABI
125 coroutine_handle
& operator=(nullptr_t
) noexcept
{
130 // [coroutine.handle.export.import], export/import
131 _LIBCPP_HIDE_FROM_ABI
132 constexpr void* address() const noexcept
{ return __handle_
; }
134 _LIBCPP_HIDE_FROM_ABI
135 static constexpr coroutine_handle
from_address(void* __addr
) noexcept
{
136 coroutine_handle __tmp
;
137 __tmp
.__handle_
= __addr
;
141 // [coroutine.handle.conv], conversion
142 _LIBCPP_HIDE_FROM_ABI
143 constexpr operator coroutine_handle
<>() const noexcept
{
144 return coroutine_handle
<>::from_address(address());
147 // [coroutine.handle.observers], observers
148 _LIBCPP_HIDE_FROM_ABI
149 constexpr explicit operator bool() const noexcept
{
150 return __handle_
!= nullptr;
153 _LIBCPP_HIDE_FROM_ABI
155 _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "done() can be called only on suspended coroutines");
156 return __builtin_coro_done(__handle_
);
159 // [coroutine.handle.resumption], resumption
160 _LIBCPP_HIDE_FROM_ABI
161 void operator()() const { resume(); }
163 _LIBCPP_HIDE_FROM_ABI
164 void resume() const {
165 _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "resume() can be called only on suspended coroutines");
166 _LIBCPP_ASSERT_UNCATEGORIZED(!done(), "resume() has undefined behavior when the coroutine is done");
167 __builtin_coro_resume(__handle_
);
170 _LIBCPP_HIDE_FROM_ABI
171 void destroy() const {
172 _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "destroy() can be called only on suspended coroutines");
173 __builtin_coro_destroy(__handle_
);
176 // [coroutine.handle.promise], promise access
177 _LIBCPP_HIDE_FROM_ABI
178 _Promise
& promise() const {
179 return *static_cast<_Promise
*>(__builtin_coro_promise(this->__handle_
, alignof(_Promise
), false));
183 _LIBCPP_HIDE_FROM_ABI
bool __is_suspended() const {
184 // FIXME actually implement a check for if the coro is suspended.
185 return __handle_
!= nullptr;
187 void* __handle_
= nullptr;
190 // [coroutine.handle.hash]
192 struct hash
<coroutine_handle
<_Tp
>> {
193 _LIBCPP_HIDE_FROM_ABI
194 size_t operator()(const coroutine_handle
<_Tp
>& __v
) const noexcept
{ return hash
<void*>()(__v
.address()); }
197 _LIBCPP_END_NAMESPACE_STD
199 #endif // __LIBCPP_STD_VER >= 20
201 #endif // _LIBCPP___COROUTINE_COROUTINE_HANDLE_H