From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from outbound-ss-820.bluehost.com (outbound-ss-820.bluehost.com [69.89.24.241]) by sourceware.org (Postfix) with ESMTPS id 3CBA03858C52 for ; Mon, 4 Apr 2022 19:53:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3CBA03858C52 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw14.mail.unifiedlayer.com (unknown [10.0.90.129]) by progateway2.mail.pro1.eigbox.com (Postfix) with ESMTP id 9956310047F9E for ; Mon, 4 Apr 2022 19:53:39 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id bSlfnd5aE2s5dbSlfnKBLN; Mon, 04 Apr 2022 19:53:39 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=Nagja0P4 c=1 sm=1 tr=0 ts=624b4cc3 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=z0gMJWrwH1QA:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=mDV3o1hIAAAA:8 a=aZiUMP4aSW3liP4bN7QA:9 a=_FVE-zBwftR9WsbkzFJk:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=d2YZNjY4ZVH2skaOoTO6eV7hQaPEpj/38Q803OKLvyA=; b=k6nKJBe3pR+F7J/yhEz2CbPFTa Bn9/hUAnXvWSBOsZfBQ0IrFGJ8ea6e6D61eqkHmDlQdumM6Fsu0XVYuMeYVG3jxZe45xIHqrPC9sk kb/zCMPQsswQ4CvSK0Y/RaIoo; Received: from 71-211-176-135.hlrn.qwest.net ([71.211.176.135]:34798 helo=prentzel.Home) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nbSle-003u7z-SD; Mon, 04 Apr 2022 13:53:38 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v4 01/34] Allow thread-pool.h to work without threads Date: Mon, 4 Apr 2022 13:53:02 -0600 Message-Id: <20220404195335.2111906-2-tom@tromey.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220404195335.2111906-1-tom@tromey.com> References: <20220404195335.2111906-1-tom@tromey.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 71.211.176.135 X-Source-L: No X-Exim-ID: 1nbSle-003u7z-SD X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 71-211-176-135.hlrn.qwest.net (prentzel.Home) [71.211.176.135]:34798 X-Source-Auth: tom+tromey.com X-Email-Count: 9 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3030.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Apr 2022 19:53:42 -0000 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 -#if CXX_STD_THREAD -#include #include "gdbsupport/thread-pool.h" -#endif 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 = 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 . */ #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 @@ -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 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 @@ -135,20 +140,24 @@ thread_pool::post_task (std::function &&func) std::packaged_task t (std::move (func)); std::future 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 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 #include #include +#if CXX_STD_THREAD #include #include +#endif #include #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