Skip to content

Commit

Permalink
Fix thread names in nano::thread_pool (#4203)
Browse files Browse the repository at this point in the history
* Bump macOS target for compatibility with <latch>
* Fix assignment of thread names in `nano::thread_pool`
  • Loading branch information
pwojcikdev authored Apr 3, 2023
1 parent 62bdaba commit 30bf99e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ endif()

# OSX compatibility needs to be set before project is declared
set(CMAKE_OSX_DEPLOYMENT_TARGET
10.15
12
CACHE STRING "")

project(nano-node)
Expand Down
30 changes: 11 additions & 19 deletions nano/lib/threading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,16 @@ void nano::thread_runner::stop_event_processing ()
io_guard.get_executor ().context ().stop ();
}

/*
* thread_pool
*/

nano::thread_pool::thread_pool (unsigned num_threads, nano::thread_role::name thread_name) :
num_threads (num_threads),
thread_pool_m (std::make_unique<boost::asio::thread_pool> (num_threads))
thread_pool_m (std::make_unique<boost::asio::thread_pool> (num_threads)),
thread_names_latch{ num_threads }
{
set_thread_names (num_threads, thread_name);
set_thread_names (thread_name);
}

nano::thread_pool::~thread_pool ()
Expand Down Expand Up @@ -294,29 +299,16 @@ uint64_t nano::thread_pool::num_queued_tasks () const
return num_tasks;
}

// Set the names of all the threads in the thread pool for easier identification
void nano::thread_pool::set_thread_names (unsigned num_threads, nano::thread_role::name thread_name)
void nano::thread_pool::set_thread_names (nano::thread_role::name thread_name)
{
std::vector<std::promise<void>> promises (num_threads);
std::vector<std::future<void>> futures;
futures.reserve (num_threads);
std::transform (promises.begin (), promises.end (), std::back_inserter (futures), [] (auto & promise) {
return promise.get_future ();
});

for (auto i = 0u; i < num_threads; ++i)
{
boost::asio::post (*thread_pool_m, [&promise = promises[i], thread_name] () {
boost::asio::post (*thread_pool_m, [this, thread_name] () {
nano::thread_role::set (thread_name);
promise.set_value ();
thread_names_latch.arrive_and_wait ();
});
}

// Wait until all threads have finished
for (auto & future : futures)
{
future.wait ();
}
thread_names_latch.wait ();
}

std::unique_ptr<nano::container_info_component> nano::collect_container_info (thread_pool & thread_pool, std::string const & name)
Expand Down
5 changes: 4 additions & 1 deletion nano/lib/threading.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <boost/thread/thread.hpp>

#include <latch>
#include <thread>

namespace nano
Expand Down Expand Up @@ -208,7 +209,9 @@ class thread_pool final
std::unique_ptr<boost::asio::thread_pool> thread_pool_m;
relaxed_atomic_integral<uint64_t> num_tasks{ 0 };

void set_thread_names (unsigned num_threads, nano::thread_role::name thread_name);
/** Set the names of all the threads in the thread pool for easier identification */
std::latch thread_names_latch;
void set_thread_names (nano::thread_role::name thread_name);
};

std::unique_ptr<nano::container_info_component> collect_container_info (thread_pool & thread_pool, std::string const & name);
Expand Down

0 comments on commit 30bf99e

Please sign in to comment.