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.
20 #include <type_traits>
22 #include <folly/Range.h>
23 #include <folly/Traits.h>
24 #include <folly/portability/Config.h>
25 #include <folly/portability/PThread.h>
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
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
46 inline bool setThreadName(T /* id */, StringPiece /* name */) {
48 #if FOLLY_HAVE_PTHREAD
49 std::is_same<T, pthread_t>::value ||
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");
59 inline bool setThreadName(std::thread::id tid, StringPiece name) {
60 #if !FOLLY_HAVE_PTHREAD
64 std::is_same<pthread_t, std::thread::native_handle_type>::value,
65 "This assumes that the native handle type is pthread_t");
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
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());
90 #if FOLLY_HAVE_PTHREAD
92 inline bool setThreadName(pthread_t pid, StringPiece name) {
94 std::is_same<pthread_t, std::thread::native_handle_type>::value,
95 "This assumes that the native handle type is pthread_t");
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
104 std::memcpy(&id, &pid, sizeof(id));
105 return setThreadName(id, name);
109 inline bool setThreadName(StringPiece name) {
110 return setThreadName(std::this_thread::get_id(), name);