[Lldb-commits] [PATCH] D40587: [lldb] Minor fixes in TaskPool

Alexander Shaposhnikov via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Nov 30 14:56:37 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL319492: [lldb] A few minor fixes in TaskPool (authored by alexshap).

Changed prior to commit:
  https://reviews.llvm.org/D40587?vs=124661&id=125036#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D40587

Files:
  lldb/trunk/include/lldb/Host/TaskPool.h
  lldb/trunk/source/Host/common/TaskPool.cpp
  lldb/trunk/unittests/Host/TaskPoolTest.cpp


Index: lldb/trunk/unittests/Host/TaskPoolTest.cpp
===================================================================
--- lldb/trunk/unittests/Host/TaskPoolTest.cpp
+++ lldb/trunk/unittests/Host/TaskPoolTest.cpp
@@ -2,6 +2,8 @@
 
 #include "lldb/Host/TaskPool.h"
 
+using namespace lldb_private;
+
 TEST(TaskPoolTest, AddTask) {
   auto fn = [](int x) { return x * x + 1; };
 
Index: lldb/trunk/source/Host/common/TaskPool.cpp
===================================================================
--- lldb/trunk/source/Host/common/TaskPool.cpp
+++ lldb/trunk/source/Host/common/TaskPool.cpp
@@ -14,6 +14,8 @@
 #include <queue>   // for queue
 #include <thread>  // for thread
 
+namespace lldb_private {
+
 namespace {
 class TaskPoolImpl {
 public:
@@ -46,13 +48,20 @@
 
 TaskPoolImpl::TaskPoolImpl() : m_thread_count(0) {}
 
+unsigned GetHardwareConcurrencyHint() {
+  // std::thread::hardware_concurrency may return 0
+  // if the value is not well defined or not computable.
+  static const unsigned g_hardware_concurrency = 
+    std::max(1u, std::thread::hardware_concurrency());
+  return g_hardware_concurrency;
+}
+
 void TaskPoolImpl::AddTask(std::function<void()> &&task_fn) {
-  static const uint32_t max_threads = std::thread::hardware_concurrency();
   const size_t min_stack_size = 8 * 1024 * 1024;
 
   std::unique_lock<std::mutex> lock(m_tasks_mutex);
   m_tasks.emplace(std::move(task_fn));
-  if (m_thread_count < max_threads) {
+  if (m_thread_count < GetHardwareConcurrencyHint()) {
     m_thread_count++;
     // Note that this detach call needs to happen with the m_tasks_mutex held.
     // This prevents the thread
@@ -77,7 +86,7 @@
       break;
     }
 
-    std::function<void()> f = pool->m_tasks.front();
+    std::function<void()> f = std::move(pool->m_tasks.front());
     pool->m_tasks.pop();
     lock.unlock();
 
@@ -87,10 +96,9 @@
 
 void TaskMapOverInt(size_t begin, size_t end,
                     const llvm::function_ref<void(size_t)> &func) {
+  const size_t num_workers = std::min<size_t>(end, GetHardwareConcurrencyHint());
   std::atomic<size_t> idx{begin};
-  size_t num_workers =
-      std::min<size_t>(end, std::thread::hardware_concurrency());
-
+  
   auto wrapper = [&idx, end, &func]() {
     while (true) {
       size_t i = idx.fetch_add(1);
@@ -107,3 +115,6 @@
   for (size_t i = 0; i < num_workers; i++)
     futures[i].wait();
 }
+
+} // namespace lldb_private
+
Index: lldb/trunk/include/lldb/Host/TaskPool.h
===================================================================
--- lldb/trunk/include/lldb/Host/TaskPool.h
+++ lldb/trunk/include/lldb/Host/TaskPool.h
@@ -18,6 +18,8 @@
 #include <mutex>       // for mutex, unique_lock, condition_variable
 #include <type_traits> // for forward, result_of, move
 
+namespace lldb_private {
+
 // Global TaskPool class for running tasks in parallel on a set of worker thread
 // created the first
 // time the task pool is used. The TaskPool provide no guarantee about the order
@@ -89,4 +91,8 @@
 void TaskMapOverInt(size_t begin, size_t end,
                     const llvm::function_ref<void(size_t)> &func);
 
+unsigned GetHardwareConcurrencyHint();
+
+} // namespace lldb_private
+
 #endif // #ifndef utility_TaskPool_h_


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40587.125036.patch
Type: text/x-patch
Size: 3229 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20171130/cd4fda56/attachment.bin>


More information about the lldb-commits mailing list