coroutine_support.ipp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* Tells C++ coroutines about Outcome's result
  2. (C) 2019 Niall Douglas <http://www.nedproductions.biz/> (12 commits)
  3. File Created: Oct 2019
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef BOOST_OUTCOME_COROUTINE_SUPPORT_NAMESPACE_BEGIN
  26. #error This header must only be included by outcome/coroutine_support.hpp or outcome/experimental/coroutine_support.hpp
  27. #endif
  28. #ifndef BOOST_OUTCOME_DETAIL_COROUTINE_SUPPORT_HPP
  29. #define BOOST_OUTCOME_DETAIL_COROUTINE_SUPPORT_HPP
  30. #include <atomic>
  31. #include <cassert>
  32. #if __cpp_coroutines
  33. #if __has_include(<coroutine>)
  34. #include <coroutine>
  35. BOOST_OUTCOME_V2_NAMESPACE_BEGIN
  36. namespace awaitables
  37. {
  38. template <class Promise = void> using coroutine_handle = std::coroutine_handle<Promise>;
  39. template <class... Args> using coroutine_traits = std::coroutine_traits<Args...>;
  40. using std::suspend_always;
  41. using std::suspend_never;
  42. } // namespace awaitables
  43. BOOST_OUTCOME_V2_NAMESPACE_END
  44. #define BOOST_OUTCOME_FOUND_COROUTINE_HEADER 1
  45. #elif __has_include(<experimental/coroutine>)
  46. #include <experimental/coroutine>
  47. BOOST_OUTCOME_V2_NAMESPACE_BEGIN
  48. namespace awaitables
  49. {
  50. template <class Promise = void> using coroutine_handle = std::experimental::coroutine_handle<Promise>;
  51. template <class... Args> using coroutine_traits = std::experimental::coroutine_traits<Args...>;
  52. using std::experimental::suspend_always;
  53. using std::experimental::suspend_never;
  54. } // namespace awaitables
  55. BOOST_OUTCOME_V2_NAMESPACE_END
  56. #define BOOST_OUTCOME_FOUND_COROUTINE_HEADER 1
  57. #endif
  58. #endif
  59. #ifdef BOOST_OUTCOME_FOUND_COROUTINE_HEADER
  60. BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN
  61. namespace awaitables
  62. {
  63. namespace detail
  64. {
  65. struct error_type_not_found
  66. {
  67. };
  68. struct exception_type_not_found
  69. {
  70. };
  71. template <class T> struct type_found
  72. {
  73. using type = T;
  74. };
  75. template <class T, class U = typename T::error_type> constexpr inline type_found<U> extract_error_type(int /*unused*/) { return {}; }
  76. template <class T> constexpr inline type_found<error_type_not_found> extract_error_type(...) { return {}; }
  77. template <class T, class U = typename T::exception_type> constexpr inline type_found<U> extract_exception_type(int /*unused*/) { return {}; }
  78. template <class T> constexpr inline type_found<exception_type_not_found> extract_exception_type(...) { return {}; }
  79. BOOST_OUTCOME_TEMPLATE(class T, class U)
  80. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_constructible<U, T>::value))
  81. inline bool try_set_error(T &e, U *result)
  82. {
  83. new(result) U(e);
  84. return true;
  85. }
  86. template <class T> inline bool try_set_error(T & /*unused*/, ...) { return false; }
  87. BOOST_OUTCOME_TEMPLATE(class T, class U)
  88. BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_constructible<U, T>::value))
  89. inline void set_or_rethrow(T &e, U *result) { new(result) U(e); }
  90. template <class T> inline void set_or_rethrow(T &e, ...) { rethrow_exception(e); }
  91. template <class T> class fake_atomic
  92. {
  93. T _v;
  94. public:
  95. constexpr fake_atomic(T v)
  96. : _v(v)
  97. {
  98. }
  99. T load(std::memory_order /*unused*/) { return _v; }
  100. void store(T v, std::memory_order /*unused*/) { _v = v; }
  101. };
  102. template <class Awaitable, bool suspend_initial, bool use_atomic, bool is_void> struct outcome_promise_type
  103. {
  104. using container_type = typename Awaitable::container_type;
  105. using result_set_type = std::conditional_t<use_atomic, std::atomic<bool>, fake_atomic<bool>>;
  106. union {
  107. BOOST_OUTCOME_V2_NAMESPACE::detail::empty_type _default{};
  108. container_type result;
  109. };
  110. result_set_type result_set{false};
  111. coroutine_handle<> continuation;
  112. outcome_promise_type() {}
  113. outcome_promise_type(const outcome_promise_type &) = delete;
  114. outcome_promise_type(outcome_promise_type &&) = delete;
  115. outcome_promise_type &operator=(const outcome_promise_type &) = delete;
  116. outcome_promise_type &operator=(outcome_promise_type &&) = delete;
  117. ~outcome_promise_type()
  118. {
  119. if(result_set.load(std::memory_order_acquire))
  120. {
  121. result.~container_type();
  122. }
  123. }
  124. auto get_return_object() { return Awaitable{*this}; }
  125. void return_value(container_type &&value)
  126. {
  127. assert(!result_set.load(std::memory_order_acquire));
  128. if(result_set.load(std::memory_order_acquire))
  129. {
  130. result.~container_type();
  131. }
  132. new(&result) container_type(static_cast<container_type &&>(value));
  133. result_set.store(true, std::memory_order_release);
  134. }
  135. void return_value(const container_type &value)
  136. {
  137. assert(!result_set.load(std::memory_order_acquire));
  138. if(result_set.load(std::memory_order_acquire))
  139. {
  140. result.~container_type();
  141. }
  142. new(&result) container_type(value);
  143. result_set.store(true, std::memory_order_release);
  144. }
  145. void unhandled_exception()
  146. {
  147. assert(!result_set.load(std::memory_order_acquire));
  148. if(result_set.load(std::memory_order_acquire))
  149. {
  150. result.~container_type();
  151. }
  152. #ifndef BOOST_NO_EXCEPTIONS
  153. auto e = std::current_exception();
  154. auto ec = detail::error_from_exception(static_cast<decltype(e) &&>(e), {});
  155. // Try to set error code first
  156. if(!detail::error_is_set(ec) || !detail::try_set_error(ec, &result))
  157. {
  158. detail::set_or_rethrow(e, &result);
  159. }
  160. #else
  161. std::terminate();
  162. #endif
  163. result_set.store(true, std::memory_order_release);
  164. }
  165. auto initial_suspend() noexcept
  166. {
  167. struct awaiter
  168. {
  169. bool await_ready() noexcept { return !suspend_initial; }
  170. void await_resume() noexcept {}
  171. void await_suspend(coroutine_handle<> /*unused*/) {}
  172. };
  173. return awaiter{};
  174. }
  175. auto final_suspend()
  176. {
  177. struct awaiter
  178. {
  179. bool await_ready() noexcept { return false; }
  180. void await_resume() noexcept {}
  181. void await_suspend(coroutine_handle<outcome_promise_type> self)
  182. {
  183. if(self.promise().continuation)
  184. {
  185. return self.promise().continuation.resume();
  186. }
  187. }
  188. };
  189. return awaiter{};
  190. }
  191. };
  192. template <class Awaitable, bool suspend_initial, bool use_atomic> struct outcome_promise_type<Awaitable, suspend_initial, use_atomic, true>
  193. {
  194. using container_type = void;
  195. using result_set_type = std::conditional_t<use_atomic, std::atomic<bool>, fake_atomic<bool>>;
  196. result_set_type result_set{false};
  197. coroutine_handle<> continuation;
  198. outcome_promise_type() {}
  199. outcome_promise_type(const outcome_promise_type &) = delete;
  200. outcome_promise_type(outcome_promise_type &&) = delete;
  201. outcome_promise_type &operator=(const outcome_promise_type &) = delete;
  202. outcome_promise_type &operator=(outcome_promise_type &&) = delete;
  203. ~outcome_promise_type() = default;
  204. auto get_return_object() { return Awaitable{*this}; }
  205. void return_void()
  206. {
  207. assert(!result_set.load(std::memory_order_acquire));
  208. result_set.store(true, std::memory_order_release);
  209. }
  210. void unhandled_exception()
  211. {
  212. assert(!result_set.load(std::memory_order_acquire));
  213. std::rethrow_exception(std::current_exception());
  214. }
  215. auto initial_suspend() noexcept
  216. {
  217. struct awaiter
  218. {
  219. bool await_ready() noexcept { return !suspend_initial; }
  220. void await_resume() noexcept {}
  221. void await_suspend(coroutine_handle<> /*unused*/) {}
  222. };
  223. return awaiter{};
  224. }
  225. auto final_suspend()
  226. {
  227. struct awaiter
  228. {
  229. bool await_ready() noexcept { return false; }
  230. void await_resume() noexcept {}
  231. void await_suspend(coroutine_handle<outcome_promise_type> self)
  232. {
  233. if(self.promise().continuation)
  234. {
  235. return self.promise().continuation.resume();
  236. }
  237. }
  238. };
  239. return awaiter{};
  240. }
  241. };
  242. template <class Awaitable, bool suspend_initial, bool use_atomic> constexpr inline auto move_result_from_promise_if_not_void(outcome_promise_type<Awaitable, suspend_initial, use_atomic, false> &p) { return static_cast<typename Awaitable::container_type &&>(p.result); }
  243. template <class Awaitable, bool suspend_initial, bool use_atomic> constexpr inline void move_result_from_promise_if_not_void(outcome_promise_type<Awaitable, suspend_initial, use_atomic, true> & /*unused*/) {}
  244. template <class Cont, bool suspend_initial, bool use_atomic> struct BOOST_OUTCOME_NODISCARD awaitable
  245. {
  246. using container_type = Cont;
  247. using promise_type = outcome_promise_type<awaitable, suspend_initial, use_atomic, std::is_void<container_type>::value>;
  248. coroutine_handle<promise_type> _h;
  249. awaitable(awaitable &&o) noexcept
  250. : _h(static_cast<coroutine_handle<promise_type> &&>(o._h))
  251. {
  252. o._h = nullptr;
  253. }
  254. awaitable(const awaitable &o) = delete;
  255. awaitable &operator=(awaitable &&) = delete; // as per P1056
  256. awaitable &operator=(const awaitable &) = delete;
  257. ~awaitable()
  258. {
  259. if(_h)
  260. {
  261. _h.destroy();
  262. }
  263. }
  264. explicit awaitable(promise_type &p)
  265. : _h(coroutine_handle<promise_type>::from_promise(p))
  266. {
  267. }
  268. bool await_ready() noexcept { return _h.promise().result_set.load(std::memory_order_acquire); }
  269. container_type await_resume()
  270. {
  271. assert(_h.promise().result_set.load(std::memory_order_acquire));
  272. if(!_h.promise().result_set.load(std::memory_order_acquire))
  273. {
  274. std::terminate();
  275. }
  276. return detail::move_result_from_promise_if_not_void(_h.promise());
  277. }
  278. void await_suspend(coroutine_handle<> cont)
  279. {
  280. _h.promise().continuation = cont;
  281. _h.resume();
  282. }
  283. };
  284. } // namespace detail
  285. } // namespace awaitables
  286. BOOST_OUTCOME_V2_NAMESPACE_END
  287. #endif
  288. #endif
  289. #ifdef BOOST_OUTCOME_FOUND_COROUTINE_HEADER
  290. BOOST_OUTCOME_COROUTINE_SUPPORT_NAMESPACE_EXPORT_BEGIN
  291. /*! AWAITING HUGO JSON CONVERSION TOOL
  292. SIGNATURE NOT RECOGNISED
  293. */
  294. template <class T> using eager = BOOST_OUTCOME_V2_NAMESPACE::awaitables::detail::awaitable<T, false, false>;
  295. /*! AWAITING HUGO JSON CONVERSION TOOL
  296. SIGNATURE NOT RECOGNISED
  297. */
  298. template <class T> using atomic_eager = BOOST_OUTCOME_V2_NAMESPACE::awaitables::detail::awaitable<T, false, true>;
  299. /*! AWAITING HUGO JSON CONVERSION TOOL
  300. SIGNATURE NOT RECOGNISED
  301. */
  302. template <class T> using lazy = BOOST_OUTCOME_V2_NAMESPACE::awaitables::detail::awaitable<T, true, false>;
  303. /*! AWAITING HUGO JSON CONVERSION TOOL
  304. SIGNATURE NOT RECOGNISED
  305. */
  306. template <class T> using atomic_lazy = BOOST_OUTCOME_V2_NAMESPACE::awaitables::detail::awaitable<T, true, true>;
  307. BOOST_OUTCOME_COROUTINE_SUPPORT_NAMESPACE_END
  308. #endif