1 //===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines helper functions for running LLVM in a multi-threaded
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Threading.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Host.h"
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
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
;
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(); }
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
69 int NumPhysical
= sys::getHostNumPhysicalCores();
70 if (NumPhysical
== -1)
71 return std::thread::hardware_concurrency();
75 unsigned llvm::hardware_concurrency() {
76 #if defined(HAVE_SCHED_GETAFFINITY) && defined(HAVE_CPU_COUNT)
78 if (sched_getaffinity(0, sizeof(Set
), &Set
))
79 return CPU_COUNT(&Set
);
81 // Guard against std::thread::hardware_concurrency() returning 0.
82 if (unsigned Val
= std::thread::hardware_concurrency())
87 // Include the platform-specific parts of this class.
89 #include "Unix/Threading.inc"
92 #include "Windows/Threading.inc"