blob: 7cb39b81b48f77840b04b32338196f73fddf5f7c [file] [log] [blame]
Eric Fiselier3ca91852017-05-25 04:36:24 +00001// -*- C++ -*-
2//===----------------------------- coroutine -----------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_EXPERIMENTAL_COROUTINE
12#define _LIBCPP_EXPERIMENTAL_COROUTINE
13
14/**
15 experimental/coroutine synopsis
16
17// C++next
18
19namespace std {
20namespace experimental {
21inline namespace coroutines_v1 {
22
23 // 18.11.1 coroutine traits
24template <typename R, typename... ArgTypes>
25class coroutine_traits;
26// 18.11.2 coroutine handle
27template <typename Promise = void>
28class coroutine_handle;
29// 18.11.2.7 comparison operators:
Eric Fiselier997a3912017-05-26 03:02:54 +000030bool operator==(coroutine_handle<> x, coroutine_handle<> y) _NOEXCEPT;
31bool operator!=(coroutine_handle<> x, coroutine_handle<> y) _NOEXCEPT;
32bool operator<(coroutine_handle<> x, coroutine_handle<> y) _NOEXCEPT;
33bool operator<=(coroutine_handle<> x, coroutine_handle<> y) _NOEXCEPT;
34bool operator>=(coroutine_handle<> x, coroutine_handle<> y) _NOEXCEPT;
35bool operator>(coroutine_handle<> x, coroutine_handle<> y) _NOEXCEPT;
Eric Fiselier3ca91852017-05-25 04:36:24 +000036// 18.11.3 trivial awaitables
37struct suspend_never;
38struct suspend_always;
39// 18.11.2.8 hash support:
40template <class T> struct hash;
41template <class P> struct hash<coroutine_handle<P>>;
42
43} // namespace coroutines_v1
44} // namespace experimental
45} // namespace std
46
47 */
48
49#include <experimental/__config>
50#include <new>
51#include <type_traits>
52#include <functional>
53#include <memory> // for hash<T*>
54#include <cstddef>
55#include <cassert>
56#include <__debug>
57
58#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
59#pragma GCC system_header
60#endif
61
Eric Fiseliereb04c8c2017-05-26 01:52:59 +000062#ifdef _LIBCPP_HAS_NO_COROUTINES
Eric Fiselier3ca91852017-05-25 04:36:24 +000063# if defined(_LIBCPP_WARNING)
64 _LIBCPP_WARNING("<experimental/coroutine> cannot be used with this compiler")
65# else
66# warning <experimental/coroutine> cannot be used with this compiler
67# endif
68#endif
69
Eric Fiseliereb04c8c2017-05-26 01:52:59 +000070#ifndef _LIBCPP_HAS_NO_COROUTINES
71
Eric Fiselier3ca91852017-05-25 04:36:24 +000072_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_COROUTINES
73
74template <class _Tp, class = void>
75struct __coroutine_traits_sfinae {};
76
77template <class _Tp>
78struct __coroutine_traits_sfinae<
79 _Tp, typename __void_t<typename _Tp::promise_type>::type>
80{
81 using promise_type = typename _Tp::promise_type;
82};
83
84template <typename _Ret, typename... _Args>
85struct _LIBCPP_TEMPLATE_VIS coroutine_traits
86 : public __coroutine_traits_sfinae<_Ret>
87{
88};
89
Eric Fiselierea968912017-05-25 23:39:29 +000090template <typename _Promise = void>
Eric Fiselier3ca91852017-05-25 04:36:24 +000091class _LIBCPP_TEMPLATE_VIS coroutine_handle;
92
Eric Fiselier3ca91852017-05-25 04:36:24 +000093template <>
94class _LIBCPP_TEMPLATE_VIS coroutine_handle<void> {
95public:
Gor Nishanov0f87a802018-04-04 22:18:03 +000096 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +000097 _LIBCPP_CONSTEXPR coroutine_handle() _NOEXCEPT : __handle_(nullptr) {}
Eric Fiselier3ca91852017-05-25 04:36:24 +000098
Gor Nishanov0f87a802018-04-04 22:18:03 +000099 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000100 _LIBCPP_CONSTEXPR coroutine_handle(nullptr_t) _NOEXCEPT : __handle_(nullptr) {}
Eric Fiselier3ca91852017-05-25 04:36:24 +0000101
Gor Nishanov0f87a802018-04-04 22:18:03 +0000102 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000103 coroutine_handle& operator=(nullptr_t) _NOEXCEPT {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000104 __handle_ = nullptr;
105 return *this;
106 }
107
Gor Nishanov0f87a802018-04-04 22:18:03 +0000108 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000109 _LIBCPP_CONSTEXPR void* address() const _NOEXCEPT { return __handle_; }
Eric Fiselier3ca91852017-05-25 04:36:24 +0000110
Gor Nishanov0f87a802018-04-04 22:18:03 +0000111 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000112 _LIBCPP_CONSTEXPR explicit operator bool() const _NOEXCEPT { return __handle_; }
Eric Fiselier3ca91852017-05-25 04:36:24 +0000113
Gor Nishanov0f87a802018-04-04 22:18:03 +0000114 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier51056ae2017-05-25 19:04:55 +0000115 void operator()() { resume(); }
Eric Fiselier3ca91852017-05-25 04:36:24 +0000116
Gor Nishanov0f87a802018-04-04 22:18:03 +0000117 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier51056ae2017-05-25 19:04:55 +0000118 void resume() {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000119 _LIBCPP_ASSERT(__is_suspended(),
120 "resume() can only be called on suspended coroutines");
121 _LIBCPP_ASSERT(!done(),
122 "resume() has undefined behavior when the coroutine is done");
123 __builtin_coro_resume(__handle_);
124 }
125
Gor Nishanov0f87a802018-04-04 22:18:03 +0000126 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier51056ae2017-05-25 19:04:55 +0000127 void destroy() {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000128 _LIBCPP_ASSERT(__is_suspended(),
129 "destroy() can only be called on suspended coroutines");
130 __builtin_coro_destroy(__handle_);
131 }
132
Gor Nishanov0f87a802018-04-04 22:18:03 +0000133 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3ca91852017-05-25 04:36:24 +0000134 bool done() const {
135 _LIBCPP_ASSERT(__is_suspended(),
136 "done() can only be called on suspended coroutines");
137 return __builtin_coro_done(__handle_);
138 }
139
140public:
Gor Nishanov0f87a802018-04-04 22:18:03 +0000141 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000142 static coroutine_handle from_address(void* __addr) _NOEXCEPT {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000143 coroutine_handle __tmp;
144 __tmp.__handle_ = __addr;
145 return __tmp;
146 }
147
Eric Fiselier3fd02282017-05-29 19:24:25 +0000148 // FIXME: Should from_address(nullptr) be allowed?
Gor Nishanov0f87a802018-04-04 22:18:03 +0000149 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier3fd02282017-05-29 19:24:25 +0000150 static coroutine_handle from_address(nullptr_t) _NOEXCEPT {
Eric Fiselier6e88ac22017-05-29 19:46:16 +0000151 return coroutine_handle(nullptr);
Eric Fiselier3fd02282017-05-29 19:24:25 +0000152 }
153
154 template <class _Tp, bool _CallIsValid = false>
155 static coroutine_handle from_address(_Tp*) {
156 static_assert(_CallIsValid,
157 "coroutine_handle<void>::from_address cannot be called with "
158 "non-void pointers");
159 }
160
Eric Fiselier3ca91852017-05-25 04:36:24 +0000161private:
Eric Fiselier997a3912017-05-26 03:02:54 +0000162 bool __is_suspended() const _NOEXCEPT {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000163 // FIXME actually implement a check for if the coro is suspended.
164 return __handle_;
165 }
166
167 template <class _PromiseT> friend class coroutine_handle;
168 void* __handle_;
169};
170
171// 18.11.2.7 comparison operators:
Gor Nishanov0f87a802018-04-04 22:18:03 +0000172inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000173bool operator==(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000174 return __x.address() == __y.address();
175}
Gor Nishanov0f87a802018-04-04 22:18:03 +0000176inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000177bool operator!=(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000178 return !(__x == __y);
179}
Gor Nishanov0f87a802018-04-04 22:18:03 +0000180inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000181bool operator<(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000182 return less<void*>()(__x.address(), __y.address());
183}
Gor Nishanov0f87a802018-04-04 22:18:03 +0000184inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000185bool operator>(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000186 return __y < __x;
187}
Gor Nishanov0f87a802018-04-04 22:18:03 +0000188inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000189bool operator<=(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000190 return !(__x > __y);
191}
Gor Nishanov0f87a802018-04-04 22:18:03 +0000192inline _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000193bool operator>=(coroutine_handle<> __x, coroutine_handle<> __y) _NOEXCEPT {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000194 return !(__x < __y);
195}
196
197template <typename _Promise>
198class _LIBCPP_TEMPLATE_VIS coroutine_handle : public coroutine_handle<> {
199 using _Base = coroutine_handle<>;
200public:
Eric Fiselier997a3912017-05-26 03:02:54 +0000201#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier3ca91852017-05-25 04:36:24 +0000202 // 18.11.2.1 construct/reset
203 using coroutine_handle<>::coroutine_handle;
Eric Fiselier997a3912017-05-26 03:02:54 +0000204#else
Gor Nishanov0f87a802018-04-04 22:18:03 +0000205 _LIBCPP_INLINE_VISIBILITY coroutine_handle() _NOEXCEPT : _Base() {}
206 _LIBCPP_INLINE_VISIBILITY coroutine_handle(nullptr_t) _NOEXCEPT : _Base(nullptr) {}
Eric Fiselier997a3912017-05-26 03:02:54 +0000207#endif
Eric Fiselier3ca91852017-05-25 04:36:24 +0000208 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000209 coroutine_handle& operator=(nullptr_t) _NOEXCEPT {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000210 _Base::operator=(nullptr);
211 return *this;
212 }
213
Eric Fiselier3ca91852017-05-25 04:36:24 +0000214 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2944c5a2017-05-25 18:52:34 +0000215 _Promise& promise() const {
Gor Nishanov0f87a802018-04-04 22:18:03 +0000216 return *static_cast<_Promise*>(
Eric Fiselierd108bf82019-01-16 01:51:12 +0000217 __builtin_coro_promise(this->__handle_, _LIBCPP_ALIGNOF(_Promise), false));
Eric Fiselier3ca91852017-05-25 04:36:24 +0000218 }
219
220public:
Gor Nishanov0f87a802018-04-04 22:18:03 +0000221 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000222 static coroutine_handle from_address(void* __addr) _NOEXCEPT {
Eric Fiselier3ca91852017-05-25 04:36:24 +0000223 coroutine_handle __tmp;
224 __tmp.__handle_ = __addr;
225 return __tmp;
226 }
227
Eric Fiselierbae0a1d2017-05-29 06:42:01 +0000228 // NOTE: this overload isn't required by the standard but is needed so
229 // the deleted _Promise* overload doesn't make from_address(nullptr)
230 // ambiguous.
231 // FIXME: should from_address work with nullptr?
Gor Nishanov0f87a802018-04-04 22:18:03 +0000232 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierbae0a1d2017-05-29 06:42:01 +0000233 static coroutine_handle from_address(nullptr_t) _NOEXCEPT {
Eric Fiselier6e88ac22017-05-29 19:46:16 +0000234 return coroutine_handle(nullptr);
Eric Fiselierbae0a1d2017-05-29 06:42:01 +0000235 }
236
Eric Fiselier3fd02282017-05-29 19:24:25 +0000237 template <class _Tp, bool _CallIsValid = false>
238 static coroutine_handle from_address(_Tp*) {
239 static_assert(_CallIsValid,
240 "coroutine_handle<promise_type>::from_address cannot be called with "
241 "non-void pointers");
242 }
243
244 template <bool _CallIsValid = false>
245 static coroutine_handle from_address(_Promise*) {
246 static_assert(_CallIsValid,
247 "coroutine_handle<promise_type>::from_address cannot be used with "
248 "pointers to the coroutine's promise type; use 'from_promise' instead");
249 }
Eric Fiselier207d13c2017-05-29 05:00:24 +0000250
Gor Nishanov0f87a802018-04-04 22:18:03 +0000251 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000252 static coroutine_handle from_promise(_Promise& __promise) _NOEXCEPT {
Eric Fiselierf9bc0582017-06-16 00:36:17 +0000253 typedef typename remove_cv<_Promise>::type _RawPromise;
Eric Fiselier3ca91852017-05-25 04:36:24 +0000254 coroutine_handle __tmp;
Eric Fiselierf9bc0582017-06-16 00:36:17 +0000255 __tmp.__handle_ = __builtin_coro_promise(
256 _VSTD::addressof(const_cast<_RawPromise&>(__promise)),
Eric Fiselierd108bf82019-01-16 01:51:12 +0000257 _LIBCPP_ALIGNOF(_Promise), true);
Eric Fiselier3ca91852017-05-25 04:36:24 +0000258 return __tmp;
259 }
260};
261
Gor Nishanov0f87a802018-04-04 22:18:03 +0000262#if __has_builtin(__builtin_coro_noop)
263struct noop_coroutine_promise {};
264
265template <>
266class _LIBCPP_TEMPLATE_VIS coroutine_handle<noop_coroutine_promise>
267 : public coroutine_handle<> {
268 using _Base = coroutine_handle<>;
269 using _Promise = noop_coroutine_promise;
270public:
271
272 _LIBCPP_INLINE_VISIBILITY
273 _Promise& promise() const {
274 return *static_cast<_Promise*>(
Eric Fiselierd108bf82019-01-16 01:51:12 +0000275 __builtin_coro_promise(this->__handle_, _LIBCPP_ALIGNOF(_Promise), false));
Gor Nishanov0f87a802018-04-04 22:18:03 +0000276 }
277
278 _LIBCPP_CONSTEXPR explicit operator bool() const _NOEXCEPT { return true; }
279 _LIBCPP_CONSTEXPR bool done() const _NOEXCEPT { return false; }
280
Gor Nishanov8f310652018-04-05 00:18:37 +0000281 _LIBCPP_CONSTEXPR_AFTER_CXX17 void operator()() const _NOEXCEPT {}
282 _LIBCPP_CONSTEXPR_AFTER_CXX17 void resume() const _NOEXCEPT {}
283 _LIBCPP_CONSTEXPR_AFTER_CXX17 void destroy() const _NOEXCEPT {}
Gor Nishanov0f87a802018-04-04 22:18:03 +0000284
285private:
Louis Dionnecf3ae382018-07-10 17:38:30 +0000286 _LIBCPP_INLINE_VISIBILITY
Gor Nishanov0f87a802018-04-04 22:18:03 +0000287 friend coroutine_handle<noop_coroutine_promise> noop_coroutine() _NOEXCEPT;
288
289 _LIBCPP_INLINE_VISIBILITY coroutine_handle() _NOEXCEPT {
290 this->__handle_ = __builtin_coro_noop();
291 }
292};
293
294using noop_coroutine_handle = coroutine_handle<noop_coroutine_promise>;
295
296inline _LIBCPP_INLINE_VISIBILITY
297noop_coroutine_handle noop_coroutine() _NOEXCEPT {
Gor Nishanovb1e985d2018-04-04 22:51:57 +0000298 return noop_coroutine_handle();
Gor Nishanov0f87a802018-04-04 22:18:03 +0000299}
300#endif // __has_builtin(__builtin_coro_noop)
301
Eric Fiselier3ca91852017-05-25 04:36:24 +0000302struct _LIBCPP_TYPE_VIS suspend_never {
Gor Nishanov0f87a802018-04-04 22:18:03 +0000303 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000304 bool await_ready() const _NOEXCEPT { return true; }
Gor Nishanov0f87a802018-04-04 22:18:03 +0000305 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000306 void await_suspend(coroutine_handle<>) const _NOEXCEPT {}
Gor Nishanov0f87a802018-04-04 22:18:03 +0000307 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000308 void await_resume() const _NOEXCEPT {}
Eric Fiselier3ca91852017-05-25 04:36:24 +0000309};
310
311struct _LIBCPP_TYPE_VIS suspend_always {
Gor Nishanov0f87a802018-04-04 22:18:03 +0000312 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000313 bool await_ready() const _NOEXCEPT { return false; }
Gor Nishanov0f87a802018-04-04 22:18:03 +0000314 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000315 void await_suspend(coroutine_handle<>) const _NOEXCEPT {}
Gor Nishanov0f87a802018-04-04 22:18:03 +0000316 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000317 void await_resume() const _NOEXCEPT {}
Eric Fiselier3ca91852017-05-25 04:36:24 +0000318};
319
Eric Fiselier3ca91852017-05-25 04:36:24 +0000320_LIBCPP_END_NAMESPACE_EXPERIMENTAL_COROUTINES
321
322_LIBCPP_BEGIN_NAMESPACE_STD
323
324template <class _Tp>
325struct hash<_VSTD_CORO::coroutine_handle<_Tp> > {
326 using __arg_type = _VSTD_CORO::coroutine_handle<_Tp>;
327 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier997a3912017-05-26 03:02:54 +0000328 size_t operator()(__arg_type const& __v) const _NOEXCEPT
329 {return hash<void*>()(__v.address());}
Eric Fiselier3ca91852017-05-25 04:36:24 +0000330};
331
332_LIBCPP_END_NAMESPACE_STD
333
Eric Fiseliereb04c8c2017-05-26 01:52:59 +0000334#endif // !defined(_LIBCPP_HAS_NO_COROUTINES)
335
Eric Fiselier3ca91852017-05-25 04:36:24 +0000336#endif /* _LIBCPP_EXPERIMENTAL_COROUTINE */