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 if (ThreadsRequested > 0) 88 return ThreadsRequested; 89 90 int MaxThreadCount = UseHyperThreads ? computeHostNumHardwareThreads() 91 : sys::getHostNumPhysicalCores(); 92 if (MaxThreadCount <= 0) 93 MaxThreadCount = 1; 94 return MaxThreadCount; 95 } 96 97 namespace { 98 struct SyncThreadInfo { 99 void (*UserFn)(void *); 100 void *UserData; 101 }; 102 103 using AsyncThreadInfo = llvm::unique_function<void()>; 104 105 enum class JoiningPolicy { Join, Detach }; 106 } // namespace 107 108 // Include the platform-specific parts of this class. 109 #ifdef LLVM_ON_UNIX 110 #include "Unix/Threading.inc" 111 #endif 112 #ifdef _WIN32 113 #include "Windows/Threading.inc" 114 #endif 115 116 void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData, 117 llvm::Optional<unsigned> StackSizeInBytes) { 118 119 SyncThreadInfo Info = {Fn, UserData}; 120 llvm_execute_on_thread_impl(threadFuncSync, &Info, StackSizeInBytes, 121 JoiningPolicy::Join); 122 } 123 124 void llvm::llvm_execute_on_thread_async( 125 llvm::unique_function<void()> Func, 126 llvm::Optional<unsigned> StackSizeInBytes) { 127 llvm_execute_on_thread_impl(&threadFuncAsync, 128 new AsyncThreadInfo(std::move(Func)), 129 StackSizeInBytes, JoiningPolicy::Detach); 130 } 131 132 #endif 133 134 Optional<ThreadPoolStrategy> 135 llvm::get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default) { 136 if (Num == "all") 137 return llvm::hardware_concurrency(); 138 if (Num.empty()) 139 return Default; 140 unsigned V; 141 if (Num.getAsInteger(10, V)) 142 return None; // malformed 'Num' value 143 if (V == 0) 144 return Default; 145 146 // Do not take the Default into account. This effectively disables 147 // heavyweight_hardware_concurrency() if the user asks for any number of 148 // threads on the cmd-line. 149 ThreadPoolStrategy S = llvm::hardware_concurrency(); 150 S.ThreadsRequested = V; 151 return S; 152 } 153