xref: /llvm-project/llvm/lib/Support/Threading.cpp (revision 8404aeb56a73ab24f9b295111de3b37a37f0b841)
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 
31 bool llvm::llvm_is_multithreaded() {
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 
50 uint32_t llvm::get_max_thread_name_length() { return 0; }
51 
52 void llvm::set_thread_name(const Twine &Name) {}
53 
54 void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
55 
56 llvm::BitVector llvm::get_thread_affinity_mask() { return {}; }
57 
58 unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
59   // When threads are disabled, ensure clients will loop at least once.
60   return 1;
61 }
62 
63 #if LLVM_ENABLE_THREADS == 0
64 void llvm::llvm_execute_on_thread_async(
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.
74 void llvm::llvm_execute_on_thread_async(
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 
84 int computeHostNumHardwareThreads();
85 
86 unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
87   int MaxThreadCount = UseHyperThreads ? computeHostNumHardwareThreads()
88                                        : sys::getHostNumPhysicalCores();
89   if (MaxThreadCount <= 0)
90     MaxThreadCount = 1;
91 
92   // No need to create more threads than there are hardware threads, it would
93   // uselessly induce more context-switching and cache eviction.
94   if (!ThreadsRequested || ThreadsRequested > (unsigned)MaxThreadCount)
95     return MaxThreadCount;
96   return ThreadsRequested;
97 }
98 
99 namespace {
100 struct SyncThreadInfo {
101   void (*UserFn)(void *);
102   void *UserData;
103 };
104 
105 using AsyncThreadInfo = llvm::unique_function<void()>;
106 
107 enum class JoiningPolicy { Join, Detach };
108 } // namespace
109 
110 // Include the platform-specific parts of this class.
111 #ifdef LLVM_ON_UNIX
112 #include "Unix/Threading.inc"
113 #endif
114 #ifdef _WIN32
115 #include "Windows/Threading.inc"
116 #endif
117 
118 void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData,
119                                   llvm::Optional<unsigned> StackSizeInBytes) {
120 
121   SyncThreadInfo Info = {Fn, UserData};
122   llvm_execute_on_thread_impl(threadFuncSync, &Info, StackSizeInBytes,
123                               JoiningPolicy::Join);
124 }
125 
126 void llvm::llvm_execute_on_thread_async(
127     llvm::unique_function<void()> Func,
128     llvm::Optional<unsigned> StackSizeInBytes) {
129   llvm_execute_on_thread_impl(&threadFuncAsync,
130                               new AsyncThreadInfo(std::move(Func)),
131                               StackSizeInBytes, JoiningPolicy::Detach);
132 }
133 
134 #endif
135