[Alignment][NFC] Use Align with TargetLowering::setMinFunctionAlignment
[llvm-core.git] / lib / Support / Threading.cpp
blobe5899a60f4dbcf366e6885bb36b85a97670d0d7a
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.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Threading.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Host.h"
18 #include <cassert>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
23 using namespace llvm;
25 //===----------------------------------------------------------------------===//
26 //=== WARNING: Implementation here must contain only TRULY operating system
27 //=== independent code.
28 //===----------------------------------------------------------------------===//
30 bool llvm::llvm_is_multithreaded() {
31 #if LLVM_ENABLE_THREADS != 0
32 return true;
33 #else
34 return false;
35 #endif
38 #if LLVM_ENABLE_THREADS == 0 || \
39 (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
40 // Support for non-Win32, non-pthread implementation.
41 void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData,
42 unsigned RequestedStackSize) {
43 (void)RequestedStackSize;
44 Fn(UserData);
47 unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
49 unsigned llvm::hardware_concurrency() { return 1; }
51 uint64_t llvm::get_threadid() { return 0; }
53 uint32_t llvm::get_max_thread_name_length() { return 0; }
55 void llvm::set_thread_name(const Twine &Name) {}
57 void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
59 #else
61 #include <thread>
62 unsigned llvm::heavyweight_hardware_concurrency() {
63 // Since we can't get here unless LLVM_ENABLE_THREADS == 1, it is safe to use
64 // `std::thread` directly instead of `llvm::thread` (and indeed, doing so
65 // allows us to not define `thread` in the llvm namespace, which conflicts
66 // with some platforms such as FreeBSD whose headers also define a struct
67 // called `thread` in the global namespace which can cause ambiguity due to
68 // ADL.
69 int NumPhysical = sys::getHostNumPhysicalCores();
70 if (NumPhysical == -1)
71 return std::thread::hardware_concurrency();
72 return NumPhysical;
75 unsigned llvm::hardware_concurrency() {
76 #if defined(HAVE_SCHED_GETAFFINITY) && defined(HAVE_CPU_COUNT)
77 cpu_set_t Set;
78 if (sched_getaffinity(0, sizeof(Set), &Set))
79 return CPU_COUNT(&Set);
80 #endif
81 // Guard against std::thread::hardware_concurrency() returning 0.
82 if (unsigned Val = std::thread::hardware_concurrency())
83 return Val;
84 return 1;
87 // Include the platform-specific parts of this class.
88 #ifdef LLVM_ON_UNIX
89 #include "Unix/Threading.inc"
90 #endif
91 #ifdef _WIN32
92 #include "Windows/Threading.inc"
93 #endif
95 #endif