]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Use only one "directory contents counting" thread per process
authorFrank Reininghaus <frank78ac@googlemail.com>
Tue, 21 Jan 2014 18:07:58 +0000 (19:07 +0100)
committerFrank Reininghaus <frank78ac@googlemail.com>
Tue, 21 Jan 2014 18:08:07 +0000 (19:08 +0100)
The pointer to QThread object is stored in a global variable, and each
view increments/decrements a reference count when it starts/stops using
the thread. If this thread reaches zero, the thread is stopped.

Note that we cannot just use a smart pointer, like QSharedPointer, to
manage the thread, because we must make sure that the thread is not
running any more before the QThread is deleted.

REVIEW: 115064

src/kitemviews/private/kdirectorycontentscounter.cpp
src/kitemviews/private/kdirectorycontentscounter.h

index fd8479febc33f7a1f266d14481ae79df91b76735..65afb7c3e93e645f7020c37d9256b36909c255bd 100644 (file)
@@ -30,7 +30,6 @@ KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObj
     QObject(parent),
     m_model(model),
     m_queue(),
-    m_workerThread(0),
     m_worker(0),
     m_workerIsBusy(false),
     m_dirWatcher(0),
@@ -39,25 +38,34 @@ KDirectoryContentsCounter::KDirectoryContentsCounter(KFileItemModel* model, QObj
     connect(m_model, SIGNAL(itemsRemoved(KItemRangeList)),
             this,    SLOT(slotItemsRemoved()));
 
-    m_workerThread = new QThread(this);
+    if (!m_workerThread) {
+        m_workerThread = new QThread();
+        m_workerThread->start();
+    }
+
     m_worker = new KDirectoryContentsCounterWorker();
     m_worker->moveToThread(m_workerThread);
+    ++m_workersCount;
 
     connect(this,     SIGNAL(requestDirectoryContentsCount(QString,KDirectoryContentsCounterWorker::Options)),
             m_worker, SLOT(countDirectoryContents(QString,KDirectoryContentsCounterWorker::Options)));
     connect(m_worker, SIGNAL(result(QString,int)),
             this,     SLOT(slotResult(QString,int)));
 
-    m_workerThread->start();
-
     m_dirWatcher = new KDirWatch(this);
     connect(m_dirWatcher, SIGNAL(dirty(QString)), this, SLOT(slotDirWatchDirty(QString)));
 }
 
 KDirectoryContentsCounter::~KDirectoryContentsCounter()
 {
-    m_workerThread->quit();
-    m_workerThread->wait();
+    --m_workersCount;
+
+    if (m_workersCount == 0) {
+        m_workerThread->quit();
+        m_workerThread->wait();
+        delete m_workerThread;
+        m_workerThread = 0;
+    }
 
     delete m_worker;
 }
@@ -162,3 +170,6 @@ void KDirectoryContentsCounter::startWorker(const QString& path)
         m_workerIsBusy = true;
     }
 }
+
+QThread* KDirectoryContentsCounter::m_workerThread = 0;
+int KDirectoryContentsCounter::m_workersCount = 0;
\ No newline at end of file
index 425c3632aa39f4e447aed5aa64ccb7dbc7afc115..c03d0249c159196ee0a980862f26a7d24f3977e2 100644 (file)
@@ -78,7 +78,9 @@ private:
 
     QQueue<QString> m_queue;
 
-    QThread* m_workerThread;
+    static QThread* m_workerThread;
+    static int m_workersCount;
+
     KDirectoryContentsCounterWorker* m_worker;
     bool m_workerIsBusy;