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 #if LLVM_ENABLE_THREADS == 0 || \ 32 (!defined(_WIN32) && !defined(HAVE_PTHREAD_H)) 33 uint64_t llvm::get_threadid() { return 0; } 34 35 uint32_t llvm::get_max_thread_name_length() { return 0; } 36 37 void llvm::set_thread_name(const Twine &Name) {} 38 39 void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); } 40 41 llvm::BitVector llvm::get_thread_affinity_mask() { return {}; } 42 43 unsigned llvm::ThreadPoolStrategy::compute_thread_count() const { 44 // When threads are disabled, ensure clients will loop at least once. 45 return 1; 46 } 47 48 #else 49 50 int computeHostNumHardwareThreads(); 51 52 unsigned llvm::ThreadPoolStrategy::compute_thread_count() const { 53 int MaxThreadCount = UseHyperThreads ? computeHostNumHardwareThreads() 54 : sys::getHostNumPhysicalCores(); 55 if (MaxThreadCount <= 0) 56 MaxThreadCount = 1; 57 if (ThreadsRequested == 0) 58 return MaxThreadCount; 59 if (!Limit) 60 return ThreadsRequested; 61 return std::min((unsigned)MaxThreadCount, ThreadsRequested); 62 } 63 64 // Include the platform-specific parts of this class. 65 #ifdef LLVM_ON_UNIX 66 #include "Unix/Threading.inc" 67 #endif 68 #ifdef _WIN32 69 #include "Windows/Threading.inc" 70 #endif 71 72 // Must be included after Threading.inc to provide definition for llvm::thread 73 // because FreeBSD's condvar.h (included by user.h) misuses the "thread" 74 // keyword. 75 #include "llvm/Support/thread.h" 76 77 #if defined(__APPLE__) 78 // Darwin's default stack size for threads except the main one is only 512KB, 79 // which is not enough for some/many normal LLVM compilations. This implements 80 // the same interface as std::thread but requests the same stack size as the 81 // main thread (8MB) before creation. 82 const llvm::Optional<unsigned> llvm::thread::DefaultStackSize = 8 * 1024 * 1024; 83 #else 84 const llvm::Optional<unsigned> llvm::thread::DefaultStackSize = None; 85 #endif 86 87 88 #endif 89 90 Optional<ThreadPoolStrategy> 91 llvm::get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default) { 92 if (Num == "all") 93 return llvm::hardware_concurrency(); 94 if (Num.empty()) 95 return Default; 96 unsigned V; 97 if (Num.getAsInteger(10, V)) 98 return None; // malformed 'Num' value 99 if (V == 0) 100 return Default; 101 102 // Do not take the Default into account. This effectively disables 103 // heavyweight_hardware_concurrency() if the user asks for any number of 104 // threads on the cmd-line. 105 ThreadPoolStrategy S = llvm::hardware_concurrency(); 106 S.ThreadsRequested = V; 107 return S; 108 } 109