Consolidate definition of USE_WIN32API
[deliverable/binutils-gdb.git] / gdb / gdbsupport / thread-pool.c
index 8282ea374bccc0bc479045a3ebb8dac33258f153..fc83ff765f655baa526f239e4969c7519aff8b28 100644 (file)
@@ -1,6 +1,6 @@
 /* Thread pool
 
-   Copyright (C) 2019 Free Software Foundation, Inc.
+   Copyright (C) 2019-2020 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
 #include "gdbsupport/block-signals.h"
 #include <algorithm>
 
+/* On the off chance that we have the pthread library on a Windows
+   host, but std::thread is not using it, avoid calling
+   pthread_setname_np on Windows.  */
+#ifndef _WIN32
+#ifdef HAVE_PTHREAD_SETNAME_NP
+#define USE_PTHREAD_SETNAME_NP
+#endif
+#endif
+
+#ifdef USE_PTHREAD_SETNAME_NP
+
+#include <pthread.h>
+
+/* 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.  */
+
+ATTRIBUTE_UNUSED static void
+set_thread_name (int (*set_name) (pthread_t, const char *), const char *name)
+{
+  set_name (pthread_self (), name);
+}
+
+/* The macOS man page says that pthread_setname_np returns "void", but
+   the headers actually declare it returning "int".  */
+ATTRIBUTE_UNUSED static void
+set_thread_name (int (*set_name) (const char *), const char *name)
+{
+  set_name (name);
+}
+
+#endif /* USE_PTHREAD_SETNAME_NP */
+
 namespace gdb
 {
 
@@ -99,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;
This page took 0.026083 seconds and 4 git commands to generate.