1 //===-- llvm/Support/Threading.cpp- Control multithreading mode --*- 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 defines helper functions for running LLVM in a multi-threaded
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Threading.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Config/llvm-config.h"
26 //===----------------------------------------------------------------------===//
27 //=== WARNING: Implementation here must contain only TRULY operating system
28 //=== independent code.
29 //===----------------------------------------------------------------------===//
31 #if LLVM_ENABLE_THREADS == 0 || \
32 (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
33 uint64_t llvm::get_threadid() { return 0; }
35 uint32_t llvm::get_max_thread_name_length() { return 0; }
37 void llvm::set_thread_name(const Twine
&Name
) {}
39 void llvm::get_thread_name(SmallVectorImpl
<char> &Name
) { Name
.clear(); }
41 llvm::BitVector
llvm::get_thread_affinity_mask() { return {}; }
43 unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
44 // When threads are disabled, ensure clients will loop at least once.
48 // Unknown if threading turned off
49 int llvm::get_physical_cores() { return -1; }
53 static int computeHostNumHardwareThreads();
55 unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
57 UseHyperThreads
? computeHostNumHardwareThreads() : get_physical_cores();
58 if (MaxThreadCount
<= 0)
60 if (ThreadsRequested
== 0)
61 return MaxThreadCount
;
63 return ThreadsRequested
;
64 return std::min((unsigned)MaxThreadCount
, ThreadsRequested
);
67 // Include the platform-specific parts of this class.
69 #include "Unix/Threading.inc"
72 #include "Windows/Threading.inc"
75 // Must be included after Threading.inc to provide definition for llvm::thread
76 // because FreeBSD's condvar.h (included by user.h) misuses the "thread"
78 #include "llvm/Support/thread.h"
80 #if defined(__APPLE__)
81 // Darwin's default stack size for threads except the main one is only 512KB,
82 // which is not enough for some/many normal LLVM compilations. This implements
83 // the same interface as std::thread but requests the same stack size as the
84 // main thread (8MB) before creation.
85 const std::optional
<unsigned> llvm::thread::DefaultStackSize
= 8 * 1024 * 1024;
87 // On AIX, the default pthread stack size limit is ~192k for 64-bit programs.
88 // This limit is easily reached when doing link-time thinLTO. AIX library
89 // developers have used 4MB, so we'll do the same.
90 const std::optional
<unsigned> llvm::thread::DefaultStackSize
= 4 * 1024 * 1024;
92 const std::optional
<unsigned> llvm::thread::DefaultStackSize
;
98 std::optional
<ThreadPoolStrategy
>
99 llvm::get_threadpool_strategy(StringRef Num
, ThreadPoolStrategy Default
) {
101 return llvm::hardware_concurrency();
105 if (Num
.getAsInteger(10, V
))
106 return std::nullopt
; // malformed 'Num' value
110 // Do not take the Default into account. This effectively disables
111 // heavyweight_hardware_concurrency() if the user asks for any number of
112 // threads on the cmd-line.
113 ThreadPoolStrategy S
= llvm::hardware_concurrency();
114 S
.ThreadsRequested
= V
;