[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kdelibs/frameworks] tier1/threadweaver/src/Weaver: Replace the success() method and isFinished() wi
From:       Mirko Boehm (Endocode) <mirko () endocode ! com>
Date:       2013-11-04 17:14:19
Message-ID: E1VdNjH-00019j-OG () scm ! kde ! org
[Download RAW message or body]

Git commit 6ca7b2905d530e0178548f0fe872ee580786a6cd by Mirko Boehm (Endocode).
Committed on 01/11/2013 at 15:42.
Pushed by mirko into branch 'frameworks'.

Replace the success() method and isFinished() with status().

The two concepts where overlapping, and it was unclear how to report an
error from a job.

M  +1    -0    tier1/threadweaver/src/Weaver/DependencyPolicy.cpp
M  +12   -6    tier1/threadweaver/src/Weaver/IdDecorator.cpp
M  +2    -1    tier1/threadweaver/src/Weaver/IdDecorator.h
M  +21   -12   tier1/threadweaver/src/Weaver/Job.cpp
M  +11   -3    tier1/threadweaver/src/Weaver/Job.h
M  +11   -4    tier1/threadweaver/src/Weaver/JobCollection.cpp
M  +13   -1    tier1/threadweaver/src/Weaver/JobInterface.h

http://commits.kde.org/kdelibs/6ca7b2905d530e0178548f0fe872ee580786a6cd

diff --git a/tier1/threadweaver/src/Weaver/DependencyPolicy.cpp \
b/tier1/threadweaver/src/Weaver/DependencyPolicy.cpp index d85ed34..93a5bb1 100644
--- a/tier1/threadweaver/src/Weaver/DependencyPolicy.cpp
+++ b/tier1/threadweaver/src/Weaver/DependencyPolicy.cpp
@@ -174,6 +174,7 @@ bool DependencyPolicy::canRun(JobPointer job)
 void DependencyPolicy::free(JobPointer job)
 {
     REQUIRE(job != 0);
+    REQUIRE(job->status() > Job::Status_Running);
     if (job->success()) {
         resolveDependencies(job);
         debug( 3, "DependencyPolicy::free: dependencies resolved for job %p.\n", \
                (void*)job.data());
diff --git a/tier1/threadweaver/src/Weaver/IdDecorator.cpp \
b/tier1/threadweaver/src/Weaver/IdDecorator.cpp index 1430973..dcb89d2 100644
--- a/tier1/threadweaver/src/Weaver/IdDecorator.cpp
+++ b/tier1/threadweaver/src/Weaver/IdDecorator.cpp
@@ -32,12 +32,6 @@ QMutex *IdDecorator::mutex() const
     return job()->mutex();
 }
 
-void IdDecorator::setFinished(bool status)
-{
-    Q_ASSERT(d1);
-    job()->setFinished(status);
-}
-
 void IdDecorator::run(JobPointer self, Thread *thread)
 {
     Q_ASSERT(d1);
@@ -128,6 +122,18 @@ int IdDecorator::priority() const
     return job()->priority();
 }
 
+void IdDecorator::setStatus(JobInterface::Status status)
+{
+    Q_ASSERT(d1);
+    job()->setStatus(status);
+}
+
+JobInterface::Status IdDecorator::status() const
+{
+    Q_ASSERT(d1);
+    return job()->status();
+}
+
 Executor *IdDecorator::executor() const
 {
     Q_ASSERT(d1);
diff --git a/tier1/threadweaver/src/Weaver/IdDecorator.h \
b/tier1/threadweaver/src/Weaver/IdDecorator.h index d96fd5f..50872e8 100644
--- a/tier1/threadweaver/src/Weaver/IdDecorator.h
+++ b/tier1/threadweaver/src/Weaver/IdDecorator.h
@@ -45,6 +45,8 @@ public:
     Executor* setExecutor(Executor* executor) Q_DECL_OVERRIDE;
     Executor* executor() const Q_DECL_OVERRIDE;
     int priority() const Q_DECL_OVERRIDE;
+    void setStatus(Status) Q_DECL_OVERRIDE;
+    Status status() const Q_DECL_OVERRIDE;
     bool success () const Q_DECL_OVERRIDE;
     void requestAbort() Q_DECL_OVERRIDE;
     void aboutToBeQueued(QueueAPI *api) Q_DECL_OVERRIDE;
@@ -62,7 +64,6 @@ protected:
     void defaultBegin(JobPointer job, Thread* thread) Q_DECL_OVERRIDE;
     void defaultEnd(JobPointer job, Thread* thread) Q_DECL_OVERRIDE;
 
-    void setFinished(bool status) Q_DECL_OVERRIDE;
     QMutex* mutex() const Q_DECL_OVERRIDE;
 
 private:
diff --git a/tier1/threadweaver/src/Weaver/Job.cpp \
b/tier1/threadweaver/src/Weaver/Job.cpp index 5f2a521..66b76cd 100644
--- a/tier1/threadweaver/src/Weaver/Job.cpp
+++ b/tier1/threadweaver/src/Weaver/Job.cpp
@@ -33,6 +33,7 @@ $Id: Job.cpp 20 2005-08-08 21:02:51Z mirko $
 #include <DebuggingAids.h>
 #include <Thread.h>
 #include <QAtomicPointer>
+#include <QAtomicInt>
 
 #include "QueuePolicy.h"
 #include "DependencyPolicy.h"
@@ -77,7 +78,7 @@ class Job::Private
 public:
     Private ()
         : mutex(QMutex::NonRecursive)
-        , finished (false)
+        , status(Job::Status_NoStatus)
         , executor(&defaultExecutor)
     {}
 
@@ -88,8 +89,8 @@ public:
     QList<QueuePolicy*> queuePolicies;
 
     mutable QMutex mutex;
-    /* d->finished is set to true when the Job has been executed. */
-    bool finished;
+    /* @brief The status of the Job. */
+    QAtomicInt status;
 
     /** The Executor that will execute this Job. */
     QAtomicPointer<Executor> executor;
@@ -107,6 +108,7 @@ Job::Job()
 #if not defined NDEBUG
     d->debugExecuteWrapper.wrap(setExecutor(&d->debugExecuteWrapper));
 #endif
+    d->status.storeRelease(Status_New);
 }
 
 Job::~Job()
@@ -122,9 +124,10 @@ void Job::execute(JobPointer job, Thread *th)
     Executor* executor = d->executor.loadAcquire();
     Q_ASSERT(executor); //may never be unset!
     executor->begin(job, th);
+    setStatus(Status_Running);
     executor->execute(job, th);
+    setStatus(Status_Success);
     executor->end(job, th);
-    setFinished (true);
     executor->cleanup(job, th);
 }
 
