coroutine 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // <coroutine> -*- C++ -*-
  2. // Copyright (C) 2019-2023 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 <type_traits>
  29. #if __cplusplus > 201703L
  30. # include <compare>
  31. #endif
  32. #if !defined __cpp_lib_three_way_comparison
  33. # include <bits/stl_function.h> // for std::less
  34. #endif
  35. /**
  36. * @defgroup coroutines Coroutines
  37. *
  38. * Components for supporting coroutine implementations.
  39. *
  40. * @since C++20 (and since C++14 as a libstdc++ extension)
  41. */
  42. namespace std _GLIBCXX_VISIBILITY (default)
  43. {
  44. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  45. #if __cpp_impl_coroutine
  46. #define __cpp_lib_coroutine 201902L
  47. inline namespace __n4861 {
  48. // C++20 17.12.2 coroutine traits
  49. /// [coroutine.traits]
  50. /// [coroutine.traits.primary]
  51. /// If _Result::promise_type is valid and denotes a type then the traits
  52. /// have a single publicly accessible member, otherwise they are empty.
  53. template <typename _Result, typename... _ArgTypes>
  54. struct coroutine_traits;
  55. template <typename _Result, typename = void>
  56. struct __coroutine_traits_impl {};
  57. template <typename _Result>
  58. #if __cpp_concepts
  59. requires requires { typename _Result::promise_type; }
  60. struct __coroutine_traits_impl<_Result, void>
  61. #else
  62. struct __coroutine_traits_impl<_Result,
  63. __void_t<typename _Result::promise_type>>
  64. #endif
  65. {
  66. using promise_type = typename _Result::promise_type;
  67. };
  68. template <typename _Result, typename... _ArgTypes>
  69. struct coroutine_traits : __coroutine_traits_impl<_Result> {};
  70. // C++20 17.12.3 Class template coroutine_handle
  71. /// [coroutine.handle]
  72. template <typename _Promise = void>
  73. struct coroutine_handle;
  74. template <> struct
  75. coroutine_handle<void>
  76. {
  77. public:
  78. // [coroutine.handle.con], construct/reset
  79. constexpr coroutine_handle() noexcept : _M_fr_ptr(0) {}
  80. constexpr coroutine_handle(std::nullptr_t __h) noexcept
  81. : _M_fr_ptr(__h)
  82. {}
  83. coroutine_handle& operator=(std::nullptr_t) noexcept
  84. {
  85. _M_fr_ptr = nullptr;
  86. return *this;
  87. }
  88. public:
  89. // [coroutine.handle.export.import], export/import
  90. constexpr void* address() const noexcept { return _M_fr_ptr; }
  91. constexpr static coroutine_handle from_address(void* __a) noexcept
  92. {
  93. coroutine_handle __self;
  94. __self._M_fr_ptr = __a;
  95. return __self;
  96. }
  97. public:
  98. // [coroutine.handle.observers], observers
  99. constexpr explicit operator bool() const noexcept
  100. {
  101. return bool(_M_fr_ptr);
  102. }
  103. bool done() const noexcept { return __builtin_coro_done(_M_fr_ptr); }
  104. // [coroutine.handle.resumption], resumption
  105. void operator()() const { resume(); }
  106. void resume() const { __builtin_coro_resume(_M_fr_ptr); }
  107. void destroy() const { __builtin_coro_destroy(_M_fr_ptr); }
  108. protected:
  109. void* _M_fr_ptr;
  110. };
  111. // [coroutine.handle.compare], comparison operators
  112. constexpr bool
  113. operator==(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  114. {
  115. return __a.address() == __b.address();
  116. }
  117. #ifdef __cpp_lib_three_way_comparison
  118. constexpr strong_ordering
  119. operator<=>(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  120. {
  121. return std::compare_three_way()(__a.address(), __b.address());
  122. }
  123. #else
  124. // These are to enable operation with std=c++14,17.
  125. constexpr bool
  126. operator!=(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  127. {
  128. return !(__a == __b);
  129. }
  130. constexpr bool
  131. operator<(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  132. {
  133. return less<void*>()(__a.address(), __b.address());
  134. }
  135. constexpr bool
  136. operator>(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  137. {
  138. return __b < __a;
  139. }
  140. constexpr bool
  141. operator<=(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  142. {
  143. return !(__a > __b);
  144. }
  145. constexpr bool
  146. operator>=(coroutine_handle<> __a, coroutine_handle<> __b) noexcept
  147. {
  148. return !(__a < __b);
  149. }
  150. #endif
  151. template <typename _Promise>
  152. struct coroutine_handle
  153. {
  154. // [coroutine.handle.con], construct/reset
  155. constexpr coroutine_handle() noexcept { }
  156. constexpr coroutine_handle(nullptr_t) noexcept { }
  157. static coroutine_handle
  158. from_promise(_Promise& __p)
  159. {
  160. coroutine_handle __self;
  161. __self._M_fr_ptr
  162. = __builtin_coro_promise((char*) &__p, __alignof(_Promise), true);
  163. return __self;
  164. }
  165. coroutine_handle& operator=(nullptr_t) noexcept
  166. {
  167. _M_fr_ptr = nullptr;
  168. return *this;
  169. }
  170. // [coroutine.handle.export.import], export/import
  171. constexpr void* address() const noexcept { return _M_fr_ptr; }
  172. constexpr static coroutine_handle from_address(void* __a) noexcept
  173. {
  174. coroutine_handle __self;
  175. __self._M_fr_ptr = __a;
  176. return __self;
  177. }
  178. // [coroutine.handle.conv], conversion
  179. constexpr operator coroutine_handle<>() const noexcept
  180. { return coroutine_handle<>::from_address(address()); }
  181. // [coroutine.handle.observers], observers
  182. constexpr explicit operator bool() const noexcept
  183. {
  184. return bool(_M_fr_ptr);
  185. }
  186. bool done() const noexcept { return __builtin_coro_done(_M_fr_ptr); }
  187. // [coroutine.handle.resumption], resumption
  188. void operator()() const { resume(); }
  189. void resume() const { __builtin_coro_resume(_M_fr_ptr); }
  190. void destroy() const { __builtin_coro_destroy(_M_fr_ptr); }
  191. // [coroutine.handle.promise], promise access
  192. _Promise& promise() const
  193. {
  194. void* __t
  195. = __builtin_coro_promise (_M_fr_ptr, __alignof(_Promise), false);
  196. return *static_cast<_Promise*>(__t);
  197. }
  198. private:
  199. void* _M_fr_ptr = nullptr;
  200. };
  201. /// [coroutine.noop]
  202. struct noop_coroutine_promise
  203. {
  204. };
  205. // 17.12.4.1 Class noop_coroutine_promise
  206. /// [coroutine.promise.noop]
  207. template <>
  208. struct coroutine_handle<noop_coroutine_promise>
  209. {
  210. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  211. // 3460. Unimplementable noop_coroutine_handle guarantees
  212. // [coroutine.handle.noop.conv], conversion
  213. constexpr operator coroutine_handle<>() const noexcept
  214. { return coroutine_handle<>::from_address(address()); }
  215. // [coroutine.handle.noop.observers], observers
  216. constexpr explicit operator bool() const noexcept { return true; }
  217. constexpr bool done() const noexcept { return false; }
  218. // [coroutine.handle.noop.resumption], resumption
  219. void operator()() const noexcept {}
  220. void resume() const noexcept {}
  221. void destroy() const noexcept {}
  222. // [coroutine.handle.noop.promise], promise access
  223. noop_coroutine_promise& promise() const noexcept
  224. { return _S_fr.__p; }
  225. // [coroutine.handle.noop.address], address
  226. constexpr void* address() const noexcept { return _M_fr_ptr; }
  227. private:
  228. friend coroutine_handle noop_coroutine() noexcept;
  229. struct __frame
  230. {
  231. static void __dummy_resume_destroy() { }
  232. void (*__r)() = __dummy_resume_destroy;
  233. void (*__d)() = __dummy_resume_destroy;
  234. struct noop_coroutine_promise __p;
  235. };
  236. static __frame _S_fr;
  237. explicit coroutine_handle() noexcept = default;
  238. void* _M_fr_ptr = &_S_fr;
  239. };
  240. using noop_coroutine_handle = coroutine_handle<noop_coroutine_promise>;
  241. inline noop_coroutine_handle::__frame
  242. noop_coroutine_handle::_S_fr{};
  243. inline noop_coroutine_handle noop_coroutine() noexcept
  244. {
  245. return noop_coroutine_handle();
  246. }
  247. // 17.12.5 Trivial awaitables
  248. /// [coroutine.trivial.awaitables]
  249. struct suspend_always
  250. {
  251. constexpr bool await_ready() const noexcept { return false; }
  252. constexpr void await_suspend(coroutine_handle<>) const noexcept {}
  253. constexpr void await_resume() const noexcept {}
  254. };
  255. struct suspend_never
  256. {
  257. constexpr bool await_ready() const noexcept { return true; }
  258. constexpr void await_suspend(coroutine_handle<>) const noexcept {}
  259. constexpr void await_resume() const noexcept {}
  260. };
  261. } // namespace __n4861
  262. template<typename _Tp> struct hash;
  263. template<typename _Promise>
  264. struct hash<coroutine_handle<_Promise>>
  265. {
  266. size_t
  267. operator()(const coroutine_handle<_Promise>& __h) const noexcept
  268. {
  269. return reinterpret_cast<size_t>(__h.address());
  270. }
  271. };
  272. #else
  273. #error "the <coroutine> header requires -fcoroutines"
  274. #endif
  275. _GLIBCXX_END_NAMESPACE_VERSION
  276. } // namespace std
  277. #endif // C++14 (we are allowing use from at least this)
  278. #endif // _GLIBCXX_COROUTINE