llvm.org GIT mirror llvm / 529e9d3
Remove support for runtime multi-threading. This patch removes the functions llvm_start_multithreaded() and llvm_stop_multithreaded(), and changes llvm_is_multithreaded() to return a constant value based on the value of the compile-time definition LLVM_ENABLE_THREADS. Previously, it was possible to have compile-time support for threads on, and runtime support for threads off, in which case certain mutexes were not allocated or ever acquired. Now, if the build is created with threads enabled, mutexes are always acquired. A test before/after patch of compiling a very large TU showed no noticeable performance impact of this change. Reviewers: rnk Differential Revision: http://reviews.llvm.org/D4076 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210600 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 5 years ago
8 changed file(s) with 42 addition(s) and 79 deletion(s). Raw diff Collapse all Expand all
102102 /// llvm_shutdown() when it is destroyed.
103103 struct llvm_shutdown_obj {
104104 llvm_shutdown_obj() { }
105 explicit llvm_shutdown_obj(bool multithreaded) {
106 if (multithreaded) llvm_start_multithreaded();
107 }
108105 ~llvm_shutdown_obj() { llvm_shutdown(); }
109106 };
110107
1313 #ifndef LLVM_SUPPORT_THREADING_H
1414 #define LLVM_SUPPORT_THREADING_H
1515
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/Support/Compiler.h"
18 #include
19
1620 namespace llvm {
17 /// llvm_start_multithreaded - Allocate and initialize structures needed to
18 /// make LLVM safe for multithreading. The return value indicates whether
19 /// multithreaded initialization succeeded. LLVM will still be operational
20 /// on "failed" return, and will still be safe for hosting threading
21 /// applications in the JIT, but will not be safe for concurrent calls to the
22 /// LLVM APIs.
23 /// THIS MUST EXECUTE IN ISOLATION FROM ALL OTHER LLVM API CALLS.
24 bool llvm_start_multithreaded();
2521
26 /// llvm_stop_multithreaded - Deallocate structures necessary to make LLVM
27 /// safe for multithreading.
28 /// THIS MUST EXECUTE IN ISOLATION FROM ALL OTHER LLVM API CALLS.
29 void llvm_stop_multithreaded();
22 #if LLVM_ENABLE_THREADS != 0
23 typedef std::mutex mutex;
24 typedef std::recursive_mutex recursive_mutex;
25 #else
26 class null_mutex {
27 public:
28 void lock() { }
29 void unlock() { }
30 bool try_lock() { return true; }
31 };
32 typedef null_mutex mutex;
33 typedef null_mutex recursive_mutex;
34 #endif
3035
31 /// llvm_is_multithreaded - Check whether LLVM is executing in thread-safe
32 /// mode or not.
36 /// llvm_get_global_lock() - returns the llvm global lock object.
37 llvm::recursive_mutex &llvm_get_global_lock();
38
39 /// llvm_is_multithreaded() - returns true if LLVM is compiled with support
40 /// for multiple threads, and false otherwise.
3341 bool llvm_is_multithreaded();
34
35 /// acquire_global_lock - Acquire the global lock. This is a no-op if called
36 /// before llvm_start_multithreaded().
37 void llvm_acquire_global_lock();
38
39 /// release_global_lock - Release the global lock. This is a no-op if called
40 /// before llvm_start_multithreaded().
41 void llvm_release_global_lock();
4242
4343 /// llvm_execute_on_thread - Execute the given \p UserFn on a separate
4444 /// thread, passing it the provided \p UserData.
28472847 * @{
28482848 */
28492849
2850 /** Allocate and initialize structures needed to make LLVM safe for
2851 multithreading. The return value indicates whether multithreaded
2852 initialization succeeded. Must be executed in isolation from all
2853 other LLVM api calls.
2854 @see llvm::llvm_start_multithreaded */
2850 /** Deprecated: Multi-threading can only be enabled/disabled with the compile
2851 time define LLVM_ENABLE_THREADS. This function always returns
2852 LLVMIsMultithreaded(). */
28552853 LLVMBool LLVMStartMultithreaded(void);
28562854
2857 /** Deallocate structures necessary to make LLVM safe for multithreading.
2858 Must be executed in isolation from all other LLVM api calls.
2859 @see llvm::llvm_stop_multithreaded */
2855 /** Deprecated: Multi-threading can only be enabled/disabled with the compile
2856 time define LLVM_ENABLE_THREADS. */
28602857 void LLVMStopMultithreaded(void);
28612858
28622859 /** Check whether LLVM is executing in thread-safe mode or not.
27012701 /*===-- Threading ------------------------------------------------------===*/
27022702
27032703 LLVMBool LLVMStartMultithreaded() {
2704 return llvm_start_multithreaded();
2704 return LLVMIsMultithreaded();
27052705 }
27062706
27072707 void LLVMStopMultithreaded() {
2708 llvm_stop_multithreaded();
27092708 }
27102709
27112710 LLVMBool LLVMIsMultithreaded() {
2222 #include "llvm/Support/raw_ostream.h"
2323 #include
2424 #include
25 #include
2526
2627 #if defined(HAVE_UNISTD_H)
2728 # include
3637 static fatal_error_handler_t ErrorHandler = nullptr;
3738 static void *ErrorHandlerUserData = nullptr;
3839
40 static llvm::recursive_mutex ErrorHandlerMutex;
41
3942 void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
4043 void *user_data) {
41 assert(!llvm_is_multithreaded() &&
42 "Cannot register error handlers after starting multithreaded mode!\n");
44 std::lock_guard Lock(ErrorHandlerMutex);
4345 assert(!ErrorHandler && "Error handler already registered!\n");
4446 ErrorHandler = handler;
4547 ErrorHandlerUserData = user_data;
4648 }
4749
4850 void llvm::remove_fatal_error_handler() {
51 std::lock_guard Lock(ErrorHandlerMutex);
4952 ErrorHandler = nullptr;
5053 }
5154
6265 }
6366
6467 void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
68 std::lock_guard Lock(ErrorHandlerMutex);
6569 if (ErrorHandler) {
6670 ErrorHandler(ErrorHandlerUserData, Reason.str(), GenCrashDiag);
6771 } else {
1414 #include "llvm/Config/config.h"
1515 #include "llvm/Support/Atomic.h"
1616 #include
17 #include
1718 using namespace llvm;
1819
1920 static const ManagedStaticBase *StaticList = nullptr;
2223 void (*Deleter)(void*)) const {
2324 assert(Creator);
2425 if (llvm_is_multithreaded()) {
25 llvm_acquire_global_lock();
26 std::lock_guard Lock(llvm_get_global_lock());
2627
2728 if (!Ptr) {
2829 void* tmp = Creator();
4344 StaticList = this;
4445 }
4546
46 llvm_release_global_lock();
4747 } else {
4848 assert(!Ptr && !DeleterFn && !Next &&
4949 "Partially initialized ManagedStatic!?");
7676 void llvm::llvm_shutdown() {
7777 while (StaticList)
7878 StaticList->destroy();
79
80 if (llvm_is_multithreaded()) llvm_stop_multithreaded();
8179 }
1818
1919 using namespace llvm;
2020
21 static bool multithreaded_mode = false;
21 llvm::recursive_mutex& llvm::llvm_get_global_lock() {
22 static llvm::recursive_mutex global_lock;
23 return global_lock;
24 }
2225
23 static sys::Mutex* global_lock = nullptr;
24
25 bool llvm::llvm_start_multithreaded() {
26 bool llvm::llvm_is_multithreaded() {
2627 #if LLVM_ENABLE_THREADS != 0
27 assert(!multithreaded_mode && "Already multithreaded!");
28 multithreaded_mode = true;
29 global_lock = new sys::Mutex(true);
30
31 // We fence here to ensure that all initialization is complete BEFORE we
32 // return from llvm_start_multithreaded().
33 sys::MemoryFence();
3428 return true;
3529 #else
3630 return false;
3731 #endif
38 }
39
40 void llvm::llvm_stop_multithreaded() {
41 #if LLVM_ENABLE_THREADS != 0
42 assert(multithreaded_mode && "Not currently multithreaded!");
43
44 // We fence here to insure that all threaded operations are complete BEFORE we
45 // return from llvm_stop_multithreaded().
46 sys::MemoryFence();
47
48 multithreaded_mode = false;
49 delete global_lock;
50 #endif
51 }
52
53 bool llvm::llvm_is_multithreaded() {
54 return multithreaded_mode;
55 }
56
57 void llvm::llvm_acquire_global_lock() {
58 if (multithreaded_mode) global_lock->acquire();
59 }
60
61 void llvm::llvm_release_global_lock() {
62 if (multithreaded_mode) global_lock->release();
6332 }
6433
6534 #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)
8383 sys::MemoryFence();
8484 if (tmp) return tmp;
8585
86 llvm_acquire_global_lock();
86 std::lock_guard Lock(llvm::llvm_get_global_lock());
8787 tmp = DefaultTimerGroup;
8888 if (!tmp) {
8989 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
9090 sys::MemoryFence();
9191 DefaultTimerGroup = tmp;
9292 }
93 llvm_release_global_lock();
9493
9594 return tmp;
9695 }