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
4#ifndef QFUTUREINTERFACE_P_H
5#define QFUTUREINTERFACE_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qglobal_p.h>
19#include <QtCore/qelapsedtimer.h>
20#include <QtCore/qcoreevent.h>
21#include <QtCore/qlist.h>
22#include <QtCore/qwaitcondition.h>
23#include <QtCore/qrunnable.h>
24#include <QtCore/qthreadpool.h>
25#include <QtCore/qfutureinterface.h>
26#include <QtCore/qexception.h>
27
28QT_REQUIRE_CONFIG(future);
29
31
32// Although QFutureCallOutEvent and QFutureCallOutInterface are private,
33// for historical reasons they were used externally (in QtJambi, see
34// https://github.com/OmixVisualization/qtjambi), so we export them to
35// not break the pre-existing code.
37{
38 Q_DECL_EVENT_COMMON(QFutureCallOutEvent)
39public:
40 enum CallOutType {
41 Started,
42 Finished,
43 Canceled,
44 Suspending,
45 Suspended,
46 Resumed,
47 Progress,
48 ProgressRange,
49 ResultsReady
50 };
51
52 QFutureCallOutEvent()
53 : QEvent(QEvent::FutureCallOut), callOutType(CallOutType(0)), index1(-1), index2(-1)
54 { }
55 explicit QFutureCallOutEvent(CallOutType callOutType, int index1 = -1)
56 : QEvent(QEvent::FutureCallOut), callOutType(callOutType), index1(index1), index2(-1)
57 { }
58 QFutureCallOutEvent(CallOutType callOutType, int index1, int index2)
59 : QEvent(QEvent::FutureCallOut), callOutType(callOutType), index1(index1), index2(index2)
60 { }
61
62 QFutureCallOutEvent(CallOutType callOutType, int index1, const QString &text)
63 : QEvent(QEvent::FutureCallOut),
64 callOutType(callOutType),
65 index1(index1),
66 index2(-1),
67 text(text)
68 { }
69
70 CallOutType callOutType;
71 int index1;
72 int index2;
73 QString text;
74};
75
76class Q_CORE_EXPORT QFutureCallOutInterface
77{
78public:
79 virtual ~QFutureCallOutInterface();
80 virtual void postCallOutEvent(const QFutureCallOutEvent &) = 0;
81 virtual void callOutInterfaceDisconnected() = 0;
82};
83
85{
86public:
87 QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState);
89
90 // When the last QFuture<T> reference is removed, we need to make
91 // sure that data stored in the ResultStore is cleaned out.
92 // Since QFutureInterfaceBasePrivate can be shared between QFuture<T>
93 // and QFuture<void> objects, we use a separate ref. counter
94 // to keep track of QFuture<T> objects.
96 {
97 public:
98 inline RefCount(int r = 0, int rt = 0)
100 // Default ref counter for QFIBP
101 inline bool ref() { return m_refCount.ref(); }
102 inline bool deref() { return m_refCount.deref(); }
103 inline int load() const { return m_refCount.loadRelaxed(); }
104 // Ref counter for type T
105 inline bool refT() { return m_refCountT.ref(); }
106 inline bool derefT() { return m_refCountT.deref(); }
107 inline int loadT() const { return m_refCountT.loadRelaxed(); }
108
109 private:
110 QAtomicInt m_refCount;
111 QAtomicInt m_refCountT;
112 };
113
114 // T: accessed from executing thread
115 // Q: accessed from the waiting/querying thread
122
123 union Data {
126
127#ifndef QT_NO_EXCEPTIONS
128 void setException(const std::exception_ptr &e)
129 {
130 m_results.~ResultStoreBase();
131 new (&m_exceptionStore) QtPrivate::ExceptionStore();
132 m_exceptionStore.setException(e);
133 }
134#endif
135
136 ~Data() { }
137 };
139
140 QRunnable *runnable = nullptr;
141 QThreadPool *m_pool = nullptr;
142 // Wrapper for continuation
145 // will reset back to nullptr when the parent future is done
146 // (finished, canceled, failed etc)
148
150 QAtomicInt state; // reads and writes can happen unprotected, both must be atomic
151
152 int m_progressValue = 0; // TQ
154 {
155 int minimum = 0; // TQ
156 int maximum = 0; // TQ
158 };
160
162 bool launchAsync = false;
163 bool isValid = false;
164 bool hasException = false;
165
169
171
172 inline QThreadPool *pool() const
173 { return m_pool ? m_pool : QThreadPool::globalInstance(); }
174
175 // Internal functions that does not change the mutex state.
176 // The mutex must be locked when calling these.
177 int internal_resultCount() const;
178 bool internal_isResultReadyAt(int index) const;
180 bool internal_updateProgressValue(int progress);
181 bool internal_updateProgress(int progress, const QString &progressText = QString());
182 void internal_setThrottled(bool enable);
183 void sendCallOut(const QFutureCallOutEvent &callOut);
184 void sendCallOuts(const QFutureCallOutEvent &callOut1, const QFutureCallOutEvent &callOut2);
185 void connectOutputInterface(QFutureCallOutInterface *iface);
186 void disconnectOutputInterface(QFutureCallOutInterface *iface);
187
188 void setState(QFutureInterfaceBase::State state);
189
191 {
192 None = 0x00,
194 };
195 Q_DECLARE_FLAGS(CancelOptions, CancelOption)
197};
198
199QT_END_NAMESPACE
200
201#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:209
const_iterator operator-(int j) const
Definition qfuture.h:205
std::bidirectional_iterator_tag iterator_category
Definition qfuture.h:173
const_iterator operator++(int)
Definition qfuture.h:191
const_iterator operator--(int)
Definition qfuture.h:197
const_iterator & operator++()
Definition qfuture.h:187
const_iterator(const const_iterator &o)
Definition qfuture.h:182
friend const_iterator operator+(int j, const_iterator k)
Definition qfuture.h:211
const_iterator & operator--()
Definition qfuture.h:189
const_iterator & operator+=(int j)
Definition qfuture.h:207
const_iterator operator+(int j) const
Definition qfuture.h:203
friend bool comparesEqual(const const_iterator &lhs, const const_iterator &rhs) noexcept
Definition qfuture.h:215
const T * operator->() const
Definition qfuture.h:186
const_iterator(QFuture const *const _future, int _index)
Definition qfuture.h:180
const T & operator*() const
Definition qfuture.h:185
const_iterator & operator=(const const_iterator &o)
Definition qfuture.h:183
QFuture< ResultType< Function > > then(Function &&function)
friend struct QtPrivate::WhenAnyContext
Definition qfuture.h:295
friend class QtPrivate::CanceledHandler
Definition qfuture.h:287
bool isValid() const
Definition qfuture.h:126
QList< T > results() const
Definition qfuture.h:115
friend class QtPrivate::CompactContinuation
Definition qfuture.h:284
QFuture< ResultType< Function > > then(QThreadPool *pool, Function &&function)
T resultAt(int index) const
Definition qfuture.h:318
const_iterator end() const
Definition qfuture.h:270
const_iterator begin() const
Definition qfuture.h:264
const_iterator ConstIterator
Definition qfuture.h:261
QFuture()
Definition qfuture.h:31
QFuture< typename QFuture< T >::template ResultType< Function > > then(Function &&function)
Definition qfuture.h:332
QFuture< T > onCanceled(Function &&handler)
Definition qfuture.h:395
QFuture< typename QFuture< T >::template ResultType< Function > > then(QtFuture::Launch policy, Function &&function)
Definition qfuture.h:340
QFuture< typename QFuture< T >::template ResultType< Function > > then(QThreadPool *pool, Function &&function)
Definition qfuture.h:350
QFuture< T > onCanceled(QObject *context, Function &&handler)
Definition qfuture.h:405
const_iterator constEnd() const
Definition qfuture.h:273
friend class const_iterator
Definition qfuture.h:260
friend class QtPrivate::FailureHandler
Definition qfuture.h:291
bool isResultReadyAt(int resultIndex) const
Definition qfuture.h:112
T takeResult()
Definition qfuture.h:118
const_iterator constBegin() const
Definition qfuture.h:267
QFuture< ResultType< Function > > then(QtFuture::Launch policy, Function &&function)
QFuture< void > makeReadyVoidFuture()
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