From 4fdc0ce08e4cd30ee54d2b473d3fbd7ac2cb0cfd Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Sun, 17 Sep 2023 19:52:28 +0100 Subject: [PATCH] Move nano::thread_runner in to its own file. Removes several boost/asio includes from threading.hpp. --- nano/core_test/socket.cpp | 1 + nano/core_test/system.cpp | 1 + nano/core_test/wallet.cpp | 1 + nano/lib/CMakeLists.txt | 2 + nano/lib/thread_runner.cpp | 84 +++++++++++++++++++++++++++++++++++ nano/lib/thread_runner.hpp | 31 +++++++++++++ nano/lib/threading.cpp | 85 ++---------------------------------- nano/lib/threading.hpp | 30 +++---------- nano/load_test/entry.cpp | 1 + nano/nano_node/daemon.cpp | 1 + nano/nano_node/entry.cpp | 1 + nano/nano_rpc/entry.cpp | 1 + nano/nano_wallet/entry.cpp | 2 +- nano/node/ipc/ipc_server.cpp | 1 + nano/rpc_test/rpc.cpp | 1 + nano/slow_test/bootstrap.cpp | 1 + nano/slow_test/node.cpp | 1 + 17 files changed, 137 insertions(+), 108 deletions(-) create mode 100644 nano/lib/thread_runner.cpp create mode 100644 nano/lib/thread_runner.hpp diff --git a/nano/core_test/socket.cpp b/nano/core_test/socket.cpp index d0b126da..ea716bb3 100644 --- a/nano/core_test/socket.cpp +++ b/nano/core_test/socket.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/nano/core_test/system.cpp b/nano/core_test/system.cpp index 2673d4de..41e7f9c8 100644 --- a/nano/core_test/system.cpp +++ b/nano/core_test/system.cpp @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/nano/core_test/wallet.cpp b/nano/core_test/wallet.cpp index e3d82d68..1e0363fc 100644 --- a/nano/core_test/wallet.cpp +++ b/nano/core_test/wallet.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include diff --git a/nano/lib/CMakeLists.txt b/nano/lib/CMakeLists.txt index 20024061..ad08425d 100644 --- a/nano/lib/CMakeLists.txt +++ b/nano/lib/CMakeLists.txt @@ -76,6 +76,8 @@ add_library( stream.hpp thread_roles.hpp thread_roles.cpp + thread_runner.hpp + thread_runner.cpp threading.hpp threading.cpp timer.hpp diff --git a/nano/lib/thread_runner.cpp b/nano/lib/thread_runner.cpp new file mode 100644 index 00000000..9826ee5e --- /dev/null +++ b/nano/lib/thread_runner.cpp @@ -0,0 +1,84 @@ +#include + +#include + +/* + * thread_runner + */ + +nano::thread_runner::thread_runner (boost::asio::io_context & io_ctx_a, unsigned num_threads, const nano::thread_role::name thread_role_a) : + io_guard{ boost::asio::make_work_guard (io_ctx_a) }, + role{ thread_role_a } +{ + for (auto i (0u); i < num_threads; ++i) + { + threads.emplace_back (nano::thread_attributes::get_default (), [this, &io_ctx_a] () { + nano::thread_role::set (role); + + // In a release build, catch and swallow any exceptions, + // In debug mode let if fall through + +#ifndef NDEBUG + run (io_ctx_a); +#else + try + { + run (io_ctx_a); + } + catch (std::exception const & ex) + { + std::cerr << ex.what () << std::endl; + } + catch (...) + { + } +#endif + }); + } +} + +nano::thread_runner::~thread_runner () +{ + join (); +} + +void nano::thread_runner::run (boost::asio::io_context & io_ctx_a) +{ +#if NANO_ASIO_HANDLER_TRACKING == 0 + io_ctx_a.run (); +#else + nano::timer<> timer; + timer.start (); + while (true) + { + timer.restart (); + // Run at most 1 completion handler and record the time it took to complete (non-blocking) + auto count = io_ctx_a.poll_one (); + if (count == 1 && timer.since_start ().count () >= NANO_ASIO_HANDLER_TRACKING) + { + auto timestamp = std::chrono::duration_cast (std::chrono::system_clock::now ().time_since_epoch ()).count (); + std::cout << (boost::format ("[%1%] io_thread held for %2%ms") % timestamp % timer.since_start ().count ()).str () << std::endl; + } + // Sleep for a bit to give more time slices to other threads + std::this_thread::sleep_for (std::chrono::milliseconds (5)); + std::this_thread::yield (); + } +#endif +} + +void nano::thread_runner::join () +{ + io_guard.reset (); + for (auto & i : threads) + { + if (i.joinable ()) + { + i.join (); + } + } +} + +void nano::thread_runner::stop_event_processing () +{ + io_guard.get_executor ().context ().stop (); +} diff --git a/nano/lib/thread_runner.hpp b/nano/lib/thread_runner.hpp new file mode 100644 index 00000000..7fbba2e0 --- /dev/null +++ b/nano/lib/thread_runner.hpp @@ -0,0 +1,31 @@ +#pragma once +#include +#include +#include +#include +#include + +#include + +namespace nano +{ +class thread_runner final +{ +public: + thread_runner (boost::asio::io_context &, unsigned num_threads, nano::thread_role::name thread_role = nano::thread_role::name::io); + ~thread_runner (); + + /** Tells the IO context to stop processing events.*/ + void stop_event_processing (); + /** Wait for IO threads to complete */ + void join (); + +private: + nano::thread_role::name const role; + std::vector threads; + boost::asio::executor_work_guard io_guard; + +private: + void run (boost::asio::io_context &); +}; +} // namespace nano diff --git a/nano/lib/threading.cpp b/nano/lib/threading.cpp index d6ef17e7..fd3ec258 100644 --- a/nano/lib/threading.cpp +++ b/nano/lib/threading.cpp @@ -1,7 +1,9 @@ -#include #include #include +#include +#include +#include #include #include @@ -19,87 +21,6 @@ boost::thread::attributes nano::thread_attributes::get_default () return attrs; } -/* - * thread_runner - */ - -nano::thread_runner::thread_runner (boost::asio::io_context & io_ctx_a, unsigned num_threads, const nano::thread_role::name thread_role_a) : - io_guard{ boost::asio::make_work_guard (io_ctx_a) }, - role{ thread_role_a } -{ - for (auto i (0u); i < num_threads; ++i) - { - threads.emplace_back (nano::thread_attributes::get_default (), [this, &io_ctx_a] () { - nano::thread_role::set (role); - - // In a release build, catch and swallow any exceptions, - // In debug mode let if fall through - -#ifndef NDEBUG - run (io_ctx_a); -#else - try - { - run (io_ctx_a); - } - catch (std::exception const & ex) - { - std::cerr << ex.what () << std::endl; - } - catch (...) - { - } -#endif - }); - } -} - -nano::thread_runner::~thread_runner () -{ - join (); -} - -void nano::thread_runner::run (boost::asio::io_context & io_ctx_a) -{ -#if NANO_ASIO_HANDLER_TRACKING == 0 - io_ctx_a.run (); -#else - nano::timer<> timer; - timer.start (); - while (true) - { - timer.restart (); - // Run at most 1 completion handler and record the time it took to complete (non-blocking) - auto count = io_ctx_a.poll_one (); - if (count == 1 && timer.since_start ().count () >= NANO_ASIO_HANDLER_TRACKING) - { - auto timestamp = std::chrono::duration_cast (std::chrono::system_clock::now ().time_since_epoch ()).count (); - std::cout << (boost::format ("[%1%] io_thread held for %2%ms") % timestamp % timer.since_start ().count ()).str () << std::endl; - } - // Sleep for a bit to give more time slices to other threads - std::this_thread::sleep_for (std::chrono::milliseconds (5)); - std::this_thread::yield (); - } -#endif -} - -void nano::thread_runner::join () -{ - io_guard.reset (); - for (auto & i : threads) - { - if (i.joinable ()) - { - i.join (); - } - } -} - -void nano::thread_runner::stop_event_processing () -{ - io_guard.get_executor ().context ().stop (); -} - /* * thread_pool */ diff --git a/nano/lib/threading.hpp b/nano/lib/threading.hpp index 3b82a2f8..f4b8ad8f 100644 --- a/nano/lib/threading.hpp +++ b/nano/lib/threading.hpp @@ -1,10 +1,5 @@ #pragma once -#include -#include -#include -#include -#include #include #include #include @@ -14,6 +9,11 @@ #include #include +namespace boost::asio +{ +class thread_pool; +} + namespace nano { namespace thread_attributes @@ -21,26 +21,6 @@ namespace thread_attributes boost::thread::attributes get_default (); } -class thread_runner final -{ -public: - thread_runner (boost::asio::io_context &, unsigned num_threads, nano::thread_role::name thread_role = nano::thread_role::name::io); - ~thread_runner (); - - /** Tells the IO context to stop processing events.*/ - void stop_event_processing (); - /** Wait for IO threads to complete */ - void join (); - -private: - nano::thread_role::name const role; - std::vector threads; - boost::asio::executor_work_guard io_guard; - -private: - void run (boost::asio::io_context &); -}; - class thread_pool final { public: diff --git a/nano/load_test/entry.cpp b/nano/load_test/entry.cpp index a2a37123..fcd06796 100644 --- a/nano/load_test/entry.cpp +++ b/nano/load_test/entry.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/nano/nano_node/daemon.cpp b/nano/nano_node/daemon.cpp index a8431fc6..50b4afda 100644 --- a/nano/nano_node/daemon.cpp +++ b/nano/nano_node/daemon.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include diff --git a/nano/nano_node/entry.cpp b/nano/nano_node/entry.cpp index 78698432..f16fd1c9 100644 --- a/nano/nano_node/entry.cpp +++ b/nano/nano_node/entry.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include diff --git a/nano/nano_rpc/entry.cpp b/nano/nano_rpc/entry.cpp index 49f5bc88..0dd7b79d 100644 --- a/nano/nano_rpc/entry.cpp +++ b/nano/nano_rpc/entry.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include diff --git a/nano/nano_wallet/entry.cpp b/nano/nano_wallet/entry.cpp index 39681540..906841cb 100644 --- a/nano/nano_wallet/entry.cpp +++ b/nano/nano_wallet/entry.cpp @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/nano/node/ipc/ipc_server.cpp b/nano/node/ipc/ipc_server.cpp index 7712871d..9eaf1e75 100644 --- a/nano/node/ipc/ipc_server.cpp +++ b/nano/node/ipc/ipc_server.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index a48fe545..59b83a9f 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include diff --git a/nano/slow_test/bootstrap.cpp b/nano/slow_test/bootstrap.cpp index 7004a74c..b722cf73 100644 --- a/nano/slow_test/bootstrap.cpp +++ b/nano/slow_test/bootstrap.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include diff --git a/nano/slow_test/node.cpp b/nano/slow_test/node.cpp index d63b2ac1..1e8ae0c0 100644 --- a/nano/slow_test/node.cpp +++ b/nano/slow_test/node.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include