Coverage Report

Created: 2025-08-28 06:48

/src/hermes/external/llvh/lib/Support/Threading.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file defines helper functions for running LLVM in a multi-threaded
11
// environment.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "llvh/Support/Threading.h"
16
#include "llvh/Config/config.h"
17
#include "llvh/Support/Host.h"
18
19
#include <cassert>
20
#include <errno.h>
21
#include <stdlib.h>
22
#include <string.h>
23
24
using namespace llvh;
25
26
//===----------------------------------------------------------------------===//
27
//=== WARNING: Implementation here must contain only TRULY operating system
28
//===          independent code.
29
//===----------------------------------------------------------------------===//
30
31
53
bool llvh::llvm_is_multithreaded() {
32
53
#if LLVM_ENABLE_THREADS != 0
33
53
  return true;
34
#else
35
  return false;
36
#endif
37
53
}
38
39
#if LLVM_ENABLE_THREADS == 0 ||                                                \
40
    (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
41
// Support for non-Win32, non-pthread implementation.
42
void llvh::llvm_execute_on_thread(void (*Fn)(void *), void *UserData,
43
                                  unsigned RequestedStackSize) {
44
  (void)RequestedStackSize;
45
  Fn(UserData);
46
}
47
48
unsigned llvh::heavyweight_hardware_concurrency() { return 1; }
49
50
unsigned llvh::hardware_concurrency() { return 1; }
51
52
uint64_t llvh::get_threadid() { return 0; }
53
54
uint32_t llvh::get_max_thread_name_length() { return 0; }
55
56
void llvh::set_thread_name(const Twine &Name) {}
57
58
void llvh::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
59
60
#else
61
62
#include <thread>
63
0
unsigned llvh::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 `llvh::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
69
  // ADL.
70
0
  int NumPhysical = sys::getHostNumPhysicalCores();
71
0
  if (NumPhysical == -1)
72
0
    return std::thread::hardware_concurrency();
73
0
  return NumPhysical;
74
0
}
75
76
0
unsigned llvh::hardware_concurrency() {
77
0
#if defined(HAVE_SCHED_GETAFFINITY) && defined(HAVE_CPU_COUNT)
78
0
  cpu_set_t Set;
79
0
  if (sched_getaffinity(0, sizeof(Set), &Set))
80
0
    return CPU_COUNT(&Set);
81
0
#endif
82
  // Guard against std::thread::hardware_concurrency() returning 0.
83
0
  if (unsigned Val = std::thread::hardware_concurrency())
84
0
    return Val;
85
0
  return 1;
86
0
}
87
88
// Include the platform-specific parts of this class.
89
#ifdef LLVM_ON_UNIX
90
#include "Unix/Threading.inc"
91
#endif
92
#ifdef _WIN32
93
#include "Windows/Threading.inc"
94
#endif
95
96
#endif