Move nano::thread_runner in to its own file.
Removes several boost/asio includes from threading.hpp.
This commit is contained in:
parent
8607a66d74
commit
4fdc0ce08e
17 changed files with 137 additions and 108 deletions
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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
|
||||
|
|
84
nano/lib/thread_runner.cpp
Normal file
84
nano/lib/thread_runner.cpp
Normal 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 ();
|
||||
}
|
31
nano/lib/thread_runner.hpp
Normal file
31
nano/lib/thread_runner.hpp
Normal 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
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue