LLVM  10.0.0svn
Threading.cpp
Go to the documentation of this file.
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.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/Threading.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Host.h"
17 
18 #include <cassert>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 using namespace llvm;
24 
25 //===----------------------------------------------------------------------===//
26 //=== WARNING: Implementation here must contain only TRULY operating system
27 //=== independent code.
28 //===----------------------------------------------------------------------===//
29 
31 #if LLVM_ENABLE_THREADS != 0
32  return true;
33 #else
34  return false;
35 #endif
36 }
37 
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);
45 }
46 
47 unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
48 
49 unsigned llvm::hardware_concurrency() { return 1; }
50 
51 uint64_t llvm::get_threadid() { return 0; }
52 
54 
55 void llvm::set_thread_name(const Twine &Name) {}
56 
58 
59 #else
60 
61 #include <thread>
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)
72  return NumPhysical;
73 }
74 
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;
85 }
86 
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
94 
95 #endif
void set_thread_name(const Twine &Name)
Set the name of the current thread.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
unsigned heavyweight_hardware_concurrency()
Get the amount of currency to use for tasks requiring significant memory or other resources...
Definition: Threading.cpp:62
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
int getHostNumPhysicalCores()
Get the number of physical cores (as opposed to logical cores returned from thread::hardware_concurre...
Definition: Host.cpp:1284
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
uint32_t get_max_thread_name_length()
Get the maximum length of a thread name on this platform.
void get_thread_name(SmallVectorImpl< char > &Name)
Get the name of the current thread.
bool llvm_is_multithreaded()
Returns true if LLVM is compiled with support for multi-threading, and false otherwise.
Definition: Threading.cpp:30
uint64_t get_threadid()
Return the current thread id, as used in various OS system calls.
void llvm_execute_on_thread(void(*UserFn)(void *), void *UserData, unsigned RequestedStackSize=0)
llvm_execute_on_thread - Execute the given UserFn on a separate thread, passing it the provided UserD...
unsigned hardware_concurrency()
Get the number of threads that the current program can execute concurrently.
Definition: Threading.cpp:75