Fix the pkg-config filter
[folly.git] / folly / ThreadName.h
index 4be83f482cfd2724571dfe0cc1b6bc89805c803a..095088567d94ef2b062ca9e244eb4301bcb42360 100644 (file)
 #pragma once
 
 #include <thread>
+#include <type_traits>
+
 #include <pthread.h>
+
 #include <folly/Range.h>
+#include <folly/Traits.h>
 
 namespace folly {
 
@@ -41,8 +45,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 +71,21 @@ inline bool setThreadName(pthread_t id, StringPiece name) {
 }
 #endif
 
+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) {
+  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);
 }