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

ThreadWeaver

  • threadweaver
  • Weaver
DependencyPolicy.cpp
Go to the documentation of this file.
1/* -*- C++ -*-
2
3This file implements the DependencyPolicy class.
4
5$ Author: Mirko Boehm $
6$ Copyright: (C) 2004-2013 Mirko Boehm $
7$ Contact: mirko@kde.org
8http://www.kde.org
9http://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: DebuggingAids.cpp 20 2005-08-08 21:02:51Z mirko $
27*/
28
29#include "DependencyPolicy.h"
30
31#include <QtCore/QMutex>
32#include <QtCore/QDebug>
33
34#include "Job.h"
35#include "DebuggingAids.h"
36
37using namespace ThreadWeaver;
38
39typedef QMultiMap<Job*, Job*> JobMultiMap;
40
41class DependencyPolicy::Private
42{
43public:
49 JobMultiMap& dependencies()
50 {
51 static JobMultiMap depMap;
52 return depMap;
53 }
54
55 QMutex& mutex()
56 {
57 static QMutex s_mutex;
58 return s_mutex;
59 }
60
61};
62
63DependencyPolicy::DependencyPolicy()
64 : QueuePolicy()
65 , d ( new Private() )
66{
67}
68
69DependencyPolicy::~DependencyPolicy()
70{
71 delete d;
72}
73
74void DependencyPolicy::addDependency( Job* jobA, Job* jobB )
75{
76 // jobA depends on jobB
77 REQUIRE ( jobA != 0 && jobB != 0 && jobA != jobB );
78
79 jobA->assignQueuePolicy( this );
80 jobB->assignQueuePolicy( this );
81 QMutexLocker l( & d->mutex() );
82 d->dependencies().insert( jobA, jobB );
83
84 ENSURE ( d->dependencies().contains (jobA));
85}
86
87bool DependencyPolicy::removeDependency( Job* jobA, Job* jobB )
88{
89 REQUIRE (jobA != 0 && jobB != 0);
90 bool result = false;
91 QMutexLocker l( & d->mutex() );
92
93 // there may be only one (!) occurrence of [this, dep]:
94 QMutableMapIterator<Job*, Job*> it( d->dependencies () );
95 while ( it.hasNext() )
96 {
97 it.next();
98 if ( it.key()==jobA && it.value()==jobB )
99 {
100 it.remove();
101 result = true;
102 break;
103 }
104 }
105
106 ENSURE ( ! d->dependencies().keys(jobB).contains(jobA) );
107 return result;
108}
109
110void DependencyPolicy::resolveDependencies( Job* job )
111{
112 if ( job->success() )
113 {
114 QMutexLocker l( & d->mutex() );
115 QMutableMapIterator<Job*, Job*> it( d->dependencies() );
116 // there has to be a better way to do this: (?)
117 while ( it.hasNext() )
118 { // we remove all entries where jobs depend on *this* :
119 it.next();
120 if ( it.value()==job )
121 {
122 it.remove();
123 }
124 }
125 }
126}
127
128QList<Job*> DependencyPolicy::getDependencies( Job* job ) const
129{
130 REQUIRE (job != 0);
131 QList<Job*> result;
132 JobMultiMap::const_iterator it;
133 QMutexLocker l( & d->mutex() );
134
135 for ( it = d->dependencies().constBegin(); it != d->dependencies().constEnd(); ++it )
136 {
137 if ( it.key() == job )
138 {
139 result.append( it.value() );
140 }
141 }
142 return result;
143}
144
145bool DependencyPolicy::hasUnresolvedDependencies( Job* job ) const
146{
147 REQUIRE (job != 0);
148 QMutexLocker l( & d->mutex() );
149 return d->dependencies().contains( job );
150}
151
152DependencyPolicy& DependencyPolicy::instance ()
153{
154 static DependencyPolicy policy;
155 return policy;
156}
157
158bool DependencyPolicy::canRun( Job* job )
159{
160 REQUIRE (job != 0);
161 return ! hasUnresolvedDependencies( job );
162}
163
164void DependencyPolicy::free( Job* job )
165{
166 REQUIRE (job != 0);
167 if ( job->success() )
168 {
169 resolveDependencies( job );
170 debug( 3, "DependencyPolicy::free: dependencies resolved for job %p.\n", (void*)job);
171 } else {
172 debug( 3, "DependencyPolicy::free: not resolving dependencies for %p (execution not successful).\n",
173 (void*)job);
174 }
175 ENSURE ( ( ! hasUnresolvedDependencies( job ) && job->success() ) || ! job->success() );
176}
177
178void DependencyPolicy::release( Job* job )
179{
180 REQUIRE (job != 0); Q_UNUSED(job)
181}
182
183void DependencyPolicy::destructed( Job* job )
184{
185 REQUIRE (job != 0);
186 resolveDependencies ( job );
187}
188
189void DependencyPolicy::dumpJobDependencies()
190{
191 QMutexLocker l( & d->mutex() );
192
193 debug ( 0, "Job Dependencies (left depends on right side):\n" );
194 for ( JobMultiMap::const_iterator it = d->dependencies().constBegin(); it != d->dependencies().constEnd(); ++it )
195 {
196 debug( 0, " : %p (%s%s) <-- %p (%s%s)\n",
197 (void*)it.key(),
198 it.key()->objectName().isEmpty() ? "" : qPrintable ( QString(it.key()->objectName() + QObject::tr ( " of type " )) ),
199 it.key()->metaObject()->className(),
200 (void*)it.value(),
201 it.value()->objectName().isEmpty() ? "" : qPrintable ( QString(it.value()->objectName() + QObject::tr ( " of type " )) ),
202 it.value()->metaObject()->className() );
203 }
204 debug ( 0, "-----------------\n" );
205}
206
DebuggingAids.h
ENSURE
#define ENSURE(x)
Definition: DebuggingAids.h:150
REQUIRE
#define REQUIRE(x)
Definition: DebuggingAids.h:146
JobMultiMap
QMultiMap< Job *, Job * > JobMultiMap
Definition: DependencyPolicy.cpp:39
DependencyPolicy.h
Job.h
QList
Definition: DependencyPolicy.h:32
ThreadWeaver::DependencyPolicy
DependencyPolicy implements the way dependencies between Jobs are handled.
Definition: DependencyPolicy.h:50
ThreadWeaver::DependencyPolicy::removeDependency
bool removeDependency(Job *jobA, Job *jobB)
Remove dependency.
Definition: DependencyPolicy.cpp:87
ThreadWeaver::DependencyPolicy::hasUnresolvedDependencies
bool hasUnresolvedDependencies(Job *) const
Query whether the job has an unresolved dependency.
Definition: DependencyPolicy.cpp:145
ThreadWeaver::DependencyPolicy::getDependencies
QList< Job * > getDependencies(Job *) const
Retrieve a list of dependencies of this job.
Definition: DependencyPolicy.cpp:128
ThreadWeaver::DependencyPolicy::free
void free(Job *)
free() is called after the job has been executed.
Definition: DependencyPolicy.cpp:164
ThreadWeaver::DependencyPolicy::addDependency
void addDependency(Job *jobA, Job *jobB)
Add jobB as a dependency of jobA.
Definition: DependencyPolicy.cpp:74
ThreadWeaver::DependencyPolicy::resolveDependencies
void resolveDependencies(Job *)
Resolve all dependencies.
Definition: DependencyPolicy.cpp:110
ThreadWeaver::DependencyPolicy::~DependencyPolicy
~DependencyPolicy()
Destructor.
Definition: DependencyPolicy.cpp:69
ThreadWeaver::DependencyPolicy::canRun
bool canRun(Job *)
canRun() is called before the job is executed.
Definition: DependencyPolicy.cpp:158
ThreadWeaver::DependencyPolicy::instance
static DependencyPolicy & instance()
Definition: DependencyPolicy.cpp:152
ThreadWeaver::DependencyPolicy::destructed
void destructed(Job *)
destructing() is called when a Job that has this queue policy assigned gets destructed.
Definition: DependencyPolicy.cpp:183
ThreadWeaver::DependencyPolicy::dumpJobDependencies
void dumpJobDependencies()
This method should be useful for debugging purposes.
Definition: DependencyPolicy.cpp:189
ThreadWeaver::DependencyPolicy::release
void release(Job *)
release() is called if canRun() returned true, but the job has not been executed for external reasons...
Definition: DependencyPolicy.cpp:178
ThreadWeaver::DependencyPolicy::DependencyPolicy
DependencyPolicy()
Definition: DependencyPolicy.cpp:63
ThreadWeaver::Job
A Job is a simple abstraction of an action that is to be executed in a thread context.
Definition: Job.h:66
ThreadWeaver::Job::success
virtual bool success() const
Return whether the Job finished successfully or not.
Definition: Job.cpp:144
ThreadWeaver::Job::assignQueuePolicy
void assignQueuePolicy(QueuePolicy *)
Assign a queue policy.
Definition: Job.cpp:203
ThreadWeaver::QueuePolicy
QueuePolicy is an interface for customizations of the queueing behaviour of sets of jobs.
Definition: QueuePolicy.h:60
ThreadWeaver
Definition: DebuggingAids.h:51
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
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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