Move nano::thread_runner in to its own file.

Removes several boost/asio includes from threading.hpp.
This commit is contained in:
Colin LeMahieu 2023-09-17 19:52:28 +01:00
commit 4fdc0ce08e
No known key found for this signature in database
GPG key ID: 43708520C8DFB938
17 changed files with 137 additions and 108 deletions

View file

@ -1,5 +1,6 @@
#include <nano/boost/asio/ip/address_v6.hpp>
#include <nano/boost/asio/ip/network_v6.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/lib/threading.hpp>
#include <nano/node/transport/socket.hpp>
#include <nano/test_common/system.hpp>

View file

@ -1,3 +1,4 @@
#include <nano/lib/thread_runner.hpp>
#include <nano/node/transport/inproc.hpp>
#include <nano/test_common/network.hpp>
#include <nano/test_common/system.hpp>

View file

@ -1,4 +1,5 @@
#include <nano/crypto_lib/random_pool.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/lib/threading.hpp>
#include <nano/node/lmdb/wallet_value.hpp>
#include <nano/test_common/system.hpp>

View file

@ -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

View file

@ -0,0 +1,84 @@
#include <nano/lib/thread_runner.hpp>
#include <iostream>
/*
* 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::microseconds> (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 ();
}

View file

@ -0,0 +1,31 @@
#pragma once
#include <nano/boost/asio/deadline_timer.hpp>
#include <nano/boost/asio/executor_work_guard.hpp>
#include <nano/boost/asio/io_context.hpp>
#include <nano/lib/thread_roles.hpp>
#include <nano/lib/threading.hpp>
#include <boost/thread.hpp>
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<boost::thread> threads;
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> io_guard;
private:
void run (boost::asio::io_context &);
};
} // namespace nano

View file

@ -1,7 +1,9 @@
#include <nano/boost/asio/post.hpp>
#include <nano/lib/config.hpp>
#include <nano/lib/threading.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/thread_pool.hpp>
#include <boost/format.hpp>
#include <future>
@ -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::microseconds> (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
*/

View file

@ -1,10 +1,5 @@
#pragma once
#include <nano/boost/asio/deadline_timer.hpp>
#include <nano/boost/asio/executor_work_guard.hpp>
#include <nano/boost/asio/io_context.hpp>
#include <nano/boost/asio/steady_timer.hpp>
#include <nano/boost/asio/thread_pool.hpp>
#include <nano/lib/relaxed_atomic.hpp>
#include <nano/lib/thread_roles.hpp>
#include <nano/lib/utility.hpp>
@ -14,6 +9,11 @@
#include <latch>
#include <thread>
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<boost::thread> threads;
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> io_guard;
private:
void run (boost::asio::io_context &);
};
class thread_pool final
{
public:

View file

@ -4,6 +4,7 @@
#include <nano/boost/beast/core/flat_buffer.hpp>
#include <nano/boost/beast/http.hpp>
#include <nano/boost/process/child.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/lib/threading.hpp>
#include <nano/lib/tomlconfig.hpp>
#include <nano/node/daemonconfig.hpp>

View file

@ -1,6 +1,7 @@
#include <nano/boost/process/child.hpp>
#include <nano/lib/signal_manager.hpp>
#include <nano/lib/stacktrace.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/lib/threading.hpp>
#include <nano/lib/tlsconfig.hpp>
#include <nano/lib/utility.hpp>

View file

@ -1,5 +1,6 @@
#include <nano/crypto_lib/random_pool.hpp>
#include <nano/lib/cli.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/lib/utility.hpp>
#include <nano/nano_node/daemon.hpp>
#include <nano/node/cli.hpp>

View file

@ -1,6 +1,7 @@
#include <nano/lib/cli.hpp>
#include <nano/lib/errors.hpp>
#include <nano/lib/signal_manager.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/lib/threading.hpp>
#include <nano/lib/tlsconfig.hpp>
#include <nano/lib/utility.hpp>

View file

@ -3,7 +3,7 @@
#include <nano/lib/cli.hpp>
#include <nano/lib/errors.hpp>
#include <nano/lib/rpcconfig.hpp>
#include <nano/lib/threading.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/lib/tlsconfig.hpp>
#include <nano/lib/tomlconfig.hpp>
#include <nano/lib/utility.hpp>

View file

@ -5,6 +5,7 @@
#include <nano/lib/config.hpp>
#include <nano/lib/ipc.hpp>
#include <nano/lib/locks.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/lib/threading.hpp>
#include <nano/lib/timer.hpp>
#include <nano/node/ipc/action_handler.hpp>

View file

@ -1,6 +1,7 @@
#include <nano/boost/beast/core/flat_buffer.hpp>
#include <nano/boost/beast/http.hpp>
#include <nano/lib/rpcconfig.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/lib/threading.hpp>
#include <nano/node/ipc/ipc_server.hpp>
#include <nano/node/json_handler.hpp>

View file

@ -1,4 +1,5 @@
#include <nano/lib/rpcconfig.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/node/bootstrap/bootstrap_server.hpp>
#include <nano/node/bootstrap_ascending/service.hpp>
#include <nano/node/ipc/ipc_server.hpp>

View file

@ -1,4 +1,5 @@
#include <nano/crypto_lib/random_pool.hpp>
#include <nano/lib/thread_runner.hpp>
#include <nano/lib/threading.hpp>
#include <nano/node/election.hpp>
#include <nano/node/scheduler/component.hpp>