public inbox for gdb-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tom Tromey <tromey@sourceware.org>
To: gdb-cvs@sourceware.org
Subject: [binutils-gdb] Only allow QUIT on the main thread
Date: Wed, 30 Mar 2022 19:46:08 +0000 (GMT)	[thread overview]
Message-ID: <20220330194608.25E84385E82D@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=47ccd6b84bdf35688fb1516f06db68462bd24585

commit 47ccd6b84bdf35688fb1516f06db68462bd24585
Author: Tom Tromey <tom@tromey.com>
Date:   Tue Mar 29 14:36:10 2022 -0600

    Only allow QUIT on the main thread
    
    Pedro pointed out that gdb worker threads should not react to quits.
    While I don't think that the new DWARF reader can call QUIT from a
    worker thread (and I don't think the existing minsym threading code
    can either), it seems safest to address this before checking in the
    new code.  This patch arranges for the QUIT macro to only work on the
    main thread.

Diff:
---
 gdb/run-on-main-thread.c | 20 ++++++++++++++++++++
 gdb/run-on-main-thread.h |  4 ++++
 gdb/utils.c              |  4 ++++
 3 files changed, 28 insertions(+)

diff --git a/gdb/run-on-main-thread.c b/gdb/run-on-main-thread.c
index ea633d9d2d0..eb22310f5c0 100644
--- a/gdb/run-on-main-thread.c
+++ b/gdb/run-on-main-thread.c
@@ -20,6 +20,7 @@
 #include "run-on-main-thread.h"
 #include "ser-event.h"
 #if CXX_STD_THREAD
+#include <thread>
 #include <mutex>
 #endif
 #include "gdbsupport/event-loop.h"
@@ -38,6 +39,10 @@ static std::vector<std::function<void ()>> runnables;
 
 static std::mutex runnable_mutex;
 
+/* The main thread.  */
+
+static std::thread::id main_thread;
+
 #endif
 
 /* Run all the queued runnables.  */
@@ -89,10 +94,25 @@ run_on_main_thread (std::function<void ()> &&func)
   serial_event_set (runnable_event);
 }
 
+/* See run-on-main-thread.h.  */
+
+bool
+is_main_thread ()
+{
+#if CXX_STD_THREAD
+  return std::this_thread::get_id () == main_thread;
+#else
+  return true;
+#endif
+}
+
 void _initialize_run_on_main_thread ();
 void
 _initialize_run_on_main_thread ()
 {
+#if CXX_STD_THREAD
+  main_thread = std::this_thread::get_id ();
+#endif
   runnable_event = make_serial_event ();
   add_file_handler (serial_event_fd (runnable_event), run_events, nullptr,
 		    "run-on-main-thread");
diff --git a/gdb/run-on-main-thread.h b/gdb/run-on-main-thread.h
index 0bfe0b7456a..888328e7a51 100644
--- a/gdb/run-on-main-thread.h
+++ b/gdb/run-on-main-thread.h
@@ -25,4 +25,8 @@
 
 extern void run_on_main_thread (std::function<void ()> &&);
 
+/* Return true on the main thread.  */
+
+extern bool is_main_thread ();
+
 #endif /* GDB_RUN_ON_MAIN_THREAD_H */
diff --git a/gdb/utils.c b/gdb/utils.c
index 2df9b1d7dcf..9ca268ad07f 100644
--- a/gdb/utils.c
+++ b/gdb/utils.c
@@ -78,6 +78,7 @@
 #include "bt-utils.h"
 #include "gdbsupport/buildargv.h"
 #include "pager.h"
+#include "run-on-main-thread.h"
 
 void (*deprecated_error_begin_hook) (void);
 
@@ -681,6 +682,9 @@ quit (void)
 void
 maybe_quit (void)
 {
+  if (!is_main_thread ())
+    return;
+
   if (sync_quit_force_run)
     quit ();


                 reply	other threads:[~2022-03-30 19:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20220330194608.25E84385E82D@sourceware.org \
    --to=tromey@sourceware.org \
    --cc=gdb-cvs@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).