2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2021 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2021-05-11 16:53:03 +02:00
|
|
|
|
|
|
|
|
#include "futuresynchronizer.h"
|
|
|
|
|
|
2024-05-16 09:29:51 +02:00
|
|
|
#include "qtcassert.h"
|
|
|
|
|
#include "threadutils.h"
|
|
|
|
|
|
2023-05-22 14:38:47 +02:00
|
|
|
/*!
|
|
|
|
|
\class Utils::FutureSynchronizer
|
|
|
|
|
\inmodule QtCreator
|
2021-05-11 16:53:03 +02:00
|
|
|
|
|
|
|
|
\brief The FutureSynchronizer is an enhanced version of QFutureSynchronizer.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Utils {
|
|
|
|
|
|
|
|
|
|
FutureSynchronizer::~FutureSynchronizer()
|
|
|
|
|
{
|
|
|
|
|
waitForFinished();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FutureSynchronizer::isEmpty() const
|
|
|
|
|
{
|
|
|
|
|
return m_futures.isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FutureSynchronizer::waitForFinished()
|
|
|
|
|
{
|
2021-05-12 15:58:39 +02:00
|
|
|
if (m_cancelOnWait)
|
|
|
|
|
cancelAllFutures();
|
2021-05-11 16:53:03 +02:00
|
|
|
for (QFuture<void> &future : m_futures)
|
|
|
|
|
future.waitForFinished();
|
|
|
|
|
clearFutures();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 15:58:39 +02:00
|
|
|
void FutureSynchronizer::cancelAllFutures()
|
|
|
|
|
{
|
|
|
|
|
for (QFuture<void> &future : m_futures)
|
|
|
|
|
future.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 16:53:03 +02:00
|
|
|
void FutureSynchronizer::clearFutures()
|
|
|
|
|
{
|
|
|
|
|
m_futures.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FutureSynchronizer::setCancelOnWait(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
m_cancelOnWait = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FutureSynchronizer::isCancelOnWait() const
|
|
|
|
|
{
|
|
|
|
|
return m_cancelOnWait;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FutureSynchronizer::flushFinishedFutures()
|
|
|
|
|
{
|
|
|
|
|
QList<QFuture<void>> newFutures;
|
2022-10-07 14:46:06 +02:00
|
|
|
for (const QFuture<void> &future : std::as_const(m_futures)) {
|
2021-05-11 16:53:03 +02:00
|
|
|
if (!future.isFinished())
|
|
|
|
|
newFutures.append(future);
|
|
|
|
|
}
|
|
|
|
|
m_futures = newFutures;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 09:29:51 +02:00
|
|
|
Q_GLOBAL_STATIC(FutureSynchronizer, s_futureSynchronizer);
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Returns a global FutureSynchronizer.
|
|
|
|
|
The application should cancel and wait for the tasks in this synchronizer before actually
|
|
|
|
|
unloading any libraries. This is for example done by the plugin manager in Qt Creator.
|
|
|
|
|
May only be accessed by the main thread.
|
|
|
|
|
*/
|
|
|
|
|
FutureSynchronizer *futureSynchronizer()
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(isMainThread(), return nullptr);
|
|
|
|
|
return s_futureSynchronizer;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 16:53:03 +02:00
|
|
|
} // namespace Utils
|