From 3fae41ef2291ffa6b71de2d5ce4503cf824a7c77 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Tue, 23 May 2023 14:41:35 +0200 Subject: [PATCH] Restart thread pool after freezing Configuration The user (-D) or we could have changed Configuration.Concurrency, so correct the thread pool's thread amount. --- icinga-app/icinga.cpp | 2 ++ lib/base/threadpool.cpp | 18 +++++++++++++++++- lib/base/threadpool.hpp | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/icinga-app/icinga.cpp b/icinga-app/icinga.cpp index f5b6809bc..5674a9ff2 100644 --- a/icinga-app/icinga.cpp +++ b/icinga-app/icinga.cpp @@ -451,6 +451,8 @@ static int Main() Configuration::Concurrency = std::thread::hardware_concurrency(); } + Application::GetTP().Restart(); + /* Ensure that all defined constants work in the way we expect them. */ HandleLegacyDefines(); diff --git a/lib/base/threadpool.cpp b/lib/base/threadpool.cpp index 21f19fd42..dc76e7b15 100644 --- a/lib/base/threadpool.cpp +++ b/lib/base/threadpool.cpp @@ -20,10 +20,15 @@ void ThreadPool::Start() boost::unique_lock lock (m_Mutex); if (!m_Pool) { - m_Pool = decltype(m_Pool)(new boost::asio::thread_pool(Configuration::Concurrency * 2u)); + InitializePool(); } } +void ThreadPool::InitializePool() +{ + m_Pool = decltype(m_Pool)(new boost::asio::thread_pool(Configuration::Concurrency * 2u)); +} + void ThreadPool::Stop() { boost::unique_lock lock (m_Mutex); @@ -33,3 +38,14 @@ void ThreadPool::Stop() m_Pool = nullptr; } } + +void ThreadPool::Restart() +{ + boost::unique_lock lock (m_Mutex); + + if (m_Pool) { + m_Pool->join(); + } + + InitializePool(); +} diff --git a/lib/base/threadpool.hpp b/lib/base/threadpool.hpp index f9760bc8b..d30fa694c 100644 --- a/lib/base/threadpool.hpp +++ b/lib/base/threadpool.hpp @@ -42,6 +42,7 @@ public: void Start(); void Stop(); + void Restart(); /** * Appends a work item to the work queue. Work items will be processed in FIFO order. @@ -91,6 +92,8 @@ private: boost::shared_mutex m_Mutex; std::unique_ptr m_Pool; Atomic m_Pending; + + void InitializePool(); }; }