summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2014-06-17 19:57:15 +0000
committerZachary Turner <zturner@google.com>2014-06-17 19:57:15 +0000
commite15a4d01d20b94111073eac67155979620ff98f8 (patch)
tree5e2044b715e8ebf754bc3171b7ee334f70b51d39 /tools
parent7d2009b4cd8dfa273b4247d479b8642eb03d93ea (diff)
downloadclang-e15a4d01d20b94111073eac67155979620ff98f8.tar.gz
clang-e15a4d01d20b94111073eac67155979620ff98f8.tar.bz2
clang-e15a4d01d20b94111073eac67155979620ff98f8.tar.xz
Change libclang initialization to use std::call_once instead of
hand rolled once-initialization, and rename the mutex to be more descriptive of its actual purpose. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@211121 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r--tools/libclang/CIndex.cpp23
1 files changed, 11 insertions, 12 deletions
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index 962e3a3406..cbe7cf5346 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -51,6 +51,7 @@
#include "llvm/Support/Threading.h"
#include "llvm/Support/Timer.h"
#include "llvm/Support/raw_ostream.h"
+#include <mutex>
#ifdef __APPLE__
#include <pthread.h>
@@ -2559,8 +2560,8 @@ buildPieces(unsigned NameFlags, bool IsMemberRefExpr,
// Misc. API hooks.
//===----------------------------------------------------------------------===//
-static llvm::sys::Mutex EnableMultithreadingMutex;
-static bool EnabledMultithreading;
+static llvm::sys::Mutex LoggingMutex;
+static std::once_flag LibclangGlobalInitFlag;
static void fatal_error_handler(void *user_data, const std::string& reason,
bool gen_crash_diag) {
@@ -2570,6 +2571,12 @@ static void fatal_error_handler(void *user_data, const std::string& reason,
::abort();
}
+static void initializeLibClang() {
+ // Install our error handler, and make sure multi-threading is enabled.
+ llvm::llvm_start_multithreaded();
+ llvm::install_fatal_error_handler(fatal_error_handler, nullptr);
+}
+
extern "C" {
CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
int displayDiagnostics) {
@@ -2578,15 +2585,7 @@ CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
if (!getenv("LIBCLANG_DISABLE_CRASH_RECOVERY"))
llvm::CrashRecoveryContext::Enable();
- // Enable support for multithreading in LLVM.
- {
- llvm::sys::ScopedLock L(EnableMultithreadingMutex);
- if (!EnabledMultithreading) {
- llvm::install_fatal_error_handler(fatal_error_handler, nullptr);
- llvm::llvm_start_multithreaded();
- EnabledMultithreading = true;
- }
- }
+ std::call_once(LibclangGlobalInitFlag, initializeLibClang);
CIndexer *CIdxr = new CIndexer();
if (excludeDeclarationsFromPCH)
@@ -6962,7 +6961,7 @@ Logger &cxindex::Logger::operator<<(const llvm::format_object_base &Fmt) {
cxindex::Logger::~Logger() {
LogOS.flush();
- llvm::sys::ScopedLock L(EnableMultithreadingMutex);
+ llvm::sys::ScopedLock L(LoggingMutex);
static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime();