[cfe-commits] r153562 - in /cfe/trunk: include/clang-c/Index.h tools/libclang/CIndex.cpp tools/libclang/CIndexCodeCompletion.cpp tools/libclang/CIndexer.h tools/libclang/CXTranslationUnit.h tools/libclang/Indexing.cpp tools/libclang/libclang.exports

Argyrios Kyrtzidis akyrtzi at gmail.com
Tue Mar 27 19:18:05 PDT 2012


Author: akirtzidis
Date: Tue Mar 27 21:18:05 2012
New Revision: 153562

URL: http://llvm.org/viewvc/llvm-project?rev=153562&view=rev
Log:
[libclang] Introduce options to control the priority for the threads
that libclang creates.

-Introduce CXGlobalOptFlags enum for the new options that can be
 set on the CXIndex object.

-CXGlobalOpt_ThreadBackgroundPriorityForIndexing affects:
  clang_indexSourceFile
  clang_indexTranslationUnit
  clang_parseTranslationUnit
  clang_saveTranslationUnit

-CXGlobalOpt_ThreadBackgroundPriorityForEditing affects:
  clang_reparseTranslationUnit
  clang_codeCompleteAt
  clang_annotateTokens

rdar://9075282

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/tools/libclang/CIndex.cpp
    cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp
    cfe/trunk/tools/libclang/CIndexer.h
    cfe/trunk/tools/libclang/CXTranslationUnit.h
    cfe/trunk/tools/libclang/Indexing.cpp
    cfe/trunk/tools/libclang/libclang.exports

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=153562&r1=153561&r2=153562&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Tue Mar 27 21:18:05 2012
@@ -204,6 +204,61 @@
  */
 CINDEX_LINKAGE void clang_disposeIndex(CXIndex index);
 
+typedef enum {
+  /**
+   * \brief Used to indicate that no special CXIndex options are needed.
+   */
+  CXGlobalOpt_None = 0x0,
+
+  /**
+   * \brief Used to indicate that threads that libclang creates for indexing
+   * purposes should use background priority.
+   * Affects \see clang_indexSourceFile, \see clang_indexTranslationUnit,
+   * \see clang_parseTranslationUnit, \see clang_saveTranslationUnit.
+   */
+  CXGlobalOpt_ThreadBackgroundPriorityForIndexing = 0x1,
+
+  /**
+   * \brief Used to indicate that threads that libclang creates for editing
+   * purposes should use background priority.
+   * Affects \see clang_reparseTranslationUnit, \see clang_codeCompleteAt,
+   * \see clang_annotateTokens
+   */
+  CXGlobalOpt_ThreadBackgroundPriorityForEditing = 0x2,
+
+  /**
+   * \brief Used to indicate that all threads that libclang creates should use
+   * background priority.
+   */
+  CXGlobalOpt_ThreadBackgroundPriorityForAll =
+      CXGlobalOpt_ThreadBackgroundPriorityForIndexing |
+      CXGlobalOpt_ThreadBackgroundPriorityForEditing
+
+} CXGlobalOptFlags;
+
+/**
+ * \brief Sets general options associated with a CXIndex. 
+ *
+ * For example:
+ * \code
+ * CXIndex idx = ...;
+ * clang_CXIndex_setGlobalOptions(idx,
+ *     clang_CXIndex_getGlobalOptions(idx) |
+ *     CXGlobalOpt_ThreadBackgroundPriorityForIndexing);
+ * \endcode
+ *
+ * \param options A bitmask of options, a bitwise OR of CXGlobalOpt_XXX flags.
+ */
+CINDEX_LINKAGE void clang_CXIndex_setGlobalOptions(CXIndex, unsigned options);
+
+/**
+ * \brief Gets the general options associated with a CXIndex.
+ *
+ * \returns A bitmask of options, a bitwise OR of CXGlobalOpt_XXX flags that
+ * are associated with the given CXIndex object.
+ */
+CINDEX_LINKAGE unsigned clang_CXIndex_getGlobalOptions(CXIndex);
+
 /**
  * \defgroup CINDEX_FILES File manipulation routines
  *

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=153562&r1=153561&r2=153562&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Tue Mar 27 21:18:05 2012
@@ -52,10 +52,11 @@
 using namespace clang::cxstring;
 using namespace clang::cxtu;
 
-CXTranslationUnit cxtu::MakeCXTranslationUnit(ASTUnit *TU) {
+CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, ASTUnit *TU) {
   if (!TU)
     return 0;
   CXTranslationUnit D = new CXTranslationUnitImpl();
+  D->CIdx = CIdx;
   D->TUData = TU;
   D->StringPool = createCXStringPool();
   D->Diagnostics = 0;
@@ -2411,6 +2412,14 @@
     CIdxr->setOnlyLocalDecls();
   if (displayDiagnostics)
     CIdxr->setDisplayDiagnostics();
+
+  if (getenv("LIBCLANG_BGPRIO_INDEX"))
+    CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() |
+                               CXGlobalOpt_ThreadBackgroundPriorityForIndexing);
+  if (getenv("LIBCLANG_BGPRIO_EDIT"))
+    CIdxr->setCXGlobalOptFlags(CIdxr->getCXGlobalOptFlags() |
+                               CXGlobalOpt_ThreadBackgroundPriorityForEditing);
+
   return CIdxr;
 }
 
@@ -2419,6 +2428,17 @@
     delete static_cast<CIndexer *>(CIdx);
 }
 
+void clang_CXIndex_setGlobalOptions(CXIndex CIdx, unsigned options) {
+  if (CIdx)
+    static_cast<CIndexer *>(CIdx)->setCXGlobalOptFlags(options);
+}
+
+unsigned clang_CXIndex_getGlobalOptions(CXIndex CIdx) {
+  if (CIdx)
+    return static_cast<CIndexer *>(CIdx)->getCXGlobalOptFlags();
+  return 0;
+}
+
 void clang_toggleCrashRecovery(unsigned isEnabled) {
   if (isEnabled)
     llvm::CrashRecoveryContext::Enable();
@@ -2441,7 +2461,7 @@
                                   0, 0,
                                   /*CaptureDiagnostics=*/true,
                                   /*AllowPCHWithCompilerErrors=*/true);
