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
|
|
|
#ifndef PLASMA_RUNNERJOBS_P_H
|
|
|
|
#define PLASMA_RUNNERJOBS_P_H
|
2009-02-11 12:16:09 +01:00
|
|
|
|
|
|
|
#include <QHash>
|
|
|
|
#include <QMutex>
|
2009-03-12 21:25:52 +01:00
|
|
|
#include <QSet>
|
2009-02-11 12:16:09 +01:00
|
|
|
|
2014-01-05 12:03:17 +01:00
|
|
|
#include <ThreadWeaver/Job>
|
|
|
|
#include <ThreadWeaver/QueuePolicy>
|
|
|
|
#include <ThreadWeaver/Queue>
|
|
|
|
#include <ThreadWeaver/QObjectDecorator>
|
2009-02-11 12:16:09 +01:00
|
|
|
|
2009-12-27 23:16:11 +01:00
|
|
|
#include "abstractrunner.h"
|
|
|
|
|
2009-02-11 12:16:09 +01:00
|
|
|
using ThreadWeaver::Job;
|
|
|
|
|
|
|
|
class QTimer;
|
|
|
|
|
|
|
|
namespace Plasma {
|
|
|
|
// Queue policies
|
|
|
|
|
|
|
|
// QueuePolicy that only allows a job to be executed after
|
|
|
|
// waiting in the queue for the specified timeout
|
|
|
|
class DelayedRunnerPolicy : public ThreadWeaver::QueuePolicy
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~DelayedRunnerPolicy();
|
|
|
|
|
|
|
|
static DelayedRunnerPolicy &instance();
|
|
|
|
|
2013-10-10 20:27:41 +02:00
|
|
|
bool canRun(ThreadWeaver::JobPointer job);
|
|
|
|
void free(ThreadWeaver::JobPointer job);
|
|
|
|
void release(ThreadWeaver::JobPointer job);
|
|
|
|
virtual void destructed(ThreadWeaver::JobInterface* job);
|
|
|
|
|
2009-02-11 12:16:09 +01:00
|
|
|
private:
|
|
|
|
DelayedRunnerPolicy();
|
|
|
|
QMutex m_mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
// QueuePolicy that limits the instances of a particular runner
|
|
|
|
class DefaultRunnerPolicy : public ThreadWeaver::QueuePolicy
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~DefaultRunnerPolicy();
|
|
|
|
|
|
|
|
static DefaultRunnerPolicy &instance();
|
|
|
|
|
|
|
|
void setCap(int cap)
|
|
|
|
{
|
|
|
|
m_cap = cap;
|
|
|
|
}
|
|
|
|
int cap() const
|
|
|
|
{
|
|
|
|
return m_cap;
|
|
|
|
}
|
|
|
|
|
2013-10-10 20:27:41 +02:00
|
|
|
bool canRun(ThreadWeaver::JobPointer job);
|
|
|
|
void free(ThreadWeaver::JobPointer job);
|
|
|
|
void release(ThreadWeaver::JobPointer job);
|
|
|
|
void destructed(ThreadWeaver::JobInterface* job);
|
2009-02-11 12:16:09 +01:00
|
|
|
private:
|
|
|
|
DefaultRunnerPolicy();
|
|
|
|
|
|
|
|
int m_cap;
|
|
|
|
QHash<QString, int> m_runCounts;
|
|
|
|
QMutex m_mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FindMatchesJob class
|
|
|
|
* Class to run queries in different threads
|
|
|
|
*/
|
|
|
|
class FindMatchesJob : public Job
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FindMatchesJob(Plasma::AbstractRunner *runner,
|
|
|
|
Plasma::RunnerContext *context, QObject *parent = 0);
|
|
|
|
~FindMatchesJob();
|
|
|
|
|
|
|
|
int priority() const;
|
|
|
|
Plasma::AbstractRunner* runner() const;
|
|
|
|
|
|
|
|
QTimer* delayTimer() const;
|
|
|
|
void setDelayTimer(QTimer *timer);
|
2013-10-10 20:27:41 +02:00
|
|
|
ThreadWeaver::QObjectDecorator* decorator() const { return m_decorator; }
|
2009-02-11 12:16:09 +01:00
|
|
|
|
|
|
|
protected:
|
2013-10-10 20:27:41 +02:00
|
|
|
virtual void run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread* thread);
|
2009-02-11 12:16:09 +01:00
|
|
|
|
|
|
|
private:
|
2009-03-12 18:25:13 +01:00
|
|
|
Plasma::RunnerContext m_context;
|
2009-02-11 12:16:09 +01:00
|
|
|
Plasma::AbstractRunner *m_runner;
|
|
|
|
QTimer *m_timer;
|
2013-10-10 20:27:41 +02:00
|
|
|
ThreadWeaver::QObjectDecorator* m_decorator;
|
2009-02-11 12:16:09 +01:00
|
|
|
};
|
|
|
|
|
2009-03-12 21:25:52 +01:00
|
|
|
class DelayedJobCleaner : public QObject
|
|
|
|
{
|
|
|
|
public:
|
2013-07-25 06:58:21 +02:00
|
|
|
DelayedJobCleaner(const QSet<QSharedPointer<FindMatchesJob> > &jobs, const QSet<AbstractRunner *> &runners = QSet<AbstractRunner *>());
|
2011-11-22 12:17:06 +01:00
|
|
|
~DelayedJobCleaner();
|
2009-03-12 21:25:52 +01:00
|
|
|
|
|
|
|
private Q_SLOTS:
|
2013-07-25 06:58:21 +02:00
|
|
|
void jobDone(ThreadWeaver::JobPointer);
|
2009-03-12 21:25:52 +01:00
|
|
|
void checkIfFinished();
|
|
|
|
|
|
|
|
private:
|
2014-01-05 02:10:24 +01:00
|
|
|
ThreadWeaver::Queue *m_weaver;
|
2013-07-25 06:58:21 +02:00
|
|
|
QSet<QSharedPointer<FindMatchesJob> > m_jobs;
|
2011-11-22 12:17:06 +01:00
|
|
|
QSet<AbstractRunner *> m_runners;
|
2009-03-12 21:25:52 +01:00
|
|
|
};
|
|
|
|
|
2009-02-11 12:16:09 +01:00
|
|
|
}
|
|
|
|
|
2009-12-27 23:16:11 +01:00
|
|
|
#endif // PLASMA_RUNNERJOBS_P_H
|