修复多线程搜索崩溃问题

This commit is contained in:
hewenfei 2023-07-10 19:18:48 +08:00
parent d461d53d01
commit 53b4d3e894
2 changed files with 17 additions and 8 deletions

View File

@ -41,6 +41,7 @@ SearchTaskPluginManager::SearchTaskPluginManager(QObject *parent) : QObject(pare
bool SearchTaskPluginManager::startSearch(const QUuid &uuid, SearchProperty::SearchType searchType, const QString &customType) bool SearchTaskPluginManager::startSearch(const QUuid &uuid, SearchProperty::SearchType searchType, const QString &customType)
{ {
QMutexLocker locker(&m_mutex);
if (m_managedPlugins.contains(uuid)) { if (m_managedPlugins.contains(uuid)) {
ManagedPlugin* managedPlugin = m_managedPlugins.value(uuid); ManagedPlugin* managedPlugin = m_managedPlugins.value(uuid);
@ -94,6 +95,7 @@ SearchTaskPluginIface *SearchTaskPluginManager::getPlugin(SearchProperty::Search
void SearchTaskPluginManager::registerPluginPath(const QString& customType, const QString &pluginPath) void SearchTaskPluginManager::registerPluginPath(const QString& customType, const QString &pluginPath)
{ {
QMutexLocker locker(&m_mutex);
if (!m_loadedPluginPath.contains(customType)) { if (!m_loadedPluginPath.contains(customType)) {
m_loadedPluginPath.insert(customType, pluginPath); m_loadedPluginPath.insert(customType, pluginPath);
} }
@ -101,6 +103,7 @@ void SearchTaskPluginManager::registerPluginPath(const QString& customType, cons
bool SearchTaskPluginManager::isSearching(const QUuid &uuid, SearchProperty::SearchType searchType, const QString &customType) bool SearchTaskPluginManager::isSearching(const QUuid &uuid, SearchProperty::SearchType searchType, const QString &customType)
{ {
QMutexLocker locker(&m_mutex);
if (m_managedPlugins.contains(uuid)) { if (m_managedPlugins.contains(uuid)) {
ManagedPlugin* managedPlugin = m_managedPlugins.value(uuid); ManagedPlugin* managedPlugin = m_managedPlugins.value(uuid);
@ -121,6 +124,7 @@ bool SearchTaskPluginManager::isSearching(const QUuid &uuid, SearchProperty::Sea
SearchTaskPluginIface *SearchTaskPluginManager::initPlugins(const QUuid& uuid, SearchProperty::SearchType searchType, const QString& customType) SearchTaskPluginIface *SearchTaskPluginManager::initPlugins(const QUuid& uuid, SearchProperty::SearchType searchType, const QString& customType)
{ {
QMutexLocker locker(&m_mutex);
if (!m_managedPlugins.contains(uuid)) { if (!m_managedPlugins.contains(uuid)) {
m_managedPlugins.insert(uuid, new ManagedPlugin(nullptr)); m_managedPlugins.insert(uuid, new ManagedPlugin(nullptr));
} }
@ -138,12 +142,8 @@ SearchTaskPluginIface *SearchTaskPluginManager::initPlugins(const QUuid& uuid, S
void SearchTaskPluginManager::destroyPlugins(const QUuid &uuid) void SearchTaskPluginManager::destroyPlugins(const QUuid &uuid)
{ {
if (m_managedPlugins.contains(uuid)) { QMutexLocker locker(&m_mutex);
ManagedPlugin *managedPlugin = m_managedPlugins.value(uuid); delete m_managedPlugins.take(uuid);
m_managedPlugins.remove(uuid);
delete managedPlugin;
}
} }
ManagedPlugin::~ManagedPlugin() ManagedPlugin::~ManagedPlugin()
@ -154,6 +154,7 @@ ManagedPlugin::~ManagedPlugin()
bool ManagedPlugin::insertInternalPlugin(const SearchProperty::SearchType &searchType, SearchTaskPluginIface *plugin) bool ManagedPlugin::insertInternalPlugin(const SearchProperty::SearchType &searchType, SearchTaskPluginIface *plugin)
{ {
QMutexLocker locker(&m_mutex);
if (plugin) { if (plugin) {
auto type = static_cast<size_t>(searchType); auto type = static_cast<size_t>(searchType);
plugin->setParent(this); plugin->setParent(this);
@ -163,13 +164,14 @@ bool ManagedPlugin::insertInternalPlugin(const SearchProperty::SearchType &searc
return true; return true;
} }
plugin->deleteLater(); delete plugin;
} }
return false; return false;
} }
bool ManagedPlugin::insertExternalPlugin(const QString &customType, SearchTaskPluginIface *plugin) bool ManagedPlugin::insertExternalPlugin(const QString &customType, SearchTaskPluginIface *plugin)
{ {
QMutexLocker locker(&m_mutex);
if (plugin) { if (plugin) {
plugin->setParent(this); plugin->setParent(this);
if (!customType.isEmpty() && !m_externalPlugin.contains(customType)) { if (!customType.isEmpty() && !m_externalPlugin.contains(customType)) {
@ -177,7 +179,7 @@ bool ManagedPlugin::insertExternalPlugin(const QString &customType, SearchTaskPl
return true; return true;
} }
plugin->deleteLater(); delete plugin;
} }
return false; return false;
@ -185,6 +187,7 @@ bool ManagedPlugin::insertExternalPlugin(const QString &customType, SearchTaskPl
inline SearchTaskPluginIface *ManagedPlugin::internalPlugin(const SearchProperty::SearchType &searchType) inline SearchTaskPluginIface *ManagedPlugin::internalPlugin(const SearchProperty::SearchType &searchType)
{ {
QMutexLocker locker(&m_mutex);
auto type = static_cast<size_t>(searchType); auto type = static_cast<size_t>(searchType);
if (m_internalPlugins.contains(type)) { if (m_internalPlugins.contains(type)) {
return m_internalPlugins.value(type); return m_internalPlugins.value(type);
@ -194,6 +197,7 @@ inline SearchTaskPluginIface *ManagedPlugin::internalPlugin(const SearchProperty
inline SearchTaskPluginIface *ManagedPlugin::externalPlugin(const QString &customType) inline SearchTaskPluginIface *ManagedPlugin::externalPlugin(const QString &customType)
{ {
QMutexLocker locker(&m_mutex);
if (m_externalPlugin.contains(customType)) { if (m_externalPlugin.contains(customType)) {
return m_externalPlugin.value(customType); return m_externalPlugin.value(customType);
} }

View File

@ -23,6 +23,8 @@
#include <QObject> #include <QObject>
#include <QUuid> #include <QUuid>
#include <QMap> #include <QMap>
#include <QMutex>
#include <QMutexLocker>
#include "search-task-plugin-iface.h" #include "search-task-plugin-iface.h"
namespace UkuiSearch { namespace UkuiSearch {
@ -41,6 +43,7 @@ public:
bool insertExternalPlugin(const QString& customType, SearchTaskPluginIface* plugin); bool insertExternalPlugin(const QString& customType, SearchTaskPluginIface* plugin);
private: private:
QMutex m_mutex;
QMap<size_t, SearchTaskPluginIface*> m_internalPlugins; QMap<size_t, SearchTaskPluginIface*> m_internalPlugins;
QMap<QString, SearchTaskPluginIface*> m_externalPlugin; QMap<QString, SearchTaskPluginIface*> m_externalPlugin;
}; };
@ -65,6 +68,8 @@ private:
QHash<QString, SearchTaskPluginIface*> m_loadedPlugin; QHash<QString, SearchTaskPluginIface*> m_loadedPlugin;
QMap<QString, QString> m_loadedPluginPath; QMap<QString, QString> m_loadedPluginPath;
QMap<QUuid, ManagedPlugin*> m_managedPlugins; QMap<QUuid, ManagedPlugin*> m_managedPlugins;
QMutex m_mutex;
}; };
} }