blob: a2ac9937efb72992e9c549ad1f6ab378c6847c74 [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:
30bool 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;
36// 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:
96 _LIBCPP_ALWAYS_INLINE
97 constexpr coroutine_handle() noexcept : __handle_(nullptr) {}
98
99 _LIBCPP_ALWAYS_INLINE
100 constexpr coroutine_handle(nullptr_t) noexcept : __handle_(nullptr) {}
101
102 _LIBCPP_ALWAYS_INLINE
103 coroutine_handle& operator=(nullptr_t) noexcept {
104 __handle_ = nullptr;
105 return *this;
106 }
107
108 _LIBCPP_ALWAYS_INLINE
109 constexpr void* address() const noexcept { return __handle_; }
110
111 _LIBCPP_ALWAYS_INLINE
112 constexpr explicit operator bool() const noexcept { return __handle_; }
113
114 _LIBCPP_ALWAYS_INLINE
Eric Fiselier51056ae2017-05-25 19:04:55 +0000115 void operator()() { resume(); }
Eric Fiselier3ca91852017-05-25 04:36:24 +0000116
117 _LIBCPP_ALWAYS_INLINE
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
126 _LIBCPP_ALWAYS_INLINE
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
133 _LIBCPP_ALWAYS_INLINE
134 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:
141 _LIBCPP_ALWAYS_INLINE
142 static coroutine_handle from_address(void* __addr) noexcept {
143 coroutine_handle __tmp;
144 __tmp.__handle_ = __addr;
145 return __tmp;
146 }
147
148private:
149 bool __is_suspended() const noexcept {
150 // FIXME actually implement a check for if the coro is suspended.
151 return __handle_;
152 }
153
154 template <class _PromiseT> friend class coroutine_handle;
155 void* __handle_;
156};
157
158// 18.11.2.7 comparison operators:
159inline _LIBCPP_ALWAYS_INLINE
160bool operator==(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
161 return __x.address() == __y.address();
162}
163inline _LIBCPP_ALWAYS_INLINE
164bool operator!=(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
165 return !(__x == __y);
166}
167inline _LIBCPP_ALWAYS_INLINE
168bool operator<(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
169 return less<void*>()(__x.address(), __y.address());
170}
171inline _LIBCPP_ALWAYS_INLINE
172bool operator>(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
173 return __y < __x;
174}
175inline _LIBCPP_ALWAYS_INLINE
176bool operator<=(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
177 return !(__x > __y);
178}
179inline _LIBCPP_ALWAYS_INLINE
180bool operator>=(coroutine_handle<> __x, coroutine_handle<> __y) noexcept {
181 return !(__x < __y);
182}
183
184template <typename _Promise>
185class _LIBCPP_TEMPLATE_VIS coroutine_handle : public coroutine_handle<> {
186 using _Base = coroutine_handle<>;
187public:
188 // 18.11.2.1 construct/reset
189 using coroutine_handle<>::coroutine_handle;
190
191 _LIBCPP_INLINE_VISIBILITY
192 coroutine_handle& operator=(nullptr_t) noexcept {
193 _Base::operator=(nullptr);
194 return *this;
195 }
196
Eric Fiselier3ca91852017-05-25 04:36:24 +0000197 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier2944c5a2017-05-25 18:52:34 +0000198 _Promise& promise() const {
199 return *reinterpret_cast<_Promise*>(
Eric Fiselier3ca91852017-05-25 04:36:24 +0000200 __builtin_coro_promise(this->__handle_, alignof(_Promise), false));
201 }
202
203public:
204 _LIBCPP_ALWAYS_INLINE
205 static coroutine_handle from_address(void* __addr) noexcept {
206 coroutine_handle __tmp;
207 __tmp.__handle_ = __addr;
208 return __tmp;
209 }
210
211 _LIBCPP_ALWAYS_INLINE
212 static coroutine_handle from_promise(_Promise& __promise) noexcept {
213 coroutine_handle __tmp;
214 __tmp.__handle_ = __builtin_coro_promise(_VSTD::addressof(__promise),
215 alignof(_Promise), true);
216 return __tmp;
217 }
218};
219
Eric Fiselier3ca91852017-05-25 04:36:24 +0000220struct _LIBCPP_TYPE_VIS suspend_never {
221 _LIBCPP_ALWAYS_INLINE
222 bool await_ready() const noexcept { return true; }
223 _LIBCPP_ALWAYS_INLINE
224 void await_suspend(coroutine_handle<>) const noexcept {}
225 _LIBCPP_ALWAYS_INLINE
226 void await_resume() const noexcept {}
227};
228
229struct _LIBCPP_TYPE_VIS suspend_always {
230 _LIBCPP_ALWAYS_INLINE
231 bool await_ready() const noexcept { return false; }
232 _LIBCPP_ALWAYS_INLINE
233 void await_suspend(coroutine_handle<>) const noexcept {}
234 _LIBCPP_ALWAYS_INLINE
235 void await_resume() const noexcept {}
236};
237
Eric Fiselier3ca91852017-05-25 04:36:24 +0000238_LIBCPP_END_NAMESPACE_EXPERIMENTAL_COROUTINES
239
240_LIBCPP_BEGIN_NAMESPACE_STD
241
242template <class _Tp>
243struct hash<_VSTD_CORO::coroutine_handle<_Tp> > {
244 using __arg_type = _VSTD_CORO::coroutine_handle<_Tp>;
245 _LIBCPP_INLINE_VISIBILITY
246 size_t operator()(__arg_type const& __v) const noexcept
247 {return hash<void*>{}(__v.address());}
248};
249
250_LIBCPP_END_NAMESPACE_STD
251
Eric Fiseliereb04c8c2017-05-26 01:52:59 +0000252#endif // !defined(_LIBCPP_HAS_NO_COROUTINES)
253
Eric Fiselier3ca91852017-05-25 04:36:24 +0000254#endif /* _LIBCPP_EXPERIMENTAL_COROUTINE */