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

ThreadWeaver

  • sources
  • kde-4.12
  • kdelibs
  • threadweaver
  • Weaver
Job.cpp
Go to the documentation of this file.
1 /* -*- C++ -*-
2 
3 This file implements the Job class.
4 
5 $ Author: Mirko Boehm $
6 $ Copyright: (C) 2004-2013 Mirko Boehm $
7 $ Contact: mirko@kde.org
8 http://www.kde.org
9 http://creative-destruction.me $
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Library General Public
13  License as published by the Free Software Foundation; either
14  version 2 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Library General Public License for more details.
20 
21  You should have received a copy of the GNU Library General Public License
22  along with this library; see the file COPYING.LIB. If not, write to
23  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  Boston, MA 02110-1301, USA.
25 
26 $Id: Job.cpp 20 2005-08-08 21:02:51Z mirko $
27 */
28 
29 #include "Job.h"
30 #include "Job_p.h"
31 
32 #include <QtCore/QSet>
33 #include <QtCore/QList>
34 #include <QtCore/QMutex>
35 #include <QtCore/QObject>
36 #include <QtCore/QMap>
37 #include <QtCore/QArgument>
38 #include <QtCore/QWaitCondition>
39 #include <DebuggingAids.h>
40 #include <Thread.h>
41 
42 #include "QueuePolicy.h"
43 #include "DependencyPolicy.h"
44 
45 using namespace ThreadWeaver;
46 
47 class ThreadWeaver::QueuePolicyList : public QList<QueuePolicy*> {};
48 
49 class Job::Private
50 {
51 public:
52  Private ()
53  : thread (0)
54  , queuePolicies ( new QueuePolicyList )
55  , mutex (new QMutex (QMutex::NonRecursive) )
56  , finished (false)
57  {}
58 
59  ~Private()
60  {
61  delete queuePolicies;
62  delete mutex;
63  }
64 
65  /* The thread that executes this job. Zero when the job is not executed. */
66  Thread * thread;
67 
68  /* The list of QueuePolicies assigned to this Job. */
69  QueuePolicyList* queuePolicies;
70 
71  QMutex *mutex;
72  /* d->finished is set to true when the Job has been executed. */
73  bool finished;
74 };
75 
76 Job::Job ( QObject *parent )
77  : QObject (parent)
78  , d(new Private())
79 {
80 }
81 
82 Job::~Job()
83 {
84  for ( int index = 0; index < d->queuePolicies->size(); ++index )
85  {
86  d->queuePolicies->at( index )->destructed( this );
87  }
88 
89  delete d;
90 }
91 
92 ThreadWeaver::JobRunHelper::JobRunHelper()
93  : QObject ( 0 )
94 {
95 }
96 
97 void ThreadWeaver::JobRunHelper::runTheJob ( Thread* th, Job* job )
98 {
99  P_ASSERT ( th == thread() );
100  job->d->mutex->lock();
101  job->d->thread = th;
102  job->d->mutex->unlock();
103 
104  emit ( started ( job ) );
105 
106  job->run();
107 
108  job->d->mutex->lock();
109  job->d->thread = 0;
110  job->setFinished (true);
111  job->d->mutex->unlock();
112  job->freeQueuePolicyResources();
113 
114  if ( ! job->success() )
115  {
116  emit ( failed( job ) );
117  }
118 
119  emit ( done( job ) );
120 }
121 
122 void Job::execute(Thread *th)
123 {
124 // P_ASSERT (sm_dep()->values(this).isEmpty());
125  JobRunHelper helper;
126  connect ( &helper, SIGNAL (started(ThreadWeaver::Job*)),
127  SIGNAL (started(ThreadWeaver::Job*)) );
128  connect ( &helper, SIGNAL (done(ThreadWeaver::Job*)),
129  SIGNAL (done(ThreadWeaver::Job*)) );
130  connect ( &helper, SIGNAL(failed(ThreadWeaver::Job*)),
131  SIGNAL(failed(ThreadWeaver::Job*)) );
132 
133  debug(3, "Job::execute: executing job of type %s %s in thread %i.\n",
134  metaObject()->className(), objectName().isEmpty() ? "" : qPrintable( objectName() ), th->id());
135  helper.runTheJob( th, this );
136  debug(3, "Job::execute: finished execution of job in thread %i.\n", th->id());
137 }
138 
139 int Job::priority () const
140 {
141  return 0;
142 }
143 
144 bool Job::success () const
145 {
146  return true;
147 }
148 
149 void Job::freeQueuePolicyResources()
150 {
151  for ( int index = 0; index < d->queuePolicies->size(); ++index )
152  {
153  d->queuePolicies->at( index )->free( this );
154  }
155 }
156 
157 void Job::aboutToBeQueued ( WeaverInterface* )
158 {
159 }
160 
161 void Job::aboutToBeDequeued ( WeaverInterface* )
162 {
163 }
164 
165 bool Job::canBeExecuted()
166 {
167  QueuePolicyList acquired;
168 
169  bool success = true;
170 
171  if ( d->queuePolicies->size() > 0 )
172  {
173  debug( 4, "Job::canBeExecuted: acquiring permission from %i queue %s.\n",
174  d->queuePolicies->size(), d->queuePolicies->size()==1 ? "policy" : "policies" );
175  for ( int index = 0; index < d->queuePolicies->size(); ++index )
176  {
177  if ( d->queuePolicies->at( index )->canRun( this ) )
178  {
179  acquired.append( d->queuePolicies->at( index ) );
180  } else {
181  success = false;
182  break;
183  }
184  }
185 
186  debug( 4, "Job::canBeExecuted: queue policies returned %s.\n", success ? "true" : "false" );
187 
188  if ( ! success )
189  {
190 
191  for ( int index = 0; index < acquired.size(); ++index )
192  {
193  acquired.at( index )->release( this );
194  }
195  }
196  } else {
197  debug( 4, "Job::canBeExecuted: no queue policies, this job can be executed.\n" );
198  }
199 
200  return success;
201 }
202 
203 void Job::assignQueuePolicy( QueuePolicy* policy )
204 {
205  if ( ! d->queuePolicies->contains( policy ) )
206  {
207  d->queuePolicies->append( policy );
208  }
209 }
210 
211 void Job::removeQueuePolicy( QueuePolicy* policy )
212 {
213  int index = d->queuePolicies->indexOf( policy );
214  if ( index != -1 )
215  {
216  d->queuePolicies->removeAt( index );
217  }
218 }
219 
220 bool Job::isFinished() const
221 {
222  return d->finished;
223 }
224 
225 Thread* Job::thread()
226 {
227  return d->thread;
228 }
229 
230 void Job::setFinished ( bool status )
231 {
232  d->finished = status;
233 }
234 
235 // QMutex& Job::mutex()
236 // {
237 // return * d->mutex;
238 // }
239 
240 #include "Job.moc"
241 #include "Job_p.moc"
ThreadWeaver::Job::done
void done(ThreadWeaver::Job *)
This signal is emitted when the job has been finished (no matter if it succeeded or not)...
DependencyPolicy.h
ThreadWeaver::Job::aboutToBeQueued
virtual void aboutToBeQueued(WeaverInterface *weaver)
The job is about to be added to the weaver's job queue.
Definition: Job.cpp:157
Job.h
ThreadWeaver::JobRunHelper
Definition: Job_p.h:38
ThreadWeaver::Thread
The class Thread is used to represent the worker threads in the weaver's inventory.
Definition: Thread.h:46
ThreadWeaver::Job::priority
virtual int priority() const
The queueing priority of the job.
Definition: Job.cpp:139
QObject
ThreadWeaver::Job::thread
Thread * thread()
Return the thread that executes this job.
Definition: Job.cpp:225
ThreadWeaver::Job::removeQueuePolicy
void removeQueuePolicy(QueuePolicy *)
Remove a queue policy from this job.
Definition: Job.cpp:211
ThreadWeaver::Job::success
virtual bool success() const
Return whether the Job finished successfully or not.
Definition: Job.cpp:144
QueuePolicy.h
P_ASSERT
#define P_ASSERT(x)
P_ASSERT ensures that error messages occur in the correct order.
Definition: DebuggingAids.h:103
ThreadWeaver::Job::aboutToBeDequeued
virtual void aboutToBeDequeued(WeaverInterface *weaver)
This Job is about the be dequeued from the weaver's job queue.
Definition: Job.cpp:161
ThreadWeaver::Job::isFinished
bool isFinished() const
Returns true if the jobs's execute method finished.
Definition: Job.cpp:220
ThreadWeaver::Job::execute
virtual void execute(Thread *)
Perform the job.
Definition: Job.cpp:122
ThreadWeaver::JobRunHelper::JobRunHelper
JobRunHelper()
Definition: Job.cpp:92
Thread.h
ThreadWeaver::Job::~Job
virtual ~Job()
Destructor.
Definition: Job.cpp:82
ThreadWeaver::Job::started
void started(ThreadWeaver::Job *)
This signal is emitted when this job is being processed by a thread.
ThreadWeaver::Job::run
virtual void run()=0
The method that actually performs the job.
DebuggingAids.h
ThreadWeaver::Job::failed
void failed(ThreadWeaver::Job *)
This job has failed.
ThreadWeaver::JobRunHelper::runTheJob
void runTheJob(Thread *th, Job *job)
Definition: Job.cpp:97
ThreadWeaver::Job::freeQueuePolicyResources
void freeQueuePolicyResources()
Free the queue policies acquired before this job has been executed.
Definition: Job.cpp:149
ThreadWeaver::Job::setFinished
void setFinished(bool status)
Call with status = true to mark this job as done.
Definition: Job.cpp:230
ThreadWeaver::Thread::id
unsigned int id()
Returns the thread id.
Definition: Thread.cpp:80
ThreadWeaver::debug
void debug(int severity, const char *cformat,...)
This method prints a text message on the screen, if debugging is enabled.
Definition: DebuggingAids.h:112
ThreadWeaver::Job::canBeExecuted
virtual bool canBeExecuted()
canBeExecuted() returns true if all the jobs queue policies agree to it.
Definition: Job.cpp:165
Job_p.h
ThreadWeaver::WeaverInterface
WeaverInterface provides a common interface for weaver implementations.
Definition: WeaverInterface.h:61
ThreadWeaver::QueuePolicy
QueuePolicy is an interface for customizations of the queueing behaviour of sets of jobs...
Definition: QueuePolicy.h:59
ThreadWeaver::Job
A Job is a simple abstraction of an action that is to be executed in a thread context.
Definition: Job.h:65
ThreadWeaver::Job::assignQueuePolicy
void assignQueuePolicy(QueuePolicy *)
Assign a queue policy.
Definition: Job.cpp:203
ThreadWeaver::Job::Job
Job(QObject *parent=0)
Construct a Job.
Definition: Job.cpp:76
ThreadWeaver::Job::d
Private * d
Definition: Job.h:210
QList
Definition: DependencyPolicy.h:32
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ThreadWeaver

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal