llvm.org GIT mirror llvm / 9be5c8c
Users of the llvm global mutex must now acquire it manually. This allows the mutex to be acquired in a guarded, RAII fashion. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211066 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 6 years ago
5 changed file(s) with 16 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
1414 #define LLVM_SUPPORT_MUTEX_H
1515
1616 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Threading.h"
1817 #include
1918
2019 namespace llvm
2120 {
21 // Forward declare.
22 bool llvm_is_multithreaded();
23
2224 namespace sys
2325 {
2426 /// @brief Platform agnostic Mutex class.
1313 #ifndef LLVM_SUPPORT_THREADING_H
1414 #define LLVM_SUPPORT_THREADING_H
1515
16 #include "llvm/Support/Mutex.h"
17
1618 namespace llvm {
19 /// llvm_get_global_lock - returns the llvm global lock object.
20 sys::Mutex& llvm_get_global_lock();
21
1722 /// llvm_start_multithreaded - Allocate and initialize structures needed to
1823 /// make LLVM safe for multithreading. The return value indicates whether
1924 /// multithreaded initialization succeeded. LLVM will still be operational
3237 /// mode or not.
3338 bool llvm_is_multithreaded();
3439
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();
42
4340 /// llvm_execute_on_thread - Execute the given \p UserFn on a separate
4441 /// thread, passing it the provided \p UserData.
4542 ///
1313 #include "llvm/Support/ManagedStatic.h"
1414 #include "llvm/Config/config.h"
1515 #include "llvm/Support/Atomic.h"
16 #include "llvm/Support/MutexGuard.h"
1617 #include
1718 using namespace llvm;
1819
2223 void (*Deleter)(void*)) const {
2324 assert(Creator);
2425 if (llvm_is_multithreaded()) {
25 llvm_acquire_global_lock();
26 llvm::MutexGuard Lock(llvm::llvm_get_global_lock());
2627
2728 if (!Ptr) {
2829 void* tmp = Creator();
4243 Next = StaticList;
4344 StaticList = this;
4445 }
45
46 llvm_release_global_lock();
4746 } else {
4847 assert(!Ptr && !DeleterFn && !Next &&
4948 "Partially initialized ManagedStatic!?");
2020
2121 static bool multithreaded_mode = false;
2222
23 static sys::Mutex* global_lock = nullptr;
23 sys::Mutex& llvm::llvm_get_global_lock() {
24 static sys::Mutex global_lock;
25 return global_lock;
26 }
2427
2528 bool llvm::llvm_start_multithreaded() {
2629 #if LLVM_ENABLE_THREADS != 0
2730 assert(!multithreaded_mode && "Already multithreaded!");
2831 multithreaded_mode = true;
29 global_lock = new sys::Mutex(true);
3032
3133 // We fence here to ensure that all initialization is complete BEFORE we
3234 // return from llvm_start_multithreaded().
4648 sys::MemoryFence();
4749
4850 multithreaded_mode = false;
49 delete global_lock;
5051 #endif
5152 }
5253
5354 bool llvm::llvm_is_multithreaded() {
5455 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();
6356 }
6457
6558 #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/Mutex.h"
20 #include "llvm/support/MutexGuard.h"
2121 #include "llvm/Support/Process.h"
2222 #include "llvm/Support/raw_ostream.h"
2323 using namespace llvm;
8383 sys::MemoryFence();
8484 if (tmp) return tmp;
8585
86 llvm_acquire_global_lock();
86 llvm::MutexGuard 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 }