Skip to contentSkip to conversation

thread-posix: add support for setting threads name on OpenBSD

PATCH Applied Brad Smith 4 participants 5 messages Dec 18, 2022 – Feb 17, 2023 Arm/Amlogic Meson SoC support

lore
Share & tools
lore

Brad Smith

author
#0 lore raw

PATCH 1/1 thread-posix: add support for setting threads name on OpenBSD

Make use of pthread_set_name_np() to be able to set the threads name on OpenBSD.

---
 meson.build              | 12 ++++++++++++
 util/qemu-thread-posix.c |  9 ++++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)
meson.build +12 -0
index 5c6b5a1c75..68adcb6291 100644
@@ -2123,6 +2123,18 @@ config_host_data.set('CONFIG_PTHREAD_SETNAME_NP_WO_TID', cc.links(gnu_source_pre
     pthread_create(&thread, 0, f, 0);
     return 0;
   }''', dependencies: threads))
+config_host_data.set('CONFIG_PTHREAD_SET_NAME_NP', cc.links(gnu_source_prefix + '''
+  #include <pthread.h>
+  #include <pthread_np.h>
+
+  static void *f(void *p) { return NULL; }
+  int main(void)
+  {
+    pthread_t thread;
+    pthread_create(&thread, 0, f, 0);
+    pthread_set_name_np(thread, "QEMU");
+    return 0;
+  }''', dependencies: threads))
 config_host_data.set('CONFIG_PTHREAD_CONDATTR_SETCLOCK', cc.links(gnu_source_prefix + '''
   #include <pthread.h>
   #include <time.h>
util/qemu-thread-posix.c +8 -1
index bae938c670..412caa45ef 100644
@@ -18,6 +18,10 @@
 #include "qemu/tsan.h"
 #include "qemu/bitmap.h"
 
+#ifdef CONFIG_PTHREAD_SET_NAME_NP
+#include <pthread_np.h>
+#endif
+
 static bool name_threads;
 
 void qemu_thread_naming(bool enable)
@@ -25,7 +29,8 @@ void qemu_thread_naming(bool enable)
     name_threads = enable;
 
 #if !defined CONFIG_PTHREAD_SETNAME_NP_W_TID && \
-    !defined CONFIG_PTHREAD_SETNAME_NP_WO_TID
+    !defined CONFIG_PTHREAD_SETNAME_NP_WO_TID && \
+    !defined CONFIG_PTHREAD_SET_NAME_NP
     /* This is a debugging option, not fatal */
     if (enable) {
         fprintf(stderr, "qemu: thread naming not supported on this host\n");
@@ -480,6 +485,8 @@ static void *qemu_thread_start(void *args)
         pthread_setname_np(pthread_self(), qemu_thread_args->name);
 # elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
         pthread_setname_np(qemu_thread_args->name);
+# elif defined(CONFIG_PTHREAD_SET_NAME_NP)
+        pthread_set_name_np(pthread_self(), qemu_thread_args->name);
 # endif
     }
     QEMU_TSAN_ANNOTATE_THREAD_NAME(qemu_thread_args->name);
Signature · 1 line
2.38.1
author
#3 lore raw
In reply to Brad Smith

ping.

Quoted 66 lines · Brad Smith

Make use of pthread_set_name_np() to be able to set the threads name on OpenBSD.

---
  meson.build              | 12 ++++++++++++
  util/qemu-thread-posix.c |  9 ++++++++-
  2 files changed, 20 insertions(+), 1 deletion(-)
meson.build +12 -0
index 5c6b5a1c75..68adcb6291 100644
@@ -2123,6 +2123,18 @@ config_host_data.set('CONFIG_PTHREAD_SETNAME_NP_WO_TID', cc.links(gnu_source_pre
      pthread_create(&thread, 0, f, 0);
      return 0;
    }''', dependencies: threads))
+config_host_data.set('CONFIG_PTHREAD_SET_NAME_NP', cc.links(gnu_source_prefix + '''
+  #include <pthread.h>
+  #include <pthread_np.h>
+
+  static void *f(void *p) { return NULL; }
+  int main(void)
+  {
+    pthread_t thread;
+    pthread_create(&thread, 0, f, 0);
+    pthread_set_name_np(thread, "QEMU");
+    return 0;
+  }''', dependencies: threads))
  config_host_data.set('CONFIG_PTHREAD_CONDATTR_SETCLOCK', cc.links(gnu_source_prefix + '''
    #include <pthread.h>
    #include <time.h>
util/qemu-thread-posix.c +8 -1
index bae938c670..412caa45ef 100644
@@ -18,6 +18,10 @@
  #include "qemu/tsan.h"
  #include "qemu/bitmap.h"
 
+#ifdef CONFIG_PTHREAD_SET_NAME_NP
+#include <pthread_np.h>
+#endif
+
  static bool name_threads;
 
  void qemu_thread_naming(bool enable)
@@ -25,7 +29,8 @@ void qemu_thread_naming(bool enable)
      name_threads = enable;
 
  #if !defined CONFIG_PTHREAD_SETNAME_NP_W_TID && \
-    !defined CONFIG_PTHREAD_SETNAME_NP_WO_TID
+    !defined CONFIG_PTHREAD_SETNAME_NP_WO_TID && \
+    !defined CONFIG_PTHREAD_SET_NAME_NP
      /* This is a debugging option, not fatal */
      if (enable) {
          fprintf(stderr, "qemu: thread naming not supported on this host\n");
@@ -480,6 +485,8 @@ static void *qemu_thread_start(void *args)
          pthread_setname_np(pthread_self(), qemu_thread_args->name);
  # elif defined(CONFIG_PTHREAD_SETNAME_NP_WO_TID)
          pthread_setname_np(qemu_thread_args->name);
+# elif defined(CONFIG_PTHREAD_SET_NAME_NP)
+        pthread_set_name_np(pthread_self(), qemu_thread_args->name);
  # endif
      }
      QEMU_TSAN_ANNOTATE_THREAD_NAME(qemu_thread_args->name);