llvm.org GIT mirror llvm / aedb552
Remove some more code out into a separate CL. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211067 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 5 years ago
8 changed file(s) with 17 addition(s) and 63 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
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // TThis file defines llvm_start_multithreaded() and friends.
9 // This file defines helper functions for running LLVM in a multi-threaded
10 // environment.
1011 //
1112 //===----------------------------------------------------------------------===//
1213
1617 #include "llvm/Support/Mutex.h"
1718
1819 namespace llvm {
19 /// llvm_get_global_lock - returns the llvm global lock object.
20 sys::Mutex& llvm_get_global_lock();
2120
22 /// llvm_start_multithreaded - Allocate and initialize structures needed to
23 /// make LLVM safe for multithreading. The return value indicates whether
24 /// multithreaded initialization succeeded. LLVM will still be operational
25 /// on "failed" return, and will still be safe for hosting threading
26 /// applications in the JIT, but will not be safe for concurrent calls to the
27 /// LLVM APIs.
28 /// THIS MUST EXECUTE IN ISOLATION FROM ALL OTHER LLVM API CALLS.
29 bool llvm_start_multithreaded();
21 /// llvm_get_global_lock() - returns the llvm global lock object.
22 sys::Mutex &llvm_get_global_lock();
3023
31 /// llvm_stop_multithreaded - Deallocate structures necessary to make LLVM
32 /// safe for multithreading.
33 /// THIS MUST EXECUTE IN ISOLATION FROM ALL OTHER LLVM API CALLS.
34 void llvm_stop_multithreaded();
35
36 /// llvm_is_multithreaded - Check whether LLVM is executing in thread-safe
37 /// mode or not.
24 /// llvm_is_multithreaded() - returns true if LLVM is compiled with support
25 /// for multiple threads, and false otherwise.
3826 bool llvm_is_multithreaded();
3927
4028 /// llvm_execute_on_thread - Execute the given \p UserFn on a separate
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() {
2323 void (*Deleter)(void*)) const {
2424 assert(Creator);
2525 if (llvm_is_multithreaded()) {
26 llvm::MutexGuard Lock(llvm::llvm_get_global_lock());
26 llvm::MutexGuard Lock(llvm_get_global_lock());
2727
2828 if (!Ptr) {
2929 void* tmp = Creator();
7575 void llvm::llvm_shutdown() {
7676 while (StaticList)
7777 StaticList->destroy();
78
79 if (llvm_is_multithreaded()) llvm_stop_multithreaded();
8078 }
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This file implements llvm_start_multithreaded() and friends.
9 // This file implements helper functions for running LLVM in a multi-threaded
10 // environment.
1011 //
1112 //===----------------------------------------------------------------------===//
1213
1819
1920 using namespace llvm;
2021
21 static bool multithreaded_mode = false;
22
2322 sys::Mutex& llvm::llvm_get_global_lock() {
2423 static sys::Mutex global_lock;
2524 return global_lock;
2625 }
2726
28 bool llvm::llvm_start_multithreaded() {
27 bool llvm::llvm_is_multithreaded() {
2928 #if LLVM_ENABLE_THREADS != 0
30 assert(!multithreaded_mode && "Already multithreaded!");
31 multithreaded_mode = true;
32
33 // We fence here to ensure that all initialization is complete BEFORE we
34 // return from llvm_start_multithreaded().
35 sys::MemoryFence();
3629 return true;
3730 #else
3831 return false;
3932 #endif
40 }
41
42 void llvm::llvm_stop_multithreaded() {
43 #if LLVM_ENABLE_THREADS != 0
44 assert(multithreaded_mode && "Not currently multithreaded!");
45
46 // We fence here to insure that all threaded operations are complete BEFORE we
47 // return from llvm_stop_multithreaded().
48 sys::MemoryFence();
49
50 multithreaded_mode = false;
51 #endif
52 }
53
54 bool llvm::llvm_is_multithreaded() {
55 return multithreaded_mode;
5633 }
5734
5835 #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)
1717 #include "llvm/Support/FileSystem.h"
1818 #include "llvm/Support/Format.h"
1919 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/support/MutexGuard.h"
20 #include "llvm/Support/MutexGuard.h"
2121 #include "llvm/Support/Process.h"
2222 #include "llvm/Support/raw_ostream.h"
2323 using namespace llvm;
4646 void *p1 = test1::allocate_stack(a1);
4747 void *p2 = test1::allocate_stack(a2);
4848
49 llvm_start_multithreaded();
5049 pthread_t t1, t2;
5150 pthread_create(&t1, &a1, test1::helper, nullptr);
5251 pthread_create(&t2, &a2, test1::helper, nullptr);
5453 pthread_join(t2, nullptr);
5554 free(p1);
5655 free(p2);
57 llvm_stop_multithreaded();
5856 }
5957 #endif
6058