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
qfutureinterface_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 QFUTUREINTERFACE_P_H
6#define QFUTUREINTERFACE_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#include <QtCore/private/qglobal_p.h>
20#include <QtCore/qelapsedtimer.h>
21#include <QtCore/qcoreevent.h>
22#include <QtCore/qlist.h>
23#include <QtCore/qwaitcondition.h>
24#include <QtCore/qrunnable.h>
25#include <QtCore/qthreadpool.h>
26#include <QtCore/qfutureinterface.h>
27#include <QtCore/qexception.h>
28
29QT_REQUIRE_CONFIG(future);
30
32
33// Although QFutureCallOutEvent and QFutureCallOutInterface are private,
34// for historical reasons they were used externally (in QtJambi, see
35// https://github.com/OmixVisualization/qtjambi), so we export them to
36// not break the pre-existing code.
38{
39 Q_DECL_EVENT_COMMON(QFutureCallOutEvent)
40public:
41 enum CallOutType {
42 Started,
43 Finished,
44 Canceled,
45 Suspending,
46 Suspended,
47 Resumed,
48 Progress,
49 ProgressRange,
50 ResultsReady
51 };
52
53 QFutureCallOutEvent()
54 : QEvent(QEvent::FutureCallOut), callOutType(CallOutType(0)), index1(-1), index2(-1)
55 { }
56 explicit QFutureCallOutEvent(CallOutType callOutType, int index1 = -1)
57 : QEvent(QEvent::FutureCallOut), callOutType(callOutType), index1(index1), index2(-1)
58 { }
59 QFutureCallOutEvent(CallOutType callOutType, int index1, int index2)
60 : QEvent(QEvent::FutureCallOut), callOutType(callOutType), index1(index1), index2(index2)
61 { }
62
63 QFutureCallOutEvent(CallOutType callOutType, int index1, const QString &text)
64 : QEvent(QEvent::FutureCallOut),
65 callOutType(callOutType),
66 index1(index1),
67 index2(-1),
68 text(text)
69 { }
70
71 CallOutType callOutType;
72 int index1;
73 int index2;
74 QString text;
75};
76
77class Q_CORE_EXPORT QFutureCallOutInterface
78{
79public:
80 virtual ~QFutureCallOutInterface();
81 virtual void postCallOutEvent(const QFutureCallOutEvent &) = 0;
82 virtual void callOutInterfaceDisconnected() = 0;
83};
84
86{
87public:
88 QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState);
90
91 // When the last QFuture<T> reference is removed, we need to make
92 // sure that data stored in the ResultStore is cleaned out.
93 // Since QFutureInterfaceBasePrivate can be shared between QFuture<T>
94 // and QFuture<void> objects, we use a separate ref. counter
95 // to keep track of QFuture<T> objects.
97 {
98 public:
99 inline RefCount(int r = 0, int rt = 0)
100 : m_refCount(r), m_refCountT(rt) {}
101 // Default ref counter for QFIBP
102 inline bool ref() { return m_refCount.ref(); }
103 inline bool deref() { return m_refCount.deref(); }
104 inline int load() const { return m_refCount.loadRelaxed(); }
105 // Ref counter for type T
106 inline bool refT() { return m_refCountT.ref(); }
107 inline bool derefT() { return m_refCountT.deref(); }
108 inline int loadT() const { return m_refCountT.loadRelaxed(); }
109
110 private:
111 QAtomicInt m_refCount;
112 QAtomicInt m_refCountT;
113 };
114
115 // T: accessed from executing thread
116 // Q: accessed from the waiting/querying thread
123
124 union Data {
127
128#ifndef QT_NO_EXCEPTIONS
129 void setException(const std::exception_ptr &e)
130 {
131 m_results.~ResultStoreBase();
132 new (&m_exceptionStore) QtPrivate::ExceptionStore();
133 m_exceptionStore.setException(e);
134 }
135#endif
136
137 ~Data() { }
138 };
140
141 QRunnable *runnable = nullptr;
142 QThreadPool *m_pool = nullptr;
143 // Wrapper for continuation
146 // will reset back to nullptr when the parent future is done
147 // (finished, canceled, failed etc)
149
151 QAtomicInt state; // reads and writes can happen unprotected, both must be atomic
152
153 int m_progressValue = 0; // TQ
155 {
156 int minimum = 0; // TQ
157 int maximum = 0; // TQ
159 };
161
163 bool launchAsync = false;
164 bool isValid = false;
165 bool hasException = false;
166
170
172
173 inline QThreadPool *pool() const
174 { return m_pool ? m_pool : QThreadPool::globalInstance(); }
175
176 // Internal functions that does not change the mutex state.
177 // The mutex must be locked when calling these.
178 int internal_resultCount() const;
179 bool internal_isResultReadyAt(int index) const;
181 bool internal_updateProgressValue(int progress);
182 bool internal_updateProgress(int progress, const QString &progressText = QString());
183 void internal_setThrottled(bool enable);
184 void sendCallOut(const QFutureCallOutEvent &callOut);
185 void sendCallOuts(const QFutureCallOutEvent &callOut1, const QFutureCallOutEvent &callOut2);
186 void connectOutputInterface(QFutureCallOutInterface *iface);
187 void disconnectOutputInterface(QFutureCallOutInterface *iface);
188
189 void setState(QFutureInterfaceBase::State state);
190
192 {
193 None = 0x00,
195 };
196 Q_DECLARE_FLAGS(CancelOptions, CancelOption)
198};
199
200QT_END_NAMESPACE
201
202#endif
std::atomic< ContinuationState > continuationState
bool internal_updateProgress(int progress, const QString &progressText=QString())
void sendCallOuts(const QFutureCallOutEvent &callOut1, const QFutureCallOutEvent &callOut2)
void sendCallOut(const QFutureCallOutEvent &callOut)
QFutureInterfaceBase::ContinuationType continuationType
void setState(QFutureInterfaceBase::State state)
QList< QFutureCallOutInterface * > outputConnections
void disconnectOutputInterface(QFutureCallOutInterface *iface)
bool internal_updateProgressValue(int progress)
bool internal_isResultReadyAt(int index) const
QFutureInterfaceBasePrivate * nonConcludedParent
void connectOutputInterface(QFutureCallOutInterface *iface)
std::function< void(const QFutureInterfaceBase &)> continuation
void internal_setThrottled(bool enable)
QFutureInterfaceBasePrivate * continuationData
QScopedPointer< ProgressData > m_progress
QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState)
const_iterator & operator-=(int j)
Definition qfuture.h:210
const_iterator operator-(int j) const
Definition qfuture.h:206
std::bidirectional_iterator_tag iterator_category
Definition qfuture.h:174
const_iterator operator++(int)
Definition qfuture.h:192
const_iterator operator--(int)
Definition qfuture.h:198
const_iterator & operator++()
Definition qfuture.h:188
const_iterator(const const_iterator &o)
Definition qfuture.h:183
friend const_iterator operator+(int j, const_iterator k)
Definition qfuture.h:212
const_iterator & operator--()
Definition qfuture.h:190
const_iterator & operator+=(int j)
Definition qfuture.h:208
const_iterator operator+(int j) const
Definition qfuture.h:204
friend bool comparesEqual(const const_iterator &lhs, const const_iterator &rhs) noexcept
Definition qfuture.h:216
const T * operator->() const
Definition qfuture.h:187
const_iterator(QFuture const *const _future, int _index)
Definition qfuture.h:181
const T & operator*() const
Definition qfuture.h:186
const_iterator & operator=(const const_iterator &o)
Definition qfuture.h:184
QFuture< ResultType< Function > > then(Function &&function)
typename QtPrivate::ResultTypeHelper< Function, T >::ResultType ResultType
Definition qfuture.h:130
friend struct QtPrivate::WhenAnyContext
Definition qfuture.h:296
friend class QtPrivate::CanceledHandler
Definition qfuture.h:288
bool isValid() const
Definition qfuture.h:127
QList< T > results() const
Definition qfuture.h:116
friend class QtPrivate::CompactContinuation
Definition qfuture.h:285
QFuture< ResultType< Function > > then(QThreadPool *pool, Function &&function)
T resultAt(int index) const
Definition qfuture.h:319
const_iterator end() const
Definition qfuture.h:271
const_iterator begin() const
Definition qfuture.h:265
const_iterator ConstIterator
Definition qfuture.h:262
QFuture()
Definition qfuture.h:32
QFuture< typename QFuture< T >::template ResultType< Function > > then(Function &&function)
Definition qfuture.h:333
QFuture< T > onCanceled(Function &&handler)
Definition qfuture.h:396
QFuture< typename QFuture< T >::template ResultType< Function > > then(QtFuture::Launch policy, Function &&function)
Definition qfuture.h:341
QFuture< typename QFuture< T >::template ResultType< Function > > then(QThreadPool *pool, Function &&function)
Definition qfuture.h:351
QFuture< T > onCanceled(QObject *context, Function &&handler)
Definition qfuture.h:406
const_iterator constEnd() const
Definition qfuture.h:274
friend class QtPrivate::FailureHandler
Definition qfuture.h:292
bool isResultReadyAt(int resultIndex) const
Definition qfuture.h:113
T takeResult()
Definition qfuture.h:119
const_iterator constBegin() const
Definition qfuture.h:268
QFuture< ResultType< Function > > then(QtFuture::Launch policy, Function &&function)
QFuture< void > makeReadyVoidFuture()
void qfutureWarnIfUnusedResults(qsizetype numResults)
static int switch_from_to(QAtomicInt &a, int from, int to)
static int switch_off(QAtomicInt &a, int which)
static int switch_on(QAtomicInt &a, int which)
QFuture< void > future
[5]
QtPrivate::ExceptionStore m_exceptionStore
void setException(const std::exception_ptr &e)
QtPrivate::ResultStoreBase m_results