File indexing completed on 2024-04-28 04:01:24

0001 /* -*- C++ -*-
0002     This file implements the public interfaces of the WeaverImpl class.
0003 
0004     SPDX-FileCopyrightText: 2005-2013 Mirko Boehm <mirko@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef WeaverImpl_H
0010 #define WeaverImpl_H
0011 
0012 #include "queueapi.h"
0013 #include <QObject>
0014 
0015 namespace ThreadWeaver
0016 {
0017 class State;
0018 class Job;
0019 class Thread;
0020 class WeaverImplState;
0021 class SuspendingState;
0022 
0023 namespace Private
0024 {
0025 class Weaver_Private;
0026 }
0027 
0028 /** @brief A Weaver manages worker threads.
0029  *
0030  * It creates an inventory of Thread objects to which it assigns jobs from its queue.
0031  * It extends the API of Queue, hiding methods that need to be public to implement state handling, but
0032  * should not be exposed in general.
0033  */
0034 class THREADWEAVER_EXPORT Weaver : public QueueAPI
0035 {
0036     Q_OBJECT
0037 public:
0038     explicit Weaver(QObject *parent = nullptr);
0039     ~Weaver() override;
0040     void shutDown() override;
0041     void shutDown_p() override;
0042 
0043     const State *state() const override;
0044     State *state() override;
0045 
0046     void setMaximumNumberOfThreads(int cap) override;
0047     int maximumNumberOfThreads() const override;
0048     int currentNumberOfThreads() const override;
0049 
0050     void setState(StateId);
0051     void enqueue(const QList<JobPointer> &jobs) override;
0052     bool dequeue(const JobPointer &job) override;
0053     void dequeue() override;
0054     void finish() override;
0055     void suspend() override;
0056     void resume() override;
0057     bool isEmpty() const override;
0058     bool isIdle() const override;
0059     int queueLength() const override;
0060     JobPointer applyForWork(Thread *thread, bool wasBusy) override;
0061     void waitForAvailableJob(Thread *th) override;
0062     void blockThreadUntilJobsAreBeingAssigned(Thread *th);
0063     void blockThreadUntilJobsAreBeingAssigned_locked(Thread *th);
0064     void incActiveThreadCount();
0065     void decActiveThreadCount();
0066     int activeThreadCount();
0067 
0068     void threadEnteredRun(Thread *thread);
0069     JobPointer takeFirstAvailableJobOrSuspendOrWait(Thread *th, bool threadWasBusy, bool suspendIfAllThreadsInactive, bool justReturning);
0070     void requestAbort() override;
0071     void reschedule() override;
0072 
0073     // FIXME: rename _p to _locked:
0074     friend class WeaverImplState;
0075     friend class SuspendingState;
0076     void setState_p(StateId);
0077     void setMaximumNumberOfThreads_p(int cap) override;
0078     int maximumNumberOfThreads_p() const override;
0079     int currentNumberOfThreads_p() const override;
0080     void enqueue_p(const QList<JobPointer> &jobs);
0081     bool dequeue_p(JobPointer job) override;
0082     void dequeue_p() override;
0083     void finish_p() override;
0084     void suspend_p() override;
0085     void resume_p() override;
0086     bool isEmpty_p() const override;
0087     bool isIdle_p() const override;
0088     int queueLength_p() const override;
0089     void requestAbort_p() override;
0090 
0091 Q_SIGNALS:
0092     /** @brief A Thread has been created. */
0093     void threadStarted(ThreadWeaver::Thread *);
0094     /** @brief A thread has exited. */
0095     void threadExited(ThreadWeaver::Thread *);
0096     /** @brief A thread has been suspended. */
0097     void threadSuspended(ThreadWeaver::Thread *);
0098 
0099 protected:
0100     void adjustActiveThreadCount(int diff);
0101     virtual Thread *createThread();
0102     void adjustInventory(int noOfNewJobs);
0103 
0104 private:
0105     ThreadWeaver::Private::Weaver_Private *d();
0106     const ThreadWeaver::Private::Weaver_Private *d() const;
0107 };
0108 
0109 } // namespace ThreadWeaver
0110 
0111 #endif // WeaverImpl_H