From: Ted Kremenek Date: Thu, 27 Oct 2011 19:44:25 +0000 (+0000) Subject: Add mutex for accessing ASTUnit's global OnDisk data. This may be an issue as libcla... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e055f8a1d8a33511394302d38af7ab0fbd2c35fa;p=clang Add mutex for accessing ASTUnit's global OnDisk data. This may be an issue as libclang could be processing multiple ASTUnit's at once. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@143138 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 282c93f047..a0cfee1303 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -46,6 +46,7 @@ #include "llvm/Support/Timer.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Mutex.h" +#include "llvm/Support/MutexGuard.h" #include "llvm/Support/CrashRecoveryContext.h" #include #include @@ -101,6 +102,11 @@ namespace { }; } +static llvm::sys::SmartMutex &getOnDiskMutex() { + static llvm::sys::SmartMutex M(/* recursive = */ true); + return M; +} + static void cleanupOnDiskMapAtExit(void); typedef llvm::DenseMap OnDiskDataMap; @@ -115,6 +121,7 @@ static OnDiskDataMap &getOnDiskDataMap() { } static void cleanupOnDiskMapAtExit(void) { + // No mutex required here since we are leaving the program. OnDiskDataMap &M = getOnDiskDataMap(); for (OnDiskDataMap::iterator I = M.begin(), E = M.end(); I != E; ++I) { // We don't worry about freeing the memory associated with OnDiskDataMap. @@ -124,6 +131,9 @@ static void cleanupOnDiskMapAtExit(void) { } static OnDiskData &getOnDiskData(const ASTUnit *AU) { + // We require the mutex since we are modifying the structure of the + // DenseMap. + llvm::MutexGuard Guard(getOnDiskMutex()); OnDiskDataMap &M = getOnDiskDataMap(); OnDiskData *&D = M[AU]; if (!D) @@ -136,6 +146,9 @@ static void erasePreambleFile(const ASTUnit *AU) { } static void removeOnDiskEntry(const ASTUnit *AU) { + // We require the mutex since we are modifying the structure of the + // DenseMap. + llvm::MutexGuard Guard(getOnDiskMutex()); OnDiskDataMap &M = getOnDiskDataMap(); OnDiskDataMap::iterator I = M.find(AU); if (I != M.end()) {