1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QFUTUREINTERFACE_P_H
41#define QFUTUREINTERFACE_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtCore/private/qglobal_p.h>
55#include <QtCore/qelapsedtimer.h>
56#include <QtCore/qcoreevent.h>
57#include <QtCore/qlist.h>
58#include <QtCore/qwaitcondition.h>
59#include <QtCore/qrunnable.h>
60#include <QtCore/qthreadpool.h>
61
62QT_REQUIRE_CONFIG(future);
63
64QT_BEGIN_NAMESPACE
65
66class QFutureCallOutEvent : public QEvent
67{
68public:
69 enum CallOutType {
70 Started,
71 Finished,
72 Canceled,
73 Suspending,
74 Suspended,
75 Resumed,
76 Progress,
77 ProgressRange,
78 ResultsReady
79 };
80
81 QFutureCallOutEvent()
82 : QEvent(QEvent::FutureCallOut), callOutType(CallOutType(0)), index1(-1), index2(-1)
83 { }
84 explicit QFutureCallOutEvent(CallOutType callOutType, int index1 = -1)
85 : QEvent(QEvent::FutureCallOut), callOutType(callOutType), index1(index1), index2(-1)
86 { }
87 QFutureCallOutEvent(CallOutType callOutType, int index1, int index2)
88 : QEvent(QEvent::FutureCallOut), callOutType(callOutType), index1(index1), index2(index2)
89 { }
90
91 QFutureCallOutEvent(CallOutType callOutType, int index1, const QString &text)
92 : QEvent(QEvent::FutureCallOut),
93 callOutType(callOutType),
94 index1(index1),
95 index2(-1),
96 text(text)
97 { }
98
99 CallOutType callOutType;
100 int index1;
101 int index2;
102 QString text;
103
104 QFutureCallOutEvent *clone() const
105 {
106 return new QFutureCallOutEvent(callOutType, index1, index2, text);
107 }
108
109private:
110 QFutureCallOutEvent(CallOutType callOutType,
111 int index1,
112 int index2,
113 const QString &text)
114 : QEvent(QEvent::FutureCallOut),
115 callOutType(callOutType),
116 index1(index1),
117 index2(index2),
118 text(text)
119 { }
120};
121
122class QFutureCallOutInterface
123{
124public:
125 virtual ~QFutureCallOutInterface() {}
126 virtual void postCallOutEvent(const QFutureCallOutEvent &) = 0;
127 virtual void callOutInterfaceDisconnected() = 0;
128};
129
130class QFutureInterfaceBasePrivate
131{
132public:
133 QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState);
134
135 // When the last QFuture<T> reference is removed, we need to make
136 // sure that data stored in the ResultStore is cleaned out.
137 // Since QFutureInterfaceBasePrivate can be shared between QFuture<T>
138 // and QFuture<void> objects, we use a separate ref. counter
139 // to keep track of QFuture<T> objects.
140 class RefCount
141 {
142 public:
143 inline RefCount(int r = 0, int rt = 0)
144 : m_refCount(r), m_refCountT(rt) {}
145 // Default ref counter for QFIBP
146 inline bool ref() { return m_refCount.ref(); }
147 inline bool deref() { return m_refCount.deref(); }
148 inline int load() const { return m_refCount.loadRelaxed(); }
149 // Ref counter for type T
150 inline bool refT() { return m_refCountT.ref(); }
151 inline bool derefT() { return m_refCountT.deref(); }
152 inline int loadT() const { return m_refCountT.loadRelaxed(); }
153
154 private:
155 QAtomicInt m_refCount;
156 QAtomicInt m_refCountT;
157 };
158
159 // T: accessed from executing thread
160 // Q: accessed from the waiting/querying thread
161 RefCount refCount;
162 mutable QMutex m_mutex;
163 QWaitCondition waitCondition;
164 QList<QFutureCallOutInterface *> outputConnections;
165 int m_progressValue; // TQ
166 int m_progressMinimum; // TQ
167 int m_progressMaximum; // TQ
168 QAtomicInt state; // reads and writes can happen unprotected, both must be atomic
169 QElapsedTimer progressTime;
170 QWaitCondition pausedWaitCondition;
171 QtPrivate::ResultStoreBase m_results;
172 bool manualProgress; // only accessed from executing thread
173 int m_expectedResultCount;
174 QtPrivate::ExceptionStore m_exceptionStore;
175 QString m_progressText;
176 QRunnable *runnable;
177 QThreadPool *m_pool;
178
179 inline QThreadPool *pool() const
180 { return m_pool ? m_pool : QThreadPool::globalInstance(); }
181
182 // Internal functions that does not change the mutex state.
183 // The mutex must be locked when calling these.
184 int internal_resultCount() const;
185 bool internal_isResultReadyAt(int index) const;
186 bool internal_waitForNextResult();
187 bool internal_updateProgress(int progress, const QString &progressText = QString());
188 void internal_setThrottled(bool enable);
189 void sendCallOut(const QFutureCallOutEvent &callOut);
190 void sendCallOuts(const QFutureCallOutEvent &callOut1, const QFutureCallOutEvent &callOut2);
191 void connectOutputInterface(QFutureCallOutInterface *iface);
192 void disconnectOutputInterface(QFutureCallOutInterface *iface);
193
194 void setState(QFutureInterfaceBase::State state);
195
196 // Wrapper for continuation
197 std::function<void()> continuation;
198 QBasicMutex continuationMutex;
199
200 bool launchAsync = false;
201 bool isValid = false;
202};
203
204QT_END_NAMESPACE
205
206#endif
207