2009-03-01 21:44:42 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2009-05-30 07:48:12 +02:00
|
|
|
* Copyright (C) 2007-2009 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "threadhandler.h"
|
|
|
|
#include <QDebug>
|
|
|
|
|
2009-06-20 18:55:23 +02:00
|
|
|
ThreadHandler::ThreadHandler(QObject *parent) :
|
|
|
|
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
|
|
|
{
|
2009-03-22 16:42:00 +01:00
|
|
|
if (recheck && mRunningThreadCount == 0)
|
|
|
|
{
|
|
|
|
mResults.SetFiles(mLastFiles);
|
|
|
|
}
|
|
|
|
|
2009-03-02 20:56:51 +01: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
|
|
|
|
|
|
|
if (mResults.GetFileCount() < mRunningThreadCount)
|
|
|
|
{
|
|
|
|
mRunningThreadCount = mResults.GetFileCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0;i < mRunningThreadCount;i++)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (mRunningThreadCount > 0 ||
|
2009-03-02 20:56:51 +01:00
|
|
|
count == mThreads.size() ||
|
2009-03-01 21:44:42 +01:00
|
|
|
count <= 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove unused old threads
|
2009-03-02 20:56:51 +01:00
|
|
|
RemoveThreads();
|
|
|
|
//Create new threads
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
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--;
|
|
|
|
if (mRunningThreadCount == 0)
|
|
|
|
{
|
|
|
|
emit Done();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::Stop()
|
|
|
|
{
|
|
|
|
for (int i = 0;i < mThreads.size();i++)
|
|
|
|
{
|
2009-06-02 01:01:53 +02:00
|
|
|
mThreads[i]->stop();
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::Initialize(ResultsView *view)
|
|
|
|
{
|
|
|
|
|
|
|
|
connect(&mResults, SIGNAL(Progress(int, int)),
|
|
|
|
view, SLOT(Progress(int, int)));
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
SetThreadCount(settings.value(tr("Check threads"), 1).toInt());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadHandler::SaveSettings(QSettings &settings)
|
|
|
|
{
|
2009-03-02 20:56:51 +01:00
|
|
|
settings.setValue(tr("Check threads"), mThreads.size());
|
2009-03-01 21:44:42 +01:00
|
|
|
}
|
|
|
|
|