Skip to content

Commit

Permalink
ASYNC SPAWN
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Apr 20, 2024
1 parent bae6e67 commit 8314179
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
13 changes: 11 additions & 2 deletions nano/lib/async.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,26 @@ class cancellation
return signal->slot ();
}

private:
nano::async::strand & strand;

private:
std::unique_ptr<asio::cancellation_signal> signal; // Wrap the signal in a unique_ptr to enable moving

bool slotted{ false };
};

auto spawn (nano::async::strand & strand, nano::async::cancellation & cancellation, auto && func)
{
debug_assert (cancellation.strand == strand);

auto fut = asio::co_spawn (strand, std::forward<decltype (func)> (func), asio::bind_cancellation_slot (cancellation.slot (), asio::use_future));
return fut;
}

auto spawn (nano::async::strand & strand, auto && func)
{
nano::async::cancellation cancellation{ strand };
auto fut = asio::co_spawn (strand, std::forward<decltype (func)> (func), asio::bind_cancellation_slot (cancellation.slot (), asio::use_future));
auto fut = spawn (strand, cancellation, std::forward<decltype (func)> (func));
return std::make_pair (std::move (fut), std::move (cancellation));
}
}
11 changes: 4 additions & 7 deletions nano/node/transport/tcp_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ void nano::transport::tcp_listener::start ()
throw;
}

future = asio::co_spawn (
strand, [this] () -> asio::awaitable<void> {
future = nano::async::spawn (
strand, cancellation, [this] () -> asio::awaitable<void> {
co_await nano::async::setup_this_coro ();
try
{
Expand All @@ -88,7 +88,7 @@ void nano::transport::tcp_listener::start ()
{
logger.critical (nano::log::type::tcp_listener, "Unknown error");
release_assert (false); // Unexpected error
} }, asio::bind_cancellation_slot (cancellation.slot (), asio::use_future));
} });

cleanup_thread = std::thread ([this] {
nano::thread_role::set (nano::thread_role::name::tcp_listener);
Expand Down Expand Up @@ -234,12 +234,9 @@ bool nano::transport::tcp_listener::connect (asio::ip::address ip, uint16_t port
stats.inc (nano::stat::type::tcp_listener, nano::stat::detail::connect_initiate, nano::stat::dir::out);
logger.debug (nano::log::type::tcp_listener, "Initiating outgoing connection to: {}", fmt::streamed (endpoint));

// auto future = asio::co_spawn (node.io_ctx, connect_impl (endpoint), asio::use_future);
auto [future, cancellation] = nano::async::spawn (strand, connect_impl (endpoint));

attempt att{ endpoint, std::move (future), std::move (cancellation) };

// attempts.emplace ();
attempts.emplace_back (attempt{ endpoint, std::move (future), std::move (cancellation) });

return true; // Attempt started
}
Expand Down

0 comments on commit 8314179

Please sign in to comment.