From b4b9b3b24c03f414c54f55e1b34b4d9124c55883 Mon Sep 17 00:00:00 2001 From: Filip Pizlo Date: Wed, 18 Sep 2013 16:40:14 +0000 Subject: [PATCH] Make DynamicLibrary use ManagedStatic. This is pretty simple and should just work as advertised - but it does have the caveat that calls to DynamicLibrary::AddSymbol will "reset" if you shutdown llvm and try to come back for seconds. This is a subtle behavior change, but I'm assuming that nobody is affected by it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190946 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/DynamicLibrary.cpp | 32 ++++++-------------------- lib/Support/Windows/DynamicLibrary.inc | 6 ++--- 2 files changed, 10 insertions(+), 28 deletions(-) diff --git a/lib/Support/DynamicLibrary.cpp b/lib/Support/DynamicLibrary.cpp index f14cb45d9dc..c063f5382d1 100644 --- a/lib/Support/DynamicLibrary.cpp +++ b/lib/Support/DynamicLibrary.cpp @@ -14,6 +14,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/DynamicLibrary.h" +#include "llvm/Support/ManagedStatic.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/StringMap.h" #include "llvm/Config/config.h" @@ -22,31 +23,12 @@ #include // Collection of symbol name/value pairs to be searched prior to any libraries. -static llvm::StringMap *ExplicitSymbols = 0; - -namespace { - -struct ExplicitSymbolsDeleter { - ~ExplicitSymbolsDeleter() { - delete ExplicitSymbols; - } -}; - -} - -static ExplicitSymbolsDeleter Dummy; - - -static llvm::sys::SmartMutex& getMutex() { - static llvm::sys::SmartMutex HandlesMutex; - return HandlesMutex; -} +static llvm::ManagedStatic > ExplicitSymbols; +static llvm::ManagedStatic > SymbolsMutex; void llvm::sys::DynamicLibrary::AddSymbol(StringRef symbolName, void *symbolValue) { - SmartScopedLock lock(getMutex()); - if (ExplicitSymbols == 0) - ExplicitSymbols = new StringMap(); + SmartScopedLock lock(*SymbolsMutex); (*ExplicitSymbols)[symbolName] = symbolValue; } @@ -72,7 +54,7 @@ static DenseSet *OpenedHandles = 0; DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, std::string *errMsg) { - SmartScopedLock lock(getMutex()); + SmartScopedLock lock(*SymbolsMutex); void *handle = dlopen(filename, RTLD_LAZY|RTLD_GLOBAL); if (handle == 0) { @@ -126,10 +108,10 @@ void *SearchForAddressOfSpecialSymbol(const char* symbolName); } void* DynamicLibrary::SearchForAddressOfSymbol(const char *symbolName) { - SmartScopedLock Lock(getMutex()); + SmartScopedLock Lock(*SymbolsMutex); // First check symbols added via AddSymbol(). - if (ExplicitSymbols) { + if (ExplicitSymbols.isConstructed()) { StringMap::iterator i = ExplicitSymbols->find(symbolName); if (i != ExplicitSymbols->end()) diff --git a/lib/Support/Windows/DynamicLibrary.inc b/lib/Support/Windows/DynamicLibrary.inc index 83da82a949c..2edaf74fa56 100644 --- a/lib/Support/Windows/DynamicLibrary.inc +++ b/lib/Support/Windows/DynamicLibrary.inc @@ -71,7 +71,7 @@ extern "C" { DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, std::string *errMsg) { - SmartScopedLock lock(getMutex()); + SmartScopedLock lock(*SymbolsMutex); if (!filename) { // When no file is specified, enumerate all DLLs and EXEs in the process. @@ -114,10 +114,10 @@ DynamicLibrary DynamicLibrary::getPermanentLibrary(const char *filename, #undef EXPLICIT_SYMBOL2 void* DynamicLibrary::SearchForAddressOfSymbol(const char* symbolName) { - SmartScopedLock Lock(getMutex()); + SmartScopedLock Lock(*SymbolsMutex); // First check symbols added via AddSymbol(). - if (ExplicitSymbols) { + if (ExplicitSymbols.isConstructed()) { StringMap::iterator i = ExplicitSymbols->find(symbolName); if (i != ExplicitSymbols->end()) -- 2.34.1