Skip to content

Commit

Permalink
adapted to recent asio changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
klemens-morgenstern committed Oct 26, 2023
1 parent c5de1d5 commit 22923f8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 39 deletions.
91 changes: 57 additions & 34 deletions include/boost/cobalt/detail/spawn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,36 @@ struct async_initiate_spawn

p->promise->exec.emplace(exec);
p->promise->exec_ = exec;

struct completion_handler
{
using allocator_type = std::decay_t<decltype(alloc)>;

allocator_type get_allocator() const { return alloc; }
allocator_type alloc;

using executor_type = std::decay_t<decltype(asio::get_associated_executor(h, exec))>;
const executor_type &get_executor() const { return exec; }
executor_type exec;

decltype(recs) r;
Handler h;

void operator()()
{
auto ex = r->exception;
T rr{};
if (r->result)
rr = std::move(*r->result);
r.reset();
std::move(h)(ex, std::move(rr));
}
};

p->awaited_from.reset(detail::post_coroutine(
asio::bind_executor(
asio::get_associated_executor(h, exec),
asio::bind_allocator(
alloc,
[r = std::move(recs),
h = std::move(h)]() mutable
{

auto ex = r->exception;
T rr{};
if (r->result)
rr = std::move(*r->result);
r.reset();
h(ex, std::move(rr));
}
)
)
).address());
completion_handler{
alloc, asio::get_associated_executor(h, exec), std::move(recs), std::move(h)
}).address());

asio::dispatch(exec, std::coroutine_handle<detail::task_promise<T>>::from_promise(*p->promise));
}
Expand Down Expand Up @@ -100,22 +111,34 @@ struct async_initiate_spawn

p->promise->exec.emplace(exec);
p->promise->exec_ = exec;
p->awaited_from.reset(detail::post_coroutine(
asio::bind_executor(
asio::get_associated_executor(h, exec),
asio::bind_allocator(
alloc,
[r = std::move(recs),
h = std::move(h)]() mutable
{
auto ex = r->exception;
r.reset();
h(ex);
}
)
)
).address());


struct completion_handler
{
using allocator_type = std::decay_t<decltype(alloc)>;

const allocator_type &get_allocator() const { return alloc; }

allocator_type alloc;

using executor_type = std::decay_t<decltype(asio::get_associated_executor(h, exec))>;
const executor_type &get_executor() const { return exec; }

executor_type exec;
decltype(recs) r;
Handler h;

void operator()()
{
auto ex = r->exception;
r.reset();
std::move(h)(ex);
}
};

p->awaited_from.reset(detail::post_coroutine(completion_handler{
alloc, asio::get_associated_executor(h, exec), std::move(recs), std::forward<Handler>(h)
}).address());

asio::dispatch(exec, std::coroutine_handle<detail::task_promise<void>>::from_promise(*p->promise));
}
};
Expand Down
19 changes: 14 additions & 5 deletions test/wrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@ BOOST_AUTO_TEST_CASE(regular)
#if !defined(BOOST_COBALT_NO_PMR)
char buf[512];
boost::cobalt::pmr::monotonic_buffer_resource res{buf, 512};
struct completion
{
bool & ran;
using allocator_type = boost::cobalt::pmr::polymorphic_allocator<void>;
allocator_type get_allocator() const { return alloc; }
boost::cobalt::pmr::polymorphic_allocator<void> alloc;
void operator()()
{
ran = true;
}
};

auto p = boost::cobalt::detail::post_coroutine(ctx.get_executor(),
boost::asio::bind_allocator(
boost::cobalt::pmr::polymorphic_allocator<void>(&res),
[&]{ran = true;}
)
completion{ran, boost::cobalt::pmr::polymorphic_allocator<void>(&res)}
);
#else
auto p = boost::cobalt::detail::post_coroutine(ctx.get_executor(), [&]{ran = true;});
auto p = boost::cobalt::detail::post_coroutine(ctx.get_executor(), [&]{ran = true;});
#endif
BOOST_CHECK(p);
BOOST_CHECK(!ran);
Expand Down

0 comments on commit 22923f8

Please sign in to comment.