coroutine 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // <coroutine> -*- C++ -*-
  2. // Copyright (C) 2019-2021 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. // [coroutine.handle.con], 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. // [coroutine.handle.export.import], 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. // [coroutine.handle.observers], 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. // [coroutine.handle.resumption], 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. // [coroutine.handle.compare], comparison operators
  104. constexpr bool
  105. operator==(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  106. {
  107. return __a.address() == __b.address();
  108. }
  109. #if _COROUTINES_USE_SPACESHIP
  110. constexpr strong_ordering
  111. operator<=>(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  112. {
  113. return std::compare_three_way()(__a.address(), __b.address());
  114. }
  115. #else
  116. // These are to enable operation with std=c++14,17.
  117. constexpr bool
  118. operator!=(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  119. {
  120. return !(__a == __b);
  121. }
  122. constexpr bool
  123. operator<(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  124. {
  125. return less<void*>()(__a.address(), __b.address());
  126. }
  127. constexpr bool
  128. operator>(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  129. {
  130. return __b < __a;
  131. }
  132. constexpr bool
  133. operator<=(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  134. {
  135. return !(__a > __b);
  136. }
  137. constexpr bool
  138. operator>=(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  139. {
  140. return !(__a < __b);
  141. }
  142. #endif
  143. template <typename _Promise>
  144. struct coroutine_handle
  145. {
  146. // [coroutine.handle.con], construct/reset
  147. constexpr coroutine_handle() noexcept { }
  148. constexpr coroutine_handle(nullptr_t) noexcept { }
  149. static coroutine_handle
  150. from_promise(_Promise& __p)
  151. {
  152. coroutine_handle __self;
  153. __self._M_fr_ptr
  154. = __builtin_coro_promise((char*) &__p, __alignof(_Promise), true);
  155. return __self;
  156. }
  157. coroutine_handle& operator=(nullptr_t) noexcept
  158. {
  159. _M_fr_ptr = nullptr;
  160. return *this;
  161. }
  162. // [coroutine.handle.export.import], export/import
  163. constexpr void* address() const noexcept { return _M_fr_ptr; }
  164. constexpr static coroutine_handle from_address(void* __a) noexcept
  165. {
  166. coroutine_handle __self;
  167. __self._M_fr_ptr = __a;
  168. return __self;
  169. }
  170. // [coroutine.handle.conv], conversion
  171. constexpr operator coroutine_handle<>() const noexcept
  172. { return coroutine_handle<>::from_address(address()); }
  173. // [coroutine.handle.observers], observers
  174. constexpr explicit operator bool() const noexcept
  175. {
  176. return bool(_M_fr_ptr);
  177. }
  178. bool done() const noexcept { return __builtin_coro_done(_M_fr_ptr); }
  179. // [coroutine.handle.resumption], resumption
  180. void operator()() const { resume(); }
  181. void resume() const { __builtin_coro_resume(_M_fr_ptr); }
  182. void destroy() const { __builtin_coro_destroy(_M_fr_ptr); }
  183. // [coroutine.handle.promise], promise access
  184. _Promise& promise() const
  185. {
  186. void* __t
  187. = __builtin_coro_promise (_M_fr_ptr, __alignof(_Promise), false);
  188. return *static_cast<_Promise*>(__t);
  189. }
  190. private:
  191. void* _M_fr_ptr = nullptr;
  192. };
  193. /// [coroutine.noop]
  194. struct noop_coroutine_promise
  195. {
  196. };
  197. // 17.12.4.1 Class noop_coroutine_promise
  198. /// [coroutine.promise.noop]
  199. template <>
  200. struct coroutine_handle<noop_coroutine_promise>
  201. {
  202. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  203. // 3460. Unimplementable noop_coroutine_handle guarantees
  204. // [coroutine.handle.noop.conv], conversion
  205. constexpr operator coroutine_handle<>() const noexcept
  206. { return coroutine_handle<>::from_address(address()); }
  207. // [coroutine.handle.noop.observers], observers
  208. constexpr explicit operator bool() const noexcept { return true; }
  209. constexpr bool done() const noexcept { return false; }
  210. // [coroutine.handle.noop.resumption], resumption
  211. void operator()() const noexcept {}
  212. void resume() const noexcept {}
  213. void destroy() const noexcept {}
  214. // [coroutine.handle.noop.promise], promise access
  215. noop_coroutine_promise& promise() const noexcept
  216. { return _S_fr.__p; }
  217. // [coroutine.handle.noop.address], address
  218. constexpr void* address() const noexcept { return _M_fr_ptr; }
  219. private:
  220. friend coroutine_handle noop_coroutine() noexcept;
  221. struct __frame
  222. {
  223. static void __dummy_resume_destroy() { }
  224. void (*__r)() = __dummy_resume_destroy;
  225. void (*__d)() = __dummy_resume_destroy;
  226. struct noop_coroutine_promise __p;
  227. };
  228. static __frame _S_fr;
  229. explicit coroutine_handle() noexcept = default;
  230. void* _M_fr_ptr = &_S_fr;
  231. };
  232. using noop_coroutine_handle = coroutine_handle<noop_coroutine_promise>;
  233. inline noop_coroutine_handle::__frame
  234. noop_coroutine_handle::_S_fr{};
  235. inline noop_coroutine_handle noop_coroutine() noexcept
  236. {
  237. return noop_coroutine_handle();
  238. }
  239. // 17.12.5 Trivial awaitables
  240. /// [coroutine.trivial.awaitables]
  241. struct suspend_always
  242. {
  243. constexpr bool await_ready() const noexcept { return false; }
  244. constexpr void await_suspend(coroutine_handle<>) const noexcept {}
  245. constexpr void await_resume() const noexcept {}
  246. };
  247. struct suspend_never
  248. {
  249. constexpr bool await_ready() const noexcept { return true; }
  250. constexpr void await_suspend(coroutine_handle<>) const noexcept {}
  251. constexpr void await_resume() const noexcept {}
  252. };
  253. } // namespace __n4861
  254. #else
  255. #error "the coroutine header requires -fcoroutines"
  256. #endif
  257. _GLIBCXX_END_NAMESPACE_VERSION
  258. } // namespace std
  259. #endif // C++14 (we are allowing use from at least this)
  260. #endif // _GLIBCXX_COROUTINE