145
|
1 // <coroutine> -*- C++ -*-
|
|
2
|
|
3 // Copyright (C) 2019-2020 Free Software Foundation, Inc.
|
|
4 //
|
|
5 // This file is part of the GNU ISO C++ Library. This library is free
|
|
6 // software; you can redistribute it and/or modify it under the
|
|
7 // terms of the GNU General Public License as published by the
|
|
8 // Free Software Foundation; either version 3, or (at your option)
|
|
9 // any later version.
|
|
10
|
|
11 // This library is distributed in the hope that it will be useful,
|
|
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 // GNU General Public License for more details.
|
|
15
|
|
16 // Under Section 7 of GPL version 3, you are granted additional
|
|
17 // permissions described in the GCC Runtime Library Exception, version
|
|
18 // 3.1, as published by the Free Software Foundation.
|
|
19
|
|
20 // You should have received a copy of the GNU General Public License and
|
|
21 // a copy of the GCC Runtime Library Exception along with this program;
|
|
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
23 // <http://www.gnu.org/licenses/>.
|
|
24
|
|
25 /** @file include/coroutine
|
|
26 * This is a Standard C++ Library header.
|
|
27 */
|
|
28
|
|
29 #ifndef _GLIBCXX_COROUTINE
|
|
30 #define _GLIBCXX_COROUTINE 1
|
|
31
|
|
32 #pragma GCC system_header
|
|
33
|
|
34 // It is very likely that earlier versions would work, but they are untested.
|
|
35 #if __cplusplus >= 201402L
|
|
36
|
|
37 #include <bits/c++config.h>
|
|
38
|
|
39 /**
|
|
40 * @defgroup coroutines Coroutines
|
|
41 *
|
|
42 * Components for supporting coroutine implementations.
|
|
43 */
|
|
44
|
|
45 #if __cplusplus > 201703L && __cpp_impl_three_way_comparison >= 201907L
|
|
46 # include <compare>
|
|
47 # define _COROUTINES_USE_SPACESHIP 1
|
|
48 #else
|
|
49 # include <bits/stl_function.h> // for std::less
|
|
50 # define _COROUTINES_USE_SPACESHIP 0
|
|
51 #endif
|
|
52
|
|
53 namespace std _GLIBCXX_VISIBILITY (default)
|
|
54 {
|
|
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
|
|
56
|
|
57 #if __cpp_coroutines
|
|
58 inline namespace __n4835 {
|
|
59
|
|
60 // 17.12.2 coroutine traits
|
|
61 /// [coroutine.traits]
|
|
62 /// [coroutine.traits.primary]
|
|
63 template <typename _Result, typename...>
|
|
64 struct coroutine_traits
|
|
65 {
|
|
66 using promise_type = typename _Result::promise_type;
|
|
67 };
|
|
68
|
|
69 // 17.12.3 Class template coroutine_handle
|
|
70 /// [coroutine.handle]
|
|
71 template <typename _Promise = void>
|
|
72 struct coroutine_handle;
|
|
73
|
|
74 template <> struct
|
|
75 coroutine_handle<void>
|
|
76 {
|
|
77 public:
|
|
78 // 17.12.3.1, construct/reset
|
|
79 constexpr coroutine_handle() noexcept : _M_fr_ptr(0) {}
|
|
80
|
|
81 constexpr coroutine_handle(std::nullptr_t __h) noexcept
|
|
82 : _M_fr_ptr(__h)
|
|
83 {}
|
|
84
|
|
85 coroutine_handle& operator=(std::nullptr_t) noexcept
|
|
86 {
|
|
87 _M_fr_ptr = nullptr;
|
|
88 return *this;
|
|
89 }
|
|
90
|
|
91 public:
|
|
92 // 17.12.3.2, export/import
|
|
93 constexpr void* address() const noexcept { return _M_fr_ptr; }
|
|
94
|
|
95 constexpr static coroutine_handle from_address(void* __a) noexcept
|
|
96 {
|
|
97 coroutine_handle __self;
|
|
98 __self._M_fr_ptr = __a;
|
|
99 return __self;
|
|
100 }
|
|
101
|
|
102 public:
|
|
103 // 17.12.3.3, observers
|
|
104 constexpr explicit operator bool() const noexcept
|
|
105 {
|
|
106 return bool(_M_fr_ptr);
|
|
107 }
|
|
108
|
|
109 bool done() const noexcept { return __builtin_coro_done(_M_fr_ptr); }
|
|
110
|
|
111 // 17.12.3.4, resumption
|
|
112 void operator()() const { resume(); }
|
|
113
|
|
114 void resume() const { __builtin_coro_resume(_M_fr_ptr); }
|
|
115
|
|
116 void destroy() const { __builtin_coro_destroy(_M_fr_ptr); }
|
|
117
|
|
118 protected:
|
|
119 void* _M_fr_ptr;
|
|
120 };
|
|
121
|
|
122 // 17.12.3.6 Comparison operators
|
|
123 /// [coroutine.handle.compare]
|
|
124 constexpr bool operator==(coroutine_handle<> __a,
|
|
125 coroutine_handle<> __b) noexcept
|
|
126 {
|
|
127 return __a.address() == __b.address();
|
|
128 }
|
|
129
|
|
130 #if _COROUTINES_USE_SPACESHIP
|
|
131 constexpr strong_ordering
|
|
132 operator<=>(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
|
|
133 { return std::compare_three_way()(__a.address(), __b.address()); }
|
|
134 #else
|
|
135 // These are to enable operation with std=c++14,17.
|
|
136 constexpr bool operator!=(coroutine_handle<> __a,
|
|
137 coroutine_handle<> __b) noexcept
|
|
138 {
|
|
139 return !(__a == __b);
|
|
140 }
|
|
141
|
|
142 constexpr bool operator<(coroutine_handle<> __a,
|
|
143 coroutine_handle<> __b) noexcept
|
|
144 {
|
|
145 return less<void*>()(__a.address(), __b.address());
|
|
146 }
|
|
147
|
|
148 constexpr bool operator>(coroutine_handle<> __a,
|
|
149 coroutine_handle<> __b) noexcept
|
|
150 {
|
|
151 return __b < __a;
|
|
152 }
|
|
153
|
|
154 constexpr bool operator<=(coroutine_handle<> __a,
|
|
155 coroutine_handle<> __b) noexcept
|
|
156 {
|
|
157 return !(__a > __b);
|
|
158 }
|
|
159
|
|
160 constexpr bool operator>=(coroutine_handle<> __a,
|
|
161 coroutine_handle<> __b) noexcept
|
|
162 {
|
|
163 return !(__a < __b);
|
|
164 }
|
|
165 #endif
|
|
166
|
|
167 template <typename _Promise>
|
|
168 struct coroutine_handle : coroutine_handle<>
|
|
169 {
|
|
170 // 17.12.3.1, construct/reset
|
|
171 using coroutine_handle<>::coroutine_handle;
|
|
172
|
|
173 static coroutine_handle from_promise(_Promise& p)
|
|
174 {
|
|
175 coroutine_handle __self;
|
|
176 __self._M_fr_ptr
|
|
177 = __builtin_coro_promise((char*) &p, __alignof(_Promise), true);
|
|
178 return __self;
|
|
179 }
|
|
180
|
|
181 coroutine_handle& operator=(std::nullptr_t) noexcept
|
|
182 {
|
|
183 coroutine_handle<>::operator=(nullptr);
|
|
184 return *this;
|
|
185 }
|
|
186
|
|
187 // 17.12.3.2, export/import
|
|
188 constexpr static coroutine_handle from_address(void* __a)
|
|
189 {
|
|
190 coroutine_handle __self;
|
|
191 __self._M_fr_ptr = __a;
|
|
192 return __self;
|
|
193 }
|
|
194
|
|
195 // 17.12.3.5, promise accesss
|
|
196 _Promise& promise() const
|
|
197 {
|
|
198 void* __t
|
|
199 = __builtin_coro_promise (this->_M_fr_ptr, __alignof(_Promise), false);
|
|
200 return *static_cast<_Promise*>(__t);
|
|
201 }
|
|
202 };
|
|
203
|
|
204 /// [coroutine.noop]
|
|
205 struct noop_coroutine_promise
|
|
206 {
|
|
207 };
|
|
208
|
|
209 void __dummy_resume_destroy() __attribute__((__weak__));
|
|
210 void __dummy_resume_destroy() {}
|
|
211
|
|
212 struct __noop_coro_frame
|
|
213 {
|
|
214 void (*__r)() = __dummy_resume_destroy;
|
|
215 void (*__d)() = __dummy_resume_destroy;
|
|
216 struct noop_coroutine_promise __p;
|
|
217 } __noop_coro_fr __attribute__((__weak__));
|
|
218
|
|
219 // 17.12.4.1 Class noop_coroutine_promise
|
|
220 /// [coroutine.promise.noop]
|
|
221 template <>
|
|
222 struct coroutine_handle<noop_coroutine_promise> : public coroutine_handle<>
|
|
223 {
|
|
224 using _Promise = noop_coroutine_promise;
|
|
225
|
|
226 public:
|
|
227 // 17.12.4.2.1, observers
|
|
228 constexpr explicit operator bool() const noexcept { return true; }
|
|
229
|
|
230 constexpr bool done() const noexcept { return false; }
|
|
231
|
|
232 // 17.12.4.2.2, resumption
|
|
233 void operator()() const noexcept {}
|
|
234
|
|
235 void resume() const noexcept {}
|
|
236
|
|
237 void destroy() const noexcept {}
|
|
238
|
|
239 // 17.12.4.2.3, promise access
|
|
240 _Promise& promise() const
|
|
241 {
|
|
242 return *static_cast<_Promise*>(
|
|
243 __builtin_coro_promise(this->_M_fr_ptr, __alignof(_Promise), false));
|
|
244 }
|
|
245
|
|
246 // 17.12.4.2.4, address
|
|
247 private:
|
|
248 friend coroutine_handle<noop_coroutine_promise> noop_coroutine() noexcept;
|
|
249
|
|
250 coroutine_handle() noexcept { this->_M_fr_ptr = (void*) &__noop_coro_fr; }
|
|
251 };
|
|
252
|
|
253 using noop_coroutine_handle = coroutine_handle<noop_coroutine_promise>;
|
|
254
|
|
255 inline noop_coroutine_handle noop_coroutine() noexcept
|
|
256 {
|
|
257 return noop_coroutine_handle();
|
|
258 }
|
|
259
|
|
260 // 17.12.5 Trivial awaitables
|
|
261 /// [coroutine.trivial.awaitables]
|
|
262 struct suspend_always
|
|
263 {
|
|
264 bool await_ready() { return false; }
|
|
265
|
|
266 void await_suspend(coroutine_handle<>) {}
|
|
267
|
|
268 void await_resume() {}
|
|
269 };
|
|
270
|
|
271 struct suspend_never
|
|
272 {
|
|
273 bool await_ready() { return true; }
|
|
274
|
|
275 void await_suspend(coroutine_handle<>) {}
|
|
276
|
|
277 void await_resume() {}
|
|
278 };
|
|
279
|
|
280 } // namespace __n4835
|
|
281
|
|
282 #else
|
|
283 #error "the coroutine header requires -fcoroutines"
|
|
284 #endif
|
|
285
|
|
286 _GLIBCXX_END_NAMESPACE_VERSION
|
|
287 } // namespace std
|
|
288
|
|
289 #endif // C++14 (we are allowing use from at least this)
|
|
290
|
|
291 #endif // _GLIBCXX_COROUTINE
|