• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Plasma

runnerjobs.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright (C) 2007, 2009 Ryan P. Bitanga <ryan.bitanga@gmail.com>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "runnermanager.h"
00021 
00022 #include <QTimer>
00023 
00024 //#include <KDebug>
00025 
00026 //#include <Weaver/DebuggingAids.h>
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         // If the timer is active, the required delay has not been reached
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 // Jobs
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 //     kDebug() << "Running match for " << m_runner->objectName()
00156 //              << " in Thread " << thread()->id() << endl;
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 } // Plasma namespace
00211 
00212 // #include "runnerjobs.moc"

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal