Commit f57ddfc7 authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot

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
parent ff66c8dc
...@@ -17,7 +17,10 @@ ...@@ -17,7 +17,10 @@
#pragma once #pragma once
#include <thread> #include <thread>
#include <type_traits>
#include <pthread.h> #include <pthread.h>
#include <folly/Range.h> #include <folly/Range.h>
namespace folly { namespace folly {
...@@ -41,8 +44,10 @@ template <typename T> ...@@ -41,8 +44,10 @@ template <typename T>
inline bool setThreadName(T /* id */, StringPiece /* name */) { inline bool setThreadName(T /* id */, StringPiece /* name */) {
static_assert( static_assert(
std::is_same<T, pthread_t>::value || std::is_same<T, pthread_t>::value ||
std::is_same<T, std::thread::native_handle_type>::value, std::is_same<T, std::thread::id>::value ||
"type must be pthread_t or std::thread::native_handle_type"); 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) { ...@@ -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) { inline bool setThreadName(StringPiece name) {
return setThreadName(pthread_self(), name); return setThreadName(pthread_self(), name);
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
#include <thread> #include <thread>
#include <folly/Baton.h> #include <folly/Baton.h>
#include <folly/ScopeGuard.h> #include <folly/ScopeGuard.h>
#include <folly/ThreadName.h> #include <folly/ThreadName.h>
...@@ -23,7 +24,7 @@ ...@@ -23,7 +24,7 @@
using namespace std; using namespace std;
using namespace folly; using namespace folly;
constexpr bool expectedSetOtherThreadNameResult = static constexpr bool expectedSetOtherThreadNameResult =
#ifdef FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME #ifdef FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
true true
#else #else
...@@ -31,7 +32,7 @@ constexpr bool expectedSetOtherThreadNameResult = ...@@ -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) || \ #if defined(FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME) || \
defined(FOLLY_HAS_PTHREAD_SETNAME_NP_NAME) defined(FOLLY_HAS_PTHREAD_SETNAME_NP_NAME)
true true
...@@ -74,3 +75,15 @@ TEST(ThreadName, setThreadName_other_native) { ...@@ -74,3 +75,15 @@ TEST(ThreadName, setThreadName_other_native) {
expectedSetOtherThreadNameResult, expectedSetOtherThreadNameResult,
setThreadName(th.native_handle(), "rockin-thread")); 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"));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment