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

Refactor setThreadName to have the std::thread::id overload as the implementation

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
parent 43c0c2c5
...@@ -34,6 +34,7 @@ namespace folly { ...@@ -34,6 +34,7 @@ namespace folly {
#define FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME 1 #define FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME 1
#endif #endif
#endif #endif
#if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED) #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
// has pthread_setname_np(const char*) (1 param) // has pthread_setname_np(const char*) (1 param)
...@@ -54,49 +55,58 @@ inline bool setThreadName(T /* id */, StringPiece /* name */) { ...@@ -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());
} #elif FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
#endif
#ifdef FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
template <>
inline bool setThreadName(pthread_t id, StringPiece name) {
// Since OS X 10.6 it is possible for a thread to set its own name, // Since OS X 10.6 it is possible for a thread to set its own name,
// but not that of some other thread. // but not that of some other thread.
if (pthread_equal(pthread_self(), id)) { if (pthread_equal(pthread_self(), id)) {
return 0 == pthread_setname_np(name.fbstr().c_str()); return 0 == pthread_setname_np(name.fbstr().c_str());
} }
return false; return false;
} #else
return false;
#endif
#endif #endif
}
#if FOLLY_HAVE_PTHREAD #if FOLLY_HAVE_PTHREAD
template < template <>
typename = folly::_t<std::enable_if< inline bool setThreadName(pthread_t pid, StringPiece name) {
std::is_same<pthread_t, std::thread::native_handle_type>::value>>> static_assert(
inline bool setThreadName(std::thread::id id, StringPiece name) { 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 " "This assumes std::thread::id is a thin wrapper around "
"std::thread::native_handle_type, but that doesn't appear to be true."); "std::thread::native_handle_type, but that doesn't appear to be true.");
// In most implementations, std::thread::id is a thin wrapper around // In most implementations, std::thread::id is a thin wrapper around
// std::thread::native_handle_type, which means we can do unsafe things to // std::thread::native_handle_type, which means we can do unsafe things to
// extract it. // extract it.
pthread_t ptid; std::thread::id id;
std::memcpy(&ptid, &id, sizeof(pthread_t)); std::memcpy(&id, &pid, sizeof(id));
return setThreadName(ptid, name); return setThreadName(id, name);
} }
#endif
inline bool setThreadName(StringPiece name) { inline bool setThreadName(StringPiece name) {
return setThreadName(pthread_self(), name); return setThreadName(std::this_thread::get_id(), name);
}
#else
inline bool setThreadName(StringPiece name) {
return false;
} }
#endif
} }
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