2009-03-01 21:44:42 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2010-04-13 21:23:17 +02:00
|
|
|
* Copyright (C) 2007-2010 Daniel Marjamäki and Cppcheck team.
|
2009-03-01 21:44:42 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-09-27 17:08:31 +02:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-03-01 21:44:42 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "threadhandler.h"
|
|
|
|
#include <QDebug>
|
|
|
|
|
2009-06-20 18:55:23 +02:00
|
|
|
ThreadHandler::ThreadHandler(QObject *parent) :
|
2010-04-15 20:08:51 +02:00
|
|
|
QObject(parent),
|
|
|
|
mRunningThreadCount(0)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
|
|
|
SetThreadCount(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadHandler::~ThreadHandler()
|
|
|
|
{
|
2009-03-02 20:56:51 +01:00
|
|
|
RemoveThreads();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::ClearFiles()
|
|
|
|
{
|
2009-03-22 16:42:00 +01:00
|
|
|
mLastFiles.clear();
|
2009-03-01 21:44:42 +01:00
|
|
|
mResults.ClearFiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::SetFiles(const QStringList &files)
|
|
|
|
{
|
|
|
|
mResults.SetFiles(files);
|
2009-03-22 16:42:00 +01:00
|
|
|
mLastFiles = files;
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2009-03-22 16:42:00 +01:00
|
|
|
void ThreadHandler::Check(Settings settings, bool recheck)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (recheck && mRunningThreadCount == 0)
|
2009-03-22 16:42:00 +01:00
|
|
|
{
|
|
|
|
mResults.SetFiles(mLastFiles);
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (mResults.GetFileCount() == 0 || mRunningThreadCount > 0 || settings._jobs <= 0)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2009-03-02 20:56:51 +01:00
|
|
|
qDebug() << "Can't start checking if there's no files to check or if check is in progress.";
|
2009-03-22 13:32:07 +01:00
|
|
|
emit Done();
|
2009-03-01 21:44:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetThreadCount(settings._jobs);
|
|
|
|
|
2009-03-02 20:56:51 +01:00
|
|
|
mRunningThreadCount = mThreads.size();
|
2009-03-01 21:44:42 +01:00
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
if (mResults.GetFileCount() < mRunningThreadCount)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
|
|
|
mRunningThreadCount = mResults.GetFileCount();
|
|
|
|
}
|
|
|
|
|
2010-04-02 07:30:58 +02:00
|
|
|
for (int i = 0; i < mRunningThreadCount; i++)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
|
|
|
mThreads[i]->Check(settings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-04 16:02:35 +02:00
|
|
|
bool ThreadHandler::IsChecking() const
|
|
|
|
{
|
|
|
|
return mRunningThreadCount > 0;
|
|
|
|
}
|
|
|
|
|
2009-03-01 21:44:42 +01:00
|
|
|
void ThreadHandler::SetThreadCount(const int count)
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (mRunningThreadCount > 0 ||
|
|
|
|
count == mThreads.size() ||
|
|
|
|
count <= 0)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove unused old threads
|
2009-03-02 20:56:51 +01:00
|
|
|
RemoveThreads();
|
|
|
|
//Create new threads
|
2010-04-02 07:30:58 +02:00
|
|
|
for (int i = mThreads.size(); i < count; i++)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2009-03-02 20:56:51 +01:00
|
|
|
mThreads << new CheckThread(mResults);
|
|
|
|
connect(mThreads.last(), SIGNAL(Done()),
|
|
|
|
this, SLOT(ThreadDone()));
|
|
|
|
connect(mThreads.last(), SIGNAL(FileChecked(const QString &)),
|
|
|
|
&mResults, SLOT(FileChecked(const QString &)));
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
2009-03-02 20:56:51 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ThreadHandler::RemoveThreads()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (int i = 0; i < mThreads.size(); i++)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2009-03-02 20:56:51 +01:00
|
|
|
mThreads[i]->terminate();
|
|
|
|
disconnect(mThreads.last(), SIGNAL(Done()),
|
|
|
|
this, SLOT(ThreadDone()));
|
|
|
|
disconnect(mThreads.last(), SIGNAL(FileChecked(const QString &)),
|
|
|
|
&mResults, SLOT(FileChecked(const QString &)));
|
|
|
|
|
|
|
|
delete mThreads[i];
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
2009-03-02 20:56:51 +01:00
|
|
|
|
|
|
|
mThreads.clear();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::ThreadDone()
|
|
|
|
{
|
|
|
|
mRunningThreadCount--;
|
2010-04-02 07:30:58 +02:00
|
|
|
if (mRunningThreadCount == 0)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
|
|
|
emit Done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::Stop()
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
for (int i = 0; i < mThreads.size(); i++)
|
2009-03-01 21:44:42 +01:00
|
|
|
{
|
2009-06-02 01:01:53 +02:00
|
|
|
mThreads[i]->stop();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::Initialize(ResultsView *view)
|
|
|
|
{
|
|
|
|
|
2010-07-04 17:54:41 +02:00
|
|
|
connect(&mResults, SIGNAL(Progress(int)),
|
|
|
|
view, SLOT(Progress(int)));
|
2009-03-01 21:44:42 +01:00
|
|
|
|
|
|
|
connect(&mResults, SIGNAL(Error(const QString &,
|
|
|
|
const QString &,
|
|
|
|
const QString &,
|
|
|
|
const QStringList &,
|
2009-06-03 20:18:22 +02:00
|
|
|
const QVariantList &,
|
|
|
|
const QString &)),
|
2009-03-01 21:44:42 +01:00
|
|
|
view, SLOT(Error(const QString &,
|
|
|
|
const QString &,
|
|
|
|
const QString &,
|
|
|
|
const QStringList &,
|
2009-06-03 20:18:22 +02:00
|
|
|
const QVariantList &,
|
|
|
|
const QString &)));
|
2009-03-01 21:44:42 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::LoadSettings(QSettings &settings)
|
|
|
|
{
|
2009-07-02 10:32:29 +02:00
|
|
|
SetThreadCount(settings.value(SETTINGS_CHECK_THREADS, 1).toInt());
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::SaveSettings(QSettings &settings)
|
|
|
|
{
|
2009-07-02 10:32:29 +02:00
|
|
|
settings.setValue(SETTINGS_CHECK_THREADS, mThreads.size());
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|
2009-10-13 10:19:00 +02:00
|
|
|
bool ThreadHandler::HasPreviousFiles() const
|
|
|
|
{
|
2010-04-02 07:30:58 +02:00
|
|
|
if (mLastFiles.size() > 0)
|
2009-10-13 10:19:00 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|