File indexing completed on 2024-04-21 04:01:27

0001 /* -*- C++ -*-
0002     This file is part of ThreadWeaver.
0003 
0004     SPDX-FileCopyrightText: 2004-2013 Mirko Boehm <mirko@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef COLLECTION_COLLECTION_P_H
0010 #define COLLECTION_COLLECTION_P_H
0011 
0012 #include <QList>
0013 #include <QMutex>
0014 
0015 #include "executewrapper_p.h"
0016 #include "job_p.h"
0017 
0018 namespace ThreadWeaver
0019 {
0020 class Collection;
0021 
0022 namespace Private
0023 {
0024 class CollectionSelfExecuteWrapper : public ThreadWeaver::ExecuteWrapper
0025 {
0026 public:
0027     void begin(const JobPointer &, Thread *) override;
0028     void end(const JobPointer &, Thread *) override;
0029 
0030     void callBegin();
0031     void callEnd();
0032 
0033 private:
0034     JobPointer job_;
0035     Thread *thread_;
0036 };
0037 
0038 class Collection_Private : public Job_Private
0039 {
0040 public:
0041     Collection_Private();
0042     ~Collection_Private() override;
0043 
0044     /** Dequeue all elements of the collection.
0045      * Note: This will not dequeue the collection itself.
0046      */
0047     void dequeueElements(Collection *collection, bool queueApiIsLocked);
0048 
0049     /** Perform the task usually done when one individual job is
0050      * finished, but in our case only when the whole collection
0051      * is finished or partly dequeued.
0052      */
0053     void finalCleanup(Collection *collection);
0054 
0055     /** @brief Enqueue the elements of the collection. */
0056     void enqueueElements();
0057 
0058     void elementStarted(Collection *collection, JobPointer, Thread *);
0059     void elementFinished(Collection *collection, JobPointer job, Thread *thread);
0060 
0061     /** @brief Prepare to enqueue the elements. */
0062     virtual void prepareToEnqueueElements();
0063 
0064     virtual JobInterface::Status updateStatus(Collection *collection, JobPointer job);
0065 
0066     /** @brief Process a completed element. */
0067     virtual void processCompletedElement(Collection *collection, JobPointer job, Thread *thread);
0068 
0069     /** @brief Implement stop. */
0070     void stop(Collection *collection);
0071 
0072     void requestAbort(Collection *collection);
0073 
0074     /** @brief Called before an element will be dequeued. */
0075     virtual void elementDequeued(const JobPointer &)
0076     {
0077     }
0078 
0079     /* The elements of the collection. */
0080     QList<JobPointer> elements;
0081 
0082     /* The Weaver interface this collection is queued in. */
0083     QueueAPI *api;
0084 
0085     /* Counter for the finished jobs.
0086        Set to the number of elements when started.
0087        When zero, all elements are done.
0088     */
0089     QAtomicInt jobCounter;
0090     QAtomicInt jobsStarted;
0091     CollectionSelfExecuteWrapper selfExecuteWrapper;
0092     JobPointer self;
0093     bool selfIsExecuting;
0094 };
0095 
0096 }
0097 
0098 }
0099 
0100 #endif // COLLECTION_COLLECTION_P_H