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 QTCONCURRENT_RUNBASE_H
6#define QTCONCURRENT_RUNBASE_H
7
8#include <QtConcurrent/qtconcurrent_global.h>
9
10#ifndef QT_NO_CONCURRENT
11
12#include <QtCore/qfuture.h>
13#include <QtCore/qrunnable.h>
14#include <QtCore/qthreadpool.h>
15
16#include <type_traits>
17#include <utility>
18
19QT_BEGIN_NAMESPACE
20
21
22#ifndef Q_QDOC
23
24namespace QtConcurrent {
25
26template <typename T>
27struct SelectSpecialization
28{
29 template <class Normal, class Void>
30 struct Type { typedef Normal type; };
31};
32
33template <>
34struct SelectSpecialization<void>
35{
36 template <class Normal, class Void>
37 struct Type { typedef Void type; };
38};
39
40struct TaskStartParameters
41{
42 QThreadPool *threadPool = QThreadPool::globalInstance();
43 int priority = 0;
44};
45
46template <typename T>
47class RunFunctionTaskBase : public QRunnable
48{
49public:
50 QFuture<T> start()
51 {
52 return start(TaskStartParameters());
53 }
54
55 QFuture<T> start(const TaskStartParameters &parameters)
56 {
57 promise.setThreadPool(parameters.threadPool);
58 promise.setRunnable(this);
59 promise.reportStarted();
60 QFuture<T> theFuture = promise.future();
61
62 if (parameters.threadPool) {
63 parameters.threadPool->start(this, parameters.priority);
64 } else {
65 promise.reportCanceled();
66 promise.reportFinished();
67 delete this;
68 }
69 return theFuture;
70 }
71
72 // For backward compatibility
73 QFuture<T> start(QThreadPool *pool) { return start({pool, 0}); }
74
75 void run() override
76 {
77 if (promise.isCanceled()) {
78 promise.reportFinished();
79 return;
80 }
81#ifndef QT_NO_EXCEPTIONS
82 try {
83#endif
84 runFunctor();
85#ifndef QT_NO_EXCEPTIONS
86 } catch (QException &e) {
87 promise.reportException(e);
88 } catch (...) {
89 promise.reportException(QUnhandledException(std::current_exception()));
90 }
91#endif
92 promise.reportFinished();
93 }
94
95protected:
96 virtual void runFunctor() = 0;
97
98 QFutureInterface<T> promise;
99};
100
101} //namespace QtConcurrent
102
103#endif //Q_QDOC
104
105QT_END_NAMESPACE
106
107#endif // QT_NO_CONCURRENT
108
109#endif
110

source code of qtbase/src/concurrent/qtconcurrentrunbase.h