From 027c31f39c7e2cc560c9fdc53825a6104bd8294b Mon Sep 17 00:00:00 2001 From: Kimmo Varis Date: Tue, 2 Jun 2009 02:01:53 +0300 Subject: [PATCH] GUI: Implement stopping the compare. Threads must be exited from check cleanly even though it takes small amount of time. Just terminating thread can have unpredictable side-effects (even weird crashes). --- gui/checkthread.cpp | 16 ++++++++++++---- gui/checkthread.h | 23 +++++++++++++++++++++++ gui/mainwindow.cpp | 3 +-- gui/threadhandler.cpp | 2 +- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/gui/checkthread.cpp b/gui/checkthread.cpp index e8e841034..e8aeb0709 100644 --- a/gui/checkthread.cpp +++ b/gui/checkthread.cpp @@ -40,10 +40,11 @@ void CheckThread::Check(Settings settings) void CheckThread::run() { + mState = Running; QString file; file = mResult.GetNextFile(); - while (!file.isEmpty()) + while (!file.isEmpty() && mState == Running) { qDebug() << "Checking file" << file; mCppcheck.addFile(file.toStdString()); @@ -51,11 +52,18 @@ void CheckThread::run() mCppcheck.clearFiles(); emit FileChecked(file); - file = mResult.GetNextFile(); + if (mState == Running) + file = mResult.GetNextFile(); } + if (mState == Running) + mState = Ready; + else + mState = Stopped; emit Done(); } - - +void CheckThread::stop() +{ + mState = Stopping; +} diff --git a/gui/checkthread.h b/gui/checkthread.h index b4c578c08..1102f3e24 100644 --- a/gui/checkthread.h +++ b/gui/checkthread.h @@ -49,6 +49,8 @@ public: */ void run(); + void stop(); + signals: @@ -60,6 +62,27 @@ signals: void FileChecked(const QString &file); protected: + + /** + * @brief States for the check thread. + * Whole purpose of these states is to allow stopping of the checking. When + * stopping we say for the thread (Stopping) that "stop when current check + * has been completed. Thread must be stopped cleanly, just terminating thread + * likely causes unpredictable side-effedts. + */ + enum State + { + Running, /**< The thread is checking. */ + Stopping, /**< The thread will stop after current work. */ + Stopped, /**< The thread has been stopped. */ + Ready, /**< The thread is ready. */ + }; + + /** + * @brief Thread's current execution state. + */ + State mState; + ThreadResult &mResult; /** * @brief Cppcheck itself diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index ed0710ca2..f28cf6502 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -105,8 +105,7 @@ MainWindow::MainWindow() : connect(&mActionReCheck, SIGNAL(triggered()), this, SLOT(ReCheck())); - //TODO: This crashed - //connect(&mActionStop, SIGNAL(triggered()), &mThread, SLOT(Stop())); + connect(&mActionStop, SIGNAL(triggered()), &mThread, SLOT(Stop())); connect(&mActionSave, SIGNAL(triggered()), this, SLOT(Save())); connect(&mActionAbout, SIGNAL(triggered()), this, SLOT(About())); diff --git a/gui/threadhandler.cpp b/gui/threadhandler.cpp index b68eba1e3..a367dd7cc 100644 --- a/gui/threadhandler.cpp +++ b/gui/threadhandler.cpp @@ -124,7 +124,7 @@ void ThreadHandler::Stop() { for (int i = 0;i < mThreads.size();i++) { - mThreads[i]->terminate(); + mThreads[i]->stop(); } }