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
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);
99void Collection::stop()
104void Collection::requestAbort()
107 d()->requestAbort(
this);
110void Collection::aboutToBeQueued_locked(QueueAPI *api)
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);
118 wrapper->wrap(setExecutor(wrapper));
119 Job::aboutToBeQueued_locked(api);
122void Collection::aboutToBeDequeued_locked(QueueAPI *api)
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);
137 Job::aboutToBeDequeued_locked(api);
145 Q_ASSERT(d()->self.isNull());
146 Q_ASSERT(d()->api !=
nullptr);
148 d()->selfIsExecuting =
true;
150 Job::execute(job, thread);
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);
175int Collection::elementCount()
const
179 return jobListLength_locked();
182int Collection::jobListLength_locked()
const
184 return d()->elements.size();
189 addJob(make_job(job));
199Collection &Collection::operator<<(JobInterface &job)
201 addJob(make_job_raw(&job));
A Collection is a vector of Jobs that will be queued together.
Thread represents a worker thread in a Queue's inventory.