public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom Tromey <tom@tromey.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tom@tromey.com>
Subject: [PATCH v4 01/34] Allow thread-pool.h to work without threads
Date: Mon,  4 Apr 2022 13:53:02 -0600	[thread overview]
Message-ID: <20220404195335.2111906-2-tom@tromey.com> (raw)
In-Reply-To: <20220404195335.2111906-1-tom@tromey.com>

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.
---
 gdb/maint.c               |  5 +----
 gdbsupport/parallel-for.h |  7 -------
 gdbsupport/thread-pool.cc | 27 ++++++++++++++++++---------
 gdbsupport/thread-pool.h  |  8 ++++++++
 4 files changed, 27 insertions(+), 20 deletions(-)

diff --git a/gdb/maint.c b/gdb/maint.c
index 8cebd6ab5af..a0c92f30916 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -41,16 +41,13 @@
 #include "maint.h"
 #include "gdbsupport/selftest.h"
 #include "inferior.h"
+#include "gdbsupport/thread-pool.h"
 
 #include "cli/cli-decode.h"
 #include "cli/cli-utils.h"
 #include "cli/cli-setshow.h"
 #include "cli/cli-cmds.h"
 
-#if CXX_STD_THREAD
-#include "gdbsupport/thread-pool.h"
-#endif
-
 static void maintenance_do_deprecate (const char *, int);
 
 #ifndef _WIN32
diff --git a/gdbsupport/parallel-for.h b/gdbsupport/parallel-for.h
index ca03094bf12..915814e485e 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 ce19ef23af5..7d446952cc7 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 68210664525..c5d5426d209 100644
--- a/gdbsupport/thread-pool.h
+++ b/gdbsupport/thread-pool.h
@@ -24,8 +24,10 @@
 #include <thread>
 #include <vector>
 #include <functional>
+#if CXX_STD_THREAD
 #include <mutex>
 #include <condition_variable>
+#endif
 #include <future>
 #include "gdbsupport/gdb_optional.h"
 
@@ -53,7 +55,11 @@ class thread_pool
   /* 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 @@ class thread_pool
 
   thread_pool () = default;
 
+#if CXX_STD_THREAD
   /* The callback for each worker thread.  */
   void thread_function ();
 
@@ -83,6 +90,7 @@ class thread_pool
      between the main thread and the worker threads.  */
   std::condition_variable m_tasks_cv;
   std::mutex m_tasks_mutex;
+#endif /* CXX_STD_THREAD */
 };
 
 }
-- 
2.34.1


  reply	other threads:[~2022-04-04 19:53 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-04 19:53 [PATCH v4 00/34] Rewrite the DWARF "partial" reader Tom Tromey
2022-04-04 19:53 ` Tom Tromey [this message]
2022-04-08 16:13   ` [PATCH v4 01/34] Allow thread-pool.h to work without threads Pedro Alves
2022-04-04 19:53 ` [PATCH v4 02/34] Split create_addrmap_from_aranges Tom Tromey
2022-04-04 19:53 ` [PATCH v4 03/34] Fix latent bug in read_addrmap_from_aranges Tom Tromey
2022-04-04 19:53 ` [PATCH v4 04/34] Add dwarf2_per_cu_data::addresses_seen Tom Tromey
2022-04-04 19:53 ` [PATCH v4 05/34] Refactor dwarf2_get_pc_bounds Tom Tromey
2022-04-04 19:53 ` [PATCH v4 06/34] Allow ada_decode not to decode operators Tom Tromey
2022-04-04 19:53 ` [PATCH v4 07/34] Let skip_one_die not skip children Tom Tromey
2022-04-04 19:53 ` [PATCH v4 08/34] Add name splitting Tom Tromey
2022-04-04 19:53 ` [PATCH v4 09/34] Add new overload of dwarf5_djb_hash Tom Tromey
2022-04-04 19:53 ` [PATCH v4 10/34] Refactor build_type_psymtabs_reader Tom Tromey
2022-04-04 19:53 ` [PATCH v4 11/34] Add batching parameter to parallel_for_each Tom Tromey
2022-04-04 19:53 ` [PATCH v4 12/34] Return vector of results from parallel_for_each Tom Tromey
2022-04-04 19:53 ` [PATCH v4 13/34] Specialize std::hash for gdb_exception Tom Tromey
2022-04-04 19:53 ` [PATCH v4 14/34] Add "fullname" handling to file_and_directory Tom Tromey
2022-04-04 19:53 ` [PATCH v4 15/34] Introduce DWARF abbrev cache Tom Tromey
2022-04-04 19:53 ` [PATCH v4 16/34] Statically examine abbrev properties Tom Tromey
2022-04-04 19:53 ` [PATCH v4 17/34] Update skip_one_die for new " Tom Tromey
2022-04-04 19:53 ` [PATCH v4 18/34] Introduce the new DWARF index class Tom Tromey
2022-04-10 17:38   ` Tom Tromey
2022-04-14  5:32   ` Enze Li
2022-04-14 11:52     ` Simon Marchi
2022-04-04 19:53 ` [PATCH v4 19/34] The new DWARF indexer Tom Tromey
2022-04-04 19:53 ` [PATCH v4 20/34] Implement quick_symbol_functions for cooked DWARF index Tom Tromey
2022-04-04 19:53 ` [PATCH v4 21/34] Wire in the new DWARF indexer Tom Tromey
2022-04-04 19:53 ` [PATCH v4 22/34] Introduce thread-safe handling for complaints Tom Tromey
2022-04-04 19:53 ` [PATCH v4 23/34] Pre-read DWARF section data Tom Tromey
2022-04-04 19:53 ` [PATCH v4 24/34] Parallelize DWARF indexing Tom Tromey
2022-04-04 19:53 ` [PATCH v4 25/34] "Finalize" the DWARF index in the background Tom Tromey
2022-04-04 19:53 ` [PATCH v4 26/34] Rename write_psymtabs_to_index Tom Tromey
2022-04-04 19:53 ` [PATCH v4 27/34] Change the key type in psym_index_map Tom Tromey
2022-04-04 19:53 ` [PATCH v4 28/34] Change parameters to write_address_map Tom Tromey
2022-04-04 19:53 ` [PATCH v4 29/34] Genericize addrmap handling in the DWARF index writer Tom Tromey
2022-04-04 19:53 ` [PATCH v4 30/34] Adapt .gdb_index writer to new DWARF scanner Tom Tromey
2022-04-04 19:53 ` [PATCH v4 31/34] Adapt .debug_names " Tom Tromey
2022-04-04 19:53 ` [PATCH v4 32/34] Enable the new DWARF indexer Tom Tromey
2022-04-04 19:53 ` [PATCH v4 33/34] Delete DWARF psymtab code Tom Tromey
2022-04-04 19:53 ` [PATCH v4 34/34] Remove dwarf2_per_cu_data::v Tom Tromey
2022-04-12 15:30 ` [PATCH v4 00/34] Rewrite the DWARF "partial" reader Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220404195335.2111906-2-tom@tromey.com \
    --to=tom@tromey.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).