@@ -132,7 +135,6 @@ void Job::blockingExecute()
 {
     execute(ManagedJobPointer<Job>(this), 0);
 }
-
 Executor *Job::setExecutor(Executor *executor)
 {
     return d->executor.fetchAndStoreOrdered(executor == 0 ? &defaultExecutor : \
executor); @@ -148,9 +150,20 @@ int Job::priority () const
     return 0;
 }
 
+void Job::setStatus(JobInterface::Status status)
+{
+    d->status.storeRelease(status);
+}
+
+JobInterface::Status Job::status() const
+{
+    // since status is set only through setStatus, this should be safe:
+    return static_cast<Status>(d->status.loadAcquire());
+}
+
 bool Job::success () const
 {
-    return true;
+    return d->status.loadAcquire() == Status_Success;
 }
 
 void Job::freeQueuePolicyResources(JobPointer job)
@@ -211,12 +224,8 @@ QList<QueuePolicy *> Job::queuePolicies() const
 
 bool Job::isFinished() const
 {
-    return d->finished;
-}
-
-void Job::setFinished(bool status)
-{
-    d->finished = status;
+    const Status s = status();
+    return s == Status_Success || s == Status_Failed || s == Status_Aborted;
 }
 
 QMutex* Job::mutex() const
diff --git a/tier1/threadweaver/src/Weaver/Job.h \
b/tier1/threadweaver/src/Weaver/Job.h index a9eb690..c8ed82c 100644
--- a/tier1/threadweaver/src/Weaver/Job.h
+++ b/tier1/threadweaver/src/Weaver/Job.h
@@ -101,6 +101,17 @@ public:
      * the execution order of jobs. */
     int priority() const Q_DECL_OVERRIDE;
 
