Fix PR8441, a thread unsafe static variable in our dynamic library loading facilities.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@118463 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/System/DynamicLibrary.cpp b/lib/System/DynamicLibrary.cpp
index 660db49..3da50a2 100644
--- a/lib/System/DynamicLibrary.cpp
+++ b/lib/System/DynamicLibrary.cpp
@@ -15,6 +15,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/System/DynamicLibrary.h"
+#include "llvm/System/Mutex.h"
 #include "llvm/Config/config.h"
 #include <cstdio>
 #include <cstring>
@@ -60,6 +61,7 @@
 //===          independent code.
 //===----------------------------------------------------------------------===//
 
+static SmartMutex<true> HandlesMutex;
 static std::vector<void *> *OpenedHandles = 0;
 
 
@@ -76,6 +78,7 @@
   if (Filename == NULL)
     H = RTLD_DEFAULT;
 #endif
+  SmartScopedLock<true> Lock(HandlesMutex);
   if (OpenedHandles == 0)
     OpenedHandles = new std::vector<void *>();
   OpenedHandles->push_back(H);
@@ -110,6 +113,7 @@
 
 #if HAVE_DLFCN_H
   // Now search the libraries.
+  SmartScopedLock<true> Lock(HandlesMutex);
   if (OpenedHandles) {
     for (std::vector<void *>::iterator I = OpenedHandles->begin(),
          E = OpenedHandles->end(); I != E; ++I) {