xref: /openbsd-src/gnu/llvm/llvm/lib/Support/Threading.cpp (revision 73471bf04ceb096474c7f0fa83b1b65c70a787a1)
109467b48Spatrick //===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This file defines helper functions for running LLVM in a multi-threaded
1009467b48Spatrick // environment.
1109467b48Spatrick //
1209467b48Spatrick //===----------------------------------------------------------------------===//
1309467b48Spatrick 
1409467b48Spatrick #include "llvm/Support/Threading.h"
1509467b48Spatrick #include "llvm/ADT/Optional.h"
1609467b48Spatrick #include "llvm/Config/config.h"
1709467b48Spatrick #include "llvm/Support/Host.h"
1809467b48Spatrick 
1909467b48Spatrick #include <cassert>
2009467b48Spatrick #include <errno.h>
2109467b48Spatrick #include <stdlib.h>
2209467b48Spatrick #include <string.h>
2309467b48Spatrick 
2409467b48Spatrick using namespace llvm;
2509467b48Spatrick 
2609467b48Spatrick //===----------------------------------------------------------------------===//
2709467b48Spatrick //=== WARNING: Implementation here must contain only TRULY operating system
2809467b48Spatrick //===          independent code.
2909467b48Spatrick //===----------------------------------------------------------------------===//
3009467b48Spatrick 
3109467b48Spatrick bool llvm::llvm_is_multithreaded() {
3209467b48Spatrick #if LLVM_ENABLE_THREADS != 0
3309467b48Spatrick   return true;
3409467b48Spatrick #else
3509467b48Spatrick   return false;
3609467b48Spatrick #endif
3709467b48Spatrick }
3809467b48Spatrick 
3909467b48Spatrick #if LLVM_ENABLE_THREADS == 0 ||                                                \
4009467b48Spatrick     (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
4109467b48Spatrick uint64_t llvm::get_threadid() { return 0; }
4209467b48Spatrick 
4309467b48Spatrick uint32_t llvm::get_max_thread_name_length() { return 0; }
4409467b48Spatrick 
4509467b48Spatrick void llvm::set_thread_name(const Twine &Name) {}
4609467b48Spatrick 
4709467b48Spatrick void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
4809467b48Spatrick 
49097a140dSpatrick llvm::BitVector llvm::get_thread_affinity_mask() { return {}; }
50097a140dSpatrick 
51097a140dSpatrick unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
52097a140dSpatrick   // When threads are disabled, ensure clients will loop at least once.
53097a140dSpatrick   return 1;
54097a140dSpatrick }
55097a140dSpatrick 
5609467b48Spatrick #else
5709467b48Spatrick 
58097a140dSpatrick int computeHostNumHardwareThreads();
5909467b48Spatrick 
60097a140dSpatrick unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
61097a140dSpatrick   int MaxThreadCount = UseHyperThreads ? computeHostNumHardwareThreads()
62097a140dSpatrick                                        : sys::getHostNumPhysicalCores();
63097a140dSpatrick   if (MaxThreadCount <= 0)
64097a140dSpatrick     MaxThreadCount = 1;
65097a140dSpatrick   if (ThreadsRequested == 0)
66097a140dSpatrick     return MaxThreadCount;
67097a140dSpatrick   if (!Limit)
68097a140dSpatrick     return ThreadsRequested;
69097a140dSpatrick   return std::min((unsigned)MaxThreadCount, ThreadsRequested);
7009467b48Spatrick }
7109467b48Spatrick 
7209467b48Spatrick // Include the platform-specific parts of this class.
7309467b48Spatrick #ifdef LLVM_ON_UNIX
7409467b48Spatrick #include "Unix/Threading.inc"
7509467b48Spatrick #endif
7609467b48Spatrick #ifdef _WIN32
7709467b48Spatrick #include "Windows/Threading.inc"
7809467b48Spatrick #endif
7909467b48Spatrick 
80*73471bf0Spatrick // Must be included after Threading.inc to provide definition for llvm::thread
81*73471bf0Spatrick // because FreeBSD's condvar.h (included by user.h) misuses the "thread"
82*73471bf0Spatrick // keyword.
83*73471bf0Spatrick #include "llvm/Support/thread.h"
8409467b48Spatrick 
85*73471bf0Spatrick #if defined(__APPLE__)
86*73471bf0Spatrick   // Darwin's default stack size for threads except the main one is only 512KB,
87*73471bf0Spatrick   // which is not enough for some/many normal LLVM compilations. This implements
88*73471bf0Spatrick   // the same interface as std::thread but requests the same stack size as the
89*73471bf0Spatrick   // main thread (8MB) before creation.
90*73471bf0Spatrick const llvm::Optional<unsigned> llvm::thread::DefaultStackSize = 8 * 1024 * 1024;
91*73471bf0Spatrick #else
92*73471bf0Spatrick const llvm::Optional<unsigned> llvm::thread::DefaultStackSize = None;
93*73471bf0Spatrick #endif
9409467b48Spatrick 
9509467b48Spatrick 
9609467b48Spatrick #endif
97097a140dSpatrick 
98097a140dSpatrick Optional<ThreadPoolStrategy>
99097a140dSpatrick llvm::get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default) {
100097a140dSpatrick   if (Num == "all")
101097a140dSpatrick     return llvm::hardware_concurrency();
102097a140dSpatrick   if (Num.empty())
103097a140dSpatrick     return Default;
104097a140dSpatrick   unsigned V;
105097a140dSpatrick   if (Num.getAsInteger(10, V))
106097a140dSpatrick     return None; // malformed 'Num' value
107097a140dSpatrick   if (V == 0)
108097a140dSpatrick     return Default;
109097a140dSpatrick 
110097a140dSpatrick   // Do not take the Default into account. This effectively disables
111097a140dSpatrick   // heavyweight_hardware_concurrency() if the user asks for any number of
112097a140dSpatrick   // threads on the cmd-line.
113097a140dSpatrick   ThreadPoolStrategy S = llvm::hardware_concurrency();
114097a140dSpatrick   S.ThreadsRequested = V;
115097a140dSpatrick   return S;
116097a140dSpatrick }
117