Support setting thread name via std::thread::id
authorChristopher Dykes <cdykes@fb.com>
Mon, 16 Jan 2017 01:46:23 +0000 (17:46 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 16 Jan 2017 01:48:02 +0000 (17:48 -0800)
Summary: It's possible via some hackery, so wrap it up nicely into the API.

Reviewed By: yfeldblum

Differential Revision: D4418728

fbshipit-source-id: f00aed95cdbdc3cc83a0ab96565117cd01141c2e

folly/ThreadName.h
folly/test/ThreadNameTest.cpp

index 4be83f482cfd2724571dfe0cc1b6bc89805c803a..018f8abc90b5ab071c220ef84e4b73c8424f275a 100644 (file)
 #pragma once
 
 #include <thread>
 #pragma once
 
 #include <thread>
+#include <type_traits>
+
 #include <pthread.h>
 #include <pthread.h>
+
 #include <folly/Range.h>
 
 namespace folly {
 #include <folly/Range.h>
 
 namespace folly {
@@ -41,8 +44,10 @@ template <typename T>
 inline bool setThreadName(T /* id */, StringPiece /* name */) {
   static_assert(
       std::is_same<T, pthread_t>::value ||
 inline bool setThreadName(T /* id */, StringPiece /* name */) {
   static_assert(
       std::is_same<T, pthread_t>::value ||
-      std::is_same<T, std::thread::native_handle_type>::value,
-      "type must be pthread_t or std::thread::native_handle_type");
+          std::is_same<T, std::thread::id>::value ||
+          std::is_same<T, std::thread::native_handle_type>::value,
+      "type must be pthread_t, std::thread::id or "
+      "std::thread::native_handle_type");
   return false;
 }
 
   return false;
 }
 
@@ -65,6 +70,22 @@ inline bool setThreadName(pthread_t id, StringPiece name) {
 }
 #endif
 
 }
 #endif
 
+template <>
+inline typename std::enable_if<
+    std::is_same<pthread_t, std::thread::native_handle_type>::value,
+    bool>::type
+setThreadName(std::thread::id id, StringPiece name) {
+  static_assert(
+      sizeof(std::thread::native_handle_type) == sizeof(decltype(id)),
+      "This assumes std::thread::id is a thin wrapper around "
+      "std::thread::native_handle_type, but that doesn't appear to be true.");
+  // In most implementations, std::thread::id is a thin wrapper around
+  // std::thread::native_handle_type, which means we can do unsafe things to
+  // extract it.
+  pthread_t ptid = *reinterpret_cast<pthread_t*>(&id);
+  return setThreadName(ptid, name);
+}
+
 inline bool setThreadName(StringPiece name) {
   return setThreadName(pthread_self(), name);
 }
 inline bool setThreadName(StringPiece name) {
   return setThreadName(pthread_self(), name);
 }
index a76364c8f21df450993aef9ca42743dca675c437..1b3b043df35d0f4954170cc5a70a0aecd3ff7e23 100644 (file)
@@ -15,6 +15,7 @@
  */
 
 #include <thread>
  */
 
 #include <thread>
+
 #include <folly/Baton.h>
 #include <folly/ScopeGuard.h>
 #include <folly/ThreadName.h>
 #include <folly/Baton.h>
 #include <folly/ScopeGuard.h>
 #include <folly/ThreadName.h>
@@ -23,7 +24,7 @@
 using namespace std;
 using namespace folly;
 
 using namespace std;
 using namespace folly;
 
-constexpr bool expectedSetOtherThreadNameResult =
+static constexpr bool expectedSetOtherThreadNameResult =
 #ifdef FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
     true
 #else
 #ifdef FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
     true
 #else
@@ -31,7 +32,7 @@ constexpr bool expectedSetOtherThreadNameResult =
 #endif
     ;
 
 #endif
     ;
 
-constexpr bool expectedSetSelfThreadNameResult =
+static constexpr bool expectedSetSelfThreadNameResult =
 #if defined(FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME) || \
     defined(FOLLY_HAS_PTHREAD_SETNAME_NP_NAME)
     true
 #if defined(FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME) || \
     defined(FOLLY_HAS_PTHREAD_SETNAME_NP_NAME)
     true
@@ -74,3 +75,15 @@ TEST(ThreadName, setThreadName_other_native) {
       expectedSetOtherThreadNameResult,
       setThreadName(th.native_handle(), "rockin-thread"));
 }
       expectedSetOtherThreadNameResult,
       setThreadName(th.native_handle(), "rockin-thread"));
 }
+
+TEST(ThreadName, setThreadName_other_id) {
+  Baton<> let_thread_end;
+  thread th([&] {
+      let_thread_end.wait();
+  });
+  SCOPE_EXIT { th.join(); };
+  SCOPE_EXIT { let_thread_end.post(); };
+  EXPECT_EQ(
+      expectedSetOtherThreadNameResult,
+      setThreadName(th.get_id(), "rockin-thread"));
+}