From: Christopher Dykes <cdykes@fb.com>
Date: Mon, 16 Jan 2017 01:46:23 +0000 (-0800)
Subject: Support setting thread name via std::thread::id
X-Git-Tag: v2017.03.06.00~100
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=f57ddfc76b3835033a36343df22ae821168dcc0f;p=folly.git

Support setting thread name via std::thread::id

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
---

diff --git a/folly/ThreadName.h b/folly/ThreadName.h
index 4be83f48..018f8abc 100644
--- a/folly/ThreadName.h
+++ b/folly/ThreadName.h
@@ -17,7 +17,10 @@
 #pragma once
 
 #include <thread>
+#include <type_traits>
+
 #include <pthread.h>
+
 #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 ||
-      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;
 }
 
@@ -65,6 +70,22 @@ inline bool setThreadName(pthread_t id, StringPiece name) {
 }
 #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);
 }
diff --git a/folly/test/ThreadNameTest.cpp b/folly/test/ThreadNameTest.cpp
index a76364c8..1b3b043d 100644
--- a/folly/test/ThreadNameTest.cpp
+++ b/folly/test/ThreadNameTest.cpp
@@ -15,6 +15,7 @@
  */
 
 #include <thread>
+
 #include <folly/Baton.h>
 #include <folly/ScopeGuard.h>
 #include <folly/ThreadName.h>
@@ -23,7 +24,7 @@
 using namespace std;
 using namespace folly;
 
-constexpr bool expectedSetOtherThreadNameResult =
+static constexpr bool expectedSetOtherThreadNameResult =
 #ifdef FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
     true
 #else
@@ -31,7 +32,7 @@ constexpr bool expectedSetOtherThreadNameResult =
 #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
@@ -74,3 +75,15 @@ TEST(ThreadName, setThreadName_other_native) {
       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"));
+}