aboutsummaryrefslogtreecommitdiff
path: root/gdbsupport
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2022-03-30 20:19:54 -0600
committerTom Tromey <tom@tromey.com>2022-04-12 09:31:15 -0600
commit0981fe1017a8111aa6544ff52bcbbc80eec6b3c0 (patch)
tree094cc0549f9ec7c55276027466718155597c7682 /gdbsupport
parent8e6b35366073a1a71df805061ecf016cc915a9f9 (diff)
downloadgdb-0981fe1017a8111aa6544ff52bcbbc80eec6b3c0.zip
gdb-0981fe1017a8111aa6544ff52bcbbc80eec6b3c0.tar.gz
gdb-0981fe1017a8111aa6544ff52bcbbc80eec6b3c0.tar.bz2
Allow thread-pool.h to work without threads
thread-pool.h requires CXX_STD_THREAD in order to even be included. However, there's no deep reason for this, and during review we found that one patch in the new DWARF indexer series unconditionally requires the thread pool. Because the thread pool already allows a task to be run in the calling thread (for example if it is configured to have no threads in the pool), it seemed straightforward to make this code ok to use when host threads aren't available at all. This patch implements this idea. I built it on a thread-less host (mingw, before my recent configure patch) and verified that the result builds. After the thread-pool change, parallel-for.h no longer needs any CXX_STD_THREAD checks at all, so this patch removes these as well.
Diffstat (limited to 'gdbsupport')
-rw-r--r--gdbsupport/parallel-for.h7
-rw-r--r--gdbsupport/thread-pool.cc27
-rw-r--r--gdbsupport/thread-pool.h10
3 files changed, 27 insertions, 17 deletions
diff --git a/gdbsupport/parallel-for.h b/gdbsupport/parallel-for.h
index ca03094..915814e 100644
--- a/gdbsupport/parallel-for.h
+++ b/gdbsupport/parallel-for.h
@@ -21,10 +21,7 @@
#define GDBSUPPORT_PARALLEL_FOR_H
#include <algorithm>
-#if CXX_STD_THREAD
-#include <thread>
#include "gdbsupport/thread-pool.h"
-#endif
namespace gdb
{
@@ -41,7 +38,6 @@ template<class RandomIt, class RangeFunction>
void
parallel_for_each (RandomIt first, RandomIt last, RangeFunction callback)
{
-#if CXX_STD_THREAD
/* So we can use a local array below. */
const size_t local_max = 16;
size_t n_threads = std::min (thread_pool::g_thread_pool->thread_count (),
@@ -70,15 +66,12 @@ parallel_for_each (RandomIt first, RandomIt last, RangeFunction callback)
first = end;
}
}
-#endif /* CXX_STD_THREAD */
/* Process all the remaining elements in the main thread. */
callback (first, last);
-#if CXX_STD_THREAD
for (int i = 0; i < n_actual_threads; ++i)
futures[i].wait ();
-#endif /* CXX_STD_THREAD */
}
}
diff --git a/gdbsupport/thread-pool.cc b/gdbsupport/thread-pool.cc
index ce19ef2..7d44695 100644
--- a/gdbsupport/thread-pool.cc
+++ b/gdbsupport/thread-pool.cc
@@ -18,10 +18,10 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "common-defs.h"
+#include "gdbsupport/thread-pool.h"
#if CXX_STD_THREAD
-#include "gdbsupport/thread-pool.h"
#include "gdbsupport/alt-stack.h"
#include "gdbsupport/block-signals.h"
#include <algorithm>
@@ -67,6 +67,7 @@ set_thread_name (int (*set_name) (const char *), const char *name)
}
#endif /* USE_PTHREAD_SETNAME_NP */
+#endif /* CXX_STD_THREAD */
namespace gdb
{
@@ -93,6 +94,7 @@ thread_pool::~thread_pool ()
void
thread_pool::set_thread_count (size_t num_threads)
{
+#if CXX_STD_THREAD
std::lock_guard<std::mutex> guard (m_tasks_mutex);
/* If the new size is larger, start some new threads. */
@@ -127,6 +129,9 @@ thread_pool::set_thread_count (size_t num_threads)
}
m_thread_count = num_threads;
+#else
+ /* No threads available, simply ignore the request. */
+#endif /* CXX_STD_THREAD */
}
std::future<void>
@@ -135,20 +140,24 @@ thread_pool::post_task (std::function<void ()> &&func)
std::packaged_task<void ()> t (std::move (func));
std::future<void> f = t.get_future ();
- if (m_thread_count == 0)
- {
- /* Just execute it now. */
- t ();
- }
- else
+#if CXX_STD_THREAD
+ if (m_thread_count != 0)
{
std::lock_guard<std::mutex> guard (m_tasks_mutex);
m_tasks.emplace (std::move (t));
m_tasks_cv.notify_one ();
}
+ else
+#endif
+ {
+ /* Just execute it now. */
+ t ();
+ }
return f;
}
+#if CXX_STD_THREAD
+
void
thread_pool::thread_function ()
{
@@ -182,6 +191,6 @@ thread_pool::thread_function ()
}
}
-}
-
#endif /* CXX_STD_THREAD */
+
+} /* namespace gdb */
diff --git a/gdbsupport/thread-pool.h b/gdbsupport/thread-pool.h
index 6821066..2672e4d 100644
--- a/gdbsupport/thread-pool.h
+++ b/gdbsupport/thread-pool.h
@@ -21,11 +21,13 @@
#define GDBSUPPORT_THREAD_POOL_H
#include <queue>
-#include <thread>
#include <vector>
#include <functional>
+#if CXX_STD_THREAD
+#include <thread>
#include <mutex>
#include <condition_variable>
+#endif
#include <future>
#include "gdbsupport/gdb_optional.h"
@@ -53,7 +55,11 @@ public:
/* Return the number of executing threads. */
size_t thread_count () const
{
+#if CXX_STD_THREAD
return m_thread_count;
+#else
+ return 0;
+#endif
}
/* Post a task to the thread pool. A future is returned, which can
@@ -64,6 +70,7 @@ private:
thread_pool () = default;
+#if CXX_STD_THREAD
/* The callback for each worker thread. */
void thread_function ();
@@ -83,6 +90,7 @@ private:
between the main thread and the worker threads. */
std::condition_variable m_tasks_cv;
std::mutex m_tasks_mutex;
+#endif /* CXX_STD_THREAD */
};
}