2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/ThreadName.h>
19 #include <type_traits>
21 #include <folly/Portability.h>
22 #include <folly/Traits.h>
23 #include <folly/portability/PThread.h>
27 // This looks a bit weird, but it's necessary to avoid
28 // having an undefined compiler function called.
29 #if defined(__GLIBC__) && !defined(__APPLE__) && !defined(__ANDROID__)
30 #if __GLIBC_PREREQ(2, 12)
31 // has pthread_setname_np(pthread_t, const char*) (2 params)
32 #define FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME 1
36 #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
37 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
38 // has pthread_setname_np(const char*) (1 param)
39 #define FOLLY_HAS_PTHREAD_SETNAME_NP_NAME 1
43 bool canSetCurrentThreadName() {
44 #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME || \
45 FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
52 bool canSetOtherThreadName() {
53 #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
60 static constexpr size_t kMaxThreadNameLength = 16;
62 Optional<std::string> getCurrentThreadName() {
63 #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME || \
64 FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
65 std::array<char, kMaxThreadNameLength> buf;
66 if (pthread_getname_np(pthread_self(), buf.data(), buf.size()) != 0) {
67 return Optional<std::string>();
69 return make_optional(std::string(buf.data()));
71 return Optional<std::string>();
75 bool setThreadName(std::thread::id tid, StringPiece name) {
76 #if !FOLLY_HAVE_PTHREAD || _WIN32
80 std::is_same<pthread_t, std::thread::native_handle_type>::value,
81 "This assumes that the native handle type is pthread_t");
83 sizeof(std::thread::native_handle_type) == sizeof(std::thread::id),
84 "This assumes std::thread::id is a thin wrapper around "
85 "std::thread::native_handle_type, but that doesn't appear to be true.");
86 // In most implementations, std::thread::id is a thin wrapper around
87 // std::thread::native_handle_type, which means we can do unsafe things to
90 std::memcpy(&id, &tid, sizeof(id));
91 auto trimmedName = name.fbstr().substr(0, kMaxThreadNameLength - 1);
92 #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
93 return 0 == pthread_setname_np(id, trimmedName.c_str());
94 #elif FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
95 // Since OS X 10.6 it is possible for a thread to set its own name,
96 // but not that of some other thread.
97 if (pthread_equal(pthread_self(), id)) {
98 return 0 == pthread_setname_np(trimmedName.c_str());
107 #if FOLLY_HAVE_PTHREAD
108 bool setThreadName(pthread_t pid, StringPiece name) {
110 // Not currently supported on Windows.
114 std::is_same<pthread_t, std::thread::native_handle_type>::value,
115 "This assumes that the native handle type is pthread_t");
117 sizeof(std::thread::native_handle_type) == sizeof(std::thread::id),
118 "This assumes std::thread::id is a thin wrapper around "
119 "std::thread::native_handle_type, but that doesn't appear to be true.");
120 // In most implementations, std::thread::id is a thin wrapper around
121 // std::thread::native_handle_type, which means we can do unsafe things to
124 std::memcpy(&id, &pid, sizeof(id));
125 return setThreadName(id, name);
130 bool setThreadName(StringPiece name) {
131 return setThreadName(std::this_thread::get_id(), name);