+    /** @brief Set the status of the Job.
+     *
+     * Do not call this method unless you know what you are doing, please :-) */
+    void setStatus(Status) Q_DECL_OVERRIDE;
+
+    /** @brief The status of the job.
+     *
+     * The status will be changed to Status_Success if the run() method exits \
normally. +     */
+    Status status() const Q_DECL_OVERRIDE;
+
     /** Return whether the Job finished successfully or not.
      * The default implementation simply returns true. Overload in derived classes \
                if the derived Job class can fail.
      *
@@ -204,9 +215,6 @@ protected:
      * decorated expose the decorator's address, not the address of the decorated \
object. */  void defaultEnd(JobPointer job, Thread* thread) Q_DECL_OVERRIDE;
 
-    /** Call with status = true to mark this job as done. */
-    void setFinished(bool status) Q_DECL_OVERRIDE;
-
     /** The mutex used to protect this job. */
     QMutex* mutex() const Q_DECL_OVERRIDE;
 
diff --git a/tier1/threadweaver/src/Weaver/JobCollection.cpp \
b/tier1/threadweaver/src/Weaver/JobCollection.cpp index 87b3de2..ad7a2aa 100644
--- a/tier1/threadweaver/src/Weaver/JobCollection.cpp
+++ b/tier1/threadweaver/src/Weaver/JobCollection.cpp
@@ -40,7 +40,7 @@ http://creative-destruction.me $
 
 namespace ThreadWeaver {
 
-class CollectionExecuteWrapper : public ThreadWeaver::ExecuteWrapper {
+class CollectionExecuteWrapper : public ExecuteWrapper {
 public:
     CollectionExecuteWrapper()
         : collection(0)
@@ -50,13 +50,20 @@ public:
         collection = collection_;
     }
 
-    void execute(ThreadWeaver::JobPointer job, ThreadWeaver::Thread *thread) \
Q_DECL_OVERRIDE { +    void begin(JobPointer job, Thread* thread) Q_DECL_OVERRIDE {
+        ExecuteWrapper::begin(job, thread);
         Q_ASSERT(collection);
         collection->elementStarted(job, thread);
-        executeWrapped(job, thread);
+    }
+
+    void end(JobPointer job, Thread* thread) Q_DECL_OVERRIDE {
+        Q_ASSERT(collection);
         collection->elementFinished(job, thread);
+        ExecuteWrapper::end(job, thread);
     }
 
+
+
     void cleanup(JobPointer job, Thread *) Q_DECL_OVERRIDE {
         //Once job is unwrapped from us, this object is dangling. Job::executor \
                points to the next higher up execute wrapper.
         //It is thus safe to "delete this". By no means add any later steps after \
delete! @@ -255,7 +262,7 @@ void JobCollection::finalCleanup()
     Q_ASSERT(!self().isNull());
     Q_ASSERT(!mutex()->tryLock());
     freeQueuePolicyResources(self());
-    setFinished(true);
+    setStatus(Status_Success);
     d->api = 0;
 }
 
diff --git a/tier1/threadweaver/src/Weaver/JobInterface.h \
b/tier1/threadweaver/src/Weaver/JobInterface.h index 8aff443..3de324c 100644
--- a/tier1/threadweaver/src/Weaver/JobInterface.h
+++ b/tier1/threadweaver/src/Weaver/JobInterface.h
@@ -20,12 +20,25 @@ typedef QSharedPointer<JobInterface> JobPointer;
 
 class THREADWEAVER_EXPORT JobInterface {
 public:
+    enum Status {
+        Status_NoStatus = 0,
+        Status_New,
+        Status_Queued,
+        Status_Running,
+        Status_Success,
+        Status_Failed,
+        Status_Aborted,
+        Status_NumberOfStatuses,
+    };
+
     virtual ~JobInterface() {}
     virtual void execute(JobPointer job, Thread*) = 0;
     virtual void blockingExecute() = 0;
     virtual Executor* setExecutor(Executor* executor) = 0;
     virtual Executor* executor() const = 0;
     virtual int priority() const = 0;
+    virtual Status status() const = 0;
+    virtual void setStatus(Status) = 0;
     virtual bool success () const = 0;
     virtual void requestAbort() = 0;
     virtual void aboutToBeQueued(QueueAPI *api) = 0;
@@ -41,7 +54,6 @@ public:
     friend class Executor;
     virtual void defaultBegin(JobPointer job, Thread* thread) = 0;
     virtual void defaultEnd(JobPointer job, Thread* thread) = 0;
-    virtual void setFinished(bool status) = 0;
     virtual QMutex* mutex() const = 0;
 };
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic