blob: 208a4d21761c24ed8d97efe389ab288c3039eaae [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
62#ifndef __cpp_coroutines
63# 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
70_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL_COROUTINES
71
72template <class _Tp, class = void>
73struct __coroutine_traits_sfinae {};
74
75template <class _Tp>
76struct __coroutine_traits_sfinae<
77 _Tp, typename __void_t<typename _Tp::promise_type>::type>
78{
79 using promise_type = typename _Tp::promise_type;
80};
81
82template <typename _Ret, typename... _Args>
83struct _LIBCPP_TEMPLATE_VIS coroutine_traits
84 : public __coroutine_traits_sfinae<_Ret>
85{
86};
87
88template <typename Promise = void>
89class _LIBCPP_TEMPLATE_VIS coroutine_handle;
90
91#if defined(__cpp_coroutines)
92
93template <>
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
115 void operator()() const { resume(); }
116
117 _LIBCPP_ALWAYS_INLINE
118 void resume() const {
119 _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
127 void destroy() const {
128 _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
197 _LIBCPP_INLINE_VISIBILITY
198 _Promise& promise() {
199 return *reinterpret_cast<_Promise*>(
200 __builtin_coro_promise(this->__handle_, alignof(_Promise), false));
201 }
202
203 _LIBCPP_INLINE_VISIBILITY
204 _Promise const& promise() const {
205 return *reinterpret_cast<_Promise const*>(
206 __builtin_coro_promise(this->__handle_, alignof(_Promise), false));
207 }
208
209public:
210 _LIBCPP_ALWAYS_INLINE
211 static coroutine_handle from_address(void* __addr) noexcept {
212 coroutine_handle __tmp;
213 __tmp.__handle_ = __addr;
214 return __tmp;
215 }
216
217 _LIBCPP_ALWAYS_INLINE
218 static coroutine_handle from_promise(_Promise& __promise) noexcept {
219 coroutine_handle __tmp;
220 __tmp.__handle_ = __builtin_coro_promise(_VSTD::addressof(__promise),
221 alignof(_Promise), true);
222 return __tmp;
223 }
224};
225
226#endif // defined(__cpp_coroutines)
227
228struct _LIBCPP_TYPE_VIS suspend_never {
229 _LIBCPP_ALWAYS_INLINE
230 bool await_ready() const noexcept { return true; }
231 _LIBCPP_ALWAYS_INLINE
232 void await_suspend(coroutine_handle<>) const noexcept {}
233 _LIBCPP_ALWAYS_INLINE
234 void await_resume() const noexcept {}
235};
236
237struct _LIBCPP_TYPE_VIS suspend_always {
238 _LIBCPP_ALWAYS_INLINE
239 bool await_ready() const noexcept { return false; }
240 _LIBCPP_ALWAYS_INLINE
241 void await_suspend(coroutine_handle<>) const noexcept {}
242 _LIBCPP_ALWAYS_INLINE
243 void await_resume() const noexcept {}
244};
245
246_LIBCPP_END_NAMESPACE_EXPERIMENTAL_COROUTINES
247
248_LIBCPP_BEGIN_NAMESPACE_STD
249
250template <class _Tp>
251struct hash<_VSTD_CORO::coroutine_handle<_Tp> > {
252 using __arg_type = _VSTD_CORO::coroutine_handle<_Tp>;
253 _LIBCPP_INLINE_VISIBILITY
254 size_t operator()(__arg_type const& __v) const noexcept
255 {return hash<void*>{}(__v.address());}
256};
257
258_LIBCPP_END_NAMESPACE_STD
259
260#endif /* _LIBCPP_EXPERIMENTAL_COROUTINE */