From 83141793891833c9a8791ec69a63641f6d507659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sun, 21 Apr 2024 01:22:57 +0200 Subject: [PATCH] ASYNC SPAWN --- nano/lib/async.hpp | 13 +++++++++++-- nano/node/transport/tcp_listener.cpp | 11 ++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/nano/lib/async.hpp b/nano/lib/async.hpp index 8f3ca2beac..e2b8aa4456 100644 --- a/nano/lib/async.hpp +++ b/nano/lib/async.hpp @@ -52,17 +52,26 @@ class cancellation return signal->slot (); } -private: nano::async::strand & strand; + +private: std::unique_ptr 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 (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 (func), asio::bind_cancellation_slot (cancellation.slot (), asio::use_future)); + auto fut = spawn (strand, cancellation, std::forward (func)); return std::make_pair (std::move (fut), std::move (cancellation)); } } \ No newline at end of file diff --git a/nano/node/transport/tcp_listener.cpp b/nano/node/transport/tcp_listener.cpp index 15c4b113dc..bcf3e34836 100644 --- a/nano/node/transport/tcp_listener.cpp +++ b/nano/node/transport/tcp_listener.cpp @@ -67,8 +67,8 @@ void nano::transport::tcp_listener::start () throw; } - future = asio::co_spawn ( - strand, [this] () -> asio::awaitable { + future = nano::async::spawn ( + strand, cancellation, [this] () -> asio::awaitable { co_await nano::async::setup_this_coro (); try { @@ -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); @@ -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 }