acd6163f66ca6be80d0de1e751e3c73a042042e1
[folly.git] / folly / ThreadName.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/ThreadName.h>
18
19 #include <type_traits>
20
21 #include <folly/Portability.h>
22 #include <folly/Traits.h>
23 #include <folly/portability/PThread.h>
24
25 namespace folly {
26
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
33 #endif
34 #endif
35
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
40 #endif
41 #endif
42
43 bool canSetCurrentThreadName() {
44 #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME || \
45     FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
46   return true;
47 #else
48   return false;
49 #endif
50 }
51
52 bool canSetOtherThreadName() {
53 #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
54   return true;
55 #else
56   return false;
57 #endif
58 }
59
60 bool setThreadName(std::thread::id tid, StringPiece name) {
61 #if !FOLLY_HAVE_PTHREAD
62   return false;
63 #else
64   static_assert(
65       std::is_same<pthread_t, std::thread::native_handle_type>::value,
66       "This assumes that the native handle type is pthread_t");
67   static_assert(
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
73   // extract it.
74   pthread_t id;
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());
83   }
84   return false;
85 #else
86   return false;
87 #endif
88 #endif
89 }
90
91 #if FOLLY_HAVE_PTHREAD
92 bool setThreadName(pthread_t pid, StringPiece name) {
93   static_assert(
94       std::is_same<pthread_t, std::thread::native_handle_type>::value,
95       "This assumes that the native handle type is pthread_t");
96   static_assert(
97       sizeof(std::thread::native_handle_type) == sizeof(std::thread::id),
98       "This assumes std::thread::id is a thin wrapper around "
99       "std::thread::native_handle_type, but that doesn't appear to be true.");
100   // In most implementations, std::thread::id is a thin wrapper around
101   // std::thread::native_handle_type, which means we can do unsafe things to
102   // extract it.
103   std::thread::id id;
104   std::memcpy(&id, &pid, sizeof(id));
105   return setThreadName(id, name);
106 }
107 #endif
108
109 bool setThreadName(StringPiece name) {
110   return setThreadName(std::this_thread::get_id(), name);
111 }
112 }