From ebe6848362dd2667300106494035f32d69a845bf Mon Sep 17 00:00:00 2001 From: Klemens Morgenstern Date: Tue, 29 Oct 2024 23:44:02 +0800 Subject: [PATCH] removed requirement for executor const refs. --- include/boost/cobalt/detail/fork.hpp | 2 +- include/boost/cobalt/detail/gather.hpp | 4 +- include/boost/cobalt/detail/handler.hpp | 50 +++++++++++-------------- include/boost/cobalt/detail/join.hpp | 4 +- include/boost/cobalt/detail/race.hpp | 4 +- 5 files changed, 29 insertions(+), 35 deletions(-) diff --git a/include/boost/cobalt/detail/fork.hpp b/include/boost/cobalt/detail/fork.hpp index 230ee049..9192ec88 100644 --- a/include/boost/cobalt/detail/fork.hpp +++ b/include/boost/cobalt/detail/fork.hpp @@ -56,7 +56,7 @@ struct fork bool outstanding_work() {return use_count != 0u;} - const executor * exec = nullptr; + std::optional exec = nullptr; bool wired_up() {return exec != nullptr;} using executor_type = executor; diff --git a/include/boost/cobalt/detail/gather.hpp b/include/boost/cobalt/detail/gather.hpp index 171be8e1..32521556 100644 --- a/include/boost/cobalt/detail/gather.hpp +++ b/include/boost/cobalt/detail/gather.hpp @@ -163,7 +163,7 @@ struct gather_variadic_impl #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) this->loc = loc; #endif - this->exec = &cobalt::detail::get_executor(h); + this->exec = cobalt::detail::get_executor(h); last_forked.release().resume(); while (last_index < tuple_size) impls[last_index++](*this).release(); @@ -362,7 +362,7 @@ struct gather_ranged_impl #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) this->loc = loc; #endif - exec = &detail::get_executor(h); + exec = detail::get_executor(h); last_forked.release().resume(); while (last_index < cancel.size()) diff --git a/include/boost/cobalt/detail/handler.hpp b/include/boost/cobalt/detail/handler.hpp index 72d9b510..3091c6a6 100644 --- a/include/boost/cobalt/detail/handler.hpp +++ b/include/boost/cobalt/detail/handler.hpp @@ -73,6 +73,24 @@ struct completion_handler_noop_executor }; +template +executor +get_executor(std::coroutine_handle h) +{ + if constexpr (requires {h.promise().get_executor();}) + { + return h.promise().get_executor(); + } + else + return this_thread::get_executor(); +} + +inline executor +get_executor(std::coroutine_handle<>) +{ + return this_thread::get_executor(); +} + struct completion_handler_base { @@ -84,7 +102,7 @@ struct completion_handler_base } using executor_type = executor; - const executor_type & executor_ ; + executor_type executor_ ; const executor_type & get_executor() const noexcept { return executor_ ; @@ -113,11 +131,10 @@ struct completion_handler_base } template - requires (requires (Promise p) {{p.get_executor()} -> std::same_as;}) completion_handler_base(std::coroutine_handle h, completed_immediately_t * completed_immediately = nullptr) : cancellation_slot(asio::get_associated_cancellation_slot(h.promise())), - executor_(h.promise().get_executor()), + executor_(cobalt::detail::get_executor(h)), #if !defined(BOOST_COBALT_NO_PMR) allocator(asio::get_associated_allocator(h.promise(), this_thread::get_allocator())), #else @@ -128,24 +145,22 @@ struct completion_handler_base } #if !defined(BOOST_COBALT_NO_PMR) template - requires (requires (Promise p) {{p.get_executor()} -> std::same_as;}) completion_handler_base(std::coroutine_handle h, pmr::memory_resource * resource, completed_immediately_t * completed_immediately = nullptr) : cancellation_slot(asio::get_associated_cancellation_slot(h.promise())), - executor_(h.promise().get_executor()), + executor_(cobalt::detail::get_executor(h)), allocator(resource), completed_immediately(completed_immediately) { } #else template - requires (requires (Promise p) {{p.get_executor()} -> std::same_as;}) completion_handler_base(std::coroutine_handle h, detail::sbo_resource * resource, completed_immediately_t * completed_immediately = nullptr) : cancellation_slot(asio::get_associated_cancellation_slot(h.promise())), - executor_(h.promise().get_executor()), + executor_(cobalt::detail::get_executor(h)), allocator(resource), completed_immediately(completed_immediately) { @@ -166,27 +181,6 @@ void assign_cancellation(std::coroutine_handle h, Handler && func) h.promise().get_cancellation_slot().assign(std::forward(func)); } -template -const executor & -get_executor(std::coroutine_handle h) -{ - if constexpr (requires {h.promise().get_executor();}) - { - static_assert(std::same_as, - "for performance reasons, the get_executor function on a promise must return a const reference"); - return h.promise().get_executor(); - } - else - return this_thread::get_executor(); -} - -inline const executor & -get_executor(std::coroutine_handle<>) -{ - return this_thread::get_executor(); -} - } template diff --git a/include/boost/cobalt/detail/join.hpp b/include/boost/cobalt/detail/join.hpp index 221ab53e..7fb1e68f 100644 --- a/include/boost/cobalt/detail/join.hpp +++ b/include/boost/cobalt/detail/join.hpp @@ -192,7 +192,7 @@ struct join_variadic_impl #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) this->loc = loc; #endif - this->exec = &detail::get_executor(h); + this->exec = detail::get_executor(h); last_forked.release().resume(); while (last_index < tuple_size) impls[last_index++](*this).release(); @@ -450,7 +450,7 @@ struct join_ranged_impl #if defined(BOOST_ASIO_ENABLE_HANDLER_TRACKING) this->loc = loc; #endif - exec = &detail::get_executor(h); + exec = detail::get_executor(h); last_forked.release().resume(); while (last_index < cancel.size()) diff --git a/include/boost/cobalt/detail/race.hpp b/include/boost/cobalt/detail/race.hpp index 87e13c4e..36854c4b 100644 --- a/include/boost/cobalt/detail/race.hpp +++ b/include/boost/cobalt/detail/race.hpp @@ -305,7 +305,7 @@ struct race_variadic_impl { this->loc = loc; - this->exec = &cobalt::detail::get_executor(h); + this->exec = cobalt::detail::get_executor(h); last_forked.release().resume(); if (!this->outstanding_work()) // already done, resume rightaway. @@ -617,7 +617,7 @@ struct race_ranged_impl const boost::source_location & loc = BOOST_CURRENT_LOCATION) { this->loc = loc; - this->exec = &detail::get_executor(h); + this->exec = detail::get_executor(h); last_forked.release().resume(); if (!this->outstanding_work()) // already done, resume rightaway.