-  return MakeCXTranslationUnit(TU);
+  return MakeCXTranslationUnit(CXXIdx, TU);
 }
 
 unsigned clang_defaultEditingTranslationUnitOptions() {
@@ -2490,6 +2510,9 @@
 
   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
 
+  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
+    setBackGroundPriority();
+
   bool PrecompilePreamble = options & CXTranslationUnit_PrecompiledPreamble;
   // FIXME: Add a flag for modules.
   TranslationUnitKind TUKind
@@ -2601,7 +2624,7 @@
     }
   }
 
-  PTUI->result = MakeCXTranslationUnit(Unit.take());
+  PTUI->result = MakeCXTranslationUnit(CXXIdx, Unit.take());
 }
 CXTranslationUnit clang_parseTranslationUnit(CXIndex CIdx,
                                              const char *source_filename,
@@ -2663,6 +2686,10 @@
   SaveTranslationUnitInfo *STUI =
     static_cast<SaveTranslationUnitInfo*>(UserData);
 
+  CIndexer *CXXIdx = (CIndexer*)STUI->TU->CIdx;
+  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
+    setBackGroundPriority();
+
   STUI->result = static_cast<ASTUnit *>(STUI->TU->TUData)->Save(STUI->FileName);
 }
 
@@ -2750,6 +2777,10 @@
   if (!TU)
     return;
 
+  CIndexer *CXXIdx = (CIndexer*)TU->CIdx;
+  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
+    setBackGroundPriority();
+
   ASTUnit *CXXUnit = static_cast<ASTUnit *>(TU->TUData);
   ASTUnit::ConcurrencyCheck Check(*CXXUnit);
   
@@ -5133,6 +5164,10 @@
   const unsigned NumTokens = ((clang_annotateTokens_Data*)UserData)->NumTokens;
   CXCursor *Cursors = ((clang_annotateTokens_Data*)UserData)->Cursors;
 
+  CIndexer *CXXIdx = (CIndexer*)TU->CIdx;
+  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
+    setBackGroundPriority();
+
   // Determine the region of interest, which contains all of the tokens.
   SourceRange RegionOfInterest;
   RegionOfInterest.setBegin(
@@ -5704,6 +5739,13 @@
   SafetyStackThreadSize = Value;
 }
 
+void clang::setBackGroundPriority() {
+  // FIXME: Move to llvm/Support and make it cross-platform.
+#ifdef __APPLE__
+  setpriority(PRIO_DARWIN_THREAD, 0, PRIO_DARWIN_BG);
+#endif
+}
+
 }
 
 extern "C" {

Modified: cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp?rev=153562&r1=153561&r2=153562&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp Tue Mar 27 21:18:05 2012
@@ -666,6 +666,10 @@
   if (!AST)
     return;
 
+  CIndexer *CXXIdx = (CIndexer*)TU->CIdx;
+  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForEditing))
+    setBackGroundPriority();
+
   ASTUnit::ConcurrencyCheck Check(*AST);
 
   // Perform the remapping of source files.

