11#include "collection_p.h"
12#include "debuggingaids.h"
20#include "dependencypolicy.h"
21#include "executewrapper_p.h"
26class CollectionExecuteWrapper :
public ExecuteWrapper
29 CollectionExecuteWrapper()
34 void setCollection(Collection *collection_)
36 collection = collection_;
39 void begin(
const JobPointer &job, Thread *thread)
override
41 TWDEBUG(4,
"CollectionExecuteWrapper::begin: collection %p\n", collection);
42 ExecuteWrapper::begin(job, thread);
44 collection->d()->elementStarted(collection, job, thread);
45 ExecuteWrapper::begin(job, thread);
48 void end(
const JobPointer &job, Thread *thread)
override
50 TWDEBUG(4,
"CollectionExecuteWrapper::end: collection %p\n", collection);
52 ExecuteWrapper::end(job, thread);
53 collection->d()->elementFinished(collection, job, thread);
56 bool ownedByJob()
const override
62 ThreadWeaver::Collection *collection;
65Collection::Collection()
66 :
Job(new Private::Collection_Private)
70Collection::Collection(Private::Collection_Private *d__)
75Collection::~Collection()
77 MUTEX_ASSERT_UNLOCKED(mutex());
81 if (d()->api !=
nullptr) {
82 d()->dequeueElements(
this,
false);
90 REQUIRE(d()->api ==
nullptr || d()->selfIsExecuting ==
true);
91 REQUIRE(job !=
nullptr);
93 CollectionExecuteWrapper *wrapper =
new CollectionExecuteWrapper();
94 wrapper->setCollection(
this);
95 wrapper->wrap(job->setExecutor(wrapper));
96 d()->elements.append(job);
107 d()->requestAbort(
this);
112 Q_ASSERT(!
mutex()->tryLock());
113 Q_ASSERT(d()->api ==
nullptr);
115 d()->selfExecuteWrapper.wrap(
setExecutor(&d()->selfExecuteWrapper));
116 CollectionExecuteWrapper *wrapper =
new CollectionExecuteWrapper();
117 wrapper->setCollection(
this);
124 Q_ASSERT(!
mutex()->tryLock());
125 Q_ASSERT(api && d()->api == api);
126 d()->dequeueElements(
this,
true);
129 Q_ASSERT(
dynamic_cast<CollectionExecuteWrapper *
>(
executor()));
130 auto wrapper =
static_cast<CollectionExecuteWrapper *
>(
executor());
131 wrapper->unwrap(
this);
134 Q_ASSERT(
executor() == &d()->selfExecuteWrapper);
135 d()->selfExecuteWrapper.unwrap(
this);
145 Q_ASSERT(d()->self.isNull());
146 Q_ASSERT(d()->api !=
nullptr);
148 d()->selfIsExecuting =
true;
158Private::Collection_Private *Collection::d()
160 return reinterpret_cast<Private::Collection_Private *
>(Job::d());
163const Private::Collection_Private *Collection::d()
const
165 return reinterpret_cast<const Private::Collection_Private *
>(Job::d());
170 Q_ASSERT(!
mutex()->tryLock());
171 Q_ASSERT(i >= 0 && i < d()->elements.size());
172 return d()->elements.at(i);
184 return d()->elements.size();
201 addJob(make_job_raw(&job));
A Collection is a vector of Jobs that will be queued together.
virtual void addJob(JobPointer)
Append a job to the collection.
virtual int jobListLength_locked() const
Return the number of jobs in the joblist.
int elementCount() const
Return the number of elements in the collection.
void aboutToBeQueued_locked(QueueAPI *api) override
Overload to queue the collection.
void execute(const JobPointer &job, Thread *) override
Overload the execute method.
Collection & operator<<(ThreadWeaver::JobInterface *job)
Add the job to this collection by pointer.
void stop()
Stop processing, dequeue all remaining Jobs.
void run(JobPointer self, Thread *thread) override
Overload run().
void aboutToBeDequeued_locked(QueueAPI *api) override
Overload to dequeue the collection.
void requestAbort() override
Dequeue all remaining Jobs and request abortion of all running jobs.
JobPointer jobAt(int i)
Return a ref-erence to the job in the job list at position i.
A Job is a simple abstraction of an action that is to be executed in a thread context.
Executor * setExecutor(Executor *executor) override
Set the Executor object that is supposed to run the job.
QMutex * mutex() const override
The mutex used to protect this job.
void aboutToBeDequeued_locked(QueueAPI *api) override
Called from aboutToBeDequeued() while the mutex is being held.
void execute(const JobPointer &job, Thread *) override
Perform the job.
Executor * executor() const override
Returns the executor currently set on the Job.
void aboutToBeQueued_locked(QueueAPI *api) override
Called from aboutToBeQueued() while the mutex is being held.
void requestAbort() override
Abort the execution of the job.
Thread represents a worker thread in a Queue's inventory.