| 1 | /* -*- C++ -*- |
| 2 | This file is part of ThreadWeaver. |
| 3 | |
| 4 | SPDX-FileCopyrightText: 2004-2013 Mirko Boehm <mirko@kde.org> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #ifndef COLLECTION_COLLECTION_P_H |
| 10 | #define COLLECTION_COLLECTION_P_H |
| 11 | |
| 12 | #include <QList> |
| 13 | #include <QMutex> |
| 14 | |
| 15 | #include "executewrapper_p.h" |
| 16 | #include "job_p.h" |
| 17 | |
| 18 | namespace ThreadWeaver |
| 19 | { |
| 20 | class Collection; |
| 21 | |
| 22 | namespace Private |
| 23 | { |
| 24 | class CollectionSelfExecuteWrapper : public ThreadWeaver::ExecuteWrapper |
| 25 | { |
| 26 | public: |
| 27 | void begin(const JobPointer &, Thread *) override; |
| 28 | void end(const JobPointer &, Thread *) override; |
| 29 | |
| 30 | void callBegin(); |
| 31 | void callEnd(); |
| 32 | |
| 33 | private: |
| 34 | JobPointer job_; |
| 35 | Thread *thread_; |
| 36 | }; |
| 37 | |
| 38 | class Collection_Private : public Job_Private |
| 39 | { |
| 40 | public: |
| 41 | Collection_Private(); |
| 42 | ~Collection_Private() override; |
| 43 | |
| 44 | /* Dequeue all elements of the collection. |
| 45 | * @note This will not dequeue the collection itself. |
| 46 | */ |
| 47 | void dequeueElements(Collection *collection, bool queueApiIsLocked); |
| 48 | |
| 49 | /* Perform the task usually done when one individual job is |
| 50 | * finished, but in our case only when the whole collection |
| 51 | * is finished or partly dequeued. |
| 52 | */ |
| 53 | void finalCleanup(Collection *collection); |
| 54 | |
| 55 | /* @brief Enqueue the elements of the collection. */ |
| 56 | void enqueueElements(); |
| 57 | |
| 58 | void elementStarted(Collection *collection, JobPointer, Thread *); |
| 59 | void elementFinished(Collection *collection, JobPointer job, Thread *thread); |
| 60 | |
| 61 | /* @brief Prepare to enqueue the elements. */ |
| 62 | virtual void prepareToEnqueueElements(); |
| 63 | |
| 64 | virtual JobInterface::Status updateStatus(Collection *collection, JobPointer job); |
| 65 | |
| 66 | /* @brief Process a completed element. */ |
| 67 | virtual void processCompletedElement(Collection *collection, JobPointer job, Thread *thread); |
| 68 | |
| 69 | /* @brief Implement stop. */ |
| 70 | void stop(Collection *collection); |
| 71 | |
| 72 | void requestAbort(Collection *collection); |
| 73 | |
| 74 | /* @brief Called before an element will be dequeued. */ |
| 75 | virtual void elementDequeued(const JobPointer &) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | /* The elements of the collection. */ |
| 80 | QList<JobPointer> elements; |
| 81 | |
| 82 | /* The Weaver interface this collection is queued in. */ |
| 83 | QueueAPI *api; |
| 84 | |
| 85 | /* Counter for the finished jobs. |
| 86 | Set to the number of elements when started. |
| 87 | When zero, all elements are done. |
| 88 | */ |
| 89 | QAtomicInt jobCounter; |
| 90 | QAtomicInt jobsStarted; |
| 91 | CollectionSelfExecuteWrapper selfExecuteWrapper; |
| 92 | JobPointer self; |
| 93 | bool selfIsExecuting; |
| 94 | }; |
| 95 | |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #endif // COLLECTION_COLLECTION_P_H |
| 100 | |