Fix build on macOS
authorTom Tromey <tromey@adacore.com>
Tue, 10 Dec 2019 21:16:19 +0000 (14:16 -0700)
committerTom Tromey <tromey@adacore.com>
Wed, 11 Dec 2019 15:02:20 +0000 (08:02 -0700)
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 adapter function
that handles both styles of pthread_setname_np.

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-11  Tom Tromey  <tromey@adacore.com>

PR build/25268:
* gdbsupport/thread-pool.c (set_thread_name): New 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
gdb/gdbsupport/thread-pool.c

index 8355da596f731ad9db158be15de929b061e98094..7fe3a2938c1b1cc263a04fd30220e30222c1b965 100644 (file)
@@ -1,3 +1,10 @@
+2019-12-11  Tom Tromey  <tromey@adacore.com>
+
+       PR build/25268:
+       * gdbsupport/thread-pool.c (set_thread_name): New function.
+       (thread_pool::set_thread_count): Don't call pthread_setname_np.
+       (thread_pool::thread_function): Call set_thread_name.
+
 2019-12-11  Tom Tromey  <tromey@adacore.com>
 
        * fbsd-tdep.c (fbsd_core_info_proc_status): Cast result of
index d19ae02e3ef18d8b42f4661ce3dcb023a28194e1..f6ea6d8b1711cd239c0487e8a2fd90f9427b77cc 100644 (file)
@@ -25,6 +25,7 @@
 #include "gdbsupport/alt-stack.h"
 #include "gdbsupport/block-signals.h"
 #include <algorithm>
+#include "diagnostics.h"
 
 /* On the off chance that we have the pthread library on a Windows
    host, but std::thread is not using it, avoid calling
 #endif
 
 #ifdef USE_PTHREAD_SETNAME_NP
+
 #include <pthread.h>
-#endif
+
+DIAGNOSTIC_PUSH
+DIAGNOSTIC_IGNORE_UNUSED_FUNCTION
+
+/* Handle platform discrepancies in pthread_setname_np: macOS uses a
+   single-argument form, while Linux uses a two-argument form.  This
+   wrapper handles the difference.  */
+
+static void
+set_thread_name (int (*set_name) (pthread_t, const char *), const char *name)
+{
+  set_name (pthread_self (), name);
+}
+
+static void
+set_thread_name (void (*set_name) (const char *), const char *name)
+{
+  set_name (name);
+}
+
+DIAGNOSTIC_POP
+
+#endif /* USE_PTHREAD_SETNAME_NP */
 
 namespace gdb
 {
@@ -75,9 +99,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 +136,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;
This page took 0.030711 seconds and 4 git commands to generate.