From a2bfcd25a1ddedac207822b2af7602413956b730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CMariusz=20Trela=E2=80=9D?= Date: Tue, 10 Oct 2023 13:29:59 +0200 Subject: [PATCH] Remove `blockchain_worker_thread_pool::get_instance` function --- .../chain/blockchain_worker_thread_pool.cpp | 16 +++++++--------- libraries/chain/database.cpp | 6 +++--- .../hive/chain/blockchain_worker_thread_pool.hpp | 15 +++++++++++---- libraries/chain/include/hive/chain/database.hpp | 4 ++-- libraries/plugins/chain/chain_plugin.cpp | 2 +- programs/util/block_log_test.cpp | 2 +- programs/util/block_log_util.cpp | 2 +- programs/util/compress_block_log.cpp | 2 +- programs/util/dump_hive_schema.cpp | 4 ++-- programs/util/test_block_log.cpp | 2 +- tests/unit/db_fixture/database_fixture.cpp | 4 ---- tests/unit/db_fixture/database_fixture.hpp | 4 ++-- tests/unit/db_fixture/hived_fixture.cpp | 3 +++ tests/unit/tests/block_tests.cpp | 2 +- 14 files changed, 36 insertions(+), 32 deletions(-) diff --git a/libraries/chain/blockchain_worker_thread_pool.cpp b/libraries/chain/blockchain_worker_thread_pool.cpp index b123cd97af..4b9ed0702d 100644 --- a/libraries/chain/blockchain_worker_thread_pool.cpp +++ b/libraries/chain/blockchain_worker_thread_pool.cpp @@ -117,10 +117,15 @@ void blockchain_worker_thread_pool::impl::thread_function() } } +void blockchain_worker_thread_pool::impl_deleter::operator()(blockchain_worker_thread_pool::impl* ptr) const +{ + delete ptr; +} + //std::shared_ptr fill_queue_thread = std::make_shared([&](){ fill_pending_queue(input_block_log_path / "block_log"); }); blockchain_worker_thread_pool::blockchain_worker_thread_pool( appbase::application& app ) : - my(std::make_unique( app, [this]( const std::vector>& full_transactions, data_source_type data_source, - std::optional block_number ){ this->enqueue_work( full_transactions, data_source, block_number ); } )) + my(std::unique_ptr( new impl( app, [this]( const std::vector>& full_transactions, data_source_type data_source, + std::optional block_number ){ this->enqueue_work( full_transactions, data_source, block_number ); } ) ) ) { lazy_init(); } @@ -510,12 +515,5 @@ void blockchain_worker_thread_pool::shutdown() thread_pool_size = new_thread_pool_size; } -/* static */ blockchain_worker_thread_pool& blockchain_worker_thread_pool::get_instance( appbase::application& app ) -{ - static blockchain_worker_thread_pool thread_pool( app ); - thread_pool.lazy_init(); - return thread_pool; -} - } } // end namespace hive::chain diff --git a/libraries/chain/database.cpp b/libraries/chain/database.cpp index f3f35e773a..3be2555f9b 100644 --- a/libraries/chain/database.cpp +++ b/libraries/chain/database.cpp @@ -156,7 +156,7 @@ database::~database() clear_pending(); } -void database::open( const open_args& args, hive::chain::blockchain_worker_thread_pool& thread_pool ) +void database::open( const open_args& args ) { try { @@ -169,14 +169,14 @@ void database::open( const open_args& args, hive::chain::blockchain_worker_threa ); const bool wipe_shared_file = args.force_replay || args.load_snapshot; chainbase::database::open( args.shared_mem_dir, args.chainbase_flags, args.shared_file_size, args.database_cfg, &environment_extension, wipe_shared_file ); - initialize_state_independent_data(args, thread_pool); + initialize_state_independent_data(args); load_state_initial_data(args); } FC_CAPTURE_LOG_AND_RETHROW( (args.data_dir)(args.shared_mem_dir)(args.shared_file_size) ) } -void database::initialize_state_independent_data(const open_args& args, hive::chain::blockchain_worker_thread_pool& thread_pool) +void database::initialize_state_independent_data(const open_args& args) { _my->create_new_decoded_types_data_storage(); _my->_decoded_types_data_storage->register_new_type(); diff --git a/libraries/chain/include/hive/chain/blockchain_worker_thread_pool.hpp b/libraries/chain/include/hive/chain/blockchain_worker_thread_pool.hpp index 899ca252db..510c3d7459 100644 --- a/libraries/chain/include/hive/chain/blockchain_worker_thread_pool.hpp +++ b/libraries/chain/include/hive/chain/blockchain_worker_thread_pool.hpp @@ -31,10 +31,18 @@ class blockchain_worker_thread_pool public: struct impl; private: - blockchain_worker_thread_pool( appbase::application& app ); + class impl_deleter + { + public: + void operator()(blockchain_worker_thread_pool::impl* ptr) const; + }; + void lazy_init(); - std::unique_ptr my; -public: + std::unique_ptr my; +public: + + blockchain_worker_thread_pool( appbase::application& app ); + // when we process a block/transaction, we need to know where it came from in // order to know what processing it needs. e.g., transactions that arrived // in blocks usually won't have their signatures validated, while stand-alone @@ -66,7 +74,6 @@ class blockchain_worker_thread_pool void shutdown(); static void set_thread_pool_size(uint32_t thread_pool_size); - static blockchain_worker_thread_pool& get_instance( appbase::application& app ); }; } } // end namespace hive::chain diff --git a/libraries/chain/include/hive/chain/database.hpp b/libraries/chain/include/hive/chain/database.hpp index b8549b9fbe..8d426c99ad 100644 --- a/libraries/chain/include/hive/chain/database.hpp +++ b/libraries/chain/include/hive/chain/database.hpp @@ -193,7 +193,7 @@ namespace chain { * * @param data_dir Path to open or create database in */ - void open( const open_args& args, hive::chain::blockchain_worker_thread_pool& thread_pool ); + void open( const open_args& args ); private: @@ -204,7 +204,7 @@ namespace chain { void remove_proposal_votes_for_accounts_without_voting_rights(); /// Allows to load all data being independent to the persistent storage held in shared memory file. - void initialize_state_independent_data(const open_args& args, hive::chain::blockchain_worker_thread_pool& thread_pool); + void initialize_state_independent_data(const open_args& args); void begin_type_register_process(util::abstract_type_registrar& r); diff --git a/libraries/plugins/chain/chain_plugin.cpp b/libraries/plugins/chain/chain_plugin.cpp index 8a03cd6a8f..4ca95f3f95 100644 --- a/libraries/plugins/chain/chain_plugin.cpp +++ b/libraries/plugins/chain/chain_plugin.cpp @@ -736,7 +736,7 @@ void chain_plugin_impl::open() db_open_args.block_log_compression_level, db_open_args.enable_block_log_auto_fixing, thread_pool ); - db.open( db_open_args, thread_pool ); + db.open( db_open_args ); if( dump_memory_details ) { diff --git a/programs/util/block_log_test.cpp b/programs/util/block_log_test.cpp index c4aee83a8a..de13bf89d8 100644 --- a/programs/util/block_log_test.cpp +++ b/programs/util/block_log_test.cpp @@ -40,7 +40,7 @@ int main(int argc, char** argv) try { appbase::application theApp; - hive::chain::blockchain_worker_thread_pool& thread_pool = hive::chain::blockchain_worker_thread_pool::get_instance( theApp ); + hive::chain::blockchain_worker_thread_pool thread_pool = hive::chain::blockchain_worker_thread_pool( theApp ); boost::program_options::options_description options("Allowed options"); options.add_options()("input-block-log,i", boost::program_options::value()->required(), "The path pointing the input block log file"); diff --git a/programs/util/block_log_util.cpp b/programs/util/block_log_util.cpp index 14349b6f34..ac7edf390a 100644 --- a/programs/util/block_log_util.cpp +++ b/programs/util/block_log_util.cpp @@ -620,7 +620,7 @@ bool get_block_artifacts(const fc::path& block_log_path, const std::optional()->default_value(4), "The number of worker threads to spawn"); diff --git a/programs/util/compress_block_log.cpp b/programs/util/compress_block_log.cpp index c40789eebf..369538d58a 100644 --- a/programs/util/compress_block_log.cpp +++ b/programs/util/compress_block_log.cpp @@ -425,7 +425,7 @@ int main(int argc, char** argv) try { appbase::application theApp; - hive::chain::blockchain_worker_thread_pool& thread_pool = hive::chain::blockchain_worker_thread_pool::get_instance( theApp ); + hive::chain::blockchain_worker_thread_pool thread_pool = hive::chain::blockchain_worker_thread_pool( theApp ); // zstd doesn't have well-defined levels, so we get these at runtime std::ostringstream zstd_levels_description_stream; zstd_levels_description_stream << "The zstd compression level to use"; diff --git a/programs/util/dump_hive_schema.cpp b/programs/util/dump_hive_schema.cpp index 9c205232a0..ad7563d4dd 100644 --- a/programs/util/dump_hive_schema.cpp +++ b/programs/util/dump_hive_schema.cpp @@ -77,7 +77,7 @@ FC_REFLECT( hive_schema, (schema_map)(chain_object_types) ) int main( int argc, char** argv, char** envp ) { appbase::application app; - hive::chain::blockchain_worker_thread_pool& thread_pool = hive::chain::blockchain_worker_thread_pool::get_instance( app ); + hive::chain::blockchain_worker_thread_pool thread_pool = hive::chain::blockchain_worker_thread_pool( app ); hive::chain::database db( app ); hive::chain::sync_block_writer block_writer( db, app ); @@ -96,7 +96,7 @@ int main( int argc, char** argv, char** envp ) db_args.block_log_compression_level, db_args.enable_block_log_auto_fixing, thread_pool ); - db.open( db_args, thread_pool ); + db.open( db_args ); hive_schema ss; diff --git a/programs/util/test_block_log.cpp b/programs/util/test_block_log.cpp index 67b819c5ba..c189903734 100644 --- a/programs/util/test_block_log.cpp +++ b/programs/util/test_block_log.cpp @@ -21,7 +21,7 @@ int main( int argc, char** argv, char** envp ) try { appbase::application theApp; - hive::chain::blockchain_worker_thread_pool& thread_pool = hive::chain::blockchain_worker_thread_pool::get_instance( theApp ); + hive::chain::blockchain_worker_thread_pool thread_pool = hive::chain::blockchain_worker_thread_pool( theApp ); //hive::chain::database db; hive::chain::block_log log( theApp ); diff --git a/tests/unit/db_fixture/database_fixture.cpp b/tests/unit/db_fixture/database_fixture.cpp index e85b43051f..4fdf4ea343 100644 --- a/tests/unit/db_fixture/database_fixture.cpp +++ b/tests/unit/db_fixture/database_fixture.cpp @@ -97,10 +97,6 @@ fc::path common_init( appbase::application& app, const std::function< void( appb return _data_dir; } -database_fixture::database_fixture(): thread_pool( hive::chain::blockchain_worker_thread_pool::get_instance( theApp ) ) -{ -} - fc::ecc::private_key database_fixture::generate_private_key(string seed) { static const fc::ecc::private_key committee = fc::ecc::private_key::regenerate( fc::sha256::hash( string( "init_key" ) ) ); diff --git a/tests/unit/db_fixture/database_fixture.hpp b/tests/unit/db_fixture/database_fixture.hpp index 524a7db3a6..05ff03105f 100644 --- a/tests/unit/db_fixture/database_fixture.hpp +++ b/tests/unit/db_fixture/database_fixture.hpp @@ -261,9 +261,9 @@ struct database_fixture { bool skip_key_index_test = false; appbase::application theApp; - hive::chain::blockchain_worker_thread_pool& thread_pool; + hive::chain::blockchain_worker_thread_pool* thread_pool = nullptr; - database_fixture(); + database_fixture() {} virtual ~database_fixture() {} static fc::ecc::private_key generate_private_key( string seed = "init_key" ); diff --git a/tests/unit/db_fixture/hived_fixture.cpp b/tests/unit/db_fixture/hived_fixture.cpp index 1c1b1c7056..40c6ef4c3a 100644 --- a/tests/unit/db_fixture/hived_fixture.cpp +++ b/tests/unit/db_fixture/hived_fixture.cpp @@ -117,6 +117,9 @@ void hived_fixture::postponed_init_impl( const config_arg_override_t& config_arg BOOST_REQUIRE( db ); db->_log_hardforks = false; + thread_pool = &chain.get_thread_pool(); + BOOST_REQUIRE( thread_pool ); + // Load configuration file into logging config structure, used to create loggers & appenders. // Store the structure for further examination (in tests). _logging_config = app.load_logging_config(); diff --git a/tests/unit/tests/block_tests.cpp b/tests/unit/tests/block_tests.cpp index ddecf8f186..b3bd28239e 100644 --- a/tests/unit/tests/block_tests.cpp +++ b/tests/unit/tests/block_tests.cpp @@ -117,7 +117,7 @@ void open_test_database( database& db, sync_block_writer& sbw, args.block_log_compression_level, args.enable_block_log_auto_fixing, thread_pool ); - db.open( args, thread_pool ); + db.open( args ); } #define SET_UP_DATABASE( NAME, APP, DATA_DIR_PATH, LOG_HARDFORKS ) \