summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qfutureinterface.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/thread/qfutureinterface.cpp')
-rw-r--r--src/corelib/thread/qfutureinterface.cpp85
1 files changed, 74 insertions, 11 deletions
diff --git a/src/corelib/thread/qfutureinterface.cpp b/src/corelib/thread/qfutureinterface.cpp
index f1fd40e7b6..92b2df7c15 100644
--- a/src/corelib/thread/qfutureinterface.cpp
+++ b/src/corelib/thread/qfutureinterface.cpp
@@ -114,6 +114,7 @@ void QFutureInterfaceBase::cancel()
d->waitCondition.wakeAll();
d->pausedWaitCondition.wakeAll();
d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Canceled));
+ d->isValid = false;
}
void QFutureInterfaceBase::setPaused(bool paused)
@@ -191,6 +192,17 @@ bool QFutureInterfaceBase::isResultReadyAt(int index) const
return d->internal_isResultReadyAt(index);
}
+bool QFutureInterfaceBase::isValid() const
+{
+ const QMutexLocker lock(&d->m_mutex);
+ return d->isValid;
+}
+
+bool QFutureInterfaceBase::isRunningOrPending() const
+{
+ return queryState(static_cast<State>(Running | Pending));
+}
+
bool QFutureInterfaceBase::waitForNextResult()
{
QMutexLocker lock(&d->m_mutex);
@@ -258,9 +270,9 @@ void QFutureInterfaceBase::reportStarted()
QMutexLocker locker(&d->m_mutex);
if (d->state.loadRelaxed() & (Started|Canceled|Finished))
return;
-
d->setState(State(Started | Running));
d->sendCallOut(QFutureCallOutEvent(QFutureCallOutEvent::Started));
+ d->isValid = true;
}
void QFutureInterfaceBase::reportCanceled()
@@ -271,6 +283,15 @@ void QFutureInterfaceBase::reportCanceled()
#ifndef QT_NO_EXCEPTIONS
void QFutureInterfaceBase::reportException(const QException &exception)
{
+ try {
+ exception.raise();
+ } catch (...) {
+ reportException(std::current_exception());
+ }
+}
+
+void QFutureInterfaceBase::reportException(std::exception_ptr exception)
+{
QMutexLocker locker(&d->m_mutex);
if (d->state.loadRelaxed() & (Canceled|Finished))
return;
@@ -315,7 +336,7 @@ void QFutureInterfaceBase::waitForResult(int resultIndex)
d->m_exceptionStore.throwPossibleException();
QMutexLocker lock(&d->m_mutex);
- if (!isRunning())
+ if (!isRunningOrPending())
return;
lock.unlock();
@@ -326,7 +347,7 @@ void QFutureInterfaceBase::waitForResult(int resultIndex)
lock.relock();
const int waitIndex = (resultIndex == -1) ? INT_MAX : resultIndex;
- while (isRunning() && !d->internal_isResultReadyAt(waitIndex))
+ while (isRunningOrPending() && !d->internal_isResultReadyAt(waitIndex))
d->waitCondition.wait(&d->m_mutex);
d->m_exceptionStore.throwPossibleException();
@@ -335,7 +356,7 @@ void QFutureInterfaceBase::waitForResult(int resultIndex)
void QFutureInterfaceBase::waitForFinished()
{
QMutexLocker lock(&d->m_mutex);
- const bool alreadyFinished = !isRunning();
+ const bool alreadyFinished = !isRunningOrPending();
lock.unlock();
if (!alreadyFinished) {
@@ -343,7 +364,7 @@ void QFutureInterfaceBase::waitForFinished()
lock.relock();
- while (isRunning())
+ while (isRunningOrPending())
d->waitCondition.wait(&d->m_mutex);
}
@@ -386,6 +407,11 @@ void QFutureInterfaceBase::setThreadPool(QThreadPool *pool)
d->m_pool = pool;
}
+QThreadPool *QFutureInterfaceBase::threadPool() const
+{
+ return d->m_pool;
+}
+
void QFutureInterfaceBase::setFilterMode(bool enable)
{
QMutexLocker locker(&d->m_mutex);
@@ -424,12 +450,7 @@ void QFutureInterfaceBase::setProgressValueAndText(int progressValue,
}
}
-QMutex *QFutureInterfaceBase::mutex() const
-{
- return &d->m_mutex;
-}
-
-QMutex &QFutureInterfaceBase::mutex(int) const
+QMutex &QFutureInterfaceBase::mutex() const
{
return d->m_mutex;
}
@@ -468,6 +489,16 @@ bool QFutureInterfaceBase::derefT() const
return d->refCount.derefT();
}
+void QFutureInterfaceBase::reset()
+{
+ d->m_progressValue = 0;
+ d->m_progressMinimum = 0;
+ d->m_progressMaximum = 0;
+ d->setState(QFutureInterfaceBase::NoState);
+ d->progressTime.invalidate();
+ d->isValid = false;
+}
+
QFutureInterfaceBasePrivate::QFutureInterfaceBasePrivate(QFutureInterfaceBase::State initialState)
: refCount(1), m_progressValue(0), m_progressMinimum(0), m_progressMaximum(0),
state(initialState),
@@ -609,4 +640,36 @@ void QFutureInterfaceBasePrivate::setState(QFutureInterfaceBase::State newState)
state.storeRelaxed(newState);
}
+void QFutureInterfaceBase::setContinuation(std::function<void()> func)
+{
+ QMutexLocker lock(&d->continuationMutex);
+ // If the state is ready, run continuation immediately,
+ // otherwise save it for later.
+ if (isFinished()) {
+ lock.unlock();
+ func();
+ } else {
+ d->continuation = std::move(func);
+ }
+}
+
+void QFutureInterfaceBase::runContinuation() const
+{
+ QMutexLocker lock(&d->continuationMutex);
+ if (d->continuation) {
+ lock.unlock();
+ d->continuation();
+ }
+}
+
+void QFutureInterfaceBase::setLaunchAsync(bool value)
+{
+ d->launchAsync = value;
+}
+
+bool QFutureInterfaceBase::launchAsync() const
+{
+ return d->launchAsync;
+}
+
QT_END_NAMESPACE