From: Enar Väikene <enar@vaikene.net>
Date: Mon, 28 Nov 2011 11:33:36 +0000 (+0200)
Subject: Improved canceling and terminating the worker thread.
X-Git-Url: https://vaikene.ee/gitweb/pswgen11.html?a=commitdiff_plain;h=5fe70cdf799cf7063df26fea9036c560cfa20ff4;p=evaf

Improved canceling and terminating the worker thread.
* The search is now also canceled when the thread is busy reading a large file;
* Fixed a deadlock situation where the thread was waiting on the wait condition, but there was nobody to wake it up.
---

diff --git a/src/apps/FileFinder/Engine/engine.cpp b/src/apps/FileFinder/Engine/engine.cpp
index b9fae0e..8ed0f7c 100644
--- a/src/apps/FileFinder/Engine/engine.cpp
+++ b/src/apps/FileFinder/Engine/engine.cpp
@@ -235,6 +235,13 @@ Internal::Worker::Worker(QObject * parent)
     , mBusy(false)
 {
     setObjectName(QString("%1.Worker").arg(VER_MODULE_NAME_STR));
+
+    EVAF_INFO("%s created", qPrintable(objectName()));
+}
+
+Internal::Worker::~Worker()
+{
+    EVAF_INFO("%s destroyed", qPrintable(objectName()));
 }
 
 void Internal::Worker::cancel()
@@ -278,6 +285,9 @@ void Internal::Worker::run()
     forever {
         QMutexLocker lock(&mLock);
 
+        if (mDoTerminate)
+            break;
+
         mSomethingToDo.wait(&mLock);
 
         if (mDoTerminate)
@@ -304,8 +314,10 @@ void Internal::Worker::run()
 
             lock.relock();
             mBusy = false;
+            bool c = mDoCancel;
+            lock.unlock();
 
-            emit finished(mDoCancel);
+            emit finished(c);
         }
     }
 }
@@ -325,7 +337,7 @@ void Internal::Worker::recursiveSearch(QString const & path)
         {
             QMutexLocker l(&mLock);
             if (mDoCancel)
-                break;
+                return;
         }
 
         // Check for the file name to match the include filter and not the exclude filter
@@ -355,6 +367,13 @@ void Internal::Worker::recursiveSearch(QString const & path)
             QByteArray buf;
             while (!f.atEnd() && (includeFilterMatched <= 0 || excludeFilterMatched <= 0)) {
 
+                // Check for the cancel flag
+                {
+                    QMutexLocker l(&mLock);
+                    if (mDoCancel)
+                        return;
+                }
+
                 /* We read ReadBufferSize bytes from the file and append to the buffer.
                  * We keep max 2 x ReadBufferSize bytes in the buffer and throw away the oldest
                  * ReadBufferSize bytes of data. Every block is checked twice, but we make sure that
@@ -388,7 +407,7 @@ void Internal::Worker::recursiveSearch(QString const & path)
             {
                 QMutexLocker l(&mLock);
                 if (mDoCancel)
-                    break;
+                    return;
             }
 
             recursiveSearch(l + directory);
diff --git a/src/apps/FileFinder/Engine/engine.h b/src/apps/FileFinder/Engine/engine.h
index 384fa40..341df0a 100644
--- a/src/apps/FileFinder/Engine/engine.h
+++ b/src/apps/FileFinder/Engine/engine.h
@@ -181,6 +181,8 @@ public:
 
     Worker(QObject * parent = 0);
 
+    virtual ~Worker();
+
     /**
      * Starts a new search.
      * @param dir Directory where to search
diff --git a/src/apps/FileFinder/Engine/version.h b/src/apps/FileFinder/Engine/version.h
index 5e2f8ae..2a39815 100644
--- a/src/apps/FileFinder/Engine/version.h
+++ b/src/apps/FileFinder/Engine/version.h
@@ -25,12 +25,12 @@
 /**
  * Module/library version number in the form major,minor,release,build
  */
-#define VER_FILE_VERSION                0,1,1,1
+#define VER_FILE_VERSION                0,1,2,2
 
 /**
  * Module/library version number in the string format (shall end with \0)
  */
-#define VER_FILE_VERSION_STR            "0.1.1.1\0"
+#define VER_FILE_VERSION_STR            "0.1.2.2\0"
 
 /**
  * Module/library name (shall end with \0)