From c483c5ca560d25ebf3bbc90c001d3440517bc0ac Mon Sep 17 00:00:00 2001 From: Vladislav Vaintroub Date: Tue, 16 Jul 2024 14:59:55 +0200 Subject: [PATCH] MDEV-33627 preparation - tpool fix Fix tpool to not use maintenance timer for fixed pool size. --- tpool/tpool_generic.cc | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/tpool/tpool_generic.cc b/tpool/tpool_generic.cc index 9ed832dd1ed..b4c6cef38cb 100644 --- a/tpool/tpool_generic.cc +++ b/tpool/tpool_generic.cc @@ -441,7 +441,7 @@ public: disarm(); } }; - timer_generic m_maintenance_timer; + timer_generic* m_maintenance_timer=nullptr; timer* create_timer(callback_func func, void *data) override { return new timer_generic(func, data, this); @@ -751,9 +751,16 @@ bool thread_pool_generic::add_thread() reset the flag in thread_pool_generic::worker_main in new thread created. The flag must be reset back in case we fail to create the thread. If this flag is not reset all future attempt to create thread for this pool would not work as - we would return from here. */ - if (m_thread_creation_pending.test_and_set()) - return false; + we would return from here. + + Do not use this flag for pool of fixed size. + (since they lack maintenence that would rectify the pool size, if it is too small) + */ + if (m_min_threads != m_max_threads) + { + if (m_thread_creation_pending.test_and_set()) + return false; + } worker_data *thread_data = m_thread_data_cache.get(); m_active_threads.push_back(thread_data); @@ -820,13 +827,16 @@ thread_pool_generic::thread_pool_generic(int min_threads, int max_threads) : m_min_threads(min_threads), m_max_threads(max_threads), m_last_thread_count(), - m_last_activity(), - m_maintenance_timer(thread_pool_generic::maintenance_func, this, nullptr) + m_last_activity() { set_concurrency(); // start the timer - m_maintenance_timer.set_time(0, (int)m_timer_interval.count()); + if (m_min_threads != m_max_threads) + { + m_maintenance_timer= new timer_generic(thread_pool_generic::maintenance_func, this, nullptr); + m_maintenance_timer->set_time(0, (int)m_timer_interval.count()); + } } @@ -933,7 +943,8 @@ void thread_pool_generic::switch_timer(timer_state_t state) long long period= (state == timer_state_t::OFF) ? m_timer_interval.count()*10: m_timer_interval.count(); - m_maintenance_timer.set_period((int)period); + if (m_maintenance_timer) + m_maintenance_timer->set_period((int)period); } @@ -951,7 +962,8 @@ thread_pool_generic::~thread_pool_generic() m_aio.reset(); /* Also stop the maintanence task early. */ - m_maintenance_timer.disarm(); + if (m_maintenance_timer) + m_maintenance_timer->disarm(); std::unique_lock lk(m_mtx); m_in_shutdown= true; @@ -967,6 +979,7 @@ thread_pool_generic::~thread_pool_generic() } lk.unlock(); + delete m_maintenance_timer; } thread_pool *create_thread_pool_generic(int min_threads, int max_threads)