Skip to content

Commit

Permalink
removed requirement for executor const refs.
Browse files Browse the repository at this point in the history
  • Loading branch information
klemens-morgenstern committed Oct 29, 2024
1 parent 2eb10b3 commit ebe6848
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion include/boost/cobalt/detail/fork.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct fork

bool outstanding_work() {return use_count != 0u;}

const executor * exec = nullptr;
std::optional<executor> exec = nullptr;
bool wired_up() {return exec != nullptr;}

using executor_type = executor;
Expand Down
4 changes: 2 additions & 2 deletions include/boost/cobalt/detail/gather.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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())
Expand Down
50 changes: 22 additions & 28 deletions include/boost/cobalt/detail/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ struct completion_handler_noop_executor

};

template<typename Promise>
executor
get_executor(std::coroutine_handle<Promise> 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
{
Expand All @@ -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_ ;
Expand Down Expand Up @@ -113,11 +131,10 @@ struct completion_handler_base
}

template<typename Promise>
requires (requires (Promise p) {{p.get_executor()} -> std::same_as<const executor&>;})
completion_handler_base(std::coroutine_handle<Promise> 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
Expand All @@ -128,24 +145,22 @@ struct completion_handler_base
}
#if !defined(BOOST_COBALT_NO_PMR)
template<typename Promise>
requires (requires (Promise p) {{p.get_executor()} -> std::same_as<const executor&>;})
completion_handler_base(std::coroutine_handle<Promise> 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<typename Promise>
requires (requires (Promise p) {{p.get_executor()} -> std::same_as<const executor&>;})
completion_handler_base(std::coroutine_handle<Promise> 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)
{
Expand All @@ -166,27 +181,6 @@ void assign_cancellation(std::coroutine_handle<Promise> h, Handler && func)
h.promise().get_cancellation_slot().assign(std::forward<Handler>(func));
}

template<typename Promise>
const executor &
get_executor(std::coroutine_handle<Promise> h)
{
if constexpr (requires {h.promise().get_executor();})
{
static_assert(std::same_as<decltype(h.promise().get_executor()),
const executor &>,
"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<typename ... Args>
Expand Down
4 changes: 2 additions & 2 deletions include/boost/cobalt/detail/join.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions include/boost/cobalt/detail/race.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit ebe6848

Please sign in to comment.