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 bool setThreadName(std::thread::id tid, StringPiece name) {
61 #if !FOLLY_HAVE_PTHREAD || _WIN32
65 std::is_same<pthread_t, std::thread::native_handle_type>::value,
66 "This assumes that the native handle type is pthread_t");
68 sizeof(std::thread::native_handle_type) == sizeof(std::thread::id),
69 "This assumes std::thread::id is a thin wrapper around "
70 "std::thread::native_handle_type, but that doesn't appear to be true.");
71 // In most implementations, std::thread::id is a thin wrapper around
72 // std::thread::native_handle_type, which means we can do unsafe things to
75 std::memcpy(&id, &tid, sizeof(id));
76 #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
77 return 0 == pthread_setname_np(id, name.fbstr().substr(0, 15).c_str());
78 #elif FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
79 // Since OS X 10.6 it is possible for a thread to set its own name,
80 // but not that of some other thread.
81 if (pthread_equal(pthread_self(), id)) {
82 return 0 == pthread_setname_np(name.fbstr().c_str());
91 #if FOLLY_HAVE_PTHREAD
92 bool setThreadName(pthread_t pid, StringPiece name) {
94 // Not currently supported on Windows.
98 std::is_same<pthread_t, std::thread::native_handle_type>::value,
99 "This assumes that the native handle type is pthread_t");
101 sizeof(std::thread::native_handle_type) == sizeof(std::thread::id),
102 "This assumes std::thread::id is a thin wrapper around "
103 "std::thread::native_handle_type, but that doesn't appear to be true.");
104 // In most implementations, std::thread::id is a thin wrapper around
105 // std::thread::native_handle_type, which means we can do unsafe things to
108 std::memcpy(&id, &pid, sizeof(id));
109 return setThreadName(id, name);
114 bool setThreadName(StringPiece name) {
115 return setThreadName(std::this_thread::get_id(), name);