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 >= 20
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     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_UNCATEGORIZED(__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_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_);
79     }
80 
81     _LIBCPP_HIDE_FROM_ABI
82     void destroy() const {
83         _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "destroy() can be called only on suspended coroutines");
84         __builtin_coro_destroy(__handle_);
85     }
86 
87 private:
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;
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     constexpr coroutine_handle() noexcept = default;
111 
112     _LIBCPP_HIDE_FROM_ABI
113     constexpr coroutine_handle(nullptr_t) noexcept {}
114 
115     _LIBCPP_HIDE_FROM_ABI
116     static coroutine_handle from_promise(_Promise& __promise) {
117         using _RawPromise = __remove_cv_t<_Promise>;
118         coroutine_handle __tmp;
119         __tmp.__handle_ =
120             __builtin_coro_promise(_VSTD::addressof(const_cast<_RawPromise&>(__promise)), alignof(_Promise), true);
121         return __tmp;
122     }
123 
124     _LIBCPP_HIDE_FROM_ABI
125     coroutine_handle& operator=(nullptr_t) noexcept {
126         __handle_ = nullptr;
127         return *this;
128     }
129 
130     // [coroutine.handle.export.import], export/import
131     _LIBCPP_HIDE_FROM_ABI
132     constexpr void* address() const noexcept { return __handle_; }
133 
134     _LIBCPP_HIDE_FROM_ABI
135     static constexpr coroutine_handle from_address(void* __addr) noexcept {
136         coroutine_handle __tmp;
137         __tmp.__handle_ = __addr;
138         return __tmp;
139     }
140 
141     // [coroutine.handle.conv], conversion
142     _LIBCPP_HIDE_FROM_ABI
143     constexpr operator coroutine_handle<>() const noexcept {
144         return coroutine_handle<>::from_address(address());
145     }
146 
147     // [coroutine.handle.observers], observers
148     _LIBCPP_HIDE_FROM_ABI
149     constexpr explicit operator bool() const noexcept {
150         return __handle_ != nullptr;
151     }
152 
153     _LIBCPP_HIDE_FROM_ABI
154     bool done() const {
155         _LIBCPP_ASSERT_UNCATEGORIZED(__is_suspended(), "done() can be called only on suspended coroutines");
156         return __builtin_coro_done(__handle_);
157     }
158 
159     // [coroutine.handle.resumption], resumption
160     _LIBCPP_HIDE_FROM_ABI
161     void operator()() const { resume(); }
162 
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_);
168     }
169 
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_);
174     }
175 
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));
180     }
181 
182 private:
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;
186     }
187     void* __handle_ = nullptr;
188 };
189 
190 // [coroutine.handle.hash]
191 template <class _Tp>
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()); }
195 };
196 
197 _LIBCPP_END_NAMESPACE_STD
198 
199 #endif // __LIBCPP_STD_VER >= 20
200 
201 #endif // _LIBCPP___COROUTINE_COROUTINE_HANDLE_H
202