llvm.org GIT mirror llvm / 39ea80c
Re-apply r211287: Remove support for LLVM runtime multi-threading. I'll fix the problems in libclang and other projects in ways that don't require <mutex> until we sort out the cygwin situation. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211900 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 5 years ago
7 changed file(s) with 17 addition(s) and 99 deletion(s). Raw diff Collapse all Expand all
21692169 using the resultant compiler to build a copy of LLVM with multithreading
21702170 support.
21712171
2172 .. _startmultithreaded:
2173
2174 Entering and Exiting Multithreaded Mode
2175 ---------------------------------------
2176
2177 In order to properly protect its internal data structures while avoiding
2178 excessive locking overhead in the single-threaded case, the LLVM must intialize
2179 certain data structures necessary to provide guards around its internals. To do
2180 so, the client program must invoke ``llvm_start_multithreaded()`` before making
2181 any concurrent LLVM API calls. To subsequently tear down these structures, use
2182 the ``llvm_stop_multithreaded()`` call. You can also use the
2183 ``llvm_is_multithreaded()`` call to check the status of multithreaded mode.
2184
2185 Note that both of these calls must be made *in isolation*. That is to say that
2186 no other LLVM API calls may be executing at any time during the execution of
2187 ``llvm_start_multithreaded()`` or ``llvm_stop_multithreaded``. It is the
2188 client's responsibility to enforce this isolation.
2189
2190 The return value of ``llvm_start_multithreaded()`` indicates the success or
2191 failure of the initialization. Failure typically indicates that your copy of
2192 LLVM was built without multithreading support, typically because GCC atomic
2193 intrinsics were not found in your system compiler. In this case, the LLVM API
2194 will not be safe for concurrent calls. However, it *will* be safe for hosting
2195 threaded applications in the JIT, though :ref:`care must be taken
2196 ` to ensure that side exits and the like do not accidentally
2197 result in concurrent LLVM API calls.
2198
21992172 .. _shutdown:
22002173
22012174 Ending Execution with ``llvm_shutdown()``
22022175 -----------------------------------------
22032176
22042177 When you are done using the LLVM APIs, you should call ``llvm_shutdown()`` to
2205 deallocate memory used for internal structures. This will also invoke
2206 ``llvm_stop_multithreaded()`` if LLVM is operating in multithreaded mode. As
2207 such, ``llvm_shutdown()`` requires the same isolation guarantees as
2208 ``llvm_stop_multithreaded()``.
2209
2210 Note that, if you use scope-based shutdown, you can use the
2211 ``llvm_shutdown_obj`` class, which calls ``llvm_shutdown()`` in its destructor.
2178 deallocate memory used for internal structures.
22122179
22132180 .. _managedstatic:
22142181
22162183 ------------------------------------------
22172184
22182185 ``ManagedStatic`` is a utility class in LLVM used to implement static
2219 initialization of static resources, such as the global type tables. Before the
2220 invocation of ``llvm_shutdown()``, it implements a simple lazy initialization
2221 scheme. Once ``llvm_start_multithreaded()`` returns, however, it uses
2186 initialization of static resources, such as the global type tables. In a
2187 single-threaded environment, it implements a simple lazy initialization scheme.
2188 When LLVM is compiled with support for multi-threading, however, it uses
22222189 double-checked locking to implement thread-safe lazy initialization.
2223
2224 Note that, because no other threads are allowed to issue LLVM API calls before
2225 ``llvm_start_multithreaded()`` returns, it is possible to have
2226 ``ManagedStatic``\ s of ``llvm::sys::Mutex``\ s.
22272190
22282191 .. _llvmcontext:
22292192
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 declares helper functions for running LLVM in a multi-threaded
10 // environment.
1011 //
1112 //===----------------------------------------------------------------------===//
1213
1415 #define LLVM_SUPPORT_THREADING_H
1516
1617 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();
25
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();
30
31 /// llvm_is_multithreaded - Check whether LLVM is executing in thread-safe
32 /// mode or not.
18 /// Returns true if LLVM is compiled with support for multi-threading, and
19 /// false otherwise.
3320 bool llvm_is_multithreaded();
3421
3522 /// 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.
27072707 /*===-- Threading ------------------------------------------------------===*/
27082708
27092709 LLVMBool LLVMStartMultithreaded() {
2710 return llvm_start_multithreaded();
2710 return LLVMIsMultithreaded();
27112711 }
27122712
27132713 void LLVMStopMultithreaded() {
2714 llvm_stop_multithreaded();
27152714 }
27162715
27172716 LLVMBool LLVMIsMultithreaded() {
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This file implements llvm_start_multithreaded() and friends.
9 // This file defines 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
23 bool llvm::llvm_start_multithreaded() {
22 bool llvm::llvm_is_multithreaded() {
2423 #if LLVM_ENABLE_THREADS != 0
25 assert(!multithreaded_mode && "Already multithreaded!");
26 multithreaded_mode = true;
27
28 // We fence here to ensure that all initialization is complete BEFORE we
29 // return from llvm_start_multithreaded().
30 sys::MemoryFence();
3124 return true;
3225 #else
3326 return false;
3427 #endif
35 }
36
37 void llvm::llvm_stop_multithreaded() {
38 #if LLVM_ENABLE_THREADS != 0
39 assert(multithreaded_mode && "Not currently multithreaded!");
40
41 // We fence here to insure that all threaded operations are complete BEFORE we
42 // return from llvm_stop_multithreaded().
43 sys::MemoryFence();
44
45 multithreaded_mode = false;
46 #endif
47 }
48
49 bool llvm::llvm_is_multithreaded() {
50 return multithreaded_mode;
5128 }
5229
5330 #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)
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