coroutine 7.7 KB

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