Refactor setThreadName to have the std::thread::id overload as the implementation
authorChristopher Dykes <cdykes@fb.com>
Tue, 18 Apr 2017 00:54:50 +0000 (17:54 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 18 Apr 2017 01:10:26 +0000 (18:10 -0700)
Summary: The pthread_t overload will die in the next diff, but cleanup needs to be done first.

Reviewed By: yfeldblum

Differential Revision: D4900830

fbshipit-source-id: d0cd56c5bd7fe22904f631c0cc64dff66448127c

folly/ThreadName.h

index 0a292a536ac27460c670c6fc433ee2fb986e25ae..52b0cdaca63d625ad41aa94c94ff9a251e4c9baf 100644 (file)
@@ -34,6 +34,7 @@ namespace folly {
 #define FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME 1
 #endif
 #endif
 #define FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME 1
 #endif
 #endif
+
 #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
 // has pthread_setname_np(const char*) (1 param)
 #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
 // has pthread_setname_np(const char*) (1 param)
@@ -54,49 +55,58 @@ inline bool setThreadName(T /* id */, StringPiece /* name */) {
   return false;
 }
 
   return false;
 }
 
-#ifdef FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
 template <>
 template <>
-inline bool setThreadName(pthread_t id, StringPiece name) {
+inline bool setThreadName(std::thread::id tid, StringPiece name) {
+#if !FOLLY_HAVE_PTHREAD
+  return false;
+#else
+  static_assert(
+      std::is_same<pthread_t, std::thread::native_handle_type>::value,
+      "This assumes that the native handle type is pthread_t");
+  static_assert(
+      sizeof(std::thread::native_handle_type) == sizeof(std::thread::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 id;
+  std::memcpy(&id, &tid, sizeof(id));
+#if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
   return 0 == pthread_setname_np(id, name.fbstr().substr(0, 15).c_str());
   return 0 == pthread_setname_np(id, name.fbstr().substr(0, 15).c_str());
-}
-#endif
-
-#ifdef FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
-template <>
-inline bool setThreadName(pthread_t id, StringPiece name) {
+#elif FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
   // Since OS X 10.6 it is possible for a thread to set its own name,
   // but not that of some other thread.
   if (pthread_equal(pthread_self(), id)) {
     return 0 == pthread_setname_np(name.fbstr().c_str());
   }
   return false;
   // Since OS X 10.6 it is possible for a thread to set its own name,
   // but not that of some other thread.
   if (pthread_equal(pthread_self(), id)) {
     return 0 == pthread_setname_np(name.fbstr().c_str());
   }
   return false;
-}
+#else
+  return false;
+#endif
 #endif
 #endif
+}
 
 #if FOLLY_HAVE_PTHREAD
 
 #if FOLLY_HAVE_PTHREAD
-template <
-    typename = folly::_t<std::enable_if<
-        std::is_same<pthread_t, std::thread::native_handle_type>::value>>>
-inline bool setThreadName(std::thread::id id, StringPiece name) {
+template <>
+inline bool setThreadName(pthread_t pid, StringPiece name) {
+  static_assert(
+      std::is_same<pthread_t, std::thread::native_handle_type>::value,
+      "This assumes that the native handle type is pthread_t");
   static_assert(
   static_assert(
-      sizeof(std::thread::native_handle_type) == sizeof(decltype(id)),
+      sizeof(std::thread::native_handle_type) == sizeof(std::thread::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.
       "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;
-  std::memcpy(&ptid, &id, sizeof(pthread_t));
-  return setThreadName(ptid, name);
+  std::thread::id id;
+  std::memcpy(&id, &pid, sizeof(id));
+  return setThreadName(id, name);
 }
 }
+#endif
 
 inline bool setThreadName(StringPiece name) {
 
 inline bool setThreadName(StringPiece name) {
-  return setThreadName(pthread_self(), name);
-}
-#else 
-inline bool setThreadName(StringPiece name) {
-  return false;
+  return setThreadName(std::this_thread::get_id(), name);
 }
 }
-#endif
-
 }
 }