Threading.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //===-- llvm/Support/Threading.h - 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 declares helper functions for running LLVM in a multi-threaded
  10. // environment.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_SUPPORT_THREADING_H
  14. #define LLVM_SUPPORT_THREADING_H
  15. #include "llvm/ADT/BitVector.h"
  16. #include "llvm/ADT/FunctionExtras.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX
  19. #include "llvm/Support/Compiler.h"
  20. #include <ciso646> // So we can check the C++ standard lib macros.
  21. #include <functional>
  22. #if defined(_MSC_VER)
  23. // MSVC's call_once implementation worked since VS 2015, which is the minimum
  24. // supported version as of this writing.
  25. #define LLVM_THREADING_USE_STD_CALL_ONCE 1
  26. #elif defined(LLVM_ON_UNIX) && \
  27. (defined(_LIBCPP_VERSION) || \
  28. !(defined(__NetBSD__) || defined(__OpenBSD__) || \
  29. (defined(__ppc__) || defined(__PPC__))))
  30. // std::call_once from libc++ is used on all Unix platforms. Other
  31. // implementations like libstdc++ are known to have problems on NetBSD,
  32. // OpenBSD and PowerPC.
  33. #define LLVM_THREADING_USE_STD_CALL_ONCE 1
  34. #elif defined(LLVM_ON_UNIX) && \
  35. ((defined(__ppc__) || defined(__PPC__)) && defined(__LITTLE_ENDIAN__))
  36. #define LLVM_THREADING_USE_STD_CALL_ONCE 1
  37. #else
  38. #define LLVM_THREADING_USE_STD_CALL_ONCE 0
  39. #endif
  40. #if LLVM_THREADING_USE_STD_CALL_ONCE
  41. #include <mutex>
  42. #else
  43. #include "llvm/Support/Atomic.h"
  44. #endif
  45. namespace llvm {
  46. class Twine;
  47. /// Returns true if LLVM is compiled with support for multi-threading, and
  48. /// false otherwise.
  49. bool llvm_is_multithreaded();
  50. /// Execute the given \p UserFn on a separate thread, passing it the provided \p
  51. /// UserData and waits for thread completion.
  52. ///
  53. /// This function does not guarantee that the code will actually be executed
  54. /// on a separate thread or honoring the requested stack size, but tries to do
  55. /// so where system support is available.
  56. ///
  57. /// \param UserFn - The callback to execute.
  58. /// \param UserData - An argument to pass to the callback function.
  59. /// \param StackSizeInBytes - A requested size (in bytes) for the thread stack
  60. /// (or None for default)
  61. void llvm_execute_on_thread(
  62. void (*UserFn)(void *), void *UserData,
  63. llvm::Optional<unsigned> StackSizeInBytes = llvm::None);
  64. /// Schedule the given \p Func for execution on a separate thread, then return
  65. /// to the caller immediately. Roughly equivalent to
  66. /// `std::thread(Func).detach()`, except it allows requesting a specific stack
  67. /// size, if supported for the platform.
  68. ///
  69. /// This function would report a fatal error if it can't execute the code
  70. /// on a separate thread.
  71. ///
  72. /// \param Func - The callback to execute.
  73. /// \param StackSizeInBytes - A requested size (in bytes) for the thread stack
  74. /// (or None for default)
  75. void llvm_execute_on_thread_async(
  76. llvm::unique_function<void()> Func,
  77. llvm::Optional<unsigned> StackSizeInBytes = llvm::None);
  78. #if LLVM_THREADING_USE_STD_CALL_ONCE
  79. typedef std::once_flag once_flag;
  80. #else
  81. enum InitStatus { Uninitialized = 0, Wait = 1, Done = 2 };
  82. /// The llvm::once_flag structure
  83. ///
  84. /// This type is modeled after std::once_flag to use with llvm::call_once.
  85. /// This structure must be used as an opaque object. It is a struct to force
  86. /// autoinitialization and behave like std::once_flag.
  87. struct once_flag {
  88. volatile sys::cas_flag status = Uninitialized;
  89. };
  90. #endif
  91. /// Execute the function specified as a parameter once.
  92. ///
  93. /// Typical usage:
  94. /// \code
  95. /// void foo() {...};
  96. /// ...
  97. /// static once_flag flag;
  98. /// call_once(flag, foo);
  99. /// \endcode
  100. ///
  101. /// \param flag Flag used for tracking whether or not this has run.
  102. /// \param F Function to call once.
  103. template <typename Function, typename... Args>
  104. void call_once(once_flag &flag, Function &&F, Args &&... ArgList) {
  105. #if LLVM_THREADING_USE_STD_CALL_ONCE
  106. std::call_once(flag, std::forward<Function>(F),
  107. std::forward<Args>(ArgList)...);
  108. #else
  109. // For other platforms we use a generic (if brittle) version based on our
  110. // atomics.
  111. sys::cas_flag old_val = sys::CompareAndSwap(&flag.status, Wait, Uninitialized);
  112. if (old_val == Uninitialized) {
  113. std::forward<Function>(F)(std::forward<Args>(ArgList)...);
  114. sys::MemoryFence();
  115. TsanIgnoreWritesBegin();
  116. TsanHappensBefore(&flag.status);
  117. flag.status = Done;
  118. TsanIgnoreWritesEnd();
  119. } else {
  120. // Wait until any thread doing the call has finished.
  121. sys::cas_flag tmp = flag.status;
  122. sys::MemoryFence();
  123. while (tmp != Done) {
  124. tmp = flag.status;
  125. sys::MemoryFence();
  126. }
  127. }
  128. TsanHappensAfter(&flag.status);
  129. #endif
  130. }
  131. /// This tells how a thread pool will be used
  132. class ThreadPoolStrategy {
  133. public:
  134. // The default value (0) means all available threads should be used,
  135. // taking the affinity mask into account. If set, this value only represents
  136. // a suggested high bound, the runtime might choose a lower value (not
  137. // higher).
  138. unsigned ThreadsRequested = 0;
  139. // If SMT is active, use hyper threads. If false, there will be only one
  140. // std::thread per core.
  141. bool UseHyperThreads = true;
  142. // If set, will constrain 'ThreadsRequested' to the number of hardware
  143. // threads, or hardware cores.
  144. bool Limit = false;
  145. /// Retrieves the max available threads for the current strategy. This
  146. /// accounts for affinity masks and takes advantage of all CPU sockets.
  147. unsigned compute_thread_count() const;
  148. /// Assign the current thread to an ideal hardware CPU or NUMA node. In a
  149. /// multi-socket system, this ensures threads are assigned to all CPU
  150. /// sockets. \p ThreadPoolNum represents a number bounded by [0,
  151. /// compute_thread_count()).
  152. void apply_thread_strategy(unsigned ThreadPoolNum) const;
  153. /// Finds the CPU socket where a thread should go. Returns 'None' if the
  154. /// thread shall remain on the actual CPU socket.
  155. Optional<unsigned> compute_cpu_socket(unsigned ThreadPoolNum) const;
  156. };
  157. /// Build a strategy from a number of threads as a string provided in \p Num.
  158. /// When Num is above the max number of threads specified by the \p Default
  159. /// strategy, we attempt to equally allocate the threads on all CPU sockets.
  160. /// "0" or an empty string will return the \p Default strategy.
  161. /// "all" for using all hardware threads.
  162. Optional<ThreadPoolStrategy>
  163. get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default = {});
  164. /// Returns a thread strategy for tasks requiring significant memory or other
  165. /// resources. To be used for workloads where hardware_concurrency() proves to
  166. /// be less efficient. Avoid this strategy if doing lots of I/O. Currently
  167. /// based on physical cores, if available for the host system, otherwise falls
  168. /// back to hardware_concurrency(). Returns 1 when LLVM is configured with
  169. /// LLVM_ENABLE_THREADS = OFF.
  170. inline ThreadPoolStrategy
  171. heavyweight_hardware_concurrency(unsigned ThreadCount = 0) {
  172. ThreadPoolStrategy S;
  173. S.UseHyperThreads = false;
  174. S.ThreadsRequested = ThreadCount;
  175. return S;
  176. }
  177. /// Like heavyweight_hardware_concurrency() above, but builds a strategy
  178. /// based on the rules described for get_threadpool_strategy().
  179. /// If \p Num is invalid, returns a default strategy where one thread per
  180. /// hardware core is used.
  181. inline ThreadPoolStrategy heavyweight_hardware_concurrency(StringRef Num) {
  182. Optional<ThreadPoolStrategy> S =
  183. get_threadpool_strategy(Num, heavyweight_hardware_concurrency());
  184. if (S)
  185. return *S;
  186. return heavyweight_hardware_concurrency();
  187. }
  188. /// Returns a default thread strategy where all available hardware resources
  189. /// are to be used, except for those initially excluded by an affinity mask.
  190. /// This function takes affinity into consideration. Returns 1 when LLVM is
  191. /// configured with LLVM_ENABLE_THREADS=OFF.
  192. inline ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount = 0) {
  193. ThreadPoolStrategy S;
  194. S.ThreadsRequested = ThreadCount;
  195. return S;
  196. }
  197. /// Returns an optimal thread strategy to execute specified amount of tasks.
  198. /// This strategy should prevent us from creating too many threads if we
  199. /// occasionaly have an unexpectedly small amount of tasks.
  200. inline ThreadPoolStrategy optimal_concurrency(unsigned TaskCount = 0) {
  201. ThreadPoolStrategy S;
  202. S.Limit = true;
  203. S.ThreadsRequested = TaskCount;
  204. return S;
  205. }
  206. /// Return the current thread id, as used in various OS system calls.
  207. /// Note that not all platforms guarantee that the value returned will be
  208. /// unique across the entire system, so portable code should not assume
  209. /// this.
  210. uint64_t get_threadid();
  211. /// Get the maximum length of a thread name on this platform.
  212. /// A value of 0 means there is no limit.
  213. uint32_t get_max_thread_name_length();
  214. /// Set the name of the current thread. Setting a thread's name can
  215. /// be helpful for enabling useful diagnostics under a debugger or when
  216. /// logging. The level of support for setting a thread's name varies
  217. /// wildly across operating systems, and we only make a best effort to
  218. /// perform the operation on supported platforms. No indication of success
  219. /// or failure is returned.
  220. void set_thread_name(const Twine &Name);
  221. /// Get the name of the current thread. The level of support for
  222. /// getting a thread's name varies wildly across operating systems, and it
  223. /// is not even guaranteed that if you can successfully set a thread's name
  224. /// that you can later get it back. This function is intended for diagnostic
  225. /// purposes, and as with setting a thread's name no indication of whether
  226. /// the operation succeeded or failed is returned.
  227. void get_thread_name(SmallVectorImpl<char> &Name);
  228. /// Returns a mask that represents on which hardware thread, core, CPU, NUMA
  229. /// group, the calling thread can be executed. On Windows, threads cannot
  230. /// cross CPU sockets boundaries.
  231. llvm::BitVector get_thread_affinity_mask();
  232. /// Returns how many physical CPUs or NUMA groups the system has.
  233. unsigned get_cpus();
  234. enum class ThreadPriority {
  235. Background = 0,
  236. Default = 1,
  237. };
  238. /// If priority is Background tries to lower current threads priority such
  239. /// that it does not affect foreground tasks significantly. Can be used for
  240. /// long-running, latency-insensitive tasks to make sure cpu is not hogged by
  241. /// this task.
  242. /// If the priority is default tries to restore current threads priority to
  243. /// default scheduling priority.
  244. enum class SetThreadPriorityResult { FAILURE, SUCCESS };
  245. SetThreadPriorityResult set_thread_priority(ThreadPriority Priority);
  246. }
  247. #endif