1 //===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines helper functions for running LLVM in a multi-threaded
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/Threading.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Host.h"
26 //===----------------------------------------------------------------------===//
27 //=== WARNING: Implementation here must contain only TRULY operating system
28 //=== independent code.
29 //===----------------------------------------------------------------------===//
31 bool llvm::llvm_is_multithreaded() {
32 #if LLVM_ENABLE_THREADS != 0
39 #if LLVM_ENABLE_THREADS == 0 || \
40 (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
41 // Support for non-Win32, non-pthread implementation.
42 void llvm::llvm_execute_on_thread(void (*Fn
)(void *), void *UserData
,
43 unsigned RequestedStackSize
) {
44 (void)RequestedStackSize
;
48 unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
50 unsigned llvm::hardware_concurrency() { return 1; }
52 uint64_t llvm::get_threadid() { return 0; }
54 uint32_t llvm::get_max_thread_name_length() { return 0; }
56 void llvm::set_thread_name(const Twine
&Name
) {}
58 void llvm::get_thread_name(SmallVectorImpl
<char> &Name
) { Name
.clear(); }
63 unsigned llvm::heavyweight_hardware_concurrency() {
64 // Since we can't get here unless LLVM_ENABLE_THREADS == 1, it is safe to use
65 // `std::thread` directly instead of `llvm::thread` (and indeed, doing so
66 // allows us to not define `thread` in the llvm namespace, which conflicts
67 // with some platforms such as FreeBSD whose headers also define a struct
68 // called `thread` in the global namespace which can cause ambiguity due to
70 int NumPhysical
= sys::getHostNumPhysicalCores();
71 if (NumPhysical
== -1)
72 return std::thread::hardware_concurrency();
76 unsigned llvm::hardware_concurrency() {
77 #if defined(HAVE_SCHED_GETAFFINITY) && defined(HAVE_CPU_COUNT)
79 if (sched_getaffinity(0, sizeof(Set
), &Set
))
80 return CPU_COUNT(&Set
);
82 // Guard against std::thread::hardware_concurrency() returning 0.
83 if (unsigned Val
= std::thread::hardware_concurrency())
88 // Include the platform-specific parts of this class.
90 #include "Unix/Threading.inc"
93 #include "Windows/Threading.inc"