Plasma
runnerjobs.h
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 #ifndef PLASMA_RUNNERJOBS_H 00021 #define PLASMA_RUNNERJOBS_H 00022 00023 #include <QHash> 00024 #include <QMutex> 00025 #include <QSet> 00026 00027 #include <Weaver/Job.h> 00028 #include <Weaver/QueuePolicy.h> 00029 00030 using ThreadWeaver::Job; 00031 00032 class QTimer; 00033 00034 namespace Plasma { 00035 // Queue policies 00036 00037 // QueuePolicy that only allows a job to be executed after 00038 // waiting in the queue for the specified timeout 00039 class DelayedRunnerPolicy : public ThreadWeaver::QueuePolicy 00040 { 00041 public: 00042 ~DelayedRunnerPolicy(); 00043 00044 static DelayedRunnerPolicy &instance(); 00045 00046 bool canRun(Job *job); 00047 void free(Job *job); 00048 void release(Job *job); 00049 void destructed(Job *job); 00050 private: 00051 DelayedRunnerPolicy(); 00052 QMutex m_mutex; 00053 }; 00054 00055 // QueuePolicy that limits the instances of a particular runner 00056 class DefaultRunnerPolicy : public ThreadWeaver::QueuePolicy 00057 { 00058 public: 00059 ~DefaultRunnerPolicy(); 00060 00061 static DefaultRunnerPolicy &instance(); 00062 00063 void setCap(int cap) 00064 { 00065 m_cap = cap; 00066 } 00067 int cap() const 00068 { 00069 return m_cap; 00070 } 00071 00072 bool canRun(Job *job); 00073 void free(Job *job); 00074 void release(Job *job); 00075 void destructed(Job *job); 00076 private: 00077 DefaultRunnerPolicy(); 00078 00079 int m_cap; 00080 QHash<QString, int> m_runCounts; 00081 QMutex m_mutex; 00082 }; 00083 00084 /* ThreadWeaver work around: 00085 * There is no method exposed that allows us to inform 00086 * ThreadWeaver that a previously unavailable job is now 00087 * available; thus, we use an empty job to wake up the threads 00088 */ 00089 class DummyJob : public ThreadWeaver::Job 00090 { 00091 public: 00092 DummyJob(QObject *parent) : Job(parent) {} 00093 ~DummyJob() {} 00094 private: 00095 void run() {} 00096 }; 00097 00098 /* 00099 * FindMatchesJob class 00100 * Class to run queries in different threads 00101 */ 00102 class FindMatchesJob : public Job 00103 { 00104 public: 00105 FindMatchesJob(Plasma::AbstractRunner *runner, 00106 Plasma::RunnerContext *context, QObject *parent = 0); 00107 ~FindMatchesJob(); 00108 00109 int priority() const; 00110 Plasma::AbstractRunner* runner() const; 00111 00112 QTimer* delayTimer() const; 00113 void setDelayTimer(QTimer *timer); 00114 00115 protected: 00116 void run(); 00117 00118 private: 00119 Plasma::RunnerContext m_context; 00120 Plasma::AbstractRunner *m_runner; 00121 QTimer *m_timer; 00122 }; 00123 00124 class DelayedJobCleaner : public QObject 00125 { 00126 public: 00127 DelayedJobCleaner(QSet<FindMatchesJob*> jobs, ThreadWeaver::WeaverInterface *weaver); 00128 00129 private Q_SLOTS: 00130 void jobDone(ThreadWeaver::Job*); 00131 void checkIfFinished(); 00132 //connect(ThreadWeaver::instance(), SIGNAL(finished()), this, SLOT(checkIfFinished())); 00133 00134 private: 00135 ThreadWeaver::WeaverInterface *m_weaver; 00136 QSet<FindMatchesJob*> m_jobs; 00137 }; 00138 00139 } 00140 00141 #endif