1/* -*- C++ -*-
2
3 This file implements the DependencyPolicy 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: DebuggingAids.cpp 20 2005-08-08 21:02:51Z mirko $
27*/
28
29#ifndef DEPENDENCYPOLICY_H
30#define DEPENDENCYPOLICY_H
31
32template <typename T> class QList;
33
34#include "QueuePolicy.h"
35
36namespace ThreadWeaver {
37
38 /** DependencyPolicy implements the way dependencies between Jobs are handled.
39
40 To declare that Job B can only be executed when Job A is finished,
41 call addDependency.
42
43 Be aware of circular dependencies. All dependencies on a Job will be
44 removed if the Job object is destructed.
45
46 JobSequence uses dependencies to implement the ordered execution of
47 the sequence elements.
48 */
49 class THREADWEAVER_EXPORT DependencyPolicy : public QueuePolicy
50 {
51 public:
52 /** Destructor. */
53 ~DependencyPolicy();
54
55 /** Add jobB as a dependency of jobA.
56 jobA will only be executed after jobB has been successfully processed.
57 @param jobA the depending job
58 @param jobB the job jobA depends on
59 */
60 void addDependency( Job* jobA, Job* jobB );
61
62 /** Remove dependency.
63
64 The dependency of jobA on jobB is removed. If no dependencies are
65 left for jobA, canRun will return true.
66
67 Returns false if the given object is not dependency of this job.
68
69 This function is inefficient, and should be used only to abort
70 execution of a job.
71
72 @param jobA the depending job
73 @param jobB the job jobA depends on
74 @return true if dependency existed, false otherwise
75 */
76 bool removeDependency( Job* jobA, Job* jobB );
77
78 /** Resolve all dependencies.
79 This method is called after the Job has been finished, or
80 when it is deleted without being executed (performed by the
81 destructor).
82 The method will remove all entries stating that another Job
83 depends on this one.
84 */
85 void resolveDependencies ( Job* );
86
87 /** Retrieve a list of dependencies of this job. */
88 QList<Job*> getDependencies( Job* ) const;
89
90 // FIXME add factory method for singleton creation
91 static DependencyPolicy& instance();
92
93 bool canRun( Job* );
94
95 void free( Job* );
96
97 void release( Job* );
98
99 void destructed( Job* );
100
101 protected:
102 /** Query whether the job has an unresolved dependency.
103 In case it does, the policy will return false from canRun().
104 */
105 bool hasUnresolvedDependencies( Job* ) const;
106
107 DependencyPolicy();
108
109 private:
110 class Private;
111 Private * const d;
112
113 public:
114 /** This method should be useful for debugging purposes. */
115 void dumpJobDependencies();
116 };
117
118}
119
120#endif
121