1/* -*- C++ -*-
2 This file implements the public interfaces of the WeaverImpl class.
3
4 SPDX-FileCopyrightText: 2005-2013 Mirko Boehm <mirko@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#ifndef WeaverImpl_H
10#define WeaverImpl_H
11
12#include "queueapi.h"
13#include <QObject>
14
15namespace ThreadWeaver
16{
17class State;
18class Job;
19class Thread;
20class WeaverImplState;
21class SuspendingState;
22
23namespace Private
24{
25class Weaver_Private;
26}
27
28/*!
29 * \class ThreadWeaver::Weaver
30 * \inheaderfile ThreadWeaver/Weaver
31 * \inmodule ThreadWeaver
32 *
33 * \brief A Weaver manages worker threads.
34 *
35 * It creates an inventory of Thread objects to which it assigns jobs from its queue.
36 * It extends the API of Queue, hiding methods that need to be public to implement state handling, but
37 * should not be exposed in general.
38 */
39class THREADWEAVER_EXPORT Weaver : public QueueAPI
40{
41 Q_OBJECT
42public:
43 /*!
44 */
45 explicit Weaver(QObject *parent = nullptr);
46 ~Weaver() override;
47 void shutDown() override;
48 void shutDown_p() override;
49
50 const State *state() const override;
51 State *state() override;
52
53 void setMaximumNumberOfThreads(int cap) override;
54 int maximumNumberOfThreads() const override;
55 int currentNumberOfThreads() const override;
56
57 /*!
58 */
59 void setState(StateId);
60 void enqueue(const QList<JobPointer> &jobs) override;
61 bool dequeue(const JobPointer &job) override;
62 void dequeue() override;
63 void finish() override;
64 void suspend() override;
65 void resume() override;
66 bool isEmpty() const override;
67 bool isIdle() const override;
68 int queueLength() const override;
69 JobPointer applyForWork(Thread *thread, bool wasBusy) override;
70 void waitForAvailableJob(Thread *th) override;
71 /*!
72 */
73 void blockThreadUntilJobsAreBeingAssigned(Thread *th);
74 /*!
75 */
76 void blockThreadUntilJobsAreBeingAssigned_locked(Thread *th);
77 /*!
78 */
79 void incActiveThreadCount();
80 /*!
81 */
82 void decActiveThreadCount();
83 /*!
84 */
85 int activeThreadCount();
86
87 /*!
88 */
89 void threadEnteredRun(Thread *thread);
90 /*!
91 */
92 JobPointer takeFirstAvailableJobOrSuspendOrWait(Thread *th, bool threadWasBusy, bool suspendIfAllThreadsInactive, bool justReturning);
93 void requestAbort() override;
94 void reschedule() override;
95
96 // FIXME: rename _p to _locked:
97 friend class WeaverImplState;
98 friend class SuspendingState;
99 /*!
100 */
101 void setState_p(StateId);
102 void setMaximumNumberOfThreads_p(int cap) override;
103 int maximumNumberOfThreads_p() const override;
104 int currentNumberOfThreads_p() const override;
105 /*!
106 */
107 void enqueue_p(const QList<JobPointer> &jobs);
108 bool dequeue_p(JobPointer job) override;
109 void dequeue_p() override;
110 void finish_p() override;
111 void suspend_p() override;
112 void resume_p() override;
113 bool isEmpty_p() const override;
114 bool isIdle_p() const override;
115 int queueLength_p() const override;
116 void requestAbort_p() override;
117
118Q_SIGNALS:
119 /*! \brief A Thread has been created. */
120 void threadStarted(ThreadWeaver::Thread *);
121 /*! \brief A thread has exited. */
122 void threadExited(ThreadWeaver::Thread *);
123 /*! \brief A thread has been suspended. */
124 void threadSuspended(ThreadWeaver::Thread *);
125
126protected:
127 /*!
128 */
129 void adjustActiveThreadCount(int diff);
130 /*!
131 */
132 virtual Thread *createThread();
133 /*!
134 */
135 void adjustInventory(int noOfNewJobs);
136
137private:
138 /*!
139 */
140 ThreadWeaver::Private::Weaver_Private *d();
141 /*!
142 */
143 const ThreadWeaver::Private::Weaver_Private *d() const;
144};
145
146} // namespace ThreadWeaver
147
148#endif // WeaverImpl_H
149

source code of threadweaver/src/weaver.h