ManagedStatic: remove from PluginLoader

Differential Revision: https://reviews.llvm.org/D129123
This commit is contained in:
Nicolai Hähnle 2022-06-29 16:14:55 +02:00
parent 48c401a60e
commit 2fe3589acd
1 changed files with 23 additions and 11 deletions

View File

@ -13,34 +13,46 @@
#define DONT_GET_PLUGIN_LOADER_OPTION
#include "llvm/Support/PluginLoader.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/raw_ostream.h"
#include <vector>
using namespace llvm;
static ManagedStatic<std::vector<std::string> > Plugins;
static ManagedStatic<sys::SmartMutex<true> > PluginsLock;
namespace {
struct Plugins {
sys::SmartMutex<true> Lock;
std::vector<std::string> List;
};
Plugins &getPlugins() {
static Plugins P;
return P;
}
} // anonymous namespace
void PluginLoader::operator=(const std::string &Filename) {
sys::SmartScopedLock<true> Lock(*PluginsLock);
auto &P = getPlugins();
sys::SmartScopedLock<true> Lock(P.Lock);
std::string Error;
if (sys::DynamicLibrary::LoadLibraryPermanently(Filename.c_str(), &Error)) {
errs() << "Error opening '" << Filename << "': " << Error
<< "\n -load request ignored.\n";
} else {
Plugins->push_back(Filename);
P.List.push_back(Filename);
}
}
unsigned PluginLoader::getNumPlugins() {
sys::SmartScopedLock<true> Lock(*PluginsLock);
return Plugins.isConstructed() ? Plugins->size() : 0;
auto &P = getPlugins();
sys::SmartScopedLock<true> Lock(P.Lock);
return P.List.size();
}
std::string &PluginLoader::getPlugin(unsigned num) {
sys::SmartScopedLock<true> Lock(*PluginsLock);
assert(Plugins.isConstructed() && num < Plugins->size() &&
"Asking for an out of bounds plugin");
return (*Plugins)[num];
auto &P = getPlugins();
sys::SmartScopedLock<true> Lock(P.Lock);
assert(num < P.List.size() && "Asking for an out of bounds plugin");
return P.List[num];
}