Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
28
QT_BEGIN_NAMESPACE
29
30
class
QDeadlineTimer;
31
32
class
QThreadPoolThread
;
33
class
QThreadPoolPrivate
:
public
QObjectPrivate
34
{
35
Q_DECLARE_PUBLIC(QThreadPool)
36
friend
class
QThreadPoolThread
;
37
38
public
:
39
struct
QueuePage
40
{
41
enum
{
42
MaxPageSize
= 256
43
};
44
45
QueuePage
(QRunnable *runnable,
int
pri) : m_priority(pri) { push(runnable); }
46
47
bool
isFull
() {
return
m_lastIndex >=
MaxPageSize
- 1; }
48
49
bool
isFinished
() {
return
m_firstIndex > m_lastIndex; }
50
51
void
push
(QRunnable *runnable)
52
{
53
Q_ASSERT(runnable !=
nullptr
);
54
Q_ASSERT(!
isFull
(
)
);
55
m_lastIndex += 1;
56
m_entries[m_lastIndex] = runnable;
57
}
58
59
void
skipToNextOrEnd
()
60
{
61
while
(!isFinished() && m_entries[m_firstIndex] ==
nullptr
) {
62
m_firstIndex += 1;
63
}
64
}
65
66
QRunnable
*
first
()
67
{
68
Q_ASSERT(!
isFinished
(
)
);
69
QRunnable *runnable = m_entries[m_firstIndex];
70
Q_ASSERT(runnable);
71
return
runnable;
72
}
73
74
QRunnable
*
pop
()
75
{
76
Q_ASSERT(!
isFinished
(
)
);
77
QRunnable *runnable = first();
78
Q_ASSERT(runnable);
79
80
// clear the entry although this should not be necessary
81
m_entries[m_firstIndex] =
nullptr
;
82
m_firstIndex += 1;
83
84
// make sure the next runnable returned by first() is not a nullptr
85
skipToNextOrEnd
(
)
;
86
87
return
runnable;
88
}
89
90
bool
tryTake
(QRunnable *runnable)
91
{
92
Q_ASSERT(!
isFinished
(
)
);
93
for
(
int
i = m_firstIndex; i <= m_lastIndex; i++) {
94
if
(m_entries[i] == runnable) {
95
m_entries[i] =
nullptr
;
96
if
(i == m_firstIndex) {
97
// make sure first() does not return a nullptr
98
skipToNextOrEnd
(
)
;
99
}
100
return
true
;
101
}
102
}
103
return
false
;
104
}
105
106
int
priority
()
const
{
return
m_priority; }
107
108
private
:
109
int
m_priority = 0;
110
int
m_firstIndex = 0;
111
int
m_lastIndex = -1;
112
QRunnable *m_entries[
MaxPageSize
];
113
};
114
115
QThreadPoolPrivate
();
116
static
void
destroyGlobalThreadPool
();
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
152
QT_END_NAMESPACE
153
154
#
endif
QThreadPoolPrivate
Definition
qthreadpool_p.h:34
QThreadPoolPrivate::areAllThreadsActive
bool areAllThreadsActive() const
Definition
qthreadpool.cpp:256
QThreadPoolPrivate::deletePageIfFinished
void deletePageIfFinished(QueuePage *page)
QThreadPoolPrivate::tryStart
bool tryStart(QRunnable *task)
Definition
qthreadpool.cpp:171
QThreadPoolPrivate::enqueueTask
void enqueueTask(QRunnable *task, int priority=0)
Definition
qthreadpool.cpp:218
QThreadPoolPrivate::objectName
QString objectName
Definition
qthreadpool_p.h:141
QThreadPoolPrivate::reset
void reset()
Definition
qthreadpool.cpp:294
QThreadPoolPrivate::QThreadPoolPrivate
QThreadPoolPrivate()
Definition
qthreadpool.cpp:153
QThreadPoolPrivate::requestedMaxThreadCount
int requestedMaxThreadCount
Definition
qthreadpool_p.h:144
QThreadPoolPrivate::tryToStartMoreThreads
void tryToStartMoreThreads()
Definition
qthreadpool.cpp:239
QThreadPoolPrivate::queue
QList< QueuePage * > queue
Definition
qthreadpool_p.h:139
QThreadPoolPrivate::expiryTimeout
std::chrono::duration< int, std::milli > expiryTimeout
Definition
qthreadpool_p.h:143
QThreadPoolPrivate::serviceLevel
QThread::QualityOfService serviceLevel
Definition
qthreadpool_p.h:149
QThreadPoolPrivate::reservedThreads
int reservedThreads
Definition
qthreadpool_p.h:145
QThreadPoolPrivate::threadPriority
QThread::Priority threadPriority
Definition
qthreadpool_p.h:148
QThreadPoolPrivate::noActiveThreads
QWaitCondition noActiveThreads
Definition
qthreadpool_p.h:140
QThreadPoolPrivate::destroyGlobalThreadPool
static void destroyGlobalThreadPool()
Definition
qthreadpool.cpp:156
QThreadPoolPrivate::expiredThreads
QQueue< QThreadPoolThread * > expiredThreads
Definition
qthreadpool_p.h:138
QThreadPoolPrivate::activeThreadCount
int activeThreadCount() const
Definition
qthreadpool.cpp:231
QThreadPoolPrivate::maxThreadCount
int maxThreadCount() const
Definition
qthreadpool_p.h:126
QThreadPoolPrivate::mutex
QMutex mutex
Definition
qthreadpool_p.h:135
QThreadPoolPrivate::allThreads
QSet< QThreadPoolThread * > allThreads
Definition
qthreadpool_p.h:136
QThreadPoolPrivate::stackSize
uint stackSize
Definition
qthreadpool_p.h:147
QThreadPoolPrivate::activeThreads
int activeThreads
Definition
qthreadpool_p.h:146
QThreadPoolPrivate::startThread
void startThread(QRunnable *runnable=nullptr)
Definition
qthreadpool.cpp:271
QThreadPoolPrivate::stealAndRunRunnable
void stealAndRunRunnable(QRunnable *runnable)
Definition
qthreadpool.cpp:396
QThreadPoolPrivate::waitingThreads
QQueue< QThreadPoolThread * > waitingThreads
Definition
qthreadpool_p.h:137
QThreadPoolPrivate::clear
void clear()
Definition
qthreadpool.cpp:335
QThreadPoolPrivate::waitForDone
bool waitForDone(const QDeadlineTimer &timer)
Definition
qthreadpool.cpp:319
QThreadPoolPrivate::tooManyThreadsActive
bool tooManyThreadsActive() const
Definition
qthreadpool.cpp:262
QThreadPoolThread
Definition
qthreadpool.cpp:35
QThreadPoolThread::run
void run() override
Definition
qthreadpool.cpp:64
QThreadPoolThread::manager
QThreadPoolPrivate * manager
Definition
qthreadpool.cpp:43
QThreadPoolThread::registerThreadInactive
void registerThreadInactive()
Definition
qthreadpool.cpp:143
QThreadPoolThread::runnable
QRunnable * runnable
Definition
qthreadpool.cpp:44
QThreadPoolThread::runnableReady
QWaitCondition runnableReady
Definition
qthreadpool.cpp:42
QThreadPool
\inmodule QtCore
Definition
qthreadpool.h:21
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
comparePriority
bool comparePriority(int priority, const QueuePage *p)
Definition
qthreadpool.cpp:213
QueuePage
QThreadPoolPrivate::QueuePage QueuePage
Definition
qthreadpool.cpp:50
QThreadPoolPrivate::QueuePage
Definition
qthreadpool_p.h:40
QThreadPoolPrivate::QueuePage::priority
int priority() const
Definition
qthreadpool_p.h:106
QThreadPoolPrivate::QueuePage::MaxPageSize
@ MaxPageSize
Definition
qthreadpool_p.h:42
QThreadPoolPrivate::QueuePage::isFull
bool isFull()
Definition
qthreadpool_p.h:47
QThreadPoolPrivate::QueuePage::push
void push(QRunnable *runnable)
Definition
qthreadpool_p.h:51
QThreadPoolPrivate::QueuePage::skipToNextOrEnd
void skipToNextOrEnd()
Definition
qthreadpool_p.h:59
QThreadPoolPrivate::QueuePage::pop
QRunnable * pop()
Definition
qthreadpool_p.h:74
QThreadPoolPrivate::QueuePage::isFinished
bool isFinished()
Definition
qthreadpool_p.h:49
QThreadPoolPrivate::QueuePage::QueuePage
QueuePage(QRunnable *runnable, int pri)
Definition
qthreadpool_p.h:45
QThreadPoolPrivate::QueuePage::tryTake
bool tryTake(QRunnable *runnable)
Definition
qthreadpool_p.h:90
QThreadPoolPrivate::QueuePage::first
QRunnable * first()
Definition
qthreadpool_p.h:66
qtbase
src
corelib
thread
qthreadpool_p.h
Generated on
for Qt by
1.18.0