1 //===- Unix/Threading.inc - Unix Threading Implementation ----- -*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file provides the Unix specific implementation of Threading functions.
11 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/ScopeExit.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
18 #if defined(__APPLE__)
19 #include <mach/mach_init.h>
20 #include <mach/mach_port.h>
25 #if defined(__FreeBSD__) || defined(__OpenBSD__)
26 #include <pthread_np.h> // For pthread_getthreadid_np() / pthread_set_name_np()
29 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
31 #include <sys/sysctl.h>
36 #if defined(__NetBSD__)
37 #include <lwp.h> // For _lwp_self()
40 #if defined(__OpenBSD__)
41 #include <unistd.h> // For getthrid()
44 #if defined(__linux__)
45 #include <sched.h> // For sched_getaffinity
46 #include <sys/syscall.h> // For syscall codes
47 #include <unistd.h> // For syscall()
50 static void *threadFuncSync(void *Arg) {
51 SyncThreadInfo *TI = static_cast<SyncThreadInfo *>(Arg);
52 TI->UserFn(TI->UserData);
56 static void *threadFuncAsync(void *Arg) {
57 std::unique_ptr<AsyncThreadInfo> Info(static_cast<AsyncThreadInfo *>(Arg));
63 llvm_execute_on_thread_impl(void *(*ThreadFunc)(void *), void *Arg,
64 llvm::Optional<unsigned> StackSizeInBytes,
68 // Construct the attributes object.
70 if ((errnum = ::pthread_attr_init(&Attr)) != 0) {
71 ReportErrnumFatal("pthread_attr_init failed", errnum);
74 auto AttrGuard = llvm::make_scope_exit([&] {
75 if ((errnum = ::pthread_attr_destroy(&Attr)) != 0) {
76 ReportErrnumFatal("pthread_attr_destroy failed", errnum);
80 // Set the requested stack size, if given.
81 if (StackSizeInBytes) {
82 if ((errnum = ::pthread_attr_setstacksize(&Attr, *StackSizeInBytes)) != 0) {
83 ReportErrnumFatal("pthread_attr_setstacksize failed", errnum);
87 // Construct and execute the thread.
89 if ((errnum = ::pthread_create(&Thread, &Attr, ThreadFunc, Arg)) != 0)
90 ReportErrnumFatal("pthread_create failed", errnum);
92 if (JP == JoiningPolicy::Join) {
93 // Wait for the thread
94 if ((errnum = ::pthread_join(Thread, nullptr)) != 0) {
95 ReportErrnumFatal("pthread_join failed", errnum);
97 } else if (JP == JoiningPolicy::Detach) {
98 if ((errnum = ::pthread_detach(Thread)) != 0) {
99 ReportErrnumFatal("pthread_detach failed", errnum);
104 uint64_t llvm::get_threadid() {
105 #if defined(__APPLE__)
106 // Calling "mach_thread_self()" bumps the reference count on the thread
107 // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
109 thread_port_t Self = mach_thread_self();
110 mach_port_deallocate(mach_task_self(), Self);
112 #elif defined(__FreeBSD__)
113 return uint64_t(pthread_getthreadid_np());
114 #elif defined(__NetBSD__)
115 return uint64_t(_lwp_self());
116 #elif defined(__OpenBSD__)
117 return uint64_t(getthrid());
118 #elif defined(__ANDROID__)
119 return uint64_t(gettid());
120 #elif defined(__linux__)
121 return uint64_t(syscall(SYS_gettid));
123 return uint64_t(pthread_self());
128 static constexpr uint32_t get_max_thread_name_length_impl() {
129 #if defined(__NetBSD__)
130 return PTHREAD_MAX_NAMELEN_NP;
131 #elif defined(__APPLE__)
133 #elif defined(__linux__)
134 #if HAVE_PTHREAD_SETNAME_NP
139 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
141 #elif defined(__OpenBSD__)
148 uint32_t llvm::get_max_thread_name_length() {
149 return get_max_thread_name_length_impl();
152 void llvm::set_thread_name(const Twine &Name) {
153 // Make sure the input is null terminated.
154 SmallString<64> Storage;
155 StringRef NameStr = Name.toNullTerminatedStringRef(Storage);
157 // Truncate from the beginning, not the end, if the specified name is too
158 // long. For one, this ensures that the resulting string is still null
159 // terminated, but additionally the end of a long thread name will usually
160 // be more unique than the beginning, since a common pattern is for similar
161 // threads to share a common prefix.
162 // Note that the name length includes the null terminator.
163 if (get_max_thread_name_length() > 0)
164 NameStr = NameStr.take_back(get_max_thread_name_length() - 1);
166 #if defined(__linux__)
167 #if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
168 #if HAVE_PTHREAD_SETNAME_NP
169 ::pthread_setname_np(::pthread_self(), NameStr.data());
172 #elif defined(__FreeBSD__) || defined(__OpenBSD__)
173 ::pthread_set_name_np(::pthread_self(), NameStr.data());
174 #elif defined(__NetBSD__)
175 ::pthread_setname_np(::pthread_self(), "%s",
176 const_cast<char *>(NameStr.data()));
177 #elif defined(__APPLE__)
178 ::pthread_setname_np(NameStr.data());
182 void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
185 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
186 int pid = ::getpid();
187 uint64_t tid = get_threadid();
189 struct kinfo_proc *kp = nullptr, *nkp;
192 int ctl[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
196 error = sysctl(ctl, 4, kp, &len, nullptr, 0);
197 if (kp == nullptr || (error != 0 && errno == ENOMEM)) {
198 // Add extra space in case threads are added before next call.
199 len += sizeof(*kp) + len / 10;
200 nkp = (struct kinfo_proc *)::realloc(kp, len);
201 if (nkp == nullptr) {
213 for (size_t i = 0; i < len / sizeof(*kp); i++) {
214 if (kp[i].ki_tid == (lwpid_t)tid) {
215 Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
221 #elif defined(__NetBSD__)
222 constexpr uint32_t len = get_max_thread_name_length_impl();
224 ::pthread_getname_np(::pthread_self(), buf, len);
226 Name.append(buf, buf + strlen(buf));
227 #elif defined(__OpenBSD__)
228 constexpr uint32_t len = get_max_thread_name_length_impl();
230 ::pthread_get_name_np(::pthread_self(), buf, len);
232 Name.append(buf, buf + strlen(buf));
233 #elif defined(__linux__)
234 #if HAVE_PTHREAD_GETNAME_NP
235 constexpr uint32_t len = get_max_thread_name_length_impl();
236 char Buffer[len] = {'\0'}; // FIXME: working around MSan false positive.
237 if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
238 Name.append(Buffer, Buffer + strlen(Buffer));
243 SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
244 #if defined(__linux__) && defined(SCHED_IDLE)
245 // Some *really* old glibcs are missing SCHED_IDLE.
246 // http://man7.org/linux/man-pages/man3/pthread_setschedparam.3.html
247 // http://man7.org/linux/man-pages/man2/sched_setscheduler.2.html
248 sched_param priority;
249 // For each of the above policies, param->sched_priority must be 0.
250 priority.sched_priority = 0;
251 // SCHED_IDLE for running very low priority background jobs.
252 // SCHED_OTHER the standard round-robin time-sharing policy;
253 return !pthread_setschedparam(
255 Priority == ThreadPriority::Background ? SCHED_IDLE : SCHED_OTHER,
257 ? SetThreadPriorityResult::SUCCESS
258 : SetThreadPriorityResult::FAILURE;
259 #elif defined(__APPLE__)
260 // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getpriority.2.html
261 // When setting a thread into background state the scheduling priority is set
262 // to lowest value, disk and network IO are throttled. Network IO will be
263 // throttled for any sockets the thread opens after going into background
264 // state. Any previously opened sockets are not affected.
266 // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/getiopolicy_np.3.html
267 // I/Os with THROTTLE policy are called THROTTLE I/Os. If a THROTTLE I/O
268 // request occurs within a small time window (usually a fraction of a second)
269 // of another NORMAL I/O request, the thread that issues the THROTTLE I/O is
270 // forced to sleep for a certain interval. This slows down the thread that
271 // issues the THROTTLE I/O so that NORMAL I/Os can utilize most of the disk
273 return !setpriority(PRIO_DARWIN_THREAD, 0,
274 Priority == ThreadPriority::Background ? PRIO_DARWIN_BG
276 ? SetThreadPriorityResult::SUCCESS
277 : SetThreadPriorityResult::FAILURE;
279 return SetThreadPriorityResult::FAILURE;
284 int computeHostNumHardwareThreads() {
287 if (sched_getaffinity(0, sizeof(Set), &Set) == 0)
288 return CPU_COUNT(&Set);
290 // Guard against std::thread::hardware_concurrency() returning 0.
291 if (unsigned Val = std::thread::hardware_concurrency())
296 void llvm::ThreadPoolStrategy::apply_thread_strategy(
297 unsigned ThreadPoolNum) const {}
299 llvm::BitVector llvm::get_thread_affinity_mask() {
301 llvm_unreachable("Not implemented!");
304 unsigned llvm::get_cpus() { return 1; }