Plasma
runnerjobs.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "runnermanager.h"
00021
00022 #include <QTimer>
00023
00024
00025
00026
00027 #include <Weaver/ThreadWeaver.h>
00028
00029 #include "runnerjobs.h"
00030 #include "querymatch.h"
00031
00032 using ThreadWeaver::Job;
00033 using ThreadWeaver::Weaver;
00034
00035 namespace Plasma {
00036
00037 DelayedRunnerPolicy::DelayedRunnerPolicy()
00038 : QueuePolicy()
00039 {}
00040
00041 DelayedRunnerPolicy::~DelayedRunnerPolicy()
00042 {}
00043
00044 DelayedRunnerPolicy& DelayedRunnerPolicy::instance()
00045 {
00046 static DelayedRunnerPolicy policy;
00047 return policy;
00048 }
00049
00050 bool DelayedRunnerPolicy::canRun(Job *job)
00051 {
00052 FindMatchesJob *aJob = static_cast<FindMatchesJob*>(job);
00053 if (QTimer *t = aJob->delayTimer()) {
00054
00055 return !t->isActive();
00056 }
00057
00058 return true;
00059 }
00060
00061 void DelayedRunnerPolicy::free(Job *job)
00062 {
00063 Q_UNUSED(job)
00064 }
00065
00066 void DelayedRunnerPolicy::release(Job *job)
00067 {
00068 free(job);
00069 }
00070
00071 void DelayedRunnerPolicy::destructed(Job *job)
00072 {
00073 Q_UNUSED(job)
00074 }
00075
00076 DefaultRunnerPolicy::DefaultRunnerPolicy()
00077 : QueuePolicy(),
00078 m_cap(2)
00079 {}
00080
00081 DefaultRunnerPolicy::~DefaultRunnerPolicy()
00082 {}
00083
00084 DefaultRunnerPolicy& DefaultRunnerPolicy::instance()
00085 {
00086 static DefaultRunnerPolicy policy;
00087 return policy;
00088 }
00089
00090 bool DefaultRunnerPolicy::canRun(Job *job)
00091 {
00092 Plasma::AbstractRunner *runner = static_cast<FindMatchesJob*>(job)->runner();
00093 QMutexLocker l(&m_mutex);
00094
00095 if (m_runCounts[runner->name()] > m_cap) {
00096 return false;
00097 } else {
00098 ++m_runCounts[runner->name()];
00099 return true;
00100 }
00101 }
00102
00103 void DefaultRunnerPolicy::free(Job *job)
00104 {
00105 Plasma::AbstractRunner *runner = static_cast<FindMatchesJob*>(job)->runner();
00106 QMutexLocker l(&m_mutex);
00107
00108 --m_runCounts[runner->name()];
00109 }
00110
00111 void DefaultRunnerPolicy::release(Job *job)
00112 {
00113 free(job);
00114 }
00115
00116 void DefaultRunnerPolicy::destructed(Job *job)
00117 {
00118 Q_UNUSED(job)
00119 }
00120
00122
00124
00125 FindMatchesJob::FindMatchesJob(Plasma::AbstractRunner *runner,
00126 Plasma::RunnerContext *context, QObject *parent)
00127 : ThreadWeaver::Job(parent),
00128 m_context(*context, 0),
00129 m_runner(runner),
00130 m_timer(0)
00131 {
00132 if (runner->speed() == Plasma::AbstractRunner::SlowSpeed) {
00133 assignQueuePolicy(&DelayedRunnerPolicy::instance());
00134 } else {
00135 assignQueuePolicy(&DefaultRunnerPolicy::instance());
00136 }
00137 }
00138
00139 FindMatchesJob::~FindMatchesJob()
00140 {
00141 }
00142
00143 QTimer* FindMatchesJob::delayTimer() const
00144 {
00145 return m_timer;
00146 }
00147
00148 void FindMatchesJob::setDelayTimer(QTimer *timer)
00149 {
00150 m_timer = timer;
00151 }
00152
00153 void FindMatchesJob::run()
00154 {
00155
00156
00157 if (m_context.isValid()) {
00158 m_runner->performMatch(m_context);
00159 }
00160 }
00161
00162 int FindMatchesJob::priority() const
00163 {
00164 return m_runner->priority();
00165 }
00166
00167 Plasma::AbstractRunner* FindMatchesJob::runner() const
00168 {
00169 return m_runner;
00170 }
00171
00172 DelayedJobCleaner::DelayedJobCleaner(QSet<FindMatchesJob*> jobs, ThreadWeaver::WeaverInterface *weaver)
00173 : QObject(weaver),
00174 m_weaver(weaver),
00175 m_jobs(jobs)
00176 {
00177 connect(m_weaver, SIGNAL(finished()), this, SLOT(checkIfFinished()));
00178
00179 foreach (FindMatchesJob *job, m_jobs) {
00180 connect(job, SIGNAL(done(ThreadWeaver::Job*)), this, SLOT(jobDone(ThreadWeaver::Job*)));
00181 }
00182 }
00183
00184 void DelayedJobCleaner::jobDone(ThreadWeaver::Job *job)
00185 {
00186 FindMatchesJob *runJob = dynamic_cast<FindMatchesJob *>(job);
00187
00188 if (!runJob) {
00189 return;
00190 }
00191
00192 m_jobs.remove(runJob);
00193 delete runJob;
00194
00195 if (m_jobs.isEmpty()) {
00196 deleteLater();
00197 }
00198 }
00199
00200 void DelayedJobCleaner::checkIfFinished()
00201 {
00202 if (m_weaver->isIdle()) {
00203 qDeleteAll(m_jobs);
00204 m_jobs.clear();
00205 deleteLater();
00206 }
00207 }
00208
00209
00210 }
00211
00212