llvm.org GIT mirror llvm / cdde33e
Revert r211287, "Remove support for LLVM runtime multi-threading." libclang still requires it on cygming, lack of incomplete <mutex>. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211592 91177308-0d34-0410-b5e6-96231b3b80d8 NAKAMURA Takumi 5 years ago
7 changed file(s) with 99 addition(s) and 17 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
21722199 .. _shutdown:
21732200
21742201 Ending Execution with ``llvm_shutdown()``
21752202 -----------------------------------------
21762203
21772204 When you are done using the LLVM APIs, you should call ``llvm_shutdown()`` to
2178 deallocate memory used for internal structures.
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.
21792212
21802213 .. _managedstatic:
21812214
21832216 ------------------------------------------
21842217
21852218 ``ManagedStatic`` is a utility class in LLVM used to implement static
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
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
21892222 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.
21902227
21912228 .. _llvmcontext:
21922229
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
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This file declares helper functions for running LLVM in a multi-threaded
10 // environment.
9 // TThis file defines llvm_start_multithreaded() and friends.
1110 //
1211 //===----------------------------------------------------------------------===//
1312
1514 #define LLVM_SUPPORT_THREADING_H
1615
1716 namespace llvm {
18 /// Returns true if LLVM is compiled with support for multi-threading, and
19 /// false otherwise.
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.
2033 bool llvm_is_multithreaded();
2134
2235 /// llvm_execute_on_thread - Execute the given \p UserFn on a separate
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.
27072707 /*===-- Threading ------------------------------------------------------===*/
27082708
27092709 LLVMBool LLVMStartMultithreaded() {
2710 return LLVMIsMultithreaded();
2710 return llvm_start_multithreaded();
27112711 }
27122712
27132713 void LLVMStopMultithreaded() {
2714 llvm_stop_multithreaded();
27142715 }
27152716
27162717 LLVMBool LLVMIsMultithreaded() {
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This file defines helper functions for running LLVM in a multi-threaded
10 // environment.
9 // This file implements llvm_start_multithreaded() and friends.
1110 //
1211 //===----------------------------------------------------------------------===//
1312
1918
2019 using namespace llvm;
2120
22 bool llvm::llvm_is_multithreaded() {
21 static bool multithreaded_mode = false;
22
23 bool llvm::llvm_start_multithreaded() {
2324 #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();
2431 return true;
2532 #else
2633 return false;
2734 #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;
2851 }
2952
3053 #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();
4950 pthread_t t1, t2;
5051 pthread_create(&t1, &a1, test1::helper, nullptr);
5152 pthread_create(&t2, &a2, test1::helper, nullptr);
5354 pthread_join(t2, nullptr);
5455 free(p1);
5556 free(p2);
57 llvm_stop_multithreaded();
5658 }
5759 #endif
5860