@@ -22,12 +22,11 @@ using namespace std::literals::chrono_literals;
2222
2323namespace org ::apache::nifi::minifi::utils {
2424
25- ThreadPool::ThreadPool (int max_worker_threads, ControllerServiceProvider controller_service_provider, std::string name)
25+ ThreadPool::ThreadPool (int max_worker_threads, std::string name)
2626 : thread_reduction_count_(0 ),
2727 max_worker_threads_ (max_worker_threads),
2828 current_workers_(0 ),
2929 running_(false ),
30- controller_service_provider_(std::move(controller_service_provider)),
3130 name_(std::move(name)),
3231 logger_(core::logging::LoggerFactory<ThreadPool>::getLogger()) {
3332}
@@ -141,71 +140,15 @@ void ThreadPool::manageWorkers() {
141140 }
142141 }
143142
144- if (nullptr != thread_manager_) {
145- while (running_) {
146- auto wait_period = 500ms;
147- {
148- std::unique_lock<std::recursive_mutex> manager_lock (manager_mutex_, std::try_to_lock);
149- if (!manager_lock.owns_lock ()) {
150- // Threadpool is being stopped/started or config is being changed, better wait a bit
151- std::this_thread::sleep_for (10ms);
152- continue ;
153- }
154- if (thread_manager_->isAboveMax (current_workers_)) {
155- auto max = 1 ; // thread_manager_->getMaxConcurrentTasks();
156- auto differential = current_workers_ - max;
157- thread_reduction_count_ += differential;
158- } else if (thread_manager_->shouldReduce ()) {
159- if (current_workers_ > 1 )
160- thread_reduction_count_++;
161- thread_manager_->reduce ();
162- } else if (thread_manager_->canIncrease () && max_worker_threads_ > current_workers_) { // increase slowly
163- std::unique_lock<std::mutex> worker_queue_lock (worker_queue_mutex_);
164- auto worker_thread = std::make_shared<WorkerThread>();
165- worker_thread->thread_ = createThread ([this , worker_thread] { run_tasks (worker_thread); });
166- thread_queue_.push_back (worker_thread);
167- current_workers_++;
168- }
169- std::shared_ptr<WorkerThread> thread_ref;
170- while (deceased_thread_queue_.tryDequeue (thread_ref)) {
171- std::unique_lock<std::mutex> worker_queue_lock (worker_queue_mutex_);
172- if (thread_ref->thread_ .joinable ())
173- thread_ref->thread_ .join ();
174- thread_queue_.erase (std::remove (thread_queue_.begin (), thread_queue_.end (), thread_ref), thread_queue_.end ());
175- }
176- }
177- std::this_thread::sleep_for (wait_period);
178- }
179- } else {
180- for (auto &thread : thread_queue_) {
181- if (thread->thread_ .joinable ())
182- thread->thread_ .join ();
183- }
184- }
185- }
186-
187- std::shared_ptr<controllers::ThreadManagementService> ThreadPool::createThreadManager () const {
188- if (!controller_service_provider_) {
189- return nullptr ;
190- }
191- auto service = controller_service_provider_ (" ThreadPoolManager" );
192- if (!service) {
193- logger_->log_info (" Could not find a ThreadPoolManager service" );
194- return nullptr ;
143+ for (auto &thread : thread_queue_) {
144+ if (thread->thread_ .joinable ())
145+ thread->thread_ .join ();
195146 }
196- auto thread_manager_service = std::dynamic_pointer_cast<controllers::ThreadManagementService>(service);
197- if (!thread_manager_service) {
198- logger_->log_error (" Found ThreadPoolManager, but it is not a ThreadManagementService" );
199- return nullptr ;
200- }
201- return thread_manager_service;
202147}
203148
204149void ThreadPool::start () {
205150 std::lock_guard<std::recursive_mutex> lock (manager_mutex_);
206151 if (!running_) {
207- thread_manager_ = createThreadManager ();
208-
209152 running_ = true ;
210153 worker_queue_.start ();
211154 manager_thread_ = std::thread (&ThreadPool::manageWorkers, this );
0 commit comments