Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH] Fix build on macOS
Date: Tue, 10 Dec 2019 21:47:00 -0000	[thread overview]
Message-ID: <20191210214717.25680-1-tromey@adacore.com> (raw)

PR build/25268 points out that the build fails on macOS, because on
macOS the "pthread_setname_np" function takes a single argument.

This patch fixes the problem, by introducing a new template adapter
function that handles both styles of pthread_setname_np.  This is a
technique I learned from Alexandre Oliva, and avoids the need for a
complicated autoconf check.

This change also meant moving the pthread_setname_np call to the
thread function, because macOS only permits setting the name of the
current thread.  This means that there can be a brief window when gdb
will see the wrong name; but I think this is a minor concern.

Tested by rebuilding on x86-64 Fedora 30, and on macOS High Sierra.
On Linux I also debugged gdb to ensure that the thread names are still
set correctly.

gdb/ChangeLog
2019-12-10  Tom Tromey  <tromey@adacore.com>

	PR build/25268:
	* gdbsupport/thread-pool.c (set_thread_name): New template
	function.
	(thread_pool::set_thread_count): Don't call pthread_setname_np.
	(thread_pool::thread_function): Call set_thread_name.

Change-Id: Id7bf28d99ca27a893a9fc87ebb90b15a9c2a9cb4
---
 gdb/ChangeLog                |  8 ++++++++
 gdb/gdbsupport/thread-pool.c | 31 +++++++++++++++++++++++++++----
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/gdb/gdbsupport/thread-pool.c b/gdb/gdbsupport/thread-pool.c
index d19ae02e3ef..8e24570ee96 100644
--- a/gdb/gdbsupport/thread-pool.c
+++ b/gdb/gdbsupport/thread-pool.c
@@ -36,8 +36,28 @@
 #endif
 
 #ifdef USE_PTHREAD_SETNAME_NP
+
 #include <pthread.h>
-#endif
+
+/* Handle platform discrepancies in pthread_setname_np: macOS uses a
+   single-argument form, while Linux uses a two-argument form.  This
+   wrapper template handles the difference.  */
+
+template<typename R, typename A1, typename A2>
+void
+set_thread_name (R (*set_name) (A1, A2), const char *name)
+{
+  set_name (pthread_self (), name);
+}
+
+template<typename R, typename A1>
+void
+set_thread_name (R (*set_name) (A1), const char *name)
+{
+  set_name (name);
+}
+
+#endif	/* USE_PTHREAD_SETNAME_NP */
 
 namespace gdb
 {
@@ -75,9 +95,6 @@ thread_pool::set_thread_count (size_t num_threads)
       for (size_t i = m_thread_count; i < num_threads; ++i)
 	{
 	  std::thread thread (&thread_pool::thread_function, this);
-#ifdef USE_PTHREAD_SETNAME_NP
-	  pthread_setname_np (thread.native_handle (), "gdb worker");
-#endif
 	  thread.detach ();
 	}
     }
@@ -115,6 +132,12 @@ thread_pool::post_task (std::function<void ()> func)
 void
 thread_pool::thread_function ()
 {
+#ifdef USE_PTHREAD_SETNAME_NP
+  /* This must be done here, because on macOS one can only set the
+     name of the current thread.  */
+  set_thread_name (pthread_setname_np, "gdb worker");
+#endif
+
   /* Ensure that SIGSEGV is delivered to an alternate signal
      stack.  */
   gdb::alternate_signal_stack signal_stack;
-- 
2.21.0


             reply	other threads:[~2019-12-10 21:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-10 21:47 Tom Tromey [this message]
2019-12-11  1:37 ` Simon Marchi
2019-12-11 13:52   ` Tom Tromey
2019-12-11 14:07     ` Simon Marchi
2020-01-31 14:43 ` Kevin Buettner
2020-01-31 16:52   ` Tom Tromey
2020-01-31 21:40     ` Kevin Buettner
2020-02-05 18:37       ` Christian Biesinger via gdb-patches

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=20191210214717.25680-1-tromey@adacore.com \
    --to=tromey@adacore.com \
    --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