Modified: cfe/trunk/tools/libclang/CIndexer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexer.h?rev=153562&r1=153561&r2=153562&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexer.h (original)
+++ cfe/trunk/tools/libclang/CIndexer.h Tue Mar 27 21:18:05 2012
@@ -29,12 +29,14 @@
 class CIndexer {
   bool OnlyLocalDecls;
   bool DisplayDiagnostics;
+  unsigned Options; // CXGlobalOptFlags.
 
   llvm::sys::Path ResourcesPath;
   std::string WorkingDir;
 
 public:
- CIndexer() : OnlyLocalDecls(false), DisplayDiagnostics(false) { }
+ CIndexer() : OnlyLocalDecls(false), DisplayDiagnostics(false),
+              Options(CXGlobalOpt_None) { }
   
   /// \brief Whether we only want to see "local" declarations (that did not
   /// come from a previous precompiled header). If false, we want to see all
@@ -47,6 +49,13 @@
     DisplayDiagnostics = Display;
   }
 
+  unsigned getCXGlobalOptFlags() const { return Options; }
+  void setCXGlobalOptFlags(unsigned options) { Options = options; }
+
+  bool isOptEnabled(CXGlobalOptFlags opt) const {
+    return Options & ~unsigned(opt);
+  }
+
   /// \brief Get the path of the clang resource files.
   std::string getClangResourcesPath();
 
@@ -79,6 +88,10 @@
   bool RunSafely(llvm::CrashRecoveryContext &CRC,
                  void (*Fn)(void*), void *UserData, unsigned Size = 0);
 
+  /// \brief Set the thread priority to background.
+  /// FIXME: Move to llvm/Support.
+  void setBackGroundPriority();
+
   /// \brief Print libclang's resource usage to standard error.
   void PrintLibclangResourceUsage(CXTranslationUnit TU);
 }

Modified: cfe/trunk/tools/libclang/CXTranslationUnit.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXTranslationUnit.h?rev=153562&r1=153561&r2=153562&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXTranslationUnit.h (original)
+++ cfe/trunk/tools/libclang/CXTranslationUnit.h Tue Mar 27 21:18:05 2012
@@ -16,6 +16,7 @@
 
 extern "C" {
 struct CXTranslationUnitImpl {
+  void *CIdx;
   void *TUData;
   void *StringPool;
   void *Diagnostics;
@@ -24,10 +25,11 @@
 
 namespace clang {
   class ASTUnit;
+  class CIndexer;
 
 namespace cxtu {
 
-CXTranslationUnitImpl *MakeCXTranslationUnit(ASTUnit *TU);
+CXTranslationUnitImpl *MakeCXTranslationUnit(CIndexer *CIdx, ASTUnit *TU);
   
 class CXTUOwner {
   CXTranslationUnitImpl *TU;

Modified: cfe/trunk/tools/libclang/Indexing.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/Indexing.cpp?rev=153562&r1=153561&r2=153562&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/Indexing.cpp (original)
+++ cfe/trunk/tools/libclang/Indexing.cpp Tue Mar 27 21:18:05 2012
@@ -275,6 +275,9 @@
 
   CIndexer *CXXIdx = static_cast<CIndexer *>(CIdx);
 
+  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
+    setBackGroundPriority();
+
   CaptureDiagnosticConsumer *CaptureDiag = new CaptureDiagnosticConsumer();
 
   // Configure the diagnostics.
@@ -351,7 +354,7 @@
 
   ASTUnit *Unit = ASTUnit::create(CInvok.getPtr(), Diags,
                                   /*CaptureDiagnostics=*/true);
-  OwningPtr<CXTUOwner> CXTU(new CXTUOwner(MakeCXTranslationUnit(Unit)));
+  OwningPtr<CXTUOwner> CXTU(new CXTUOwner(MakeCXTranslationUnit(CXXIdx, Unit)));
 
   // Recover resources if we crash before exiting this method.
   llvm::CrashRecoveryContextCleanupRegistrar<CXTUOwner>
@@ -502,6 +505,10 @@
   if (!client_index_callbacks || index_callbacks_size == 0)
     return;
 
+  CIndexer *CXXIdx = (CIndexer*)TU->CIdx;
+  if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
+    setBackGroundPriority();
+
   IndexerCallbacks CB;
   memset(&CB, 0, sizeof(CB));
   unsigned ClientCBSize = index_callbacks_size < sizeof(CB)

Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=153562&r1=153561&r2=153562&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Tue Mar 27 21:18:05 2012
@@ -1,5 +1,7 @@
 clang_CXCursorSet_contains
 clang_CXCursorSet_insert
+clang_CXIndex_getGlobalOptions
+clang_CXIndex_setGlobalOptions
 clang_CXXMethod_isStatic
 clang_CXXMethod_isVirtual
 clang_Cursor_getTranslationUnit





More information about the cfe-commits mailing list