spawn.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_SPAWN_HPP
  8. #define BOOST_COBALT_DETAIL_SPAWN_HPP
  9. #include <boost/cobalt/task.hpp>
  10. #include <boost/asio/dispatch.hpp>
  11. #include <boost/smart_ptr/allocate_unique.hpp>
  12. namespace boost::cobalt
  13. {
  14. template<typename T>
  15. struct task;
  16. }
  17. namespace boost::cobalt::detail
  18. {
  19. struct async_initiate_spawn
  20. {
  21. template<typename Handler, typename T>
  22. void operator()(Handler && h, task<T> a, executor exec)
  23. {
  24. auto & rec = a.receiver_;
  25. if (rec.done)
  26. return asio::dispatch(
  27. asio::get_associated_immediate_executor(h, exec),
  28. asio::append(std::forward<Handler>(h), rec.exception, rec.exception ? T() : *rec.get_result()));
  29. #if !defined(BOOST_COBALT_NO_PMR)
  30. auto dalloc = pmr::polymorphic_allocator<void>{boost::cobalt::this_thread::get_default_resource()};
  31. auto alloc = asio::get_associated_allocator(h, dalloc);
  32. #else
  33. auto alloc = asio::get_associated_allocator(h);
  34. #endif
  35. auto recs = allocate_unique<detail::task_receiver<T>>(alloc, std::move(rec));
  36. auto sl = asio::get_associated_cancellation_slot(h);
  37. if (sl.is_connected())
  38. sl.template emplace<detail::forward_dispatch_cancellation>(recs->promise->signal, exec);
  39. auto p = recs.get();
  40. p->promise->exec.emplace(exec);
  41. p->promise->exec_ = exec;
  42. struct completion_handler
  43. {
  44. using allocator_type = std::decay_t<decltype(alloc)>;
  45. allocator_type get_allocator() const { return alloc_; }
  46. allocator_type alloc_;
  47. using executor_type = std::decay_t<decltype(asio::get_associated_executor(h, exec))>;
  48. const executor_type &get_executor() const { return exec_; }
  49. executor_type exec_;
  50. decltype(recs) r;
  51. Handler handler;
  52. void operator()()
  53. {
  54. auto ex = r->exception;
  55. T rr{};
  56. if (r->result)
  57. rr = std::move(*r->result);
  58. r.reset();
  59. std::move(handler)(ex, std::move(rr));
  60. }
  61. };
  62. p->awaited_from.reset(detail::post_coroutine(
  63. completion_handler{
  64. alloc, asio::get_associated_executor(h, exec), std::move(recs), std::move(h)
  65. }).address());
  66. asio::dispatch(exec, std::coroutine_handle<detail::task_promise<T>>::from_promise(*p->promise));
  67. }
  68. template<typename Handler>
  69. void operator()(Handler && h, task<void> a, executor exec)
  70. {
  71. if (a.receiver_.done)
  72. return asio::dispatch(
  73. asio::get_associated_immediate_executor(h, exec),
  74. asio::append(std::forward<Handler>(h), a.receiver_.exception));
  75. #if !defined(BOOST_COBALT_NO_PMR)
  76. auto alloc = asio::get_associated_allocator(h, pmr::polymorphic_allocator<void>{boost::cobalt::this_thread::get_default_resource()});
  77. #else
  78. auto alloc = asio::get_associated_allocator(h);
  79. #endif
  80. auto recs = allocate_unique<detail::task_receiver<void>>(alloc, std::move(a.receiver_));
  81. if (recs->done)
  82. return asio::dispatch(asio::get_associated_immediate_executor(h, exec),
  83. asio::append(std::forward<Handler>(h), recs->exception));
  84. auto sl = asio::get_associated_cancellation_slot(h);
  85. if (sl.is_connected())
  86. sl.template emplace<detail::forward_dispatch_cancellation>(recs->promise->signal, exec);
  87. auto p = recs.get();
  88. p->promise->exec.emplace(exec);
  89. p->promise->exec_ = exec;
  90. struct completion_handler
  91. {
  92. using allocator_type = std::decay_t<decltype(alloc)>;
  93. const allocator_type &get_allocator() const { return alloc_; }
  94. allocator_type alloc_;
  95. using executor_type = std::decay_t<decltype(asio::get_associated_executor(h, exec))>;
  96. const executor_type &get_executor() const { return exec_; }
  97. executor_type exec_;
  98. decltype(recs) r;
  99. Handler handler;
  100. void operator()()
  101. {
  102. auto ex = r->exception;
  103. r.reset();
  104. std::move(handler)(ex);
  105. }
  106. };
  107. p->awaited_from.reset(detail::post_coroutine(completion_handler{
  108. alloc, asio::get_associated_executor(h, exec), std::move(recs), std::forward<Handler>(h)
  109. }).address());
  110. asio::dispatch(exec, std::coroutine_handle<detail::task_promise<void>>::from_promise(*p->promise));
  111. }
  112. };
  113. }
  114. #endif //BOOST_COBALT_DETAIL_SPAWN_HPP