Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 1 | //===- Unix/Threading.inc - Unix Threading Implementation ----- -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Unix specific implementation of Threading functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/ADT/SmallString.h" |
| 15 | #include "llvm/ADT/Twine.h" |
| 16 | |
| 17 | #if defined(__APPLE__) |
| 18 | #include <mach/mach_init.h> |
| 19 | #include <mach/mach_port.h> |
| 20 | #endif |
| 21 | |
| 22 | #include <pthread.h> |
| 23 | |
| 24 | #if defined(__FreeBSD__) |
Zachary Turner | d973813 | 2017-03-03 18:38:22 +0000 | [diff] [blame] | 25 | #include <pthread_np.h> // For pthread_getthreadid_np() |
| 26 | #endif |
| 27 | |
| 28 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
Kamil Rytarowski | 71efce2 | 2017-03-04 17:42:46 +0000 | [diff] [blame^] | 29 | #include <sys/sysctl.h> |
| 30 | #include <sys/user.h> |
Zachary Turner | d973813 | 2017-03-03 18:38:22 +0000 | [diff] [blame] | 31 | #include <errno.h> |
| 32 | #include <unistd.h> |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | #if defined(__NetBSD__) |
Zachary Turner | d973813 | 2017-03-03 18:38:22 +0000 | [diff] [blame] | 36 | #include <lwp.h> // For _lwp_self() |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | #if defined(__linux__) |
Zachary Turner | d973813 | 2017-03-03 18:38:22 +0000 | [diff] [blame] | 40 | #include <unistd.h> // For syscall() |
| 41 | #include <sys/syscall.h> // For syscall codes |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 44 | namespace { |
| 45 | struct ThreadInfo { |
| 46 | void(*UserFn)(void *); |
| 47 | void *UserData; |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | static void *ExecuteOnThread_Dispatch(void *Arg) { |
| 52 | ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg); |
| 53 | TI->UserFn(TI->UserData); |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
| 57 | void llvm::llvm_execute_on_thread(void(*Fn)(void*), void *UserData, |
| 58 | unsigned RequestedStackSize) { |
| 59 | ThreadInfo Info = { Fn, UserData }; |
| 60 | pthread_attr_t Attr; |
| 61 | pthread_t Thread; |
| 62 | |
| 63 | // Construct the attributes object. |
| 64 | if (::pthread_attr_init(&Attr) != 0) |
| 65 | return; |
| 66 | |
| 67 | // Set the requested stack size, if given. |
| 68 | if (RequestedStackSize != 0) { |
| 69 | if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0) |
| 70 | goto error; |
| 71 | } |
| 72 | |
| 73 | // Construct and execute the thread. |
| 74 | if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0) |
| 75 | goto error; |
| 76 | |
| 77 | // Wait for the thread and clean up. |
| 78 | ::pthread_join(Thread, nullptr); |
| 79 | |
| 80 | error: |
| 81 | ::pthread_attr_destroy(&Attr); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | uint64_t llvm::get_threadid() { |
| 86 | #if defined(__APPLE__) |
| 87 | // Calling "mach_thread_self()" bumps the reference count on the thread |
| 88 | // port, so we need to deallocate it. mach_task_self() doesn't bump the ref |
| 89 | // count. |
| 90 | thread_port_t Self = mach_thread_self(); |
| 91 | mach_port_deallocate(mach_task_self(), Self); |
| 92 | return Self; |
| 93 | #elif defined(__FreeBSD__) |
| 94 | return uint64_t(pthread_getthreadid_np()); |
| 95 | #elif defined(__NetBSD__) |
| 96 | return uint64_t(_lwp_self()); |
| 97 | #elif defined(__ANDROID__) |
| 98 | return uint64_t(gettid()); |
| 99 | #elif defined(__linux__) |
| 100 | return uint64_t(syscall(SYS_gettid)); |
| 101 | #elif defined(LLVM_ON_WIN32) |
| 102 | return uint64_t(::GetCurrentThreadId()); |
| 103 | #else |
| 104 | return uint64_t(pthread_self()); |
| 105 | #endif |
| 106 | } |
| 107 | |
| 108 | |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 109 | constexpr uint32_t llvm::get_max_thread_name_length() { |
| 110 | #if defined(__NetBSD__) |
| 111 | return PTHREAD_MAX_NAMELEN_NP; |
| 112 | #elif defined(__APPLE__) |
| 113 | return 64; |
| 114 | #elif defined(__linux__) |
| 115 | #if HAVE_PTHREAD_SETNAME_NP |
| 116 | return 16; |
| 117 | #else |
| 118 | return 0; |
| 119 | #endif |
| 120 | #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
| 121 | return 16; |
| 122 | #else |
| 123 | return 0; |
| 124 | #endif |
| 125 | } |
| 126 | |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 127 | void llvm::set_thread_name(const Twine &Name) { |
| 128 | // Make sure the input is null terminated. |
| 129 | SmallString<64> Storage; |
| 130 | StringRef NameStr = Name.toNullTerminatedStringRef(Storage); |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 131 | |
| 132 | // Truncate from the beginning, not the end, if the specified name is too |
| 133 | // long. For one, this ensures that the resulting string is still null |
| 134 | // terminated, but additionally the end of a long thread name will usually |
| 135 | // be more unique than the beginning, since a common pattern is for similar |
| 136 | // threads to share a common prefix. |
| 137 | NameStr = NameStr.take_back(get_max_thread_name_length()); |
Krzysztof Parzyszek | 75464e1 | 2017-03-03 22:21:02 +0000 | [diff] [blame] | 138 | (void)NameStr; |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 139 | #if defined(__linux__) |
| 140 | #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__) |
Krzysztof Parzyszek | 6cf2540 | 2017-03-03 21:53:12 +0000 | [diff] [blame] | 141 | #if HAVE_PTHREAD_SETNAME_NP |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 142 | ::pthread_setname_np(::pthread_self(), NameStr.data()); |
| 143 | #endif |
Krzysztof Parzyszek | 6cf2540 | 2017-03-03 21:53:12 +0000 | [diff] [blame] | 144 | #endif |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 145 | #elif defined(__FreeBSD__) |
| 146 | ::pthread_set_name_np(::pthread_self(), NameStr.data()); |
| 147 | #elif defined(__NetBSD__) |
| 148 | ::pthread_setname_np(::pthread_self(), "%s", |
| 149 | const_cast<char *>(NameStr.data())); |
| 150 | #elif defined(__APPLE__) |
| 151 | ::pthread_setname_np(NameStr.data()); |
| 152 | #endif |
| 153 | } |
| 154 | |
| 155 | void llvm::get_thread_name(SmallVectorImpl<char> &Name) { |
| 156 | Name.clear(); |
| 157 | |
| 158 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) |
Zachary Turner | 45337cf | 2017-03-03 18:21:04 +0000 | [diff] [blame] | 159 | int pid = ::getpid(); |
| 160 | uint64_t tid = get_threadid(); |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 161 | |
| 162 | struct kinfo_proc *kp = nullptr, *nkp; |
| 163 | size_t len = 0; |
| 164 | int error; |
| 165 | int ctl[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, |
| 166 | (int)pid }; |
| 167 | |
| 168 | while (1) { |
| 169 | error = sysctl(ctl, 4, kp, &len, nullptr, 0); |
| 170 | if (kp == nullptr || (error != 0 && errno == ENOMEM)) { |
| 171 | // Add extra space in case threads are added before next call. |
| 172 | len += sizeof(*kp) + len / 10; |
| 173 | nkp = (struct kinfo_proc *)realloc(kp, len); |
| 174 | if (nkp == nullptr) { |
| 175 | free(kp); |
| 176 | return; |
| 177 | } |
| 178 | kp = nkp; |
| 179 | continue; |
| 180 | } |
| 181 | if (error != 0) |
| 182 | len = 0; |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | for (size_t i = 0; i < len / sizeof(*kp); i++) { |
| 187 | if (kp[i].ki_tid == (lwpid_t)tid) { |
| 188 | Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname)); |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | free(kp); |
| 193 | return; |
| 194 | #elif defined(__NetBSD__) |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 195 | char buf[get_max_thread_name_length()]; |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 196 | ::pthread_getname_np(::pthread_self(), buf, PTHREAD_MAX_NAMELEN_NP); |
| 197 | |
| 198 | Name.append(buf, buf + strlen(buf)); |
| 199 | #elif defined(__linux__) |
| 200 | #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__) |
Krzysztof Parzyszek | 6cf2540 | 2017-03-03 21:53:12 +0000 | [diff] [blame] | 201 | #if HAVE_PTHREAD_GETNAME_NP |
Zachary Turner | 777de77 | 2017-03-04 16:42:25 +0000 | [diff] [blame] | 202 | char Buffer[get_max_thread_name_length()]; |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 203 | if (0 == ::pthread_getname_np(::pthread_self(), Buffer, MAXNAMELEN)) |
| 204 | Name.append(Buffer, Buffer + strlen(Buffer)); |
| 205 | #endif |
| 206 | #endif |
Krzysztof Parzyszek | 6cf2540 | 2017-03-03 21:53:12 +0000 | [diff] [blame] | 207 | #endif |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 208 | } |