Converting thread entrypoint from lamda to nano::thread_runner::run. (#4147)

This commit is contained in:
clemahieu 2023-02-22 11:55:21 +00:00 committed by GitHub
commit 430346c0ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 36 deletions

View file

@ -142,16 +142,44 @@ void nano::thread_attributes::set (boost::thread::attributes & attrs)
attrs_l->set_stack_size (8000000); // 8MB attrs_l->set_stack_size (8000000); // 8MB
} }
nano::thread_runner::thread_runner (boost::asio::io_context & io_ctx_a, unsigned service_threads_a) : nano::thread_runner::thread_runner (boost::asio::io_context & io_ctx_a, unsigned num_threads) :
io_guard (boost::asio::make_work_guard (io_ctx_a)) io_guard (boost::asio::make_work_guard (io_ctx_a))
{ {
boost::thread::attributes attrs; boost::thread::attributes attrs;
nano::thread_attributes::set (attrs); nano::thread_attributes::set (attrs);
for (auto i (0u); i < service_threads_a; ++i) for (auto i (0u); i < num_threads; ++i)
{ {
threads.emplace_back (attrs, [&io_ctx_a] () { threads.emplace_back (attrs, [this, &io_ctx_a] () {
nano::thread_role::set (nano::thread_role::name::io); nano::thread_role::set (nano::thread_role::name::io);
// In a release build, catch and swallow any exceptions,
// In debug mode let if fall through
#ifndef NDEBUG
run (io_ctx_a);
#else
try 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 #if NANO_ASIO_HANDLER_TRACKING == 0
io_ctx_a.run (); io_ctx_a.run ();
@ -174,32 +202,6 @@ nano::thread_runner::thread_runner (boost::asio::io_context & io_ctx_a, unsigned
} }
#endif #endif
} }
catch (std::exception const & ex)
{
std::cerr << ex.what () << std::endl;
#ifndef NDEBUG
throw;
#endif
}
catch (...)
{
#ifndef NDEBUG
/*
* In a release build, catch and swallow the
* io_context exception, in debug mode pass it
* on
*/
throw;
#endif
}
});
}
}
nano::thread_runner::~thread_runner ()
{
join ();
}
void nano::thread_runner::join () void nano::thread_runner::join ()
{ {

View file

@ -80,14 +80,18 @@ namespace thread_attributes
class thread_runner final class thread_runner final
{ {
public: public:
thread_runner (boost::asio::io_context &, unsigned); thread_runner (boost::asio::io_context &, unsigned num_threads);
~thread_runner (); ~thread_runner ();
/** Tells the IO context to stop processing events.*/ /** Tells the IO context to stop processing events.*/
void stop_event_processing (); void stop_event_processing ();
/** Wait for IO threads to complete */ /** Wait for IO threads to complete */
void join (); void join ();
std::vector<boost::thread> threads; std::vector<boost::thread> threads;
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> io_guard; boost::asio::executor_work_guard<boost::asio::io_context::executor_type> io_guard;
private:
void run (boost::asio::io_context &);
}; };
/* Default memory order of normal std::atomic operations is std::memory_order_seq_cst which provides /* Default memory order of normal std::atomic operations is std::memory_order_seq_cst which provides