xref: /openbsd-src/gnu/llvm/llvm/lib/Support/Threading.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
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/Config/config.h"
16*d415bd75Srobert #include "llvm/Config/llvm-config.h"
1709467b48Spatrick 
1809467b48Spatrick #include <cassert>
1909467b48Spatrick #include <errno.h>
20*d415bd75Srobert #include <optional>
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 #if LLVM_ENABLE_THREADS == 0 ||                                                \
3209467b48Spatrick     (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
get_threadid()3309467b48Spatrick uint64_t llvm::get_threadid() { return 0; }
3409467b48Spatrick 
get_max_thread_name_length()3509467b48Spatrick uint32_t llvm::get_max_thread_name_length() { return 0; }
3609467b48Spatrick 
set_thread_name(const Twine & Name)3709467b48Spatrick void llvm::set_thread_name(const Twine &Name) {}
3809467b48Spatrick 
get_thread_name(SmallVectorImpl<char> & Name)3909467b48Spatrick void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
4009467b48Spatrick 
get_thread_affinity_mask()41097a140dSpatrick llvm::BitVector llvm::get_thread_affinity_mask() { return {}; }
42097a140dSpatrick 
compute_thread_count() const43097a140dSpatrick unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
44097a140dSpatrick   // When threads are disabled, ensure clients will loop at least once.
45097a140dSpatrick   return 1;
46097a140dSpatrick }
47097a140dSpatrick 
48*d415bd75Srobert // Unknown if threading turned off
get_physical_cores()49*d415bd75Srobert int llvm::get_physical_cores() { return -1; }
50*d415bd75Srobert 
5109467b48Spatrick #else
5209467b48Spatrick 
53*d415bd75Srobert static int computeHostNumHardwareThreads();
5409467b48Spatrick 
compute_thread_count() const55097a140dSpatrick unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
56*d415bd75Srobert   int MaxThreadCount =
57*d415bd75Srobert       UseHyperThreads ? computeHostNumHardwareThreads() : get_physical_cores();
58097a140dSpatrick   if (MaxThreadCount <= 0)
59097a140dSpatrick     MaxThreadCount = 1;
60097a140dSpatrick   if (ThreadsRequested == 0)
61097a140dSpatrick     return MaxThreadCount;
62097a140dSpatrick   if (!Limit)
63097a140dSpatrick     return ThreadsRequested;
64097a140dSpatrick   return std::min((unsigned)MaxThreadCount, ThreadsRequested);
6509467b48Spatrick }
6609467b48Spatrick 
6709467b48Spatrick // Include the platform-specific parts of this class.
6809467b48Spatrick #ifdef LLVM_ON_UNIX
6909467b48Spatrick #include "Unix/Threading.inc"
7009467b48Spatrick #endif
7109467b48Spatrick #ifdef _WIN32
7209467b48Spatrick #include "Windows/Threading.inc"
7309467b48Spatrick #endif
7409467b48Spatrick 
7573471bf0Spatrick // Must be included after Threading.inc to provide definition for llvm::thread
7673471bf0Spatrick // because FreeBSD's condvar.h (included by user.h) misuses the "thread"
7773471bf0Spatrick // keyword.
7873471bf0Spatrick #include "llvm/Support/thread.h"
7909467b48Spatrick 
8073471bf0Spatrick #if defined(__APPLE__)
8173471bf0Spatrick   // Darwin's default stack size for threads except the main one is only 512KB,
8273471bf0Spatrick   // which is not enough for some/many normal LLVM compilations. This implements
8373471bf0Spatrick   // the same interface as std::thread but requests the same stack size as the
8473471bf0Spatrick   // main thread (8MB) before creation.
85*d415bd75Srobert const std::optional<unsigned> llvm::thread::DefaultStackSize = 8 * 1024 * 1024;
8673471bf0Spatrick #else
87*d415bd75Srobert const std::optional<unsigned> llvm::thread::DefaultStackSize;
8873471bf0Spatrick #endif
8909467b48Spatrick 
9009467b48Spatrick 
9109467b48Spatrick #endif
92097a140dSpatrick 
93*d415bd75Srobert std::optional<ThreadPoolStrategy>
get_threadpool_strategy(StringRef Num,ThreadPoolStrategy Default)94097a140dSpatrick llvm::get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default) {
95097a140dSpatrick   if (Num == "all")
96097a140dSpatrick     return llvm::hardware_concurrency();
97097a140dSpatrick   if (Num.empty())
98097a140dSpatrick     return Default;
99097a140dSpatrick   unsigned V;
100097a140dSpatrick   if (Num.getAsInteger(10, V))
101*d415bd75Srobert     return std::nullopt; // malformed 'Num' value
102097a140dSpatrick   if (V == 0)
103097a140dSpatrick     return Default;
104097a140dSpatrick 
105097a140dSpatrick   // Do not take the Default into account. This effectively disables
106097a140dSpatrick   // heavyweight_hardware_concurrency() if the user asks for any number of
107097a140dSpatrick   // threads on the cmd-line.
108097a140dSpatrick   ThreadPoolStrategy S = llvm::hardware_concurrency();
109097a140dSpatrick   S.ThreadsRequested = V;
110097a140dSpatrick   return S;
111097a140dSpatrick }
112