Make Range.h and FBString.h mutually independent
[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 namespace {
44
45 #if FOLLY_HAVE_PTHREAD && !_WIN32
46 pthread_t stdTidToPthreadId(std::thread::id tid) {
47   static_assert(
48       std::is_same<pthread_t, std::thread::native_handle_type>::value,
49       "This assumes that the native handle type is pthread_t");
50   static_assert(
51       sizeof(std::thread::native_handle_type) == sizeof(std::thread::id),
52       "This assumes std::thread::id is a thin wrapper around "
53       "std::thread::native_handle_type, but that doesn't appear to be true.");
54   // In most implementations, std::thread::id is a thin wrapper around
55   // std::thread::native_handle_type, which means we can do unsafe things to
56   // extract it.
57   pthread_t id;
58   std::memcpy(&id, &tid, sizeof(id));
59   return id;
60 }
61 #endif
62
63 } // namespace
64
65 bool canSetCurrentThreadName() {
66 #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME || \
67     FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
68   return true;
69 #else
70   return false;
71 #endif
72 }
73
74 bool canSetOtherThreadName() {
75 #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
76   return true;
77 #else
78   return false;
79 #endif
80 }
81
82 static constexpr size_t kMaxThreadNameLength = 16;
83
84 Optional<std::string> getThreadName(std::thread::id id) {
85 #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME || \
86     FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
87   std::array<char, kMaxThreadNameLength> buf;
88   if (pthread_getname_np(stdTidToPthreadId(id), buf.data(), buf.size()) != 0) {
89     return Optional<std::string>();
90   }
91   return make_optional(std::string(buf.data()));
92 #else
93   return Optional<std::string>();
94 #endif
95 }
96
97 Optional<std::string> getCurrentThreadName() {
98   return getThreadName(std::this_thread::get_id());
99 }
100
101 bool setThreadName(std::thread::id tid, StringPiece name) {
102 #if !FOLLY_HAVE_PTHREAD || _WIN32
103   return false;
104 #else
105   auto const piece = name.subpiece(0, kMaxThreadNameLength - 1);
106   auto const data = piece.data();
107   auto const size = piece.size();
108   char trimmedName[kMaxThreadNameLength];
109   std::memcpy(trimmedName, data, size);
110   std::memset(trimmedName + size, 0, kMaxThreadNameLength - size);
111   auto id = stdTidToPthreadId(tid);
112 #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
113   return 0 == pthread_setname_np(id, const_cast<char const*>(trimmedName));
114 #elif FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
115   // Since OS X 10.6 it is possible for a thread to set its own name,
116   // but not that of some other thread.
117   if (pthread_equal(pthread_self(), id)) {
118     return 0 == pthread_setname_np(const_cast<char const*>(trimmedName));
119   }
120   return false;
121 #else
122   (void)id;
123   return false;
124 #endif
125 #endif
126 }
127
128 #if FOLLY_HAVE_PTHREAD
129 bool setThreadName(pthread_t pid, StringPiece name) {
130 #if _WIN32
131   // Not currently supported on Windows.
132   return false;
133 #else
134   static_assert(
135       std::is_same<pthread_t, std::thread::native_handle_type>::value,
136       "This assumes that the native handle type is pthread_t");
137   static_assert(
138       sizeof(std::thread::native_handle_type) == sizeof(std::thread::id),
139       "This assumes std::thread::id is a thin wrapper around "
140       "std::thread::native_handle_type, but that doesn't appear to be true.");
141   // In most implementations, std::thread::id is a thin wrapper around
142   // std::thread::native_handle_type, which means we can do unsafe things to
143   // extract it.
144   std::thread::id id;
145   std::memcpy(&id, &pid, sizeof(id));
146   return setThreadName(id, name);
147 #endif
148 }
149 #endif
150
151 bool setThreadName(StringPiece name) {
152   return setThreadName(std::this_thread::get_id(), name);
153 }
154 }