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