blob: b031ff4c03405204228a0b7f29b3a7851746c682 [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);
Krzysztof Parzyszek75464e12017-03-03 22:21:02 +0000116 (void)NameStr;
Zachary Turner757dbc92017-03-03 17:15:17 +0000117#if defined(__linux__)
118#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
Krzysztof Parzyszek6cf25402017-03-03 21:53:12 +0000119#if HAVE_PTHREAD_SETNAME_NP
Zachary Turner757dbc92017-03-03 17:15:17 +0000120 ::pthread_setname_np(::pthread_self(), NameStr.data());
121#endif
Krzysztof Parzyszek6cf25402017-03-03 21:53:12 +0000122#endif
Zachary Turner757dbc92017-03-03 17:15:17 +0000123#elif defined(__FreeBSD__)
124 ::pthread_set_name_np(::pthread_self(), NameStr.data());
125#elif defined(__NetBSD__)
126 ::pthread_setname_np(::pthread_self(), "%s",
127 const_cast<char *>(NameStr.data()));
128#elif defined(__APPLE__)
129 ::pthread_setname_np(NameStr.data());
130#endif
131}
132
133void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
134 Name.clear();
135
136#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
Zachary Turner45337cf2017-03-03 18:21:04 +0000137 int pid = ::getpid();
138 uint64_t tid = get_threadid();
Zachary Turner757dbc92017-03-03 17:15:17 +0000139
140 struct kinfo_proc *kp = nullptr, *nkp;
141 size_t len = 0;
142 int error;
143 int ctl[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
144 (int)pid };
145
146 while (1) {
147 error = sysctl(ctl, 4, kp, &len, nullptr, 0);
148 if (kp == nullptr || (error != 0 && errno == ENOMEM)) {
149 // Add extra space in case threads are added before next call.
150 len += sizeof(*kp) + len / 10;
151 nkp = (struct kinfo_proc *)realloc(kp, len);
152 if (nkp == nullptr) {
153 free(kp);
154 return;
155 }
156 kp = nkp;
157 continue;
158 }
159 if (error != 0)
160 len = 0;
161 break;
162 }
163
164 for (size_t i = 0; i < len / sizeof(*kp); i++) {
165 if (kp[i].ki_tid == (lwpid_t)tid) {
166 Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
167 break;
168 }
169 }
170 free(kp);
171 return;
172#elif defined(__NetBSD__)
173 char buf[PTHREAD_MAX_NAMELEN_NP];
174 ::pthread_getname_np(::pthread_self(), buf, PTHREAD_MAX_NAMELEN_NP);
175
176 Name.append(buf, buf + strlen(buf));
177#elif defined(__linux__)
178#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
Krzysztof Parzyszek6cf25402017-03-03 21:53:12 +0000179#if HAVE_PTHREAD_GETNAME_NP
Zachary Turner757dbc92017-03-03 17:15:17 +0000180 constexpr int MAXNAMELEN = 16;
181 char Buffer[MAXNAMELEN];
182 if (0 == ::pthread_getname_np(::pthread_self(), Buffer, MAXNAMELEN))
183 Name.append(Buffer, Buffer + strlen(Buffer));
184#endif
185#endif
Krzysztof Parzyszek6cf25402017-03-03 21:53:12 +0000186#endif
Zachary Turner757dbc92017-03-03 17:15:17 +0000187}