]> vaikene.ee Git - evaf/commitdiff
Improved canceling and terminating the worker thread.
authorEnar Väikene <enar@vaikene.net>
Mon, 28 Nov 2011 11:33:36 +0000 (13:33 +0200)
committerEnar Väikene <enar@vaikene.net>
Mon, 28 Nov 2011 11:33:36 +0000 (13:33 +0200)
* 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.

src/apps/FileFinder/Engine/engine.cpp
src/apps/FileFinder/Engine/engine.h
src/apps/FileFinder/Engine/version.h

index b9fae0eddd917263fc16288bc4f2042406f5cd4a..8ed0f7ca7069104020ccb7643f23b53fd0dfd371 100644 (file)
@@ -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);
index 384fa40ae0ac03d44a128b5d417f0bc6e2d4ea12..341df0a8cd8f9cb179cba3658045d9cf4853c664 100644 (file)
@@ -181,6 +181,8 @@ public:
 
     Worker(QObject * parent = 0);
 
+    virtual ~Worker();
+
     /**
      * Starts a new search.
      * @param dir Directory where to search
index 5e2f8aee80a839426521ada3380a8235798ac7d1..2a39815dee217dcc40bc6ff6eed3243c5eec1baf 100644 (file)
 /**
  * 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)