[Openmp-commits] [openmp] [OpenMP] Support setting POSIX thread name on *BSD's and macOS (PR #106489)

Brad Smith via Openmp-commits openmp-commits at lists.llvm.org
Wed Aug 28 22:11:06 PDT 2024


https://github.com/brad0 created https://github.com/llvm/llvm-project/pull/106489

None

>From eff3998abd1572e0dcd257041630c0259f8e91bc Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
Date: Thu, 29 Aug 2024 01:07:15 -0400
Subject: [PATCH] [OpenMP] Support setting POSIX thread name on *BSD's and
 macOS

---
 openmp/runtime/cmake/LibompDefinitions.cmake |  4 ++--
 openmp/runtime/cmake/config-ix.cmake         |  5 ++---
 openmp/runtime/src/z_Linux_util.cpp          | 17 +++++++++++++++--
 3 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/openmp/runtime/cmake/LibompDefinitions.cmake b/openmp/runtime/cmake/LibompDefinitions.cmake
index c894be5870f705..1d8a88966d9393 100644
--- a/openmp/runtime/cmake/LibompDefinitions.cmake
+++ b/openmp/runtime/cmake/LibompDefinitions.cmake
@@ -23,8 +23,8 @@ function(libomp_get_definitions_flags cppflags)
   else()
     libomp_append(cppflags_local "-D _GNU_SOURCE")
     libomp_append(cppflags_local "-D _REENTRANT")
-    # or use HAVE_PTHREAD_SETNAME_NP from top-level cmake/config-ix.cmake
-    libomp_append(cppflags_local "-D LIBOMP_HAVE_LINUX_PTHREAD_SETNAME" LIBOMP_HAVE_LINUX_PTHREAD_SETNAME)
+    libomp_append(cppflags_local "-D LIBOMP_HAVE_PTHREAD_SETNAME_NP" LIBOMP_HAVE_PTHREAD_SETNAME_NP)
+    libomp_append(cppflags_local "-D LIBOMP_HAVE_PTHREAD_SET_NAME_NP" LIBOMP_HAVE_PTHREAD_SET_NAME_NP)
   endif()
 
   # CMake doesn't include CPPFLAGS from environment, but we will.
diff --git a/openmp/runtime/cmake/config-ix.cmake b/openmp/runtime/cmake/config-ix.cmake
index 3adaf5bed68f33..ac2bbb902161ed 100644
--- a/openmp/runtime/cmake/config-ix.cmake
+++ b/openmp/runtime/cmake/config-ix.cmake
@@ -102,9 +102,8 @@ if(${LIBOMP_FORTRAN_MODULES})
 endif()
 
 # Check non-posix pthread API here before CMAKE_REQUIRED_DEFINITIONS gets messed up
-if("${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
-  check_symbol_exists(pthread_setname_np "pthread.h" LIBOMP_HAVE_LINUX_PTHREAD_SETNAME)
-endif()
+check_symbol_exists(pthread_setname_np "pthread.h" LIBOMP_HAVE_PTHREAD_SETNAME_NP)
+check_symbol_exists(pthread_set_name_np "pthread.h;pthread_np.h" LIBOMP_HAVE_PTHREAD_SET_NAME_NP)
 
 # Check for Unix shared memory
 check_symbol_exists(shm_open "sys/mman.h" LIBOMP_HAVE_SHM_OPEN_NO_LRT)
diff --git a/openmp/runtime/src/z_Linux_util.cpp b/openmp/runtime/src/z_Linux_util.cpp
index 337a34518da2f6..3f3327378f5974 100644
--- a/openmp/runtime/src/z_Linux_util.cpp
+++ b/openmp/runtime/src/z_Linux_util.cpp
@@ -71,6 +71,9 @@
 #if KMP_OS_NETBSD
 #include <sched.h>
 #endif
+#if KMP_OS_OPENBSD
+#include <pthread_np.h>
+#endif
 #elif KMP_OS_SOLARIS
 #include <libproc.h>
 #include <procfs.h>
@@ -878,10 +881,20 @@ void __kmp_create_worker(int gtid, kmp_info_t *th, size_t stack_size) {
     KMP_SYSFAIL("pthread_create", status);
   }
 
-#ifdef LIBOMP_HAVE_LINUX_PTHREAD_SETNAME
+#if defined(LIBOMP_HAVE_PTHREAD_SETNAME_NP) || \
+    defined(LIBOMP_HAVE_PTHREAD_SET_NAME_NP)
   // Rename worker threads for improved debuggability
   if (!KMP_UBER_GTID(gtid)) {
-    pthread_setname_np(handle, "openmp_worker");
+#if defined(__linux__)
+  pthread_setname_np(handle, "openmp_worker");
+#elif defined(__FreeBSD__) || defined(__OpenBSD__)
+  pthread_set_name_np(handle, "openmp_worker");
+#elif defined(__NetBSD__)
+  pthread_setname_np(handle, "%s",
+                       const_cast<char *>("openmp_worker"));
+#elif defined(__APPLE__)
+  pthread_setname_np("openmp_worker");
+#endif
   }
 #endif
 



More information about the Openmp-commits mailing list