Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qthreadpool_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QTHREADPOOL_P_H
6#define QTHREADPOOL_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18//
19
20#include "QtCore/qmutex.h"
21#include "QtCore/qthread.h"
22#include "QtCore/qwaitcondition.h"
23#include "QtCore/qthreadpool.h"
24#include "QtCore/qset.h"
25#include "QtCore/qqueue.h"
26#include "private/qobject_p.h"
27
29
30class QDeadlineTimer;
31
33{
34public:
35 enum {
37 };
38
39 QueuePage(QRunnable *runnable, int pri) : m_priority(pri) { push(runnable); }
40
41 bool isFull() { return m_lastIndex >= MaxPageSize - 1; }
42
43 bool isFinished() { return m_firstIndex > m_lastIndex; }
44
45 void push(QRunnable *runnable)
46 {
47 Q_ASSERT(runnable != nullptr);
48 Q_ASSERT(!isFull());
49 m_lastIndex += 1;
50 m_entries[m_lastIndex] = runnable;
51 }
52
54 {
55 while (!isFinished() && m_entries[m_firstIndex] == nullptr) {
56 m_firstIndex += 1;
57 }
58 }
59
61 {
62 Q_ASSERT(!isFinished());
63 QRunnable *runnable = m_entries[m_firstIndex];
64 Q_ASSERT(runnable);
65 return runnable;
66 }
67
69 {
70 Q_ASSERT(!isFinished());
71 QRunnable *runnable = first();
72 Q_ASSERT(runnable);
73
74 // clear the entry although this should not be necessary
75 m_entries[m_firstIndex] = nullptr;
76 m_firstIndex += 1;
77
78 // make sure the next runnable returned by first() is not a nullptr
80
81 return runnable;
82 }
83
84 bool tryTake(QRunnable *runnable)
85 {
86 Q_ASSERT(!isFinished());
87 for (int i = m_firstIndex; i <= m_lastIndex; i++) {
88 if (m_entries[i] == runnable) {
89 m_entries[i] = nullptr;
90 if (i == m_firstIndex) {
91 // make sure first() does not return a nullptr
93 }
94 return true;
95 }
96 }
97 return false;
98 }
99
100 int priority() const { return m_priority; }
101
102private:
103 int m_priority = 0;
104 int m_firstIndex = 0;
105 int m_lastIndex = -1;
106 QRunnable *m_entries[MaxPageSize];
107};
108
110class Q_CORE_EXPORT QThreadPoolPrivate : public QObjectPrivate
111{
112 Q_DECLARE_PUBLIC(QThreadPool)
113 friend class QThreadPoolThread;
114
115public:
116 QThreadPoolPrivate();
117
118 bool tryStart(QRunnable *task);
119 void enqueueTask(QRunnable *task, int priority = 0);
120 int activeThreadCount() const;
121
122 void tryToStartMoreThreads();
123 bool areAllThreadsActive() const;
124 bool tooManyThreadsActive() const;
125
126 int maxThreadCount() const
127 { return qMax(requestedMaxThreadCount, 1); } // documentation says we start at least one
128 void startThread(QRunnable *runnable = nullptr);
129 void reset();
130 bool waitForDone(const QDeadlineTimer &timer);
131 void clear();
132 void stealAndRunRunnable(QRunnable *runnable);
133 void deletePageIfFinished(QueuePage *page);
134
135 mutable QMutex mutex;
136 QSet<QThreadPoolThread *> allThreads;
137 QQueue<QThreadPoolThread *> waitingThreads;
138 QQueue<QThreadPoolThread *> expiredThreads;
139 QList<QueuePage *> queue;
140 QWaitCondition noActiveThreads;
141 QString objectName;
142
143 std::chrono::duration<int, std::milli> expiryTimeout = std::chrono::seconds(30);
144 int requestedMaxThreadCount = QThread::idealThreadCount(); // don't use this directly
145 int reservedThreads = 0;
146 int activeThreads = 0;
147 uint stackSize = 0;
148 QThread::Priority threadPriority = QThread::InheritPriority;
149 QThread::QualityOfService serviceLevel = QThread::QualityOfService::Auto;
150};
151
152QT_END_NAMESPACE
153
154#endif
void run() override
QThreadPoolPrivate * manager
void registerThreadInactive()
QRunnable * runnable
QWaitCondition runnableReady
\inmodule QtCore
Definition qthreadpool.h:21
QueuePage(QRunnable *runnable, int pri)
bool isFinished()
bool tryTake(QRunnable *runnable)
QRunnable * first()
void push(QRunnable *runnable)
QRunnable * pop()
bool isFull()
void skipToNextOrEnd()
int priority() const
bool comparePriority(int priority, const QueuePage *p)