9 #include "collection.h"
11 #include "collection_p.h"
12 #include "debuggingaids.h"
20 #include "dependencypolicy.h"
21 #include "executewrapper_p.h"
24 namespace ThreadWeaver
26 class 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 void cleanup(
const JobPointer &job, Thread *)
override
67 Collection::Collection()
68 : Job(new Private::Collection_Private)
72 Collection::Collection(Private::Collection_Private *d__)
77 Collection::~Collection()
79 MUTEX_ASSERT_UNLOCKED(mutex());
83 if (d()->api !=
nullptr) {
84 d()->dequeueElements(
this,
false);
92 REQUIRE(d()->api ==
nullptr || d()->selfIsExecuting ==
true);
93 REQUIRE(job !=
nullptr);
95 CollectionExecuteWrapper *wrapper =
new CollectionExecuteWrapper();
96 wrapper->setCollection(
this);
97 wrapper->wrap(job->setExecutor(wrapper));
98 d()->elements.append(job);
106 d()->stop_locked(
this);
109 void Collection::aboutToBeQueued_locked(QueueAPI *api)
111 Q_ASSERT(!mutex()->tryLock());
112 Q_ASSERT(d()->api ==
nullptr);
114 d()->selfExecuteWrapper.wrap(setExecutor(&d()->selfExecuteWrapper));
115 CollectionExecuteWrapper *wrapper =
new CollectionExecuteWrapper();
116 wrapper->setCollection(
this);
117 wrapper->wrap(setExecutor(wrapper));
118 Job::aboutToBeQueued_locked(api);
121 void Collection::aboutToBeDequeued_locked(QueueAPI *api)
123 Q_ASSERT(!mutex()->tryLock());
124 Q_ASSERT(api && d()->api == api);
125 d()->dequeueElements(
this,
true);
127 Job::aboutToBeDequeued_locked(api);
135 Q_ASSERT(d()->
self.isNull());
136 Q_ASSERT(d()->api !=
nullptr);
138 d()->selfIsExecuting =
true;
140 Job::execute(job, thread);
148 Private::Collection_Private *Collection::d()
150 return reinterpret_cast<Private::Collection_Private *
>(Job::d());
153 const Private::Collection_Private *Collection::d()
const
155 return reinterpret_cast<const Private::Collection_Private *
>(Job::d());
160 Q_ASSERT(!mutex()->tryLock());
161 Q_ASSERT(i >= 0 && i < d()->elements.size());
162 return d()->elements.at(i);
165 int Collection::elementCount()
const
169 return jobListLength_locked();
172 #if THREADWEAVER_BUILD_DEPRECATED_SINCE(5, 0)
173 int Collection::jobListLength()
const
177 return jobListLength_locked();
181 int Collection::jobListLength_locked()
const
183 return d()->elements.size();
188 addJob(make_job(job));
198 Collection &Collection::operator<<(JobInterface &job)
200 addJob(make_job_raw(&job));