From 7fe1278b1e2aaec2408bf053092f8f11ef4cbfdb Mon Sep 17 00:00:00 2001 From: Simeon Bird Date: Sat, 12 Jan 2013 10:37:26 -0500 Subject: [PATCH] We don't need the mutex guarding m_itemStates in the UpdateItemStatesThread, because m_itemStates is only accessed by the when the thread is done, and set before the thread starts. Also combine the setData function with the constructor. --- .../versioncontrol/updateitemstatesthread.cpp | 26 +++---------------- .../versioncontrol/updateitemstatesthread.h | 9 +++---- .../versioncontrol/versioncontrolobserver.cpp | 17 +++++------- 3 files changed, 14 insertions(+), 38 deletions(-) diff --git a/src/views/versioncontrol/updateitemstatesthread.cpp b/src/views/versioncontrol/updateitemstatesthread.cpp index e07d72c76..fa005f8f1 100644 --- a/src/views/versioncontrol/updateitemstatesthread.cpp +++ b/src/views/versioncontrol/updateitemstatesthread.cpp @@ -23,13 +23,13 @@ #include -UpdateItemStatesThread::UpdateItemStatesThread() : +UpdateItemStatesThread::UpdateItemStatesThread(KVersionControlPlugin* plugin, + const QList& itemStates) : QThread(), m_globalPluginMutex(0), - m_plugin(0), - m_itemMutex(), + m_plugin(plugin), m_retrievedItems(false), - m_itemStates() + m_itemStates(itemStates) { // Several threads may share one instance of a plugin. A global // mutex is required to serialize the retrieval of version control @@ -42,32 +42,16 @@ UpdateItemStatesThread::~UpdateItemStatesThread() { } -void UpdateItemStatesThread::setData(KVersionControlPlugin* plugin, - const QList& itemStates) -{ - // The locks are taken in the same order as in run() - // to avoid potential deadlock. - QMutexLocker pluginLocker(m_globalPluginMutex); - QMutexLocker itemLocker(&m_itemMutex); - - m_itemStates = itemStates; - m_plugin = plugin; -} - void UpdateItemStatesThread::run() { Q_ASSERT(!m_itemStates.isEmpty()); Q_ASSERT(m_plugin); - QMutexLocker itemLocker(&m_itemMutex); - const QString directory = m_itemStates.first().item.url().directory(KUrl::AppendTrailingSlash); m_retrievedItems = false; - itemLocker.unlock(); QMutexLocker pluginLocker(m_globalPluginMutex); if (m_plugin->beginRetrieval(directory)) { - itemLocker.relock(); const int count = m_itemStates.count(); KVersionControlPlugin2* pluginV2 = qobject_cast(m_plugin); @@ -99,13 +83,11 @@ void UpdateItemStatesThread::unlockPlugin() QList UpdateItemStatesThread::itemStates() const { - QMutexLocker locker(&m_itemMutex); return m_itemStates; } bool UpdateItemStatesThread::retrievedItems() const { - QMutexLocker locker(&m_itemMutex); return m_retrievedItems; } diff --git a/src/views/versioncontrol/updateitemstatesthread.h b/src/views/versioncontrol/updateitemstatesthread.h index f0f91d7d2..a28169755 100644 --- a/src/views/versioncontrol/updateitemstatesthread.h +++ b/src/views/versioncontrol/updateitemstatesthread.h @@ -38,9 +38,6 @@ class LIBDOLPHINPRIVATE_EXPORT UpdateItemStatesThread : public QThread Q_OBJECT public: - UpdateItemStatesThread(); - virtual ~UpdateItemStatesThread(); - /** * @param plugin Version control plugin that is used to update the * state of the items. Whenever the plugin is accessed @@ -49,8 +46,9 @@ public: * UpdateItemStatesThread::unlockPlugin() must be used. * @param itemStates List of items, where the states get updated. */ - void setData(KVersionControlPlugin* plugin, - const QList& itemStates); + UpdateItemStatesThread(KVersionControlPlugin* plugin, + const QList& itemStates); + virtual ~UpdateItemStatesThread(); /** * Whenever the plugin is accessed by the thread creator, lockPlugin() must @@ -76,7 +74,6 @@ private: QMutex* m_globalPluginMutex; // Protects the m_plugin globally KVersionControlPlugin* m_plugin; - mutable QMutex m_itemMutex; // Protects m_retrievedItems and m_itemStates bool m_retrievedItems; QList m_itemStates; }; diff --git a/src/views/versioncontrol/versioncontrolobserver.cpp b/src/views/versioncontrol/versioncontrolobserver.cpp index 64bc26867..f4dd877d3 100644 --- a/src/views/versioncontrol/versioncontrolobserver.cpp +++ b/src/views/versioncontrol/versioncontrolobserver.cpp @@ -238,20 +238,12 @@ void VersionControlObserver::slotThreadFinished() void VersionControlObserver::updateItemStates() { Q_ASSERT(m_plugin); - if (!m_updateItemStatesThread) { - m_updateItemStatesThread = new UpdateItemStatesThread(); - connect(m_updateItemStatesThread, SIGNAL(finished()), - this, SLOT(slotThreadFinished())); - connect(m_updateItemStatesThread, SIGNAL(finished()), - m_updateItemStatesThread, SLOT(deleteLater())); - } - else { + if (m_updateItemStatesThread) { // An update is currently ongoing. Wait until the thread has finished // the update (see slotThreadFinished()). m_pendingItemStatesUpdate = true; return; } - QList itemStates; const int itemCount = m_model->count(); itemStates.reserve(itemCount); @@ -269,7 +261,12 @@ void VersionControlObserver::updateItemStates() if (!m_silentUpdate) { emit infoMessage(i18nc("@info:status", "Updating version information...")); } - m_updateItemStatesThread->setData(m_plugin, itemStates); + m_updateItemStatesThread = new UpdateItemStatesThread(m_plugin, itemStates); + connect(m_updateItemStatesThread, SIGNAL(finished()), + this, SLOT(slotThreadFinished())); + connect(m_updateItemStatesThread, SIGNAL(finished()), + m_updateItemStatesThread, SLOT(deleteLater())); + m_updateItemStatesThread->start(); // slotThreadFinished() is called when finished } } -- 2.47.3