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 | |
4 | #ifndef QTHREADPOOL_H |
5 | #define QTHREADPOOL_H |
6 | |
7 | #include <QtCore/qglobal.h> |
8 | |
9 | #include <QtCore/qthread.h> |
10 | #include <QtCore/qrunnable.h> |
11 | |
12 | #if QT_CORE_REMOVED_SINCE(6, 6) |
13 | #include <functional> |
14 | #endif |
15 | |
16 | QT_REQUIRE_CONFIG(thread); |
17 | |
18 | QT_BEGIN_NAMESPACE |
19 | |
20 | class QThreadPoolPrivate; |
21 | class Q_CORE_EXPORT QThreadPool : public QObject |
22 | { |
23 | Q_OBJECT |
24 | Q_DECLARE_PRIVATE(QThreadPool) |
25 | Q_PROPERTY(int expiryTimeout READ expiryTimeout WRITE setExpiryTimeout) |
26 | Q_PROPERTY(int maxThreadCount READ maxThreadCount WRITE setMaxThreadCount) |
27 | Q_PROPERTY(int activeThreadCount READ activeThreadCount) |
28 | Q_PROPERTY(uint stackSize READ stackSize WRITE setStackSize) |
29 | Q_PROPERTY(QThread::Priority threadPriority READ threadPriority WRITE setThreadPriority) |
30 | friend class QFutureInterfaceBase; |
31 | |
32 | public: |
33 | QThreadPool(QObject *parent = nullptr); |
34 | ~QThreadPool(); |
35 | |
36 | static QThreadPool *globalInstance(); |
37 | |
38 | void start(QRunnable *runnable, int priority = 0); |
39 | bool tryStart(QRunnable *runnable); |
40 | |
41 | #if QT_CORE_REMOVED_SINCE(6, 6) |
42 | void start(std::function<void()> functionToRun, int priority = 0); |
43 | bool tryStart(std::function<void()> functionToRun); |
44 | #endif |
45 | |
46 | void startOnReservedThread(QRunnable *runnable); |
47 | #if QT_CORE_REMOVED_SINCE(6, 6) |
48 | void startOnReservedThread(std::function<void()> functionToRun); |
49 | #endif |
50 | |
51 | template <typename Callable, QRunnable::if_callable<Callable> = true> |
52 | void start(Callable &&functionToRun, int priority = 0); |
53 | template <typename Callable, QRunnable::if_callable<Callable> = true> |
54 | bool tryStart(Callable &&functionToRun); |
55 | template <typename Callable, QRunnable::if_callable<Callable> = true> |
56 | void startOnReservedThread(Callable &&functionToRun); |
57 | |
58 | int expiryTimeout() const; |
59 | void setExpiryTimeout(int expiryTimeout); |
60 | |
61 | int maxThreadCount() const; |
62 | void setMaxThreadCount(int maxThreadCount); |
63 | |
64 | int activeThreadCount() const; |
65 | |
66 | void setStackSize(uint stackSize); |
67 | uint stackSize() const; |
68 | |
69 | void setThreadPriority(QThread::Priority priority); |
70 | QThread::Priority threadPriority() const; |
71 | |
72 | void reserveThread(); |
73 | void releaseThread(); |
74 | |
75 | QT_CORE_INLINE_SINCE(6, 8) |
76 | bool waitForDone(int msecs); |
77 | bool waitForDone(QDeadlineTimer deadline = QDeadlineTimer::Forever); |
78 | |
79 | void clear(); |
80 | |
81 | bool contains(const QThread *thread) const; |
82 | |
83 | [[nodiscard]] bool tryTake(QRunnable *runnable); |
84 | }; |
85 | |
86 | template <typename Callable, QRunnable::if_callable<Callable>> |
87 | void QThreadPool::start(Callable &&functionToRun, int priority) |
88 | { |
89 | start(QRunnable::create(std::forward<Callable>(functionToRun)), priority); |
90 | } |
91 | |
92 | template <typename Callable, QRunnable::if_callable<Callable>> |
93 | bool QThreadPool::tryStart(Callable &&functionToRun) |
94 | { |
95 | QRunnable *runnable = QRunnable::create(std::forward<Callable>(functionToRun)); |
96 | if (tryStart(runnable)) |
97 | return true; |
98 | delete runnable; |
99 | return false; |
100 | } |
101 | |
102 | template <typename Callable, QRunnable::if_callable<Callable>> |
103 | void QThreadPool::startOnReservedThread(Callable &&functionToRun) |
104 | { |
105 | startOnReservedThread(QRunnable::create(std::forward<Callable>(functionToRun))); |
106 | } |
107 | |
108 | #if QT_CORE_INLINE_IMPL_SINCE(6, 8) |
109 | bool QThreadPool::waitForDone(int msecs) |
110 | { |
111 | return waitForDone(deadline: QDeadlineTimer(msecs)); |
112 | } |
113 | #endif |
114 | |
115 | QT_END_NAMESPACE |
116 | |
117 | #endif |
118 | |