From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 4CF653858D28; Tue, 12 Apr 2022 15:39:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4CF653858D28 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Allow thread-pool.h to work without threads X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 8e6b35366073a1a71df805061ecf016cc915a9f9 X-Git-Newrev: 0981fe1017a8111aa6544ff52bcbbc80eec6b3c0 Message-Id: <20220412153910.4CF653858D28@sourceware.org> Date: Tue, 12 Apr 2022 15:39:10 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Apr 2022 15:39:10 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D0981fe1017a8= 111aa6544ff52bcbbc80eec6b3c0 commit 0981fe1017a8111aa6544ff52bcbbc80eec6b3c0 Author: Tom Tromey Date: Wed Mar 30 20:19:54 2022 -0600 Allow thread-pool.h to work without threads =20 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. =20 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. =20 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. =20 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. Diff: --- gdb/maint.c | 5 +---- gdbsupport/parallel-for.h | 7 ------- gdbsupport/thread-pool.cc | 27 ++++++++++++++++++--------- gdbsupport/thread-pool.h | 10 +++++++++- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/gdb/maint.c b/gdb/maint.c index 3f3dad5bd79..60e183efdd1 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" =20 #include "cli/cli-decode.h" #include "cli/cli-utils.h" #include "cli/cli-setshow.h" #include "cli/cli-cmds.h" =20 -#if CXX_STD_THREAD -#include "gdbsupport/thread-pool.h" -#endif - static void maintenance_do_deprecate (const char *, int); =20 #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 =20 #include -#if CXX_STD_THREAD -#include #include "gdbsupport/thread-pool.h" -#endif =20 namespace gdb { @@ -41,7 +38,6 @@ template 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 =3D 16; size_t n_threads =3D std::min (thread_pool::g_thread_pool->thread_count = (), @@ -70,15 +66,12 @@ parallel_for_each (RandomIt first, RandomIt last, Range= Function callback) first =3D end; } } -#endif /* CXX_STD_THREAD */ =20 /* Process all the remaining elements in the main thread. */ callback (first, last); =20 -#if CXX_STD_THREAD for (int i =3D 0; i < n_actual_threads; ++i) futures[i].wait (); -#endif /* CXX_STD_THREAD */ } =20 } 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 . = */ =20 #include "common-defs.h" +#include "gdbsupport/thread-pool.h" =20 #if CXX_STD_THREAD =20 -#include "gdbsupport/thread-pool.h" #include "gdbsupport/alt-stack.h" #include "gdbsupport/block-signals.h" #include @@ -67,6 +67,7 @@ set_thread_name (int (*set_name) (const char *), const ch= ar *name) } =20 #endif /* USE_PTHREAD_SETNAME_NP */ +#endif /* CXX_STD_THREAD */ =20 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 guard (m_tasks_mutex); =20 /* If the new size is larger, start some new threads. */ @@ -127,6 +129,9 @@ thread_pool::set_thread_count (size_t num_threads) } =20 m_thread_count =3D num_threads; +#else + /* No threads available, simply ignore the request. */ +#endif /* CXX_STD_THREAD */ } =20 std::future @@ -135,20 +140,24 @@ thread_pool::post_task (std::function &&func) std::packaged_task t (std::move (func)); std::future f =3D t.get_future (); =20 - if (m_thread_count =3D=3D 0) - { - /* Just execute it now. */ - t (); - } - else +#if CXX_STD_THREAD + if (m_thread_count !=3D 0) { std::lock_guard guard (m_tasks_mutex); m_tasks.emplace (std::move (t)); m_tasks_cv.notify_one (); } + else +#endif + { + /* Just execute it now. */ + t (); + } return f; } =20 +#if CXX_STD_THREAD + void thread_pool::thread_function () { @@ -182,6 +191,6 @@ thread_pool::thread_function () } } =20 -} - #endif /* CXX_STD_THREAD */ + +} /* namespace gdb */ diff --git a/gdbsupport/thread-pool.h b/gdbsupport/thread-pool.h index 68210664525..2672e4d739a 100644 --- a/gdbsupport/thread-pool.h +++ b/gdbsupport/thread-pool.h @@ -21,11 +21,13 @@ #define GDBSUPPORT_THREAD_POOL_H =20 #include -#include #include #include +#if CXX_STD_THREAD +#include #include #include +#endif #include #include "gdbsupport/gdb_optional.h" =20 @@ -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 } =20 /* Post a task to the thread pool. A future is returned, which can @@ -64,6 +70,7 @@ private: =20 thread_pool () =3D default; =20 +#if CXX_STD_THREAD /* The callback for each worker thread. */ void thread_function (); =20 @@ -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 */ }; =20 }