[PATCH] [clang] Change llvm::sys::Mutex implementation to use STL-provided mutexes.

Zachary Turner zturner at google.com
Thu Jun 5 13:56:22 PDT 2014


During an atexit handler, std::mutex cannot be used.  This revision addresses this by changing to a mutex which is safe for use during shutdown.

http://reviews.llvm.org/D3961

Files:
  lib/Frontend/ASTUnit.cpp
  tools/libclang/CIndex.cpp
  tools/libclang/Indexing.cpp

Index: lib/Frontend/ASTUnit.cpp
===================================================================
--- lib/Frontend/ASTUnit.cpp
+++ lib/Frontend/ASTUnit.cpp
@@ -99,8 +99,8 @@
   };
 }
 
-static llvm::sys::SmartMutex<false> &getOnDiskMutex() {
-  static llvm::sys::SmartMutex<false> M(/* recursive = */ true);
+static llvm::sys::MutexBase &getOnDiskMutex() {
+  static llvm::sys::AtexitSafeMutex M;
   return M;
 }
 
@@ -2893,20 +2893,20 @@
 
 #ifndef NDEBUG
 ASTUnit::ConcurrencyState::ConcurrencyState() {
-  Mutex = new llvm::sys::MutexImpl(/*recursive=*/true);
+  Mutex = new llvm::sys::RecursiveMutex;
 }
 
 ASTUnit::ConcurrencyState::~ConcurrencyState() {
-  delete static_cast<llvm::sys::MutexImpl *>(Mutex);
+  delete static_cast<llvm::sys::RecursiveMutex *>(Mutex);
 }
 
 void ASTUnit::ConcurrencyState::start() {
-  bool acquired = static_cast<llvm::sys::MutexImpl *>(Mutex)->tryacquire();
+  bool acquired = static_cast<llvm::sys::RecursiveMutex *>(Mutex)->tryacquire();
   assert(acquired && "Concurrent access to ASTUnit!");
 }
 
 void ASTUnit::ConcurrencyState::finish() {
-  static_cast<llvm::sys::MutexImpl *>(Mutex)->release();
+  static_cast<llvm::sys::RecursiveMutex *>(Mutex)->release();
 }
 
 #else // NDEBUG
Index: tools/libclang/CIndex.cpp
===================================================================
--- tools/libclang/CIndex.cpp
+++ tools/libclang/CIndex.cpp
@@ -45,6 +45,7 @@
 #include "llvm/Support/Format.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Mutex.h"
+#include "llvm/Support/MutexGuard.h"
 #include "llvm/Support/Program.h"
 #include "llvm/Support/SaveAndRestore.h"
 #include "llvm/Support/Signals.h"
@@ -2556,7 +2557,7 @@
 // Misc. API hooks.
 //===----------------------------------------------------------------------===//               
 
-static llvm::sys::Mutex EnableMultithreadingMutex;
+static llvm::sys::RecursiveMutex EnableMultithreadingMutex;
 static bool EnabledMultithreading;
 
 static void fatal_error_handler(void *user_data, const std::string& reason,
@@ -2577,7 +2578,7 @@
 
   // Enable support for multithreading in LLVM.
   {
-    llvm::sys::ScopedLock L(EnableMultithreadingMutex);
+    llvm::MutexGuard L(EnableMultithreadingMutex);
     if (!EnabledMultithreading) {
       llvm::install_fatal_error_handler(fatal_error_handler, 0);
       llvm::llvm_start_multithreaded();
@@ -6958,7 +6959,7 @@
 cxindex::Logger::~Logger() {
   LogOS.flush();
 
-  llvm::sys::ScopedLock L(EnableMultithreadingMutex);
+  llvm::MutexGuard L(EnableMultithreadingMutex);
 
   static llvm::TimeRecord sBeginTR = llvm::TimeRecord::getCurrentTime();
 
Index: tools/libclang/Indexing.cpp
===================================================================
--- tools/libclang/Indexing.cpp
+++ tools/libclang/Indexing.cpp
@@ -146,7 +146,7 @@
 namespace {
 
 class SessionSkipBodyData {
-  llvm::sys::Mutex Mux;
+  llvm::sys::RecursiveMutex Mux;
   PPRegionSetTy ParsedRegions;
 
 public:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3961.10156.patch
Type: text/x-patch
Size: 2953 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140605/ed6b1d9e/attachment.bin>


More information about the cfe-commits mailing list