llvm.org GIT mirror llvm / 63ce00b
Revert "Remove support for runtime multi-threading." This reverts revision r210600. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210603 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 5 years ago
8 changed file(s) with 79 addition(s) and 42 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 }
105108 ~llvm_shutdown_obj() { llvm_shutdown(); }
106109 };
107110
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
16 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();
1925
20 namespace llvm {
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();
2130
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
31 /// llvm_is_multithreaded - Check whether LLVM is executing in thread-safe
32 /// mode or not.
33 bool llvm_is_multithreaded();
3534
36 /// llvm_get_global_lock() - returns the llvm global lock object.
37 llvm::recursive_mutex &llvm_get_global_lock();
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();
3838
39 /// llvm_is_multithreaded() - returns true if LLVM is compiled with support
40 /// for multiple threads, and false otherwise.
41 bool llvm_is_multithreaded();
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 /** Deprecated: Multi-threading can only be enabled/disabled with the compile
2851 time define LLVM_ENABLE_THREADS. This function always returns
2852 LLVMIsMultithreaded(). */
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 */
28532855 LLVMBool LLVMStartMultithreaded(void);
28542856
2855 /** Deprecated: Multi-threading can only be enabled/disabled with the compile
2856 time define LLVM_ENABLE_THREADS. */
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 */
28572860 void LLVMStopMultithreaded(void);
28582861
28592862 /** Check whether LLVM is executing in thread-safe mode or not.
27012701 /*===-- Threading ------------------------------------------------------===*/
27022702
27032703 LLVMBool LLVMStartMultithreaded() {
2704 return LLVMIsMultithreaded();
2704 return llvm_start_multithreaded();
27052705 }
27062706
27072707 void LLVMStopMultithreaded() {
2708 llvm_stop_multithreaded();
27082709 }
27092710
27102711 LLVMBool LLVMIsMultithreaded() {
2222 #include "llvm/Support/raw_ostream.h"
2323 #include
2424 #include
25 #include
2625
2726 #if defined(HAVE_UNISTD_H)
2827 # include
3736 static fatal_error_handler_t ErrorHandler = nullptr;
3837 static void *ErrorHandlerUserData = nullptr;
3938
40 static llvm::recursive_mutex ErrorHandlerMutex;
41
4239 void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
4340 void *user_data) {
44 std::lock_guard Lock(ErrorHandlerMutex);
41 assert(!llvm_is_multithreaded() &&
42 "Cannot register error handlers after starting multithreaded mode!\n");
4543 assert(!ErrorHandler && "Error handler already registered!\n");
4644 ErrorHandler = handler;
4745 ErrorHandlerUserData = user_data;
4846 }
4947
5048 void llvm::remove_fatal_error_handler() {
51 std::lock_guard Lock(ErrorHandlerMutex);
5249 ErrorHandler = nullptr;
5350 }
5451
6562 }
6663
6764 void llvm::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
68 std::lock_guard Lock(ErrorHandlerMutex);
6965 if (ErrorHandler) {
7066 ErrorHandler(ErrorHandlerUserData, Reason.str(), GenCrashDiag);
7167 } else {
1414 #include "llvm/Config/config.h"
1515 #include "llvm/Support/Atomic.h"
1616 #include
17 #include
1817 using namespace llvm;
1918
2019 static const ManagedStaticBase *StaticList = nullptr;
2322 void (*Deleter)(void*)) const {
2423 assert(Creator);
2524 if (llvm_is_multithreaded()) {
26 std::lock_guard Lock(llvm_get_global_lock());
25 llvm_acquire_global_lock();
2726
2827 if (!Ptr) {
2928 void* tmp = Creator();
4443 StaticList = this;
4544 }
4645
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();
7981 }
1818
1919 using namespace llvm;
2020
21 llvm::recursive_mutex& llvm::llvm_get_global_lock() {
22 static llvm::recursive_mutex global_lock;
23 return global_lock;
24 }
21 static bool multithreaded_mode = false;
2522
26 bool llvm::llvm_is_multithreaded() {
23 static sys::Mutex* global_lock = nullptr;
24
25 bool llvm::llvm_start_multithreaded() {
2726 #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();
2834 return true;
2935 #else
3036 return false;
3137 #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();
3263 }
3364
3465 #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)
8383 sys::MemoryFence();
8484 if (tmp) return tmp;
8585
86 std::lock_guard Lock(llvm::llvm_get_global_lock());
86 llvm_acquire_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();
9394
9495 return tmp;
9596 }