LLVM  12.0.0git
Threading.cpp
Go to the documentation of this file.
1 //===-- llvm/Support/Threading.cpp- Control multithreading mode --*- 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 defines helper functions for running LLVM in a multi-threaded
10 // environment.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/Threading.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Host.h"
18 
19 #include <cassert>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 using namespace llvm;
25 
26 //===----------------------------------------------------------------------===//
27 //=== WARNING: Implementation here must contain only TRULY operating system
28 //=== independent code.
29 //===----------------------------------------------------------------------===//
30 
32 #if LLVM_ENABLE_THREADS != 0
33  return true;
34 #else
35  return false;
36 #endif
37 }
38 
39 #if LLVM_ENABLE_THREADS == 0 || \
40  (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
41 // Support for non-Win32, non-pthread implementation.
42 void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData,
43  llvm::Optional<unsigned> StackSizeInBytes) {
44  (void)StackSizeInBytes;
45  Fn(UserData);
46 }
47 
48 uint64_t llvm::get_threadid() { return 0; }
49 
51 
52 void llvm::set_thread_name(const Twine &Name) {}
53 
55 
57 
59  // When threads are disabled, ensure clients will loop at least once.
60  return 1;
61 }
62 
63 #if LLVM_ENABLE_THREADS == 0
65  llvm::unique_function<void()> Func,
66  llvm::Optional<unsigned> StackSizeInBytes) {
67  (void)Func;
68  (void)StackSizeInBytes;
69  report_fatal_error("Spawning a detached thread doesn't make sense with no "
70  "threading support");
71 }
72 #else
73 // Support for non-Win32, non-pthread implementation.
75  llvm::unique_function<void()> Func,
76  llvm::Optional<unsigned> StackSizeInBytes) {
77  (void)StackSizeInBytes;
78  std::thread(std::move(Func)).detach();
79 }
80 #endif
81 
82 #else
83 
85 
87  int MaxThreadCount = UseHyperThreads ? computeHostNumHardwareThreads()
89  if (MaxThreadCount <= 0)
90  MaxThreadCount = 1;
91  if (ThreadsRequested == 0)
92  return MaxThreadCount;
93  if (!Limit)
94  return ThreadsRequested;
95  return std::min((unsigned)MaxThreadCount, ThreadsRequested);
96 }
97 
98 namespace {
99 struct SyncThreadInfo {
100  void (*UserFn)(void *);
101  void *UserData;
102 };
103 
104 using AsyncThreadInfo = llvm::unique_function<void()>;
105 
106 enum class JoiningPolicy { Join, Detach };
107 } // namespace
108 
109 // Include the platform-specific parts of this class.
110 #ifdef LLVM_ON_UNIX
111 #include "Unix/Threading.inc"
112 #endif
113 #ifdef _WIN32
114 #include "Windows/Threading.inc"
115 #endif
116 
117 void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData,
118  llvm::Optional<unsigned> StackSizeInBytes) {
119 
120  SyncThreadInfo Info = {Fn, UserData};
121  llvm_execute_on_thread_impl(threadFuncSync, &Info, StackSizeInBytes,
122  JoiningPolicy::Join);
123 }
124 
126  llvm::unique_function<void()> Func,
127  llvm::Optional<unsigned> StackSizeInBytes) {
128  llvm_execute_on_thread_impl(&threadFuncAsync,
129  new AsyncThreadInfo(std::move(Func)),
130  StackSizeInBytes, JoiningPolicy::Detach);
131 }
132 
133 #endif
134 
137  if (Num == "all")
139  if (Num.empty())
140  return Default;
141  unsigned V;
142  if (Num.getAsInteger(10, V))
143  return None; // malformed 'Num' value
144  if (V == 0)
145  return Default;
146 
147  // Do not take the Default into account. This effectively disables
148  // heavyweight_hardware_concurrency() if the user asks for any number of
149  // threads on the cmd-line.
151  S.ThreadsRequested = V;
152  return S;
153 }
void set_thread_name(const Twine &Name)
Set the name of the current thread.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
llvm::BitVector get_thread_affinity_mask()
Returns a mask that represents on which hardware thread, core, CPU, NUMA group, the calling thread ca...
void llvm_execute_on_thread(void(*UserFn)(void *), void *UserData, llvm::Optional< unsigned > StackSizeInBytes=llvm::None)
Execute the given UserFn on a separate thread, passing it the provided UserData and waits for thread ...
Definition: Threading.cpp:117
int getHostNumPhysicalCores()
Get the number of physical cores (as opposed to logical cores returned from thread::hardware_concurre...
Definition: Host.cpp:1301
Optional< ThreadPoolStrategy > get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default={})
Build a strategy from a number of threads as a string provided in Num.
Definition: Threading.cpp:136
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
uint32_t get_max_thread_name_length()
Get the maximum length of a thread name on this platform.
void get_thread_name(SmallVectorImpl< char > &Name)
Get the name of the current thread.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:156
Analysis containing CSE Info
Definition: CSEInfo.cpp:25
std::enable_if_t< std::numeric_limits< T >::is_signed, bool > getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:511
bool llvm_is_multithreaded()
Returns true if LLVM is compiled with support for multi-threading, and false otherwise.
Definition: Threading.cpp:31
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:305
This tells how a thread pool will be used.
Definition: Threading.h:148
uint64_t get_threadid()
Return the current thread id, as used in various OS system calls.
unsigned compute_thread_count() const
Retrieves the max available threads for the current strategy.
Definition: Threading.cpp:86
unique_function is a type-erasing functor similar to std::function.
JoiningPolicy
Definition: Threading.cpp:106
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware ressources are to be used...
Definition: Threading.h:217
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
std::thread thread
Definition: thread.h:26
int computeHostNumHardwareThreads()
void llvm_execute_on_thread_async(llvm::unique_function< void()> Func, llvm::Optional< unsigned > StackSizeInBytes=llvm::None)
Schedule the given Func for execution on a separate thread, then return to the caller immediately...
Definition: Threading.cpp:125