Fix issue where compiler cannot determine address of ::free at compile time
[folly.git] / folly / ThreadName.h
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 #pragma once
18
19 #include <thread>
20 #include <type_traits>
21
22 #include <folly/Range.h>
23 #include <folly/Traits.h>
24 #include <folly/portability/Config.h>
25 #include <folly/portability/PThread.h>
26
27 namespace folly {
28
29 // This looks a bit weird, but it's necessary to avoid
30 // having an undefined compiler function called.
31 #if defined(__GLIBC__) && !defined(__APPLE__) && !defined(__ANDROID__)
32 #if __GLIBC_PREREQ(2, 12)
33 // has pthread_setname_np(pthread_t, const char*) (2 params)
34 #define FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME 1
35 #endif
36 #endif
37
38 #if defined(__APPLE__) && defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
39 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
40 // has pthread_setname_np(const char*) (1 param)
41 #define FOLLY_HAS_PTHREAD_SETNAME_NP_NAME 1
42 #endif
43 #endif
44
45 template <typename T>
46 inline bool setThreadName(T /* id */, StringPiece /* name */) {
47   static_assert(
48 #if FOLLY_HAVE_PTHREAD
49       std::is_same<T, pthread_t>::value ||
50 #endif
51       std::is_same<T, std::thread::id>::value ||
52       std::is_same<T, std::thread::native_handle_type>::value,
53       "type must be pthread_t, std::thread::id or "
54       "std::thread::native_handle_type");
55   return false;
56 }
57
58 template <>
59 inline bool setThreadName(std::thread::id tid, StringPiece name) {
60 #if !FOLLY_HAVE_PTHREAD
61   return false;
62 #else
63   static_assert(
64       std::is_same<pthread_t, std::thread::native_handle_type>::value,
65       "This assumes that the native handle type is pthread_t");
66   static_assert(
67       sizeof(std::thread::native_handle_type) == sizeof(std::thread::id),
68       "This assumes std::thread::id is a thin wrapper around "
69       "std::thread::native_handle_type, but that doesn't appear to be true.");
70   // In most implementations, std::thread::id is a thin wrapper around
71   // std::thread::native_handle_type, which means we can do unsafe things to
72   // extract it.
73   pthread_t id;
74   std::memcpy(&id, &tid, sizeof(id));
75 #if FOLLY_HAS_PTHREAD_SETNAME_NP_THREAD_NAME
76   return 0 == pthread_setname_np(id, name.fbstr().substr(0, 15).c_str());
77 #elif FOLLY_HAS_PTHREAD_SETNAME_NP_NAME
78   // Since OS X 10.6 it is possible for a thread to set its own name,
79   // but not that of some other thread.
80   if (pthread_equal(pthread_self(), id)) {
81     return 0 == pthread_setname_np(name.fbstr().c_str());
82   }
83   return false;
84 #else
85   return false;
86 #endif
87 #endif
88 }
89
90 #if FOLLY_HAVE_PTHREAD
91 template <>
92 inline 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 inline bool setThreadName(StringPiece name) {
110   return setThreadName(std::this_thread::get_id(), name);
111 }
112 }