summaryrefslogtreecommitdiffstats
path: root/src/corelib/plugin/qlibrary.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/plugin/qlibrary.cpp')
-rw-r--r--src/corelib/plugin/qlibrary.cpp83
1 files changed, 61 insertions, 22 deletions
diff --git a/src/corelib/plugin/qlibrary.cpp b/src/corelib/plugin/qlibrary.cpp
index 406a83c7dd..47f09e58a2 100644
--- a/src/corelib/plugin/qlibrary.cpp
+++ b/src/corelib/plugin/qlibrary.cpp
@@ -407,7 +407,7 @@ inline void QLibraryStore::cleanup()
QLibraryPrivate *lib = it.value();
if (lib->libraryRefCount.loadRelaxed() == 1) {
if (lib->libraryUnloadCount.loadRelaxed() > 0) {
- Q_ASSERT(lib->pHnd);
+ Q_ASSERT(lib->pHnd.loadRelaxed());
lib->libraryUnloadCount.storeRelaxed(1);
#ifdef __GLIBC__
// glibc has a bug in unloading from global destructors
@@ -498,8 +498,7 @@ inline void QLibraryStore::releaseLibrary(QLibraryPrivate *lib)
}
QLibraryPrivate::QLibraryPrivate(const QString &canonicalFileName, const QString &version, QLibrary::LoadHints loadHints)
- : pHnd(nullptr), fileName(canonicalFileName), fullVersion(version), instance(nullptr),
- libraryRefCount(0), libraryUnloadCount(0), pluginState(MightBeAPlugin)
+ : fileName(canonicalFileName), fullVersion(version), pluginState(MightBeAPlugin)
{
loadHintsInt.storeRelaxed(loadHints);
if (canonicalFileName.isEmpty())
@@ -519,7 +518,7 @@ QLibraryPrivate::~QLibraryPrivate()
void QLibraryPrivate::mergeLoadHints(QLibrary::LoadHints lh)
{
// if the library is already loaded, we can't change the load hints
- if (pHnd)
+ if (pHnd.loadRelaxed())
return;
loadHintsInt.storeRelaxed(lh);
@@ -527,7 +526,7 @@ void QLibraryPrivate::mergeLoadHints(QLibrary::LoadHints lh)
QFunctionPointer QLibraryPrivate::resolve(const char *symbol)
{
- if (!pHnd)
+ if (!pHnd.loadRelaxed())
return nullptr;
return resolve_sys(symbol);
}
@@ -539,9 +538,36 @@ void QLibraryPrivate::setLoadHints(QLibrary::LoadHints lh)
mergeLoadHints(lh);
}
+QObject *QLibraryPrivate::pluginInstance()
+{
+ // first, check if the instance is cached and hasn't been deleted
+ QObject *obj = (QMutexLocker(&mutex), inst.data());
+ if (obj)
+ return obj;
+
+ // We need to call the plugin's factory function. Is that cached?
+ // skip increasing the reference count (why? -Thiago)
+ QtPluginInstanceFunction factory = instanceFactory.loadAcquire();
+ if (!factory)
+ factory = loadPlugin();
+
+ if (!factory)
+ return nullptr;
+
+ obj = factory();
+
+ // cache again
+ QMutexLocker locker(&mutex);
+ if (inst)
+ obj = inst;
+ else
+ inst = obj;
+ return obj;
+}
+
bool QLibraryPrivate::load()
{
- if (pHnd) {
+ if (pHnd.loadRelaxed()) {
libraryUnloadCount.ref();
return true;
}
@@ -550,7 +576,9 @@ bool QLibraryPrivate::load()
Q_TRACE(QLibraryPrivate_load_entry, fileName);
+ mutex.lock();
bool ret = load_sys();
+ mutex.unlock();
if (qt_debug_component()) {
if (ret) {
qDebug() << "loaded library" << fileName;
@@ -573,9 +601,10 @@ bool QLibraryPrivate::load()
bool QLibraryPrivate::unload(UnloadFlag flag)
{
- if (!pHnd)
+ if (!pHnd.loadRelaxed())
return false;
if (libraryUnloadCount.loadRelaxed() > 0 && !libraryUnloadCount.deref()) { // only unload if ALL QLibrary instance wanted to
+ QMutexLocker locker(&mutex);
delete inst.data();
if (flag == NoUnloadSys || unload_sys()) {
if (qt_debug_component())
@@ -584,12 +613,13 @@ bool QLibraryPrivate::unload(UnloadFlag flag)
//when the library is unloaded, we release the reference on it so that 'this'
//can get deleted
libraryRefCount.deref();
- pHnd = nullptr;
- instance = nullptr;
+ pHnd.storeRelaxed(nullptr);
+ instanceFactory.storeRelaxed(nullptr);
+ return true;
}
}
- return (pHnd == nullptr);
+ return false;
}
void QLibraryPrivate::release()
@@ -597,22 +627,23 @@ void QLibraryPrivate::release()
QLibraryStore::releaseLibrary(this);
}
-bool QLibraryPrivate::loadPlugin()
+QtPluginInstanceFunction QLibraryPrivate::loadPlugin()
{
- if (instance) {
+ if (auto ptr = instanceFactory.loadAcquire()) {
libraryUnloadCount.ref();
- return true;
+ return ptr;
}
if (pluginState == IsNotAPlugin)
- return false;
+ return nullptr;
if (load()) {
- instance = (QtPluginInstanceFunction)resolve("qt_plugin_instance");
- return instance;
+ auto ptr = reinterpret_cast<QtPluginInstanceFunction>(resolve("qt_plugin_instance"));
+ instanceFactory.storeRelease(ptr); // two threads may store the same value
+ return ptr;
}
if (qt_debug_component())
qWarning() << "QLibraryPrivate::loadPlugin failed on" << fileName << ":" << errorString;
pluginState = IsNotAPlugin;
- return false;
+ return nullptr;
}
/*!
@@ -719,6 +750,7 @@ bool QLibraryPrivate::isPlugin()
void QLibraryPrivate::updatePluginState()
{
+ QMutexLocker locker(&mutex);
errorString.clear();
if (pluginState != MightBeAPlugin)
return;
@@ -739,7 +771,7 @@ void QLibraryPrivate::updatePluginState()
}
#endif
- if (!pHnd) {
+ if (!pHnd.loadRelaxed()) {
// scan for the plugin metadata without loading
success = findPatternUnloaded(fileName, this);
} else {
@@ -803,7 +835,7 @@ bool QLibrary::load()
if (!d)
return false;
if (did_load)
- return d->pHnd;
+ return d->pHnd.loadRelaxed();
did_load = true;
return d->load();
}
@@ -839,7 +871,7 @@ bool QLibrary::unload()
*/
bool QLibrary::isLoaded() const
{
- return d && d->pHnd;
+ return d && d->pHnd.loadRelaxed();
}
@@ -950,8 +982,10 @@ void QLibrary::setFileName(const QString &fileName)
QString QLibrary::fileName() const
{
- if (d)
+ if (d) {
+ QMutexLocker locker(&d->mutex);
return d->qualifiedFileName.isEmpty() ? d->fileName : d->qualifiedFileName;
+ }
return QString();
}
@@ -1092,7 +1126,12 @@ QFunctionPointer QLibrary::resolve(const QString &fileName, const QString &versi
*/
QString QLibrary::errorString() const
{
- return (!d || d->errorString.isEmpty()) ? tr("Unknown error") : d->errorString;
+ QString str;
+ if (d) {
+ QMutexLocker locker(&d->mutex);
+ str = d->errorString;
+ }
+ return str.isEmpty() ? tr("Unknown error") : str;
}
/*!