From cea4068700ae99f134443c4eee598d4b5fbbdc87 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Thu, 18 Nov 2010 18:49:05 +0000 Subject: [PATCH] Use thread-safe statics to avoid a static constructor here. This isn't thread-safe on MSVC, but we don't support threaded LLVM there anyways. llvm-svn: 119718 --- lib/System/DynamicLibrary.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/System/DynamicLibrary.cpp b/lib/System/DynamicLibrary.cpp index 3da50a28b65..33f86334e38 100644 --- a/lib/System/DynamicLibrary.cpp +++ b/lib/System/DynamicLibrary.cpp @@ -61,9 +61,19 @@ using namespace llvm::sys; //=== independent code. //===----------------------------------------------------------------------===// -static SmartMutex HandlesMutex; +static SmartMutex* HandlesMutex; static std::vector *OpenedHandles = 0; +static bool InitializeMutex() { + HandlesMutex = new SmartMutex; + return HandlesMutex != 0; +} + +static bool EnsureMutexInitialized() { + static bool result = InitializeMutex(); + return result; +} + bool DynamicLibrary::LoadLibraryPermanently(const char *Filename, std::string *ErrMsg) { @@ -78,7 +88,8 @@ bool DynamicLibrary::LoadLibraryPermanently(const char *Filename, if (Filename == NULL) H = RTLD_DEFAULT; #endif - SmartScopedLock Lock(HandlesMutex); + EnsureMutexInitialized(); + SmartScopedLock Lock(*HandlesMutex); if (OpenedHandles == 0) OpenedHandles = new std::vector(); OpenedHandles->push_back(H); @@ -113,7 +124,8 @@ void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { #if HAVE_DLFCN_H // Now search the libraries. - SmartScopedLock Lock(HandlesMutex); + EnsureMutexInitialized(); + SmartScopedLock Lock(*HandlesMutex); if (OpenedHandles) { for (std::vector::iterator I = OpenedHandles->begin(), E = OpenedHandles->end(); I != E; ++I) {