Threading.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #include "llvm/Support/Threading.h"
  14. #include "llvm/Config/config.h"
  15. #include "llvm/Config/llvm-config.h"
  16. #include <cassert>
  17. #include <errno.h>
  18. #include <optional>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. using namespace llvm;
  22. //===----------------------------------------------------------------------===//
  23. //=== WARNING: Implementation here must contain only TRULY operating system
  24. //=== independent code.
  25. //===----------------------------------------------------------------------===//
  26. #if LLVM_ENABLE_THREADS == 0 || \
  27. (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
  28. uint64_t llvm::get_threadid() { return 0; }
  29. uint32_t llvm::get_max_thread_name_length() { return 0; }
  30. void llvm::set_thread_name(const Twine &Name) {}
  31. void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
  32. llvm::BitVector llvm::get_thread_affinity_mask() { return {}; }
  33. unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
  34. // When threads are disabled, ensure clients will loop at least once.
  35. return 1;
  36. }
  37. // Unknown if threading turned off
  38. int llvm::get_physical_cores() { return -1; }
  39. #else
  40. static int computeHostNumHardwareThreads();
  41. unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
  42. int MaxThreadCount =
  43. UseHyperThreads ? computeHostNumHardwareThreads() : get_physical_cores();
  44. if (MaxThreadCount <= 0)
  45. MaxThreadCount = 1;
  46. if (ThreadsRequested == 0)
  47. return MaxThreadCount;
  48. if (!Limit)
  49. return ThreadsRequested;
  50. return std::min((unsigned)MaxThreadCount, ThreadsRequested);
  51. }
  52. // Include the platform-specific parts of this class.
  53. #ifdef LLVM_ON_UNIX
  54. #include "Unix/Threading.inc"
  55. #endif
  56. #ifdef _WIN32
  57. #include "Windows/Threading.inc"
  58. #endif
  59. // Must be included after Threading.inc to provide definition for llvm::thread
  60. // because FreeBSD's condvar.h (included by user.h) misuses the "thread"
  61. // keyword.
  62. #include "llvm/Support/thread.h"
  63. #if defined(__APPLE__)
  64. // Darwin's default stack size for threads except the main one is only 512KB,
  65. // which is not enough for some/many normal LLVM compilations. This implements
  66. // the same interface as std::thread but requests the same stack size as the
  67. // main thread (8MB) before creation.
  68. const std::optional<unsigned> llvm::thread::DefaultStackSize = 8 * 1024 * 1024;
  69. #else
  70. const std::optional<unsigned> llvm::thread::DefaultStackSize;
  71. #endif
  72. #endif
  73. std::optional<ThreadPoolStrategy>
  74. llvm::get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default) {
  75. if (Num == "all")
  76. return llvm::hardware_concurrency();
  77. if (Num.empty())
  78. return Default;
  79. unsigned V;
  80. if (Num.getAsInteger(10, V))
  81. return std::nullopt; // malformed 'Num' value
  82. if (V == 0)
  83. return Default;
  84. // Do not take the Default into account. This effectively disables
  85. // heavyweight_hardware_concurrency() if the user asks for any number of
  86. // threads on the cmd-line.
  87. ThreadPoolStrategy S = llvm::hardware_concurrency();
  88. S.ThreadsRequested = V;
  89. return S;
  90. }