From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id AD8BF3858D33 for ; Mon, 18 Jul 2022 10:05:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AD8BF3858D33 Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id AEC7A3746C for ; Mon, 18 Jul 2022 10:05:28 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 9B87513A37 for ; Mon, 18 Jul 2022 10:05:28 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id KE3pJGgw1WIPQgAAMHmgww (envelope-from ) for ; Mon, 18 Jul 2022 10:05:28 +0000 Date: Mon, 18 Jul 2022 12:05:22 +0200 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH][gdbsupport] Avoid main thread in parallel for with thread sanitizer Message-ID: <20220718100520.GA26230@delia.home> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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, 18 Jul 2022 10:05:31 -0000 Hi, When running tasks in a parallel for, some elements may be handled by a worker thread and others by the main thread. When running with -fsanitize=thread, avoid the main thread in the parallel for, to prevent multi-threading problems being hidden. Tested on x86_64-linux, with -fsanitize=thread. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=29359 Any comments? Thanks, - Tom [gdbsupport] Avoid main thread in parallel for with thread sanitizer --- gdbsupport/parallel-for.h | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/gdbsupport/parallel-for.h b/gdbsupport/parallel-for.h index bf40f125f0f..81a2cfa215b 100644 --- a/gdbsupport/parallel-for.h +++ b/gdbsupport/parallel-for.h @@ -24,6 +24,15 @@ #include #include "gdbsupport/thread-pool.h" +/* GCC does not understand __has_feature. */ +#if !defined(__has_feature) +# define __has_feature(x) 0 +#endif + +#if __has_feature(thread_sanitizer) +#define __SANITIZE_ADDRESS__ 1 +#endif + namespace gdb { @@ -161,7 +170,22 @@ parallel_for_each (unsigned n, RandomIt first, RandomIt last, /* n_elements == n_threads * elts_per_thread + elts_left_over. */ } - size_t count = n_threads == 0 ? 0 : n_threads - 1; + /* Number of worker threads we're going to use. */ + size_t count; +#ifdef __SANITIZE_THREAD__ + /* Avoid the main thread. */ + count = (n_threads == 0 + ? (/* Only use the first worker thread. */ + 1) + : (/* Only use worker threads. */ + n_threads)); +#else + count = (n_threads == 0 + ? (/* Only use the main thread. */ + 0) + : (/* Leave at least one worker thread unused. */ + n_threads - 1)); +#endif gdb::detail::par_for_accumulator results (count); if (parallel_for_each_debug)