2009-02-11 12:16:09 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007, 2009 Ryan P. Bitanga <ryan.bitanga@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, 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 Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2009-12-27 23:16:11 +01:00
|
|
|
#include "runnerjobs_p.h"
|
2009-02-11 12:16:09 +01:00
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
2009-11-25 02:54:44 +01:00
|
|
|
#include <kdebug.h>
|
2009-02-11 12:16:09 +01:00
|
|
|
|
|
|
|
//#include <Weaver/DebuggingAids.h>
|
2013-01-30 17:31:32 +01:00
|
|
|
#include <threadweaver/ThreadWeaver.h>
|
2009-02-11 12:16:09 +01:00
|
|
|
|
2009-12-27 23:16:11 +01:00
|
|
|
#include "runnermanager.h"
|
2009-09-04 10:58:34 +02:00
|
|
|
#include "plasma/querymatch.h"
|
2009-02-11 12:16:09 +01:00
|
|
|
|
|
|
|
using ThreadWeaver::Job;
|
|
|
|
using ThreadWeaver::Weaver;
|
|
|
|
|
|
|
|
namespace Plasma {
|
|
|
|
|
|
|
|
DelayedRunnerPolicy::DelayedRunnerPolicy()
|
|
|
|
: QueuePolicy()
|
|
|
|
{}
|
|
|
|
|
|
|
|
DelayedRunnerPolicy::~DelayedRunnerPolicy()
|
|
|
|
{}
|
|
|
|
|
|
|
|
DelayedRunnerPolicy& DelayedRunnerPolicy::instance()
|
|
|
|
{
|
|
|
|
static DelayedRunnerPolicy policy;
|
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DelayedRunnerPolicy::canRun(Job *job)
|
|
|
|
{
|
|
|
|
FindMatchesJob *aJob = static_cast<FindMatchesJob*>(job);
|
|
|
|
if (QTimer *t = aJob->delayTimer()) {
|
|
|
|
// If the timer is active, the required delay has not been reached
|
2009-11-25 02:54:44 +01:00
|
|
|
//kDebug() << "delayed timer" << aJob->runner()->name() << !t->isActive();
|
2009-02-11 12:16:09 +01:00
|
|
|
return !t->isActive();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DelayedRunnerPolicy::free(Job *job)
|
|
|
|
{
|
|
|
|
Q_UNUSED(job)
|
|
|
|
}
|
|
|
|
|
|
|
|
void DelayedRunnerPolicy::release(Job *job)
|
|
|
|
{
|
|
|
|
free(job);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DelayedRunnerPolicy::destructed(Job *job)
|
|
|
|
{
|
|
|
|
Q_UNUSED(job)
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultRunnerPolicy::DefaultRunnerPolicy()
|
|
|
|
: QueuePolicy(),
|
|
|
|
m_cap(2)
|
|
|
|
{}
|
|
|
|
|
|
|
|
DefaultRunnerPolicy::~DefaultRunnerPolicy()
|
|
|
|
{}
|
|
|
|
|
|
|
|
DefaultRunnerPolicy& DefaultRunnerPolicy::instance()
|
|
|
|
{
|
|
|
|
static DefaultRunnerPolicy policy;
|
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DefaultRunnerPolicy::canRun(Job *job)
|
|
|
|
{
|
|
|
|
Plasma::AbstractRunner *runner = static_cast<FindMatchesJob*>(job)->runner();
|
|
|
|
QMutexLocker l(&m_mutex);
|
|
|
|
|
|
|
|
if (m_runCounts[runner->name()] > m_cap) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
++m_runCounts[runner->name()];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultRunnerPolicy::free(Job *job)
|
|
|
|
{
|
|
|
|
Plasma::AbstractRunner *runner = static_cast<FindMatchesJob*>(job)->runner();
|
|
|
|
QMutexLocker l(&m_mutex);
|
|
|
|
|
|
|
|
--m_runCounts[runner->name()];
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultRunnerPolicy::release(Job *job)
|
|
|
|
{
|
|
|
|
free(job);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultRunnerPolicy::destructed(Job *job)
|
|
|
|
{
|
|
|
|
Q_UNUSED(job)
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////
|
|
|
|
// Jobs
|
|
|
|
////////////////////
|
|
|
|
|
|
|
|
FindMatchesJob::FindMatchesJob(Plasma::AbstractRunner *runner,
|
|
|
|
Plasma::RunnerContext *context, QObject *parent)
|
|
|
|
: ThreadWeaver::Job(parent),
|
2009-03-12 18:25:13 +01:00
|
|
|
m_context(*context, 0),
|
2009-02-11 12:16:09 +01:00
|
|
|
m_runner(runner),
|
2009-03-12 19:53:22 +01:00
|
|
|
m_timer(0)
|
2009-02-11 12:16:09 +01:00
|
|
|
{
|
|
|
|
if (runner->speed() == Plasma::AbstractRunner::SlowSpeed) {
|
|
|
|
assignQueuePolicy(&DelayedRunnerPolicy::instance());
|
|
|
|
} else {
|
|
|
|
assignQueuePolicy(&DefaultRunnerPolicy::instance());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FindMatchesJob::~FindMatchesJob()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QTimer* FindMatchesJob::delayTimer() const
|
|
|
|
{
|
|
|
|
return m_timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindMatchesJob::setDelayTimer(QTimer *timer)
|
|
|
|
{
|
|
|
|
m_timer = timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindMatchesJob::run()
|
|
|
|
{
|
|
|
|
// kDebug() << "Running match for " << m_runner->objectName()
|
|
|
|
// << " in Thread " << thread()->id() << endl;
|
2009-04-23 04:13:54 +02:00
|
|
|
if (m_context.isValid()) {
|
|
|
|
m_runner->performMatch(m_context);
|
|
|
|
}
|
2009-02-11 12:16:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int FindMatchesJob::priority() const
|
|
|
|
{
|
|
|
|
return m_runner->priority();
|
|
|
|
}
|
|
|
|
|
|
|
|
Plasma::AbstractRunner* FindMatchesJob::runner() const
|
|
|
|
{
|
|
|
|
return m_runner;
|
|
|
|
}
|
|
|
|
|
2011-11-22 12:17:06 +01:00
|
|
|
DelayedJobCleaner::DelayedJobCleaner(const QSet<FindMatchesJob *> &jobs, const QSet<AbstractRunner *> &runners)
|
|
|
|
: QObject(Weaver::instance()),
|
|
|
|
m_weaver(Weaver::instance()),
|
|
|
|
m_jobs(jobs),
|
|
|
|
m_runners(runners)
|
2009-03-12 21:25:52 +01:00
|
|
|
{
|
|
|
|
connect(m_weaver, SIGNAL(finished()), this, SLOT(checkIfFinished()));
|
2009-03-12 23:01:22 +01:00
|
|
|
|
|
|
|
foreach (FindMatchesJob *job, m_jobs) {
|
|
|
|
connect(job, SIGNAL(done(ThreadWeaver::Job*)), this, SLOT(jobDone(ThreadWeaver::Job*)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-22 12:17:06 +01:00
|
|
|
DelayedJobCleaner::~DelayedJobCleaner()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_runners);
|
|
|
|
}
|
|
|
|
|
2009-03-12 23:01:22 +01:00
|
|
|
void DelayedJobCleaner::jobDone(ThreadWeaver::Job *job)
|
|
|
|
{
|
|
|
|
FindMatchesJob *runJob = dynamic_cast<FindMatchesJob *>(job);
|
|
|
|
|
|
|
|
if (!runJob) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_jobs.remove(runJob);
|
2009-10-09 11:33:20 +02:00
|
|
|
runJob->deleteLater();
|
2009-03-12 23:02:31 +01:00
|
|
|
|
|
|
|
if (m_jobs.isEmpty()) {
|
|
|
|
deleteLater();
|
|
|
|
}
|
2009-03-12 21:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DelayedJobCleaner::checkIfFinished()
|
|
|
|
{
|
|
|
|
if (m_weaver->isIdle()) {
|
|
|
|
qDeleteAll(m_jobs);
|
|
|
|
m_jobs.clear();
|
|
|
|
deleteLater();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-11 12:16:09 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|