]> granicus.if.org Git - llvm/commitdiff
Try to fix thread name truncation on non-Windows.
authorZachary Turner <zturner@google.com>
Sat, 4 Mar 2017 18:53:09 +0000 (18:53 +0000)
committerZachary Turner <zturner@google.com>
Sat, 4 Mar 2017 18:53:09 +0000 (18:53 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@296976 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/Threading.h
lib/Support/Threading.cpp
lib/Support/Unix/Threading.inc
lib/Support/Windows/Threading.inc

index 9b6a3b4efdfb20d71ebcf7fcd7f596c828f39f56..03963a24c107eeed24dd39611c6b3d57ea63cb78 100644 (file)
@@ -139,7 +139,7 @@ void llvm_execute_on_thread(void (*UserFn)(void *), void *UserData,
 
   /// \brief Get the maximum length of a thread name on this platform.
   /// A value of 0 means there is no limit.
-  constexpr uint32_t get_max_thread_name_length();
+  uint32_t get_max_thread_name_length();
 
   /// \brief Set the name of the current thread.  Setting a thread's name can
   /// be helpful for enabling useful diagnostics under a debugger or when
index e49d328106928e92321b6a57df8c7e07f880aa0c..6a10b988d4648a9e95e42c2761b34f0717c52122 100644 (file)
@@ -49,6 +49,8 @@ unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
 
 uint64_t llvm::get_threadid() { return 0; }
 
+uint32_t llvm::get_max_thread_name_length() { return 0; }
+
 void llvm::set_thread_name(const Twine &Name) {}
 
 void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
index 2c528bde49ac0b5564fa80f0a89cf472b72df53f..407b194e1b6ae4fb02c387e4aa7f3d81ca44f474 100644 (file)
@@ -106,7 +106,7 @@ uint64_t llvm::get_threadid() {
 }
 
 
-constexpr uint32_t llvm::get_max_thread_name_length() {
+static constexpr uint32_t get_max_thread_name_length_impl() {
 #if defined(__NetBSD__)
        return PTHREAD_MAX_NAMELEN_NP;
 #elif defined(__APPLE__)
@@ -124,6 +124,10 @@ constexpr uint32_t llvm::get_max_thread_name_length() {
 #endif
 }
 
+uint32_t llvm::get_max_thread_name_length() {
+  return get_max_thread_name_length_impl();
+}
+
 void llvm::set_thread_name(const Twine &Name) {
   // Make sure the input is null terminated.
   SmallString<64> Storage;
@@ -134,7 +138,8 @@ void llvm::set_thread_name(const Twine &Name) {
   // terminated, but additionally the end of a long thread name will usually
   // be more unique than the beginning, since a common pattern is for similar
   // threads to share a common prefix.
-  NameStr = NameStr.take_back(get_max_thread_name_length());
+  if (get_max_thread_name_length() > 0)
+    NameStr = NameStr.take_back(get_max_thread_name_length());
   (void)NameStr;
 #if defined(__linux__)
 #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
@@ -192,15 +197,17 @@ void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
   free(kp);
   return;
 #elif defined(__NetBSD__)
-  char buf[get_max_thread_name_length()];
-  ::pthread_getname_np(::pthread_self(), buf, PTHREAD_MAX_NAMELEN_NP);
+  constexpr uint32_t len = get_max_thread_name_length_impl();
+  char buf[len];
+  ::pthread_getname_np(::pthread_self(), buf, len);
 
   Name.append(buf, buf + strlen(buf));
 #elif defined(__linux__)
 #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
 #if HAVE_PTHREAD_GETNAME_NP
-  char Buffer[get_max_thread_name_length()];
-  if (0 == ::pthread_getname_np(::pthread_self(), Buffer, MAXNAMELEN))
+  constexpr uint32_t len = get_max_thread_name_length_impl();
+  char Buffer[len];
+  if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
     Name.append(Buffer, Buffer + strlen(Buffer));
 #endif
 #endif
index 8f36f79ca43665ef5478138d3e173a2fcb44c385..93b2dfe09457dd4dd77f486037de069a278bc48a 100644 (file)
@@ -59,7 +59,7 @@ uint64_t llvm::get_threadid() {
   return uint64_t(::GetCurrentThreadId());
 }
 
-constexpr uint32_t llvm::get_max_thread_name_length() { return 0; }
+uint32_t llvm::get_max_thread_name_length() { return 0; }
 
 void llvm::set_thread_name(const Twine &Name) {
 #if defined(_MSC_VER)