1//===- Unix/Threading.inc - Unix Threading Implementation ----- -*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides the Unix specific implementation of Threading functions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "Unix.h"
14#include "llvm/ADT/ScopeExit.h"
15#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Twine.h"
17
18#if defined(__APPLE__)
19#include <mach/mach_init.h>
20#include <mach/mach_port.h>
21#endif
22
23#include <pthread.h>
24
25#if defined(__FreeBSD__) || defined(__OpenBSD__)
26#include <pthread_np.h> // For pthread_getthreadid_np() / pthread_set_name_np()
27#endif
28
29#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
30#include <errno.h>
31#include <sys/sysctl.h>
32#include <sys/user.h>
33#include <unistd.h>
34#endif
35
36#if defined(__NetBSD__)
37#include <lwp.h> // For _lwp_self()
38#endif
39
40#if defined(__linux__)
41#include <sched.h>       // For sched_getaffinity
42#include <sys/syscall.h> // For syscall codes
43#include <unistd.h>      // For syscall()
44#endif
45
46static void *threadFuncSync(void *Arg) {
47  SyncThreadInfo *TI = static_cast<SyncThreadInfo *>(Arg);
48  TI->UserFn(TI->UserData);
49  return nullptr;
50}
51
52static void *threadFuncAsync(void *Arg) {
53  std::unique_ptr<AsyncThreadInfo> Info(static_cast<AsyncThreadInfo *>(Arg));
54  (*Info)();
55  return nullptr;
56}
57
58static void
59llvm_execute_on_thread_impl(void *(*ThreadFunc)(void *), void *Arg,
60                            llvm::Optional<unsigned> StackSizeInBytes,
61                            JoiningPolicy JP) {
62  int errnum;
63
64  // Construct the attributes object.
65  pthread_attr_t Attr;
66  if ((errnum = ::pthread_attr_init(&Attr)) != 0) {
67    ReportErrnumFatal("pthread_attr_init failed", errnum);
68  }
69
70  auto AttrGuard = llvm::make_scope_exit([&] {
71    if ((errnum = ::pthread_attr_destroy(&Attr)) != 0) {
72      ReportErrnumFatal("pthread_attr_destroy failed", errnum);
73    }
74  });
75
76  // Set the requested stack size, if given.
77  if (StackSizeInBytes) {
78    if ((errnum = ::pthread_attr_setstacksize(&Attr, *StackSizeInBytes)) != 0) {
79      ReportErrnumFatal("pthread_attr_setstacksize failed", errnum);
80    }
81  }
82
83  // Construct and execute the thread.
84  pthread_t Thread;
85  if ((errnum = ::pthread_create(&Thread, &Attr, ThreadFunc, Arg)) != 0)
86    ReportErrnumFatal("pthread_create failed", errnum);
87
88  if (JP == JoiningPolicy::Join) {
89    // Wait for the thread
90    if ((errnum = ::pthread_join(Thread, nullptr)) != 0) {
91      ReportErrnumFatal("pthread_join failed", errnum);
92    }
93  }
94}
95
96uint64_t llvm::get_threadid() {
97#if defined(__APPLE__)
98  // Calling "mach_thread_self()" bumps the reference count on the thread
99  // port, so we need to deallocate it. mach_task_self() doesn't bump the ref
100  // count.
101  thread_port_t Self = mach_thread_self();
102  mach_port_deallocate(mach_task_self(), Self);
103  return Self;
104#elif defined(__FreeBSD__)
105  return uint64_t(pthread_getthreadid_np());
106#elif defined(__NetBSD__)
107  return uint64_t(_lwp_self());
108#elif defined(__ANDROID__)
109  return uint64_t(gettid());
110#elif defined(__linux__)
111  return uint64_t(syscall(SYS_gettid));
112#else
113  return uint64_t(pthread_self());
114#endif
115}
116
117
118static constexpr uint32_t get_max_thread_name_length_impl() {
119#if defined(__NetBSD__)
120  return PTHREAD_MAX_NAMELEN_NP;
121#elif defined(__APPLE__)
122  return 64;
123#elif defined(__linux__)
124#if HAVE_PTHREAD_SETNAME_NP
125  return 16;
126#else
127  return 0;
128#endif
129#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
130  return 16;
131#elif defined(__OpenBSD__)
132  return 32;
133#else
134  return 0;
135#endif
136}
137
138uint32_t llvm::get_max_thread_name_length() {
139  return get_max_thread_name_length_impl();
140}
141
142void llvm::set_thread_name(const Twine &Name) {
143  // Make sure the input is null terminated.
144  SmallString<64> Storage;
145  StringRef NameStr = Name.toNullTerminatedStringRef(Storage);
146
147  // Truncate from the beginning, not the end, if the specified name is too
148  // long.  For one, this ensures that the resulting string is still null
149  // terminated, but additionally the end of a long thread name will usually
150  // be more unique than the beginning, since a common pattern is for similar
151  // threads to share a common prefix.
152  // Note that the name length includes the null terminator.
153  if (get_max_thread_name_length() > 0)
154    NameStr = NameStr.take_back(get_max_thread_name_length() - 1);
155  (void)NameStr;
156#if defined(__linux__)
157#if (defined(__GLIBC__) && defined(_GNU_SOURCE)) || defined(__ANDROID__)
158#if HAVE_PTHREAD_SETNAME_NP
159  ::pthread_setname_np(::pthread_self(), NameStr.data());
160#endif
161#endif
162#elif defined(__FreeBSD__) || defined(__OpenBSD__)
163  ::pthread_set_name_np(::pthread_self(), NameStr.data());
164#elif defined(__NetBSD__)
165  ::pthread_setname_np(::pthread_self(), "%s",
166    const_cast<char *>(NameStr.data()));
167#elif defined(__APPLE__)
168  ::pthread_setname_np(NameStr.data());
169#endif
170}
171
172void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
173  Name.clear();
174
175#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
176  int pid = ::getpid();
177  uint64_t tid = get_threadid();
178
179  struct kinfo_proc *kp = nullptr, *nkp;
180  size_t len = 0;
181  int error;
182  int ctl[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD,
183    (int)pid };
184
185  while (1) {
186    error = sysctl(ctl, 4, kp, &len, nullptr, 0);
187    if (kp == nullptr || (error != 0 && errno == ENOMEM)) {
188      // Add extra space in case threads are added before next call.
189      len += sizeof(*kp) + len / 10;
190      nkp = (struct kinfo_proc *)::realloc(kp, len);
191      if (nkp == nullptr) {
192        free(kp);
193        return;
194      }
195      kp = nkp;
196      continue;
197    }
198    if (error != 0)
199      len = 0;
200    break;
201  }
202
203  for (size_t i = 0; i < len / sizeof(*kp); i++) {
204    if (kp[i].ki_tid == (lwpid_t)tid) {
205      Name.append(kp[i].ki_tdname, kp[i].ki_tdname + strlen(kp[i].ki_tdname));
206      break;
207    }
208  }
209  free(kp);
210  return;
211#elif defined(__NetBSD__)
212  constexpr uint32_t len = get_max_thread_name_length_impl();
213  char buf[len];
214  ::pthread_getname_np(::pthread_self(), buf, len);
215
216  Name.append(buf, buf + strlen(buf));
217#elif defined(__OpenBSD__)
218  constexpr uint32_t len = get_max_thread_name_length_impl();
219  char buf[len];
220  ::pthread_get_name_np(::pthread_self(), buf, len);
221
222  Name.append(buf, buf + strlen(buf));
223#elif defined(__linux__)
224#if HAVE_PTHREAD_GETNAME_NP
225  constexpr uint32_t len = get_max_thread_name_length_impl();
226  char Buffer[len] = {'\0'};  // FIXME: working around MSan false positive.
227  if (0 == ::pthread_getname_np(::pthread_self(), Buffer, len))
228    Name.append(Buffer, Buffer + strlen(Buffer));
229#endif
230#endif
231}
232
233SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
234#if defined(__linux__) && defined(SCHED_IDLE)
235  // Some *really* old glibcs are missing SCHED_IDLE.
236  // http://man7.org/linux/man-pages/man3/pthread_setschedparam.3.html
237  // http://man7.org/linux/man-pages/man2/sched_setscheduler.2.html
238  sched_param priority;
239  // For each of the above policies, param->sched_priority must be 0.
240  priority.sched_priority = 0;
241  // SCHED_IDLE    for running very low priority background jobs.
242  // SCHED_OTHER   the standard round-robin time-sharing policy;
243  return !pthread_setschedparam(
244             pthread_self(),
245             Priority == ThreadPriority::Background ? SCHED_IDLE : SCHED_OTHER,
246             &priority)
247             ? SetThreadPriorityResult::SUCCESS
248             : SetThreadPriorityResult::FAILURE;
249#elif defined(__APPLE__)
250  // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getpriority.2.html
251  // When setting a thread into background state the scheduling priority is set
252  // to lowest value, disk and network IO are throttled. Network IO will be
253  // throttled for any sockets the thread opens after going into background
254  // state. Any previously opened sockets are not affected.
255
256  // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/getiopolicy_np.3.html
257  // I/Os with THROTTLE policy are called THROTTLE I/Os. If a THROTTLE I/O
258  // request occurs within a small time window (usually a fraction of a second)
259  // of another NORMAL I/O request, the thread that issues the THROTTLE I/O is
260  // forced to sleep for a certain interval. This slows down the thread that
261  // issues the THROTTLE I/O so that NORMAL I/Os can utilize most of the disk
262  // I/O bandwidth.
263  return !setpriority(PRIO_DARWIN_THREAD, 0,
264                      Priority == ThreadPriority::Background ? PRIO_DARWIN_BG
265                                                             : 0)
266             ? SetThreadPriorityResult::SUCCESS
267             : SetThreadPriorityResult::FAILURE;
268#endif
269  return SetThreadPriorityResult::FAILURE;
270}
271
272#include <thread>
273
274int computeHostNumHardwareThreads() {
275#ifdef __linux__
276  cpu_set_t Set;
277  if (sched_getaffinity(0, sizeof(Set), &Set) == 0)
278    return CPU_COUNT(&Set);
279#endif
280  // Guard against std::thread::hardware_concurrency() returning 0.
281  if (unsigned Val = std::thread::hardware_concurrency())
282    return Val;
283  return 1;
284}
285
286void llvm::ThreadPoolStrategy::apply_thread_strategy(
287    unsigned ThreadPoolNum) const {}
288
289llvm::BitVector llvm::get_thread_affinity_mask() {
290  // FIXME: Implement
291  llvm_unreachable("Not implemented!");
292}
293
294unsigned llvm::get_cpus() { return 1; }
295