blob: 833b6bed4e7f55c7b98146b1c359009168793c93 [file] [log] [blame]
Zachary Turner757dbc92017-03-03 17:15:17 +00001//===- 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 Turnerd9738132017-03-03 18:38:22 +000025#include <pthread_np.h> // For pthread_getthreadid_np()
26#endif
27
28#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
29#include <errno.h>
30#include <unistd.h>
Zachary Turner757dbc92017-03-03 17:15:17 +000031#endif
32
33#if defined(__NetBSD__)
Zachary Turnerd9738132017-03-03 18:38:22 +000034#include <lwp.h> // For _lwp_self()
Zachary Turner757dbc92017-03-03 17:15:17 +000035#endif
36
37#if defined(__linux__)
Zachary Turnerd9738132017-03-03 18:38:22 +000038#include <unistd.h> // For syscall()
39#include <sys/syscall.h> // For syscall codes
Zachary Turner757dbc92017-03-03 17:15:17 +000040#endif
41
42#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
43#include <sys/sysctl.h>
44#include <sys/user.h>
45#endif
46
47namespace {
48 struct ThreadInfo {
49 void(*UserFn)(void *);
50 void *UserData;
51 };
52}
53
54static void *ExecuteOnThread_Dispatch(void *Arg) {
55 ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg);
56 TI->UserFn(TI->UserData);
57 return nullptr;
58}
59
60void llvm::llvm_execute_on_thread(void(*Fn)(void*), void *UserData,
61 unsigned RequestedStackSize) {
62 ThreadInfo Info = { Fn, UserData };
63 pthread_attr_t Attr;
64 pthread_t Thread;
65
66 // Construct the attributes object.
67 if (::pthread_attr_init(&Attr) != 0)
68 return;
69
70 // Set the requested stack size, if given.
71 if (RequestedStackSize != 0) {
72 if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0)
73 goto error;
74 }
75
76 // Construct and execute the thread.
77 if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0)
78 goto error;
79
80 // Wait for the thread and clean up.
81 ::pthread_join(Thread, nullptr);
82
83error:
84 ::pthread_attr_destroy(&Attr);
85}
86
87
88uint64_t llvm::get_threadid() {
89#if defined(__APPLE__)
90 // Calling "mach_thread_self()" bumps the reference count on the thread
91 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
92 // count.
93 thread_port_t Self = mach_thread_self();
94 mach_port_deallocate(mach_task_self(), Self);
95 return Self;
96#elif defined(__FreeBSD__)
97 return uint64_t(pthread_getthreadid_np());
98#elif defined(__NetBSD__)
99 return uint64_t(_lwp_self());
100#elif defined(__ANDROID__)
101 return uint64_t(gettid());
102#elif defined(__linux__)
103 return uint64_t(syscall(SYS_gettid));
104#elif defined(LLVM_ON_WIN32)
105 return uint64_t(::GetCurrentThreadId());
106#else
107 return uint64_t(pthread_self());
108#endif
109}
110
111
Zachary Turner777de772017-03-04 16:42:25 +0000112constexpr uint32_t llvm::get_max_thread_name_length() {
113#if defined(__NetBSD__)
114 return PTHREAD_MAX_NAMELEN_NP;
115#elif defined(__APPLE__)
116 return 64;
117#elif defined(__linux__)
118#if HAVE_PTHREAD_SETNAME_NP
119 return 16;
120#else
121 return 0;
122#endif
123#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
124 return 16;
125#else
126 return 0;
127#endif
128}
129
Zachary Turner757dbc92017-03-03 17:15:17 +0000130void llvm::set_thread_name(const Twine &Name) {
131 // Make sure the input is null terminated.
132 SmallString<64> Storage;
133 StringRef NameStr = Name.toNullTerminatedStringRef(Storage);
Zachary Turner777de772017-03-04 16:42:25 +0000134
135 // Truncate from the beginning, not the end, if the specified name is too
136 // long. For one, this ensures that the resulting string is still null
137 // terminated, but additionally the end of a long thread name will usually
138 // be more unique than the beginning, since a common pattern is for similar
139 // threads to share a common prefix.
140 NameStr = NameStr.take_back(get_max_thread_name_length());
Krzysztof Parzyszek75464e12017-03-03 22:21:02 +0000141 (void)NameStr;
Zachary Turner757dbc92017-03-03 17:15:17 +0000142#if defined(__linux__)
143#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
Krzysztof Parzyszek6cf25402017-03-03 21:53:12 +0000144#if HAVE_PTHREAD_SETNAME_NP
Zachary Turner757dbc92017-03-03 17:15:17 +0000145 ::pthread_setname_np(::pthread_self(), NameStr.data());
146#endif
Krzysztof Parzyszek6cf25402017-03-03 21:53:12 +0000147#endif
Zachary Turner757dbc92017-03-03 17:15:17 +0000148#elif defined(__FreeBSD__)
149 ::pthread_set_name_np(::pthread_self(), NameStr.data());
150#elif defined(__NetBSD__)
151 ::pthread_setname_np(::pthread_self(), "%s",
152 const_cast<char *>(NameStr.data()));
153#elif defined(__APPLE__)
154 ::pthread_setname_np(NameStr.data());
155#endif
156}
157
158void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
159 Name.clear();
160
161#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
Zachary Turner45337cf2017-03-03 18:21:04 +0000162 int pid = ::getpid();
163 uint64_t tid = get_threadid();
Zachary Turner757dbc92017-03-03 17:15:17 +0000164
165 struct kinfo_proc *kp = nullptr, *nkp;
166 size_t len = 0;
167 int error;
168 int ctl[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
169 (int)pid };
170
171 while (1) {
172 error = sysctl(ctl, 4, kp, &len, nullptr, 0);
173 if (kp == nullptr || (error != 0 && errno == ENOMEM)) {
174 // Add extra space in case threads are added before next call.
175 len += sizeof(*kp) + len / 10;
176 nkp = (struct kinfo_proc *)realloc(kp, len);
177 if (nkp == nullptr) {
178 free(kp);
179 return;
180 }
181 kp = nkp;
182 continue;
183 }
184 if (error != 0)
185 len = 0;
186 break;
187 }
188
189 for (size_t i = 0; i < len / sizeof(*kp); i++) {
190 if (kp[i].ki_tid == (lwpid_t)tid) {
191 Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
192 break;
193 }
194 }
195 free(kp);
196 return;
197#elif defined(__NetBSD__)
Zachary Turner777de772017-03-04 16:42:25 +0000198 char buf[get_max_thread_name_length()];
Zachary Turner757dbc92017-03-03 17:15:17 +0000199 ::pthread_getname_np(::pthread_self(), buf, PTHREAD_MAX_NAMELEN_NP);
200
201 Name.append(buf, buf + strlen(buf));
202#elif defined(__linux__)
203#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
Krzysztof Parzyszek6cf25402017-03-03 21:53:12 +0000204#if HAVE_PTHREAD_GETNAME_NP
Zachary Turner777de772017-03-04 16:42:25 +0000205 char Buffer[get_max_thread_name_length()];
Zachary Turner757dbc92017-03-03 17:15:17 +0000206 if (0 == ::pthread_getname_np(::pthread_self(), Buffer, MAXNAMELEN))
207 Name.append(Buffer, Buffer + strlen(Buffer));
208#endif
209#endif
Krzysztof Parzyszek6cf25402017-03-03 21:53:12 +0000210#endif
Zachary Turner757dbc92017-03-03 17:15:17 +0000211}