Threading.cpp 3.6 KB

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