arpadboda commented on a change in pull request #735: WIP: MINIFICPP-1158 - 
Event driven processors can starve each other
URL: https://github.com/apache/nifi-minifi-cpp/pull/735#discussion_r382065632
 
 

 ##########
 File path: libminifi/src/utils/ThreadPool.cpp
 ##########
 @@ -0,0 +1,238 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "utils/ThreadPool.h"
+#include "core/state/StateManager.h"
+
+namespace org {
+namespace apache {
+namespace nifi {
+namespace minifi {
+namespace utils {
+
+template<typename T>
+void ThreadPool<T>::run_tasks(std::shared_ptr<WorkerThread> thread) {
+  thread->is_running_ = true;
+  while (running_.load()) {
+    if (UNLIKELY(thread_reduction_count_ > 0)) {
+      if (--thread_reduction_count_ >= 0) {
+        deceased_thread_queue_.enqueue(thread);
+        thread->is_running_ = false;
+        break;
+      } else {
+        thread_reduction_count_++;
+      }
+    }
+
+    Worker<T> task;
+    if (worker_queue_.try_dequeue(task)) {
+      if (task_status_[task.getIdentifier()] && task.run()) {
+        if(task.getTimeSlice() <= std::chrono::steady_clock::now()) {
+          // it can be rescheduled again as soon as there is a worker available
+          worker_queue_.enqueue(std::move(task));
+          continue;
+        }
+        // Task will be put to the delayed queue as next exec time is in the 
future
+        std::unique_lock<std::mutex> lock(worker_queue_mutex_);
+        bool need_to_notify =
+            delayed_worker_queue_.empty() || task.getTimeSlice() < 
delayed_worker_queue_.top().getTimeSlice();
+
+        delayed_worker_queue_.push(std::move(task));
+        if(need_to_notify) {
+          delayed_task_available_.notify_all();
+        }
+      }
+    } else {
+      std::unique_lock<std::mutex> lock(worker_queue_mutex_);
+      tasks_available_.wait(lock);
+    }
+  }
+  current_workers_--;
+}
+
+template<typename T>
+void ThreadPool<T>::manage_delayed_queue() {
+  while(running_) {
+    std::unique_lock<std::mutex> lock(worker_queue_mutex_);
+
+    // Put the tasks ready to run in the worker queue
+    while(!delayed_worker_queue_.empty() && 
delayed_worker_queue_.top().getTimeSlice() < std::chrono::steady_clock::now()) {
+      // I'm very sorry for this - committee must has been seriously drunk 
when the interface of prio queue was submitted.
+      Worker<T> task = 
std::move(const_cast<Worker<T>&>(delayed_worker_queue_.top()));
+      delayed_worker_queue_.pop();
+      worker_queue_.enqueue(std::move(task));
+      tasks_available_.notify_one();
+    }
+    if(delayed_worker_queue_.empty()) {
+      delayed_task_available_.wait(lock);
+    } else {
+      auto wait_time = 
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now()
 - delayed_worker_queue_.top().getTimeSlice());
+      delayed_task_available_.wait_for(lock, wait_time);
+    }
+  }
+}
+
+template<typename T>
+bool ThreadPool<T>::execute(Worker<T> &&task, std::future<T> &future) {
+  {
+    std::unique_lock<std::mutex> lock(worker_queue_mutex_);
+    task_status_[task.getIdentifier()] = true;
+  }
+  future = std::move(task.getPromise()->get_future());
+  bool enqueued = worker_queue_.enqueue(std::move(task));
+  if (running_) {
+    tasks_available_.notify_one();
+  }
+
+  task_count_++;
+
+  return enqueued;
+}
+
+template<typename T>
+void ThreadPool<T>::manageWorkers() {
+  for (int i = 0; i < max_worker_threads_; i++) {
+    std::stringstream thread_name;
+    thread_name << name_ << " #" << i;
+    auto worker_thread = std::make_shared<WorkerThread>(thread_name.str());
+    worker_thread->thread_ = createThread(std::bind(&ThreadPool::run_tasks, 
this, worker_thread));
+    thread_queue_.push_back(worker_thread);
+    current_workers_++;
+  }
+
+  if (daemon_threads_) {
+    for (auto &thread : thread_queue_) {
+      thread->thread_.detach();
+    }
+  }
+
+// likely don't have a thread manager
+  if (LIKELY(nullptr != thread_manager_)) {
+    while (running_) {
+      auto waitperiod = std::chrono::milliseconds(500);
+      {
+        std::lock_guard<std::recursive_mutex> lock(manager_mutex_);
+        if (thread_manager_->isAboveMax(current_workers_)) {
+          auto max = thread_manager_->getMaxConcurrentTasks();
+          auto differential = current_workers_ - max;
+          thread_reduction_count_ += differential;
+        } else if (thread_manager_->shouldReduce()) {
+          if (current_workers_ > 1)
+            thread_reduction_count_++;
+          thread_manager_->reduce();
+        } else if (thread_manager_->canIncrease() && max_worker_threads_ > 
current_workers_) {  // increase slowly
+          std::unique_lock<std::mutex> lock(worker_queue_mutex_);
+          auto worker_thread = std::make_shared<WorkerThread>();
+          worker_thread->thread_ = 
createThread(std::bind(&ThreadPool::run_tasks, this, worker_thread));
+          if (daemon_threads_) {
+            worker_thread->thread_.detach();
+          }
+          thread_queue_.push_back(worker_thread);
+          current_workers_++;
+        }
+      }
+      {
+        std::lock_guard<std::recursive_mutex> lock(manager_mutex_);
 
 Review comment:
   Ah, sorry, I missed the lines. In this case you are right, merged the blocks 
to grab the mutex only once. 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to