From be318d6ed4f4db048a2c58c41c66a78789e17f27 Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Fri, 4 Feb 2022 22:15:59 +0100 Subject: [PATCH] threads pool: inital code + unit tests dummy --- src/common/threads_pool.h | 102 +++++++++++++++++++ tests/performance_tests/main.cpp | 30 +++--- tests/performance_tests/threads_pool_tests.h | 15 +++ 3 files changed, 134 insertions(+), 13 deletions(-) create mode 100644 src/common/threads_pool.h create mode 100644 tests/performance_tests/threads_pool_tests.h diff --git a/src/common/threads_pool.h b/src/common/threads_pool.h new file mode 100644 index 00000000..b30e7b57 --- /dev/null +++ b/src/common/threads_pool.h @@ -0,0 +1,102 @@ +// Copyright (c) 2014-2019 Zano Project +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#pragma once + +#include +#include +#include + +namespace utils +{ + + struct call_executor_base + { + virtual void execute()=0; + }; + + template + struct call_executor_t : public call_executor_base + { + call_executor_t(t_executor_func f) :m_func(f) + {} + t_executor_func m_func; + virtual void execute() + { + m_func(); + } + }; + + template + std::shared_ptr build_call_executor(t_executor_func func) + { + std::shared_ptr res = new call_executor_t(func); + return res; + } + + class threads_pool + { + void init() + { + m_is_stop = false; + int num_threads = std::thread::hardware_concurrency(); + + for (int i = 0; i < num_threads; i++) + { + m_threads.push_back(std::thread(worker_func)); + } + } + + threads_pool(): m_is_stop(false), m_threads_counter(0) + {} + + template + bool add_job(t_executor_func func) + { + { + std::unique_lock lock(m_queue_mutex); + m_jobs_que.push_back(build_call_executor(func)); + } + m_condition.notify_one(); + return true; + } + + private: + void worker_func() + { + LOG_PRINT_L0("Worker thread is started"); + while (true) + { + std::shared_ptr job; + { + std::unique_lock lock(m_queue_mutex); + + m_condition.wait(lock, [this]() + { + return !m_jobs_que.empty() || m_is_stop; + }); + if (m_is_stop) + { + LOG_PRINT_L0("Worker thread is finished"); + return; + } + + job = m_jobs_que.front(); + m_jobs_que.pop_front(); + } + + job->execute(); + } + } + + + + std::list> m_jobs_que; + std::condition_variable m_condition; + std::mutex m_queue_mutex; + std::vector m_threads; + atomic m_is_stop; + std::atomic m_threads_counter; + }; +} \ No newline at end of file diff --git a/tests/performance_tests/main.cpp b/tests/performance_tests/main.cpp index c3f0a77a..e8d7a2ed 100644 --- a/tests/performance_tests/main.cpp +++ b/tests/performance_tests/main.cpp @@ -23,6 +23,8 @@ #include "print_struct_to_json.h" #include "free_space_check.h" #include "htlc_hash_tests.h" +#include "threads_pool_tests.h" + int main(int argc, char** argv) { @@ -33,21 +35,23 @@ int main(int argc, char** argv) epee::log_space::log_singletone::get_default_log_file().c_str(), epee::log_space::log_singletone::get_default_log_folder().c_str()); - - std::string buf1 = tools::get_varint_data(CURRENCY_PUBLIC_ADDRESS_BASE58_PREFIX); - std::string buf2 = tools::get_varint_data(CURRENCY_PUBLIC_INTEG_ADDRESS_BASE58_PREFIX); - std::string buf3 = tools::get_varint_data(CURRENCY_PUBLIC_INTEG_ADDRESS_V2_BASE58_PREFIX); - std::string buf4 = tools::get_varint_data(CURRENCY_PUBLIC_AUDITABLE_ADDRESS_BASE58_PREFIX); - std::string buf5 = tools::get_varint_data(CURRENCY_PUBLIC_AUDITABLE_INTEG_ADDRESS_BASE58_PREFIX); - std::cout << "Buf1: " << epee::string_tools::buff_to_hex_nodelimer(buf1) << ENDL; - std::cout << "Buf2: " << epee::string_tools::buff_to_hex_nodelimer(buf2) << ENDL; - std::cout << "Buf3: " << epee::string_tools::buff_to_hex_nodelimer(buf3) << ENDL; - std::cout << "Buf4: " << epee::string_tools::buff_to_hex_nodelimer(buf4) << ENDL; - std::cout << "Buf5: " << epee::string_tools::buff_to_hex_nodelimer(buf5) << ENDL; + thread_pool_tests(); - - do_htlc_hash_tests(); +// std::string buf1 = tools::get_varint_data(CURRENCY_PUBLIC_ADDRESS_BASE58_PREFIX); +// std::string buf2 = tools::get_varint_data(CURRENCY_PUBLIC_INTEG_ADDRESS_BASE58_PREFIX); +// std::string buf3 = tools::get_varint_data(CURRENCY_PUBLIC_INTEG_ADDRESS_V2_BASE58_PREFIX); +// std::string buf4 = tools::get_varint_data(CURRENCY_PUBLIC_AUDITABLE_ADDRESS_BASE58_PREFIX); +// std::string buf5 = tools::get_varint_data(CURRENCY_PUBLIC_AUDITABLE_INTEG_ADDRESS_BASE58_PREFIX); +// +// std::cout << "Buf1: " << epee::string_tools::buff_to_hex_nodelimer(buf1) << ENDL; +// std::cout << "Buf2: " << epee::string_tools::buff_to_hex_nodelimer(buf2) << ENDL; +// std::cout << "Buf3: " << epee::string_tools::buff_to_hex_nodelimer(buf3) << ENDL; +// std::cout << "Buf4: " << epee::string_tools::buff_to_hex_nodelimer(buf4) << ENDL; +// std::cout << "Buf5: " << epee::string_tools::buff_to_hex_nodelimer(buf5) << ENDL; +// +// + //do_htlc_hash_tests(); //run_serialization_performance_test(); //return 1; //run_core_market_performance_tests(100000); diff --git a/tests/performance_tests/threads_pool_tests.h b/tests/performance_tests/threads_pool_tests.h new file mode 100644 index 00000000..2494dae8 --- /dev/null +++ b/tests/performance_tests/threads_pool_tests.h @@ -0,0 +1,15 @@ +// Copyright (c) 2019 The Zano developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#pragma once + +#include +#include "include_base_utils.h" +#include "threads_pool.h" + + +inline +void thread_pool_tests() +{ +} \ No newline at end of file