ThreadWeaver

weaver.h
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/** @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 */
34class THREADWEAVER_EXPORT Weaver : public QueueAPI
35{
37public:
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;
67
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
92 /** @brief A Thread has been created. */
94 /** @brief A thread has exited. */
96 /** @brief A thread has been suspended. */
98
99protected:
100 void adjustActiveThreadCount(int diff);
101 virtual Thread *createThread();
102 void adjustInventory(int noOfNewJobs);
103
104private:
105 ThreadWeaver::Private::Weaver_Private *d();
106 const ThreadWeaver::Private::Weaver_Private *d() const;
107};
108
109} // namespace ThreadWeaver
110
111#endif // WeaverImpl_H
A Job is a simple abstraction of an action that is to be executed in a thread context.
Definition job.h:47
We use a State pattern to handle the system state in ThreadWeaver.
Definition state.h:56
SuspendingState is the state after suspend() has been called, but before all threads finished executi...
Thread represents a worker thread in a Queue's inventory.
Definition thread.h:28
Base class for all WeaverImpl states.
int queueLength() const override
Returns the number of pending jobs.
Definition weaver.cpp:360
void enqueue(const QList< JobPointer > &jobs) override
Queue a vector of jobs.
Definition weaver.cpp:203
void finish() override
Finish all queued operations, then return.
Definition weaver.cpp:284
void decActiveThreadCount()
brief Decrement the count of active threads.
Definition weaver.cpp:450
void threadEnteredRun(Thread *thread)
Called from a new thread when entering the run method.
Definition weaver.cpp:489
virtual Thread * createThread()
Factory method to create the threads.
Definition weaver.cpp:438
bool isEmpty() const override
Is the queue empty?
Definition weaver.cpp:334
void incActiveThreadCount()
Increment the count of active threads.
Definition weaver.cpp:444
void adjustInventory(int noOfNewJobs)
Adjust the inventory size.
Definition weaver.cpp:403
void threadSuspended(ThreadWeaver::Thread *)
A thread has been suspended.
void blockThreadUntilJobsAreBeingAssigned_locked(Thread *th)
Blocks the calling thread until jobs can be assigned.
Definition weaver.cpp:596
void reschedule() override
Reschedule the jobs in the queue.
Definition weaver.cpp:380
void shutDown() override
Enter Destructed state.
Definition weaver.cpp:64
const State * state() const override
Return the state of the weaver object.
Definition weaver.cpp:148
void adjustActiveThreadCount(int diff)
Adjust active thread count.
Definition weaver.cpp:462
JobPointer takeFirstAvailableJobOrSuspendOrWait(Thread *th, bool threadWasBusy, bool suspendIfAllThreadsInactive, bool justReturning)
Take the first available job out of the queue and return it.
Definition weaver.cpp:505
int activeThreadCount()
Returns the number of active threads.
Definition weaver.cpp:482
bool dequeue(const JobPointer &job) override
Remove a job from the queue.
Definition weaver.cpp:238
void waitForAvailableJob(Thread *th) override
Wait for a job to become available.
Definition weaver.cpp:579
int maximumNumberOfThreads() const override
Get the maximum number of threads this Weaver may start.
Definition weaver.cpp:177
void threadExited(ThreadWeaver::Thread *)
A thread has exited.
void setState(StateId)
Set the Weaver state.
Definition weaver.cpp:126
int currentNumberOfThreads() const override
Returns the current number of threads in the inventory.
Definition weaver.cpp:190
void setMaximumNumberOfThreads(int cap) override
Set the maximum number of threads this Weaver object may start.
Definition weaver.cpp:158
Weaver(QObject *parent=nullptr)
Constructs a Weaver object.
Definition weaver.cpp:36
bool isIdle() const override
Is the weaver idle?
Definition weaver.cpp:347
void threadStarted(ThreadWeaver::Thread *)
A Thread has been created.
void blockThreadUntilJobsAreBeingAssigned(Thread *th)
Blocks the calling thread until jobs can be assigned.
Definition weaver.cpp:585
JobPointer applyForWork(Thread *thread, bool wasBusy) override
Assign a job to the calling thread.
Definition weaver.cpp:573
void resume() override
Resume job queueing.
Definition weaver.cpp:322
void requestAbort() override
Request aborts of the currently executed jobs.
Definition weaver.cpp:373
void suspend()
QObject(QObject *parent)
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
QObject * parent() const const
QThread * thread() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 31 2025 11:57:50 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.