detached.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // Copyright (c) 2022 Klemens Morgenstern ([email protected])
  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_DETACHED_HPP
  8. #define BOOST_COBALT_DETAIL_DETACHED_HPP
  9. #include <boost/cobalt/detail/exception.hpp>
  10. #include <boost/cobalt/detail/forward_cancellation.hpp>
  11. #include <boost/cobalt/detail/wrapper.hpp>
  12. #include <boost/cobalt/detail/this_thread.hpp>
  13. #include <boost/asio/cancellation_signal.hpp>
  14. #include <boost/core/exchange.hpp>
  15. #include <coroutine>
  16. #include <optional>
  17. #include <utility>
  18. #include <boost/asio/bind_allocator.hpp>
  19. namespace boost::cobalt
  20. {
  21. struct detached;
  22. namespace detail
  23. {
  24. struct detached_promise
  25. : promise_memory_resource_base,
  26. promise_cancellation_base<asio::cancellation_slot, asio::enable_total_cancellation>,
  27. promise_throw_if_cancelled_base,
  28. enable_awaitables<detached_promise>,
  29. enable_await_allocator<detached_promise>,
  30. enable_await_executor<detached_promise>
  31. {
  32. using promise_cancellation_base<asio::cancellation_slot, asio::enable_total_cancellation>::await_transform;
  33. using promise_throw_if_cancelled_base::await_transform;
  34. using enable_awaitables<detached_promise>::await_transform;
  35. using enable_await_allocator<detached_promise>::await_transform;
  36. using enable_await_executor<detached_promise>::await_transform;
  37. [[nodiscard]] detached get_return_object();
  38. std::suspend_never await_transform(
  39. cobalt::this_coro::reset_cancellation_source_t<asio::cancellation_slot> reset) noexcept
  40. {
  41. this->reset_cancellation_source(reset.source);
  42. return {};
  43. }
  44. using executor_type = executor;
  45. executor_type exec;
  46. const executor_type & get_executor() const {return exec;}
  47. template<typename ... Args>
  48. detached_promise(Args & ...args)
  49. :
  50. #if !defined(BOOST_COBALT_NO_PMR)
  51. promise_memory_resource_base(detail::get_memory_resource_from_args(args...)),
  52. #endif
  53. exec{detail::get_executor_from_args(args...)}
  54. {
  55. }
  56. std::suspend_never initial_suspend() {return {};}
  57. std::suspend_never final_suspend() noexcept {return {};}
  58. void return_void() {}
  59. void unhandled_exception()
  60. {
  61. throw ;
  62. }
  63. };
  64. }
  65. }
  66. #endif //BOOST_COBALT_DETAIL_DETACHED_HPP