1 //===-- llvm/Support/Threading.h - Control multithreading mode --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares helper functions for running LLVM in a multi-threaded
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_THREADING_H
16 #define LLVM_SUPPORT_THREADING_H
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX
20 #include "llvm/Support/Compiler.h"
21 #include <ciso646> // So we can check the C++ standard lib macros.
25 // MSVC's call_once implementation worked since VS 2015, which is the minimum
26 // supported version as of this writing.
27 #define LLVM_THREADING_USE_STD_CALL_ONCE 1
28 #elif defined(LLVM_ON_UNIX) && \
29 (defined(_LIBCPP_VERSION) || \
30 !(defined(__NetBSD__) || defined(__OpenBSD__) || \
31 (defined(__ppc__) || defined(__PPC__))))
32 // std::call_once from libc++ is used on all Unix platforms. Other
33 // implementations like libstdc++ are known to have problems on NetBSD,
34 // OpenBSD and PowerPC.
35 #define LLVM_THREADING_USE_STD_CALL_ONCE 1
37 #define LLVM_THREADING_USE_STD_CALL_ONCE 0
40 #if LLVM_THREADING_USE_STD_CALL_ONCE
43 #include "llvm/Support/Atomic.h"
49 /// Returns true if LLVM is compiled with support for multi-threading, and
51 bool llvm_is_multithreaded();
53 /// llvm_execute_on_thread - Execute the given \p UserFn on a separate
54 /// thread, passing it the provided \p UserData and waits for thread
57 /// This function does not guarantee that the code will actually be executed
58 /// on a separate thread or honoring the requested stack size, but tries to do
59 /// so where system support is available.
61 /// \param UserFn - The callback to execute.
62 /// \param UserData - An argument to pass to the callback function.
63 /// \param RequestedStackSize - If non-zero, a requested size (in bytes) for
65 void llvm_execute_on_thread(void (*UserFn)(void *), void *UserData,
66 unsigned RequestedStackSize = 0);
68 #if LLVM_THREADING_USE_STD_CALL_ONCE
70 typedef std::once_flag once_flag;
74 enum InitStatus { Uninitialized = 0, Wait = 1, Done = 2 };
76 /// The llvm::once_flag structure
78 /// This type is modeled after std::once_flag to use with llvm::call_once.
79 /// This structure must be used as an opaque object. It is a struct to force
80 /// autoinitialization and behave like std::once_flag.
82 volatile sys::cas_flag status = Uninitialized;
87 /// Execute the function specified as a parameter once.
93 /// static once_flag flag;
94 /// call_once(flag, foo);
97 /// \param flag Flag used for tracking whether or not this has run.
98 /// \param F Function to call once.
99 template <typename Function, typename... Args>
100 void call_once(once_flag &flag, Function &&F, Args &&... ArgList) {
101 #if LLVM_THREADING_USE_STD_CALL_ONCE
102 std::call_once(flag, std::forward<Function>(F),
103 std::forward<Args>(ArgList)...);
105 // For other platforms we use a generic (if brittle) version based on our
107 sys::cas_flag old_val = sys::CompareAndSwap(&flag.status, Wait, Uninitialized);
108 if (old_val == Uninitialized) {
109 std::forward<Function>(F)(std::forward<Args>(ArgList)...);
111 TsanIgnoreWritesBegin();
112 TsanHappensBefore(&flag.status);
114 TsanIgnoreWritesEnd();
116 // Wait until any thread doing the call has finished.
117 sys::cas_flag tmp = flag.status;
119 while (tmp != Done) {
124 TsanHappensAfter(&flag.status);
128 /// Get the amount of currency to use for tasks requiring significant
129 /// memory or other resources. Currently based on physical cores, if
130 /// available for the host system, otherwise falls back to
131 /// thread::hardware_concurrency().
132 /// Returns 1 when LLVM is configured with LLVM_ENABLE_THREADS=OFF
133 unsigned heavyweight_hardware_concurrency();
135 /// Get the number of threads that the current program can execute
136 /// concurrently. On some systems std::thread::hardware_concurrency() returns
137 /// the total number of cores, without taking affinity into consideration.
138 /// Returns 1 when LLVM is configured with LLVM_ENABLE_THREADS=OFF.
139 /// Fallback to std::thread::hardware_concurrency() if sched_getaffinity is
141 unsigned hardware_concurrency();
143 /// Return the current thread id, as used in various OS system calls.
144 /// Note that not all platforms guarantee that the value returned will be
145 /// unique across the entire system, so portable code should not assume
147 uint64_t get_threadid();
149 /// Get the maximum length of a thread name on this platform.
150 /// A value of 0 means there is no limit.
151 uint32_t get_max_thread_name_length();
153 /// Set the name of the current thread. Setting a thread's name can
154 /// be helpful for enabling useful diagnostics under a debugger or when
155 /// logging. The level of support for setting a thread's name varies
156 /// wildly across operating systems, and we only make a best effort to
157 /// perform the operation on supported platforms. No indication of success
158 /// or failure is returned.
159 void set_thread_name(const Twine &Name);
161 /// Get the name of the current thread. The level of support for
162 /// getting a thread's name varies wildly across operating systems, and it
163 /// is not even guaranteed that if you can successfully set a thread's name
164 /// that you can later get it back. This function is intended for diagnostic
165 /// purposes, and as with setting a thread's name no indication of whether
166 /// the operation succeeded or failed is returned.
167 void get_thread_name(SmallVectorImpl<char> &Name);