]> granicus.if.org Git - clang/commitdiff
Teach libclang to enable multithreading in LLVM, since libclang clients are likely...
authorDouglas Gregor <dgregor@apple.com>
Fri, 24 Sep 2010 21:18:36 +0000 (21:18 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 24 Sep 2010 21:18:36 +0000 (21:18 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@114760 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Frontend/ASTUnit.cpp
tools/libclang/CIndex.cpp

index b6c4eaac5ff39764d8d7dd8d4b684dd20fccb5a7..bd7e712bc7890d5537d19ef0254f8b15051fac9b 100644 (file)
@@ -84,6 +84,9 @@ ASTUnit::~ASTUnit() {
 
   ClearCachedCompletionResults();
   
+  if (TimerGroup)
+    TimerGroup->printAll(llvm::errs());
+
   for (unsigned I = 0, N = Timers.size(); I != N; ++I)
     delete Timers[I];
 }
index d2d00571c1d8f824ffbb9fbf5cc747a45249704a..554edd824ef0b5e3d5d48e5017021baac81d2370 100644 (file)
 #include "llvm/Support/CrashRecoveryContext.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Timer.h"
+#include "llvm/System/Mutex.h"
 #include "llvm/System/Program.h"
 #include "llvm/System/Signals.h"
+#include "llvm/System/Threading.h"
 
 // Needed to define L_TMPNAM on some systems.
 #include <cstdio>
@@ -1910,6 +1912,9 @@ bool CursorVisitor::VisitAttributes(Decl *D) {
   return false;
 }
 
+static llvm::sys::Mutex EnableMultithreadingMutex;
+static bool EnabledMultithreading;
+
 extern "C" {
 CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
                           int displayDiagnostics) {
@@ -1917,6 +1922,15 @@ CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
   // enable it.
   llvm::CrashRecoveryContext::Enable();
 
+  // Enable support for multithreading in LLVM.
+  {
+    llvm::sys::ScopedLock L(EnableMultithreadingMutex);
+    if (!EnabledMultithreading) {
+      llvm::llvm_start_multithreaded();
+      EnabledMultithreading = true;
+    }
+  }
+
   CIndexer *CIdxr = new CIndexer();
   if (excludeDeclarationsFromPCH)
     CIdxr->setOnlyLocalDecls();
@@ -1928,8 +1942,6 @@ CXIndex clang_createIndex(int excludeDeclarationsFromPCH,
 void clang_disposeIndex(CXIndex CIdx) {
   if (CIdx)
     delete static_cast<CIndexer *>(CIdx);
-  if (getenv("LIBCLANG_TIMING"))
-    llvm::TimerGroup::printAll(llvm::errs());
 }
 
 void clang_setUseExternalASTGeneration(CXIndex CIdx, int value) {