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
34{
35 Q_DECLARE_PUBLIC(QThreadPool)
36 friend class QThreadPoolThread;
37
38public:
39 struct QueuePage
40 {
41 enum {
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
60 {
61 while (!isFinished() && m_entries[m_firstIndex] == nullptr) {
62 m_firstIndex += 1;
63 }
64 }
65
67 {
68 Q_ASSERT(!isFinished());
69 QRunnable *runnable = m_entries[m_firstIndex];
70 Q_ASSERT(runnable);
71 return runnable;
72 }
73
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
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
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
116 static void destroyGlobalThreadPool();
117
118 bool tryStart(QRunnable *task);
119 void enqueueTask(QRunnable *task, int priority = 0);
120 int activeThreadCount() const;
121
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);
134
135 mutable QMutex mutex;
142
144 int requestedMaxThreadCount = QThread::idealThreadCount(); // don't use this directly
150};
151
152QT_END_NAMESPACE
153
154#endif
bool areAllThreadsActive() const
void deletePageIfFinished(QueuePage *page)
bool tryStart(QRunnable *task)
void enqueueTask(QRunnable *task, int priority=0)
QList< QueuePage * > queue
std::chrono::duration< int, std::milli > expiryTimeout
QThread::QualityOfService serviceLevel
QThread::Priority threadPriority
QWaitCondition noActiveThreads
static void destroyGlobalThreadPool()
QQueue< QThreadPoolThread * > expiredThreads
int activeThreadCount() const
int maxThreadCount() const
QSet< QThreadPoolThread * > allThreads
void startThread(QRunnable *runnable=nullptr)
void stealAndRunRunnable(QRunnable *runnable)
QQueue< QThreadPoolThread * > waitingThreads
bool waitForDone(const QDeadlineTimer &timer)
bool tooManyThreadsActive() const
void run() override
QThreadPoolPrivate * manager
void registerThreadInactive()
QRunnable * runnable
QWaitCondition runnableReady
\inmodule QtCore
Definition qthreadpool.h:21
Combined button and popup list for selecting options.
bool comparePriority(int priority, const QueuePage *p)
QThreadPoolPrivate::QueuePage QueuePage
void push(QRunnable *runnable)
QueuePage(QRunnable *runnable, int pri)
bool tryTake(QRunnable *runnable)