diff --git a/src/lib_spark/spark/utils/thread_manager.h b/src/lib_spark/spark/utils/thread_manager.h index 1b3177b88..b873706ac 100644 --- a/src/lib_spark/spark/utils/thread_manager.h +++ b/src/lib_spark/spark/utils/thread_manager.h @@ -1,24 +1,46 @@ #pragma once +#include "spark/config/config.h" #include "system/protobufs/threads.pb.h" +#include "system/threading/thread_manager.h" #include +#include #include #include -namespace bts::system::protobufs { -class ThreadGroupCfg; -} - namespace bts { namespace threading { -std::thread make_thread_from_function( +namespace internal { + +inline std::mutex instance_mx_; +inline std::unique_ptr instance_{nullptr}; + +inline system::threading::ThreadManager &instance() { + if (instance_ == nullptr) { + std::lock_guard lock{instance_mx_}; + if (instance_ == nullptr) { + instance_ = std::make_unique( + config::get()); + } + } + return *instance_; +} + +} // namespace internal + +inline std::thread make_thread_from_function( std::string const &name, std::function const &c, - system::protobufs::ThreadGroupCfg const &cfg); -std::thread make_thread_from_function( - std::string const &name, std::function const &c); + system::protobufs::ThreadGroupCfg const &cfg) { + return internal::instance().make_thread_from_function(name, c, cfg); +} + +inline std::thread make_thread_from_function( + std::string const &name, std::function const &c) { + return internal::instance().make_thread_from_function(name, c); +} template std::thread make_thread(std::string const &name, C callable) { @@ -33,13 +55,22 @@ std::thread make_thread( return make_thread_from_function(name, std::function(callable), cfg); } -void configure_current_thread(std::string const &name); -void configure_current_thread( - std::string const &name, system::protobufs::ThreadGroupCfg const &); +inline void configure_current_thread(std::string const &name) { + return internal::instance().configure_current_thread(name); +} +inline void configure_current_thread( + std::string const &name, system::protobufs::ThreadGroupCfg const &cfg) { + return internal::instance().configure_current_thread(name, cfg); +} -system::protobufs::ThreadGroupCfg get_thread_cfg(std::string const &); +inline system::protobufs::ThreadGroupCfg get_thread_cfg( + std::string const &name) { + return internal::instance().get_thread_cfg(name); +} -std::vector get_affinity(std::string const &name); +inline std::vector get_affinity(std::string const &name) { + return internal::instance().get_affinity(name); +} } // namespace threading } // namespace bts diff --git a/src/lib_spark/spark/utils/thread_manager.cpp b/src/lib_system/system/threading/thread_manager.cpp similarity index 69% rename from src/lib_spark/spark/utils/thread_manager.cpp rename to src/lib_system/system/threading/thread_manager.cpp index 8301948fd..631075da0 100644 --- a/src/lib_spark/spark/utils/thread_manager.cpp +++ b/src/lib_system/system/threading/thread_manager.cpp @@ -1,7 +1,6 @@ #include "thread_manager.h" -#include "spark/config/config.h" -#include "spark/logging/logging.h" -#include "spark/utils/bts_assert.h" +#include "system/facades/bts_assert.h" +#include "system/os.h" #include "system/protobufs/threads.pb.h" #include @@ -11,21 +10,32 @@ #include #endif -namespace bts { -namespace threading { +namespace bts::system::threading { -system::protobufs::ThreadGroupCfg get_thread_cfg(std::string const &name) { - for (auto const &cfg : - config::get().thread_group()) { +struct ThreadManager::PImpl { + protobufs::Threads config; +}; + +ThreadManager::ThreadManager(protobufs::Threads config) + : me_(std::make_unique()) { + me_->config = std::move(config); +} + +ThreadManager::~ThreadManager() = default; + +system::protobufs::ThreadGroupCfg ThreadManager::get_thread_cfg( + std::string const &name) { + for (auto const &cfg : me_->config.thread_group()) { boost::regex rx(cfg.name_rx()); - if (boost::regex_match(name, rx)) + if (boost::regex_match(name, rx)) { return cfg; + } } bts_assert_msg( false, fmt::format("No configuration found for thread {}", name)); } -std::vector get_affinity(std::string const &name) { +std::vector ThreadManager::get_affinity(std::string const &name) { std::vector result; #ifndef _WIN32 auto cfg = get_thread_cfg(name); @@ -44,7 +54,7 @@ std::vector get_affinity(std::string const &name) { } #ifndef _WIN32 -void configure_thread( +void ThreadManager::configure_thread( std::string const &name, const system::protobufs::ThreadGroupCfg &cfg, pthread_t t) { @@ -65,23 +75,24 @@ void configure_thread( int rc = pthread_setaffinity_np(t, sizeof(cpu_set_t), &cpuset); - if (rc != 0) - LOG(Gen, Error, - "Could not set core affinity for {} on thread {}. Error " - "code={}", + if (rc != 0) { + const std::string err_msg = fmt::format( + "Could not set core affinity for {} on thread {}. Error code={}", name, cfg.name_rx(), rc); + std::cerr << err_msg << std::endl; + } } } #endif -void configure_current_thread(std::string const &name) { +void ThreadManager::configure_current_thread(std::string const &name) { #ifndef _WIN32 configure_thread(name, get_thread_cfg(name), pthread_self()); #endif os::set_thread_name(name); } -void configure_current_thread( +void ThreadManager::configure_current_thread( std::string const &name, const system::protobufs::ThreadGroupCfg &cfg) { #ifndef _WIN32 configure_thread(name, cfg, pthread_self()); @@ -89,7 +100,7 @@ void configure_current_thread( os::set_thread_name(name); } -std::thread make_thread_from_function( +std::thread ThreadManager::make_thread_from_function( std::string const &name, std::function const &c) { auto result = std::thread([c, name]() { os::set_thread_name(name); @@ -102,7 +113,7 @@ std::thread make_thread_from_function( return result; } -std::thread make_thread_from_function( +std::thread ThreadManager::make_thread_from_function( std::string const &name, std::function const &c, const system::protobufs::ThreadGroupCfg &cfg) { @@ -116,5 +127,4 @@ std::thread make_thread_from_function( return result; } -} // namespace threading -} // namespace bts +} // namespace bts::system::threading diff --git a/src/lib_system/system/threading/thread_manager.h b/src/lib_system/system/threading/thread_manager.h new file mode 100644 index 000000000..aab90c57f --- /dev/null +++ b/src/lib_system/system/threading/thread_manager.h @@ -0,0 +1,78 @@ +#pragma once + +#include "system/protobufs/threads.pb.h" + +#include +#include +#include +#include + +namespace bts::system::threading { + +class ThreadManager { + public: + explicit ThreadManager(protobufs::Threads config); + ~ThreadManager(); + + static system::protobufs::Threads default_thread_config(); + + static std::thread make_thread_from_function( + std::string const &name, + std::function const &c, + system::protobufs::ThreadGroupCfg const &cfg); + std::thread make_thread_from_function( + std::string const &name, std::function const &c); + + template + std::thread make_thread(std::string const &name, C callable); + + template + static std::thread make_thread( + std::string const &name, + C callable, + system::protobufs::ThreadGroupCfg const &cfg); + + void configure_current_thread(std::string const &name); + void configure_current_thread( + std::string const &name, system::protobufs::ThreadGroupCfg const &); + + system::protobufs::ThreadGroupCfg get_thread_cfg(std::string const &); + + std::vector get_affinity(std::string const &name); + + private: + struct PImpl; + std::unique_ptr me_; + +#ifndef _WIN32 + static void configure_thread( + std::string const &name, + const system::protobufs::ThreadGroupCfg &cfg, + pthread_t t); +#endif +}; + +////////////////////////////////////////////////// +// Definition +////////////////////////////////////////////////// + +inline system::protobufs::Threads ThreadManager::default_thread_config() { + system::protobufs::Threads rv; + *rv.mutable_thread_group()->Add()->mutable_name_rx() = ".*"; + return rv; +} + +template +std::thread ThreadManager::make_thread(std::string const &name, C callable) { + return make_thread_from_function(name, std::function(callable)); +} + +template +std::thread ThreadManager::make_thread( + std::string const &name, + C callable, + system::protobufs::ThreadGroupCfg const &cfg) { + return make_thread_from_function(name, std::function(callable), cfg); +} + +} // namespace bts::system::threading