public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH][gdbsupport] Avoid main thread in parallel for with thread sanitizer
Date: Mon, 18 Jul 2022 12:05:22 +0200	[thread overview]
Message-ID: <20220718100520.GA26230@delia.home> (raw)

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 <type_traits>
 #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<result_type> results (count);
 
   if (parallel_for_each_debug)

             reply	other threads:[~2022-07-18 10:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-18 10:05 Tom de Vries [this message]
2022-07-18 10:31 ` Tom de Vries
2022-09-21 16:09 ` 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=20220718100520.GA26230@delia.home \
    --to=tdevries@suse.de \
    --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).