File indexing completed on 2024-04-21 04:01:27

0001 /* -*- C++ -*-
0002     This file implements the DependencyPolicy class.
0003 
0004     SPDX-FileCopyrightText: 2004-2013 Mirko Boehm <mirko@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 
0008     $Id: DebuggingAids.cpp 20 2005-08-08 21:02:51Z mirko $
0009 */
0010 
0011 #ifndef DEPENDENCYPOLICY_H
0012 #define DEPENDENCYPOLICY_H
0013 
0014 #include <QtGlobal>
0015 
0016 // template <typename T> class QList;
0017 
0018 #include "queuepolicy.h"
0019 
0020 namespace ThreadWeaver
0021 {
0022 class JobInterface;
0023 class Dependency;
0024 
0025 /** @brief DependencyPolicy implements execution-time dependencies dependencies between Jobs.
0026  *
0027  *  To declare that Job B can only be executed when Job A is finished, call addDependency.
0028  *
0029  *  Be aware of circular dependencies. All dependencies on a Job will be removed if the Job object is destructed.
0030  *  Sequence uses dependencies to implement the ordered execution of the sequence elements.
0031  */
0032 class THREADWEAVER_EXPORT DependencyPolicy : public QueuePolicy
0033 {
0034 public:
0035     /** Destructor. */
0036     ~DependencyPolicy() override;
0037 
0038     /** @brief Add jobB as a dependency of jobA.
0039      *  jobA will only be executed after jobB has been successfully processed.
0040      *  @param jobA the depending job
0041      *  @param jobB the job jobA depends on
0042      */
0043     void addDependency(JobPointer jobA, JobPointer jobB);
0044     void addDependency(const Dependency &dep);
0045 
0046     /** @brief Remove a dependency.
0047      *  The dependency of jobA on jobB is removed. If no dependencies are left for jobA, canRun will return true.
0048      *  Returns false if the given object is not dependency of this job.
0049      *  @param jobA the depending job
0050      *  @param jobB the job jobA depends on
0051      *  @return true if dependency existed, false otherwise
0052      */
0053     bool removeDependency(JobPointer jobA, JobPointer jobB);
0054     bool removeDependency(const Dependency &dep);
0055 
0056     /** @brief Resolve all dependencies for a job.
0057      *  This method is called after the Job has been finished, or when it is deleted without being executed (performed by the
0058      *  destructor). The method will remove all entries stating that another Job depends on this one.
0059      */
0060     void resolveDependencies(JobPointer);
0061 
0062     // FIXME remove
0063     //    /** @brief Retrieve a list of dependencies of this job. */
0064     //    QList<JobPointer> getDependencies(JobPointer) const;
0065 
0066     static DependencyPolicy &instance();
0067 
0068     bool canRun(JobPointer) override;
0069 
0070     void free(JobPointer) override;
0071 
0072     void release(JobPointer) override;
0073 
0074     void destructed(JobInterface *job) override;
0075 
0076     bool isEmpty() const;
0077 
0078 protected:
0079     /** @brief Query whether the job has an unresolved dependency.
0080      *  In case it does, the policy will return false from canRun().
0081      */
0082     bool hasUnresolvedDependencies(JobPointer) const;
0083 
0084 private:
0085     DependencyPolicy();
0086     class Private;
0087     Private *const d;
0088 };
0089 
0090 }
0091 
0092 #endif