join.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. //
  2. // Copyright (c) 2022 Klemens Morgenstern (klemens.morgenstern@gmx.net)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_COBALT_DETAIL_JOIN_HPP
  8. #define BOOST_COBALT_DETAIL_JOIN_HPP
  9. #include <boost/cobalt/detail/await_result_helper.hpp>
  10. #include <boost/cobalt/detail/exception.hpp>
  11. #include <boost/cobalt/detail/fork.hpp>
  12. #include <boost/cobalt/detail/forward_cancellation.hpp>
  13. #include <boost/cobalt/detail/util.hpp>
  14. #include <boost/cobalt/detail/wrapper.hpp>
  15. #include <boost/cobalt/task.hpp>
  16. #include <boost/cobalt/this_thread.hpp>
  17. #include <boost/asio/associated_cancellation_slot.hpp>
  18. #include <boost/asio/bind_cancellation_slot.hpp>
  19. #include <boost/asio/cancellation_signal.hpp>
  20. #include <boost/core/ignore_unused.hpp>
  21. #include <boost/intrusive_ptr.hpp>
  22. #include <boost/system/result.hpp>
  23. #include <boost/variant2/variant.hpp>
  24. #include <array>
  25. #include <coroutine>
  26. #include <algorithm>
  27. namespace boost::cobalt::detail
  28. {
  29. template<typename ... Args>
  30. struct join_variadic_impl
  31. {
  32. using tuple_type = std::tuple<decltype(get_awaitable_type(std::declval<Args&&>()))...>;
  33. BOOST_COBALT_MSVC_NOINLINE
  34. join_variadic_impl(Args && ... args)
  35. : args{std::forward<Args>(args)...}
  36. {
  37. }
  38. std::tuple<Args...> args;
  39. constexpr static std::size_t tuple_size = sizeof...(Args);
  40. struct awaitable : fork::static_shared_state<256 * tuple_size>
  41. {
  42. template<std::size_t ... Idx>
  43. awaitable(std::tuple<Args...> & args, std::index_sequence<Idx...>) :
  44. aws(awaitable_type_getter<Args>(std::get<Idx>(args))...)
  45. {
  46. }
  47. tuple_type aws;
  48. std::array<asio::cancellation_signal, tuple_size> cancel_;
  49. template<typename > constexpr static auto make_null() {return nullptr;};
  50. std::array<asio::cancellation_signal*, tuple_size> cancel = {make_null<Args>()...};
  51. constexpr static bool all_void = (std::is_void_v<co_await_result_t<Args>> && ...);
  52. template<typename T>
  53. using result_store_part =
  54. std::optional<void_as_monostate<co_await_result_t<T>>>;
  55. std::conditional_t<all_void,
  56. variant2::monostate,
  57. std::tuple<result_store_part<Args>...>> result;
  58. std::exception_ptr error;
  59. template<std::size_t Idx>
  60. void cancel_step()
  61. {
  62. auto &r = cancel[Idx];
  63. if (r)
  64. std::exchange(r, nullptr)->emit(asio::cancellation_type::all);
  65. }
  66. void cancel_all()
  67. {
  68. mp11::mp_for_each<mp11::mp_iota_c<sizeof...(Args)>>
  69. ([&](auto idx)
  70. {
  71. cancel_step<idx>();
  72. });
  73. }
  74. template<std::size_t Idx>
  75. void interrupt_await_step()
  76. {
  77. using type = std::tuple_element_t<Idx, tuple_type>;
  78. using t = std::conditional_t<std::is_reference_v<std::tuple_element_t<Idx, std::tuple<Args...>>>,
  79. type &,
  80. type &&>;
  81. if constexpr (interruptible<t>)
  82. if (this->cancel[Idx] != nullptr)
  83. static_cast<t>(std::get<Idx>(aws)).interrupt_await();
  84. }
  85. void interrupt_await()
  86. {
  87. mp11::mp_for_each<mp11::mp_iota_c<sizeof...(Args)>>
  88. ([&](auto idx)
  89. {
  90. interrupt_await_step<idx>();
  91. });
  92. }
  93. // GCC doesn't like member funs
  94. template<std::size_t Idx>
  95. static detail::fork await_impl(awaitable & this_)
  96. BOOST_TRY
  97. {
  98. auto & aw = std::get<Idx>(this_.aws);
  99. // check manually if we're ready
  100. auto rd = aw.await_ready();
  101. if (!rd)
  102. {
  103. this_.cancel[Idx] = &this_.cancel_[Idx];
  104. co_await this_.cancel[Idx]->slot();
  105. // make sure the executor is set
  106. co_await detail::fork::wired_up;
  107. // do the await - this doesn't call await-ready again
  108. if constexpr (std::is_void_v<decltype(aw.await_resume())>)
  109. {
  110. co_await aw;
  111. if constexpr (!all_void)
  112. std::get<Idx>(this_.result).emplace();
  113. }
  114. else
  115. std::get<Idx>(this_.result).emplace(co_await aw);
  116. }
  117. else
  118. {
  119. if constexpr (std::is_void_v<decltype(aw.await_resume())>)
  120. {
  121. aw.await_resume();
  122. if constexpr (!all_void)
  123. std::get<Idx>(this_.result).emplace();
  124. }
  125. else
  126. std::get<Idx>(this_.result).emplace(aw.await_resume());
  127. }
  128. }
  129. BOOST_CATCH(...)
  130. {
  131. if (!this_.error)
  132. this_.error = std::current_exception();
  133. this_.cancel_all();
  134. }
  135. BOOST_CATCH_END
  136. std::array<detail::fork(*)(awaitable&), tuple_size> impls {
  137. []<std::size_t ... Idx>(std::index_sequence<Idx...>)
  138. {
  139. return std::array<detail::fork(*)(awaitable&), tuple_size>{&await_impl<Idx>...};
  140. }(std::make_index_sequence<tuple_size>{})
  141. };
  142. detail::fork last_forked;
  143. std::size_t last_index = 0u;
  144. bool await_ready()
  145. {
  146. while (last_index < tuple_size)
  147. {
  148. last_forked = impls[last_index++](*this);
  149. if (!last_forked.done())
  150. return false; // one coro didn't immediately complete!
  151. }
  152. last_forked.release();
  153. return true;
  154. }
  155. template<typename H>
  156. auto await_suspend(
  157. std::coroutine_handle<H> h
  158. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  159. , const boost::source_location & loc = BOOST_CURRENT_LOCATION
  160. #endif
  161. )
  162. {
  163. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  164. this->loc = loc;
  165. #endif
  166. this->exec = detail::get_executor(h);
  167. last_forked.release().resume();
  168. while (last_index < tuple_size)
  169. impls[last_index++](*this).release();
  170. if (error)
  171. cancel_all();
  172. if (!this->outstanding_work()) // already done, resume rightaway.
  173. return false;
  174. // arm the cancel
  175. assign_cancellation(
  176. h,
  177. [&](asio::cancellation_type ct)
  178. {
  179. for (auto cs : cancel)
  180. if (cs)
  181. cs->emit(ct);
  182. });
  183. this->coro.reset(h.address());
  184. return true;
  185. }
  186. BOOST_COBALT_MSVC_NOINLINE
  187. auto await_resume()
  188. {
  189. if (error)
  190. std::rethrow_exception(error);
  191. if constexpr(!all_void)
  192. return mp11::tuple_transform(
  193. []<typename T>(std::optional<T> & var)
  194. -> T
  195. {
  196. BOOST_ASSERT(var.has_value());
  197. return std::move(*var);
  198. }, result);
  199. }
  200. auto await_resume(const as_tuple_tag &)
  201. {
  202. using t = decltype(await_resume());
  203. if constexpr(!all_void)
  204. {
  205. if (error)
  206. return std::make_tuple(error, t{});
  207. else
  208. return std::make_tuple(std::current_exception(),
  209. mp11::tuple_transform(
  210. []<typename T>(std::optional<T> & var)
  211. -> T
  212. {
  213. BOOST_ASSERT(var.has_value());
  214. return std::move(*var);
  215. }, result));
  216. }
  217. else
  218. return std::make_tuple(error);
  219. }
  220. auto await_resume(const as_result_tag &)
  221. {
  222. using t = decltype(await_resume());
  223. using rt = system::result<t, std::exception_ptr>;
  224. if (error)
  225. return rt(system::in_place_error, error);
  226. if constexpr(!all_void)
  227. return mp11::tuple_transform(
  228. []<typename T>(std::optional<T> & var)
  229. -> rt
  230. {
  231. BOOST_ASSERT(var.has_value());
  232. return std::move(*var);
  233. }, result);
  234. else
  235. return rt{system::in_place_value};
  236. }
  237. };
  238. awaitable operator co_await() &&
  239. {
  240. return awaitable(args, std::make_index_sequence<sizeof...(Args)>{});
  241. }
  242. };
  243. template<typename Range>
  244. struct join_ranged_impl
  245. {
  246. Range aws;
  247. using result_type = co_await_result_t<std::decay_t<decltype(*std::begin(std::declval<Range>()))>>;
  248. constexpr static std::size_t result_size =
  249. sizeof(std::conditional_t<std::is_void_v<result_type>, variant2::monostate, result_type>);
  250. struct awaitable : fork::shared_state
  251. {
  252. struct dummy
  253. {
  254. template<typename ... Args>
  255. dummy(Args && ...) {}
  256. };
  257. using type = std::decay_t<decltype(*std::begin(std::declval<Range>()))>;
  258. #if !defined(BOOST_COBALT_NO_PMR)
  259. pmr::polymorphic_allocator<void> alloc{&resource};
  260. std::conditional_t<awaitable_type<type>, Range &,
  261. pmr::vector<co_awaitable_type<type>>> aws;
  262. pmr::vector<bool> ready{std::size(aws), alloc};
  263. pmr::vector<asio::cancellation_signal> cancel_{std::size(aws), alloc};
  264. pmr::vector<asio::cancellation_signal*> cancel{std::size(aws), alloc};
  265. std::conditional_t<
  266. std::is_void_v<result_type>,
  267. dummy,
  268. pmr::vector<std::optional<void_as_monostate<result_type>>>>
  269. result{
  270. cancel.size(),
  271. alloc};
  272. #else
  273. std::allocator<void> alloc;
  274. std::conditional_t<awaitable_type<type>, Range &, std::vector<co_awaitable_type<type>>> aws;
  275. std::vector<bool> ready{std::size(aws), alloc};
  276. std::vector<asio::cancellation_signal> cancel_{std::size(aws), alloc};
  277. std::vector<asio::cancellation_signal*> cancel{std::size(aws), alloc};
  278. std::conditional_t<
  279. std::is_void_v<result_type>,
  280. dummy,
  281. std::vector<std::optional<void_as_monostate<result_type>>>>
  282. result{
  283. cancel.size(),
  284. alloc};
  285. #endif
  286. std::exception_ptr error{};
  287. awaitable(Range & aws_, std::false_type /* needs operator co_await */)
  288. : fork::shared_state((512 + sizeof(co_awaitable_type<type>) + result_size) * std::size(aws_))
  289. , aws{alloc}
  290. , ready{std::size(aws_), alloc}
  291. , cancel_{std::size(aws_), alloc}
  292. , cancel{std::size(aws_), alloc}
  293. {
  294. aws.reserve(std::size(aws_));
  295. for (auto && a : aws_)
  296. {
  297. using a_0 = std::decay_t<decltype(a)>;
  298. using a_t = std::conditional_t<
  299. std::is_lvalue_reference_v<Range>, a_0 &, a_0 &&>;
  300. aws.emplace_back(awaitable_type_getter<a_t>(static_cast<a_t>(a)));
  301. }
  302. std::transform(std::begin(this->aws),
  303. std::end(this->aws),
  304. std::begin(ready),
  305. [](auto & aw) {return aw.await_ready();});
  306. }
  307. awaitable(Range & aws, std::true_type /* needs operator co_await */)
  308. : fork::shared_state((512 + sizeof(co_awaitable_type<type>) + result_size) * std::size(aws))
  309. , aws(aws)
  310. {
  311. std::transform(std::begin(aws), std::end(aws), std::begin(ready), [](auto & aw) {return aw.await_ready();});
  312. }
  313. awaitable(Range & aws)
  314. : awaitable(aws, std::bool_constant<awaitable_type<type>>{})
  315. {
  316. }
  317. void cancel_all()
  318. {
  319. for (auto & r : cancel)
  320. if (r)
  321. std::exchange(r, nullptr)->emit(asio::cancellation_type::all);
  322. }
  323. void interrupt_await()
  324. {
  325. using t = std::conditional_t<std::is_reference_v<Range>,
  326. co_awaitable_type<type> &,
  327. co_awaitable_type<type> &&>;
  328. if constexpr (interruptible<t>)
  329. {
  330. std::size_t idx = 0u;
  331. for (auto & aw : aws)
  332. if (cancel[idx])
  333. static_cast<t>(aw).interrupt_await();
  334. }
  335. }
  336. static detail::fork await_impl(awaitable & this_, std::size_t idx)
  337. BOOST_TRY
  338. {
  339. auto & aw = *std::next(std::begin(this_.aws), idx);
  340. auto rd = aw.await_ready();
  341. if (!rd)
  342. {
  343. this_.cancel[idx] = &this_.cancel_[idx];
  344. co_await this_.cancel[idx]->slot();
  345. co_await detail::fork::wired_up;
  346. if constexpr (std::is_void_v<decltype(aw.await_resume())>)
  347. co_await aw;
  348. else
  349. this_.result[idx].emplace(co_await aw);
  350. }
  351. else
  352. {
  353. if constexpr (std::is_void_v<decltype(aw.await_resume())>)
  354. aw.await_resume();
  355. else
  356. this_.result[idx].emplace(aw.await_resume());
  357. }
  358. }
  359. BOOST_CATCH(...)
  360. {
  361. if (!this_.error)
  362. this_.error = std::current_exception();
  363. this_.cancel_all();
  364. }
  365. BOOST_CATCH_END
  366. detail::fork last_forked;
  367. std::size_t last_index = 0u;
  368. bool await_ready()
  369. {
  370. while (last_index < cancel.size())
  371. {
  372. last_forked = await_impl(*this, last_index++);
  373. if (!last_forked.done())
  374. return false; // one coro didn't immediately complete!
  375. }
  376. last_forked.release();
  377. return true;
  378. }
  379. template<typename H>
  380. auto await_suspend(
  381. std::coroutine_handle<H> h
  382. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  383. , const boost::source_location & loc = BOOST_CURRENT_LOCATION
  384. #endif
  385. )
  386. {
  387. #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
  388. this->loc = loc;
  389. #endif
  390. exec = detail::get_executor(h);
  391. last_forked.release().resume();
  392. while (last_index < cancel.size())
  393. await_impl(*this, last_index++).release();
  394. if (error)
  395. cancel_all();
  396. if (!this->outstanding_work()) // already done, resume right away.
  397. return false;
  398. // arm the cancel
  399. assign_cancellation(
  400. h,
  401. [&](asio::cancellation_type ct)
  402. {
  403. for (auto cs : cancel)
  404. if (cs)
  405. cs->emit(ct);
  406. });
  407. this->coro.reset(h.address());
  408. return true;
  409. }
  410. auto await_resume(const as_tuple_tag & )
  411. {
  412. #if defined(BOOST_COBALT_NO_PMR)
  413. std::vector<result_type> rr;
  414. #else
  415. pmr::vector<result_type> rr{this_thread::get_allocator()};
  416. #endif
  417. if (error)
  418. return std::make_tuple(error, rr);
  419. if constexpr (!std::is_void_v<result_type>)
  420. {
  421. rr.reserve(result.size());
  422. for (auto & t : result)
  423. rr.push_back(*std::move(t));
  424. return std::make_tuple(std::exception_ptr(), std::move(rr));
  425. }
  426. }
  427. auto await_resume(const as_result_tag & )
  428. {
  429. #if defined(BOOST_COBALT_NO_PMR)
  430. std::vector<result_type> rr;
  431. #else
  432. pmr::vector<result_type> rr{this_thread::get_allocator()};
  433. #endif
  434. if (error)
  435. return system::result<decltype(rr), std::exception_ptr>(error);
  436. if constexpr (!std::is_void_v<result_type>)
  437. {
  438. rr.reserve(result.size());
  439. for (auto & t : result)
  440. rr.push_back(*std::move(t));
  441. return system::result<decltype(rr), std::exception_ptr>(std::move(rr));
  442. }
  443. }
  444. BOOST_COBALT_MSVC_NOINLINE
  445. auto await_resume()
  446. {
  447. if (error)
  448. std::rethrow_exception(error);
  449. if constexpr (!std::is_void_v<result_type>)
  450. {
  451. #if defined(BOOST_COBALT_NO_PMR)
  452. std::vector<result_type> rr;
  453. #else
  454. pmr::vector<result_type> rr{this_thread::get_allocator()};
  455. #endif
  456. rr.reserve(result.size());
  457. for (auto & t : result)
  458. rr.push_back(*std::move(t));
  459. return rr;
  460. }
  461. }
  462. };
  463. awaitable operator co_await() && {return awaitable{aws};}
  464. };
  465. }
  466. #endif //BOOST_COBALT_DETAIL_JOIN_HPP