blob: 6301a674df0544e4ef50d7880ddc9682bfe6835b [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
112void llvm::set_thread_name(const Twine &Name) {
113 // Make sure the input is null terminated.
114 SmallString<64> Storage;
115 StringRef NameStr = Name.toNullTerminatedStringRef(Storage);
116#if defined(__linux__)
117#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
Krzysztof Parzyszek6cf25402017-03-03 21:53:12 +0000118#if HAVE_PTHREAD_SETNAME_NP
Zachary Turner757dbc92017-03-03 17:15:17 +0000119 ::pthread_setname_np(::pthread_self(), NameStr.data());
120#endif
Krzysztof Parzyszek6cf25402017-03-03 21:53:12 +0000121#endif
Zachary Turner757dbc92017-03-03 17:15:17 +0000122#elif defined(__FreeBSD__)
123 ::pthread_set_name_np(::pthread_self(), NameStr.data());
124#elif defined(__NetBSD__)
125 ::pthread_setname_np(::pthread_self(), "%s",
126 const_cast<char *>(NameStr.data()));
127#elif defined(__APPLE__)
128 ::pthread_setname_np(NameStr.data());
129#endif
130}
131
132void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
133 Name.clear();
134
135#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
Zachary Turner45337cf2017-03-03 18:21:04 +0000136 int pid = ::getpid();
137 uint64_t tid = get_threadid();
Zachary Turner757dbc92017-03-03 17:15:17 +0000138
139 struct kinfo_proc *kp = nullptr, *nkp;
140 size_t len = 0;
141 int error;
142 int ctl[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
143 (int)pid };
144
145 while (1) {
146 error = sysctl(ctl, 4, kp, &len, nullptr, 0);
147 if (kp == nullptr || (error != 0 && errno == ENOMEM)) {
148 // Add extra space in case threads are added before next call.
149 len += sizeof(*kp) + len / 10;
150 nkp = (struct kinfo_proc *)realloc(kp, len);
151 if (nkp == nullptr) {
152 free(kp);
153 return;
154 }
155 kp = nkp;
156 continue;
157 }
158 if (error != 0)
159 len = 0;
160 break;
161 }
162
163 for (size_t i = 0; i < len / sizeof(*kp); i++) {
164 if (kp[i].ki_tid == (lwpid_t)tid) {
165 Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
166 break;
167 }
168 }
169 free(kp);
170 return;
171#elif defined(__NetBSD__)
172 char buf[PTHREAD_MAX_NAMELEN_NP];
173 ::pthread_getname_np(::pthread_self(), buf, PTHREAD_MAX_NAMELEN_NP);
174
175 Name.append(buf, buf + strlen(buf));
176#elif defined(__linux__)
177#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
Krzysztof Parzyszek6cf25402017-03-03 21:53:12 +0000178#if HAVE_PTHREAD_GETNAME_NP
Zachary Turner757dbc92017-03-03 17:15:17 +0000179 constexpr int MAXNAMELEN = 16;
180 char Buffer[MAXNAMELEN];
181 if (0 == ::pthread_getname_np(::pthread_self(), Buffer, MAXNAMELEN))
182 Name.append(Buffer, Buffer + strlen(Buffer));
183#endif
184#endif
Krzysztof Parzyszek6cf25402017-03-03 21:53:12 +0000185#endif
Zachary Turner757dbc92017-03-03 17:15:17 +0000186}