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 | |
15 | namespace ThreadWeaver |
16 | { |
17 | class State; |
18 | class Job; |
19 | class Thread; |
20 | class WeaverImplState; |
21 | class SuspendingState; |
22 | |
23 | namespace Private |
24 | { |
25 | class Weaver_Private; |
26 | } |
27 | |
28 | /** @brief A Weaver manages worker threads. |
29 | * |
30 | * It creates an inventory of Thread objects to which it assigns jobs from its queue. |
31 | * It extends the API of Queue, hiding methods that need to be public to implement state handling, but |
32 | * should not be exposed in general. |
33 | */ |
34 | class THREADWEAVER_EXPORT Weaver : public QueueAPI |
35 | { |
36 | Q_OBJECT |
37 | public: |
38 | explicit Weaver(QObject *parent = nullptr); |
39 | ~Weaver() override; |
40 | void shutDown() override; |
41 | void shutDown_p() override; |
42 | |
43 | const State *state() const override; |
44 | State *state() override; |
45 | |
46 | void setMaximumNumberOfThreads(int cap) override; |
47 | int maximumNumberOfThreads() const override; |
48 | int currentNumberOfThreads() const override; |
49 | |
50 | void setState(StateId); |
51 | void enqueue(const QList<JobPointer> &jobs) override; |
52 | bool dequeue(const JobPointer &job) override; |
53 | void dequeue() override; |
54 | void finish() override; |
55 | void suspend() override; |
56 | void resume() override; |
57 | bool isEmpty() const override; |
58 | bool isIdle() const override; |
59 | int queueLength() const override; |
60 | JobPointer applyForWork(Thread *thread, bool wasBusy) override; |
61 | void waitForAvailableJob(Thread *th) override; |
62 | void blockThreadUntilJobsAreBeingAssigned(Thread *th); |
63 | void blockThreadUntilJobsAreBeingAssigned_locked(Thread *th); |
64 | void incActiveThreadCount(); |
65 | void decActiveThreadCount(); |
66 | int activeThreadCount(); |
67 | |
68 | void threadEnteredRun(Thread *thread); |
69 | JobPointer takeFirstAvailableJobOrSuspendOrWait(Thread *th, bool threadWasBusy, bool suspendIfAllThreadsInactive, bool justReturning); |
70 | void requestAbort() override; |
71 | void reschedule() override; |
72 | |
73 | // FIXME: rename _p to _locked: |
74 | friend class WeaverImplState; |
75 | friend class SuspendingState; |
76 | void setState_p(StateId); |
77 | void setMaximumNumberOfThreads_p(int cap) override; |
78 | int maximumNumberOfThreads_p() const override; |
79 | int currentNumberOfThreads_p() const override; |
80 | void enqueue_p(const QList<JobPointer> &jobs); |
81 | bool dequeue_p(JobPointer job) override; |
82 | void dequeue_p() override; |
83 | void finish_p() override; |
84 | void suspend_p() override; |
85 | void resume_p() override; |
86 | bool isEmpty_p() const override; |
87 | bool isIdle_p() const override; |
88 | int queueLength_p() const override; |
89 | void requestAbort_p() override; |
90 | |
91 | Q_SIGNALS: |
92 | /** @brief A Thread has been created. */ |
93 | void threadStarted(ThreadWeaver::Thread *); |
94 | /** @brief A thread has exited. */ |
95 | void threadExited(ThreadWeaver::Thread *); |
96 | /** @brief A thread has been suspended. */ |
97 | void threadSuspended(ThreadWeaver::Thread *); |
98 | |
99 | protected: |
100 | void adjustActiveThreadCount(int diff); |
101 | virtual Thread *createThread(); |
102 | void adjustInventory(int noOfNewJobs); |
103 | |
104 | private: |
105 | ThreadWeaver::Private::Weaver_Private *d(); |
106 | const ThreadWeaver::Private::Weaver_Private *d() const; |
107 | }; |
108 | |
109 | } // namespace ThreadWeaver |
110 | |
111 | #endif // WeaverImpl_H |
112 | |