From f57ddfc76b3835033a36343df22ae821168dcc0f Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Sun, 15 Jan 2017 17:46:23 -0800 Subject: [PATCH] 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 --- folly/ThreadName.h | 25 +++++++++++++++++++++++-- folly/test/ThreadNameTest.cpp | 17 +++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) 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 +#include + #include + #include namespace folly { @@ -41,8 +44,10 @@ template inline bool setThreadName(T /* id */, StringPiece /* name */) { static_assert( std::is_same::value || - std::is_same::value, - "type must be pthread_t or std::thread::native_handle_type"); + std::is_same::value || + std::is_same::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::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(&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 + #include #include #include @@ -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")); +} -- 2.34.1