From 53b4d3e894d48f3d68262cdeb84290a03d854ba7 Mon Sep 17 00:00:00 2001 From: hewenfei Date: Mon, 10 Jul 2023 19:18:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=B4=A9=E6=BA=83=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../search-task-plugin-manager.cpp | 20 +++++++++++-------- .../pluginmanage/search-task-plugin-manager.h | 5 +++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/libsearch/pluginmanage/search-task-plugin-manager.cpp b/libsearch/pluginmanage/search-task-plugin-manager.cpp index d29d620..f7d7583 100644 --- a/libsearch/pluginmanage/search-task-plugin-manager.cpp +++ b/libsearch/pluginmanage/search-task-plugin-manager.cpp @@ -41,6 +41,7 @@ SearchTaskPluginManager::SearchTaskPluginManager(QObject *parent) : QObject(pare bool SearchTaskPluginManager::startSearch(const QUuid &uuid, SearchProperty::SearchType searchType, const QString &customType) { + QMutexLocker locker(&m_mutex); if (m_managedPlugins.contains(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) { + QMutexLocker locker(&m_mutex); if (!m_loadedPluginPath.contains(customType)) { 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) { + QMutexLocker locker(&m_mutex); if (m_managedPlugins.contains(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) { + QMutexLocker locker(&m_mutex); if (!m_managedPlugins.contains(uuid)) { m_managedPlugins.insert(uuid, new ManagedPlugin(nullptr)); } @@ -138,12 +142,8 @@ SearchTaskPluginIface *SearchTaskPluginManager::initPlugins(const QUuid& uuid, S void SearchTaskPluginManager::destroyPlugins(const QUuid &uuid) { - if (m_managedPlugins.contains(uuid)) { - ManagedPlugin *managedPlugin = m_managedPlugins.value(uuid); - m_managedPlugins.remove(uuid); - - delete managedPlugin; - } + QMutexLocker locker(&m_mutex); + delete m_managedPlugins.take(uuid); } ManagedPlugin::~ManagedPlugin() @@ -154,6 +154,7 @@ ManagedPlugin::~ManagedPlugin() bool ManagedPlugin::insertInternalPlugin(const SearchProperty::SearchType &searchType, SearchTaskPluginIface *plugin) { + QMutexLocker locker(&m_mutex); if (plugin) { auto type = static_cast(searchType); plugin->setParent(this); @@ -163,13 +164,14 @@ bool ManagedPlugin::insertInternalPlugin(const SearchProperty::SearchType &searc return true; } - plugin->deleteLater(); + delete plugin; } return false; } bool ManagedPlugin::insertExternalPlugin(const QString &customType, SearchTaskPluginIface *plugin) { + QMutexLocker locker(&m_mutex); if (plugin) { plugin->setParent(this); if (!customType.isEmpty() && !m_externalPlugin.contains(customType)) { @@ -177,7 +179,7 @@ bool ManagedPlugin::insertExternalPlugin(const QString &customType, SearchTaskPl return true; } - plugin->deleteLater(); + delete plugin; } return false; @@ -185,6 +187,7 @@ bool ManagedPlugin::insertExternalPlugin(const QString &customType, SearchTaskPl inline SearchTaskPluginIface *ManagedPlugin::internalPlugin(const SearchProperty::SearchType &searchType) { + QMutexLocker locker(&m_mutex); auto type = static_cast(searchType); if (m_internalPlugins.contains(type)) { return m_internalPlugins.value(type); @@ -194,6 +197,7 @@ inline SearchTaskPluginIface *ManagedPlugin::internalPlugin(const SearchProperty inline SearchTaskPluginIface *ManagedPlugin::externalPlugin(const QString &customType) { + QMutexLocker locker(&m_mutex); if (m_externalPlugin.contains(customType)) { return m_externalPlugin.value(customType); } diff --git a/libsearch/pluginmanage/search-task-plugin-manager.h b/libsearch/pluginmanage/search-task-plugin-manager.h index a0f70fb..e5a9196 100644 --- a/libsearch/pluginmanage/search-task-plugin-manager.h +++ b/libsearch/pluginmanage/search-task-plugin-manager.h @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include "search-task-plugin-iface.h" namespace UkuiSearch { @@ -41,6 +43,7 @@ public: bool insertExternalPlugin(const QString& customType, SearchTaskPluginIface* plugin); private: + QMutex m_mutex; QMap m_internalPlugins; QMap m_externalPlugin; }; @@ -65,6 +68,8 @@ private: QHash m_loadedPlugin; QMap m_loadedPluginPath; QMap m_managedPlugins; + + QMutex m_mutex; }; }