chore(cpn): delay loading libsims (#5778)
This commit is contained in:
@@ -30,11 +30,7 @@
|
|||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef SIMULATOR_INTERFACE_LOADER_METHOD
|
QMap<QString, QPair<QString, QLibrary *>> SimulatorLoader::registeredSimulators;
|
||||||
#define SIMULATOR_INTERFACE_LOADER_DYNAMIC 1 // How to load simulator libraries: 1=dynamic load and unload; 0=load once (old way)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
QMap<QString, QLibrary *> SimulatorLoader::registeredSimulators;
|
|
||||||
|
|
||||||
QStringList SimulatorLoader::getAvailableSimulators()
|
QStringList SimulatorLoader::getAvailableSimulators()
|
||||||
{
|
{
|
||||||
@@ -45,42 +41,26 @@ int SimulatorLoader::registerSimulators(const QDir & dir)
|
|||||||
{
|
{
|
||||||
QStringList filters;
|
QStringList filters;
|
||||||
#if defined(__APPLE__)
|
#if defined(__APPLE__)
|
||||||
filters << "*-simulator.dylib";
|
filters << "libedgetx-*-simulator.dylib";
|
||||||
#elif defined(WIN32) || defined(__CYGWIN__)
|
#elif defined(WIN32) || defined(__CYGWIN__)
|
||||||
filters << "*-simulator.dll";
|
filters << "libedgetx-*-simulator.dll";
|
||||||
#else
|
#else
|
||||||
filters << "*-simulator.so";
|
filters << "libedgetx-*-simulator.so";
|
||||||
#endif
|
#endif
|
||||||
registeredSimulators.clear();
|
registeredSimulators.clear();
|
||||||
|
|
||||||
qCDebug(simulatorInterfaceLoader) << "Searching for simulators in" << dir.path() << "matching pattern" << filters;
|
qCDebug(simulatorInterfaceLoader) << "Searching for simulators in" << dir.path() << "matching pattern" << filters;
|
||||||
|
|
||||||
foreach(QString filename, dir.entryList(filters, QDir::Files)) {
|
foreach(QString filename, dir.entryList(filters, QDir::Files)) {
|
||||||
QLibrary * lib = new QLibrary( dir.path() + "/" + filename);
|
QString simuName(filename.mid(3, filename.lastIndexOf('-') - 3));
|
||||||
|
QString libPath(dir.path() + "/" + filename);
|
||||||
|
|
||||||
qCDebug(simulatorInterfaceLoader) << "Trying to register simulator in " << filename;
|
if (getAvailableSimulators().contains(simuName))
|
||||||
|
continue;
|
||||||
SimulatorFactory * factory;
|
|
||||||
RegisterSimulator registerFunc = (RegisterSimulator)lib->resolve("registerSimu");
|
|
||||||
|
|
||||||
if (registerFunc && (factory = registerFunc())) {
|
|
||||||
if (getAvailableSimulators().contains(factory->name()))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
lib->setProperty("instances_used", 0);
|
|
||||||
registeredSimulators.insert(factory->name(), lib);
|
|
||||||
delete factory;
|
|
||||||
#if SIMULATOR_INTERFACE_LOADER_DYNAMIC
|
|
||||||
lib->unload();
|
|
||||||
#endif
|
|
||||||
qCDebug(simulatorInterfaceLoader) << "Registered" << registeredSimulators.lastKey() << "simulator in " << lib->fileName() << "and unloaded:" << !lib->isLoaded();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
qWarning() << "Library error" << lib->fileName() << lib->errorString();
|
|
||||||
delete lib;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
registeredSimulators.insert(simuName, {libPath, nullptr});
|
||||||
}
|
}
|
||||||
|
|
||||||
qCDebug(simulatorInterfaceLoader) << "Found libraries:" << (registeredSimulators.size() ? registeredSimulators.keys() : QStringList() << "none");
|
qCDebug(simulatorInterfaceLoader) << "Found libraries:" << (registeredSimulators.size() ? registeredSimulators.keys() : QStringList() << "none");
|
||||||
return registeredSimulators.size();
|
return registeredSimulators.size();
|
||||||
}
|
}
|
||||||
@@ -106,8 +86,10 @@ void SimulatorLoader::registerSimulators()
|
|||||||
|
|
||||||
void SimulatorLoader::unregisterSimulators()
|
void SimulatorLoader::unregisterSimulators()
|
||||||
{
|
{
|
||||||
foreach(QLibrary * lib, registeredSimulators)
|
for(QPair<QString, QLibrary *> lib : registeredSimulators) {
|
||||||
delete lib;
|
if (lib.second)
|
||||||
|
delete lib.second;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SimulatorLoader::findSimulatorByName(const QString & name)
|
QString SimulatorLoader::findSimulatorByName(const QString & name)
|
||||||
@@ -133,21 +115,28 @@ QString SimulatorLoader::findSimulatorByName(const QString & name)
|
|||||||
|
|
||||||
SimulatorInterface * SimulatorLoader::loadSimulator(const QString & name)
|
SimulatorInterface * SimulatorLoader::loadSimulator(const QString & name)
|
||||||
{
|
{
|
||||||
SimulatorInterface * si = NULL;
|
SimulatorInterface * si = nullptr;
|
||||||
QString libname = findSimulatorByName(name);
|
QString simuName = findSimulatorByName(name);
|
||||||
|
|
||||||
if (libname.isEmpty()) {
|
if (simuName.isEmpty()) {
|
||||||
qWarning() << "Simulator" << name << "not found.";
|
qWarning() << "Simulator" << name << "not found.";
|
||||||
return si;
|
return si;
|
||||||
}
|
}
|
||||||
|
|
||||||
QLibrary * lib = registeredSimulators.value(libname, NULL);
|
QPair<QString, QLibrary *> libInfo = registeredSimulators.value(simuName, {QString(), nullptr});
|
||||||
if (!lib) {
|
QString libPath = libInfo.first;
|
||||||
qWarning() << "Simulator library is NULL";
|
QLibrary *lib = libInfo.second;
|
||||||
return si;
|
qCDebug(simulatorInterfaceLoader) << "Trying to load simulator in " << libPath;
|
||||||
}
|
|
||||||
|
|
||||||
qCDebug(simulatorInterfaceLoader) << "Trying to load simulator in " << lib->fileName();
|
if (!lib) {
|
||||||
|
lib = new QLibrary(libPath);
|
||||||
|
if (lib)
|
||||||
|
registeredSimulators.insert(simuName, {libPath, lib});
|
||||||
|
else {
|
||||||
|
qWarning() << "Unable to load library";
|
||||||
|
return si;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SimulatorFactory * factory;
|
SimulatorFactory * factory;
|
||||||
RegisterSimulator registerFunc = (RegisterSimulator)lib->resolve("registerSimu");
|
RegisterSimulator registerFunc = (RegisterSimulator)lib->resolve("registerSimu");
|
||||||
@@ -158,20 +147,21 @@ SimulatorInterface * SimulatorLoader::loadSimulator(const QString & name)
|
|||||||
delete factory;
|
delete factory;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
qWarning() << "Library error" << lib->fileName() << lib->errorString();
|
qWarning() << "Library error" << libPath << lib->errorString();
|
||||||
}
|
}
|
||||||
|
|
||||||
return si;
|
return si;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SimulatorLoader::unloadSimulator(const QString & name)
|
bool SimulatorLoader::unloadSimulator(const QString & name)
|
||||||
{
|
{
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
#if SIMULATOR_INTERFACE_LOADER_DYNAMIC
|
|
||||||
QString simuName = findSimulatorByName(name);
|
QString simuName = findSimulatorByName(name);
|
||||||
|
|
||||||
if (simuName.isEmpty())
|
if (simuName.isEmpty())
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
QLibrary * lib = registeredSimulators.value(simuName, NULL);
|
QLibrary * lib = registeredSimulators.value(simuName).second;
|
||||||
|
|
||||||
if (lib && lib->isLoaded()) {
|
if (lib && lib->isLoaded()) {
|
||||||
quint8 instance = lib->property("instances_used").toUInt();
|
quint8 instance = lib->property("instances_used").toUInt();
|
||||||
@@ -188,9 +178,6 @@ bool SimulatorLoader::unloadSimulator(const QString & name)
|
|||||||
else {
|
else {
|
||||||
qCDebug(simulatorInterfaceLoader) << "Simulator library for " << simuName << "already unloaded.";
|
qCDebug(simulatorInterfaceLoader) << "Simulator library for " << simuName << "already unloaded.";
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
qCDebug(simulatorInterfaceLoader) << "Keeping simulator library" << simuName << "loaded.";
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -218,5 +218,5 @@ class SimulatorLoader
|
|||||||
typedef SimulatorFactory * (*RegisterSimulator)();
|
typedef SimulatorFactory * (*RegisterSimulator)();
|
||||||
|
|
||||||
static int registerSimulators(const QDir & dir);
|
static int registerSimulators(const QDir & dir);
|
||||||
static QMap<QString, QLibrary *> registeredSimulators;
|
static QMap<QString, QPair<QString, QLibrary *>> registeredSimulators;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user