ExtraCompiler: Use QPromise for async calls

Change-Id: I810603da8ccee4618ca02f29682fa5f8abe9d33e
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Jarek Kobus
2023-02-12 01:00:58 +01:00
parent 84ab58dfef
commit 60a8ecd1b0
2 changed files with 32 additions and 34 deletions

View File

@@ -17,11 +17,11 @@
#include <utils/qtcprocess.h> #include <utils/qtcprocess.h>
#include <QDateTime> #include <QDateTime>
#include <QFutureInterface>
#include <QLoggingCategory> #include <QLoggingCategory>
#include <QThreadPool> #include <QThreadPool>
#include <QTimer> #include <QTimer>
using namespace Core;
using namespace Utils; using namespace Utils;
namespace ProjectExplorer { namespace ProjectExplorer {
@@ -37,10 +37,10 @@ public:
FilePath source; FilePath source;
FileNameToContentsHash contents; FileNameToContentsHash contents;
QDateTime compileTime; QDateTime compileTime;
Core::IEditor *lastEditor = nullptr; IEditor *lastEditor = nullptr;
QMetaObject::Connection activeBuildConfigConnection; QMetaObject::Connection activeBuildConfigConnection;
QMetaObject::Connection activeEnvironmentConnection; QMetaObject::Connection activeEnvironmentConnection;
Utils::Guard lock; Guard lock;
bool dirty = false; bool dirty = false;
QTimer timer; QTimer timer;
@@ -69,10 +69,10 @@ ExtraCompiler::ExtraCompiler(const Project *project, const FilePath &source,
deleteLater(); deleteLater();
}); });
Core::EditorManager *editorManager = Core::EditorManager::instance(); EditorManager *editorManager = EditorManager::instance();
connect(editorManager, &Core::EditorManager::currentEditorChanged, connect(editorManager, &EditorManager::currentEditorChanged,
this, &ExtraCompiler::onEditorChanged); this, &ExtraCompiler::onEditorChanged);
connect(editorManager, &Core::EditorManager::editorAboutToClose, connect(editorManager, &EditorManager::editorAboutToClose,
this, &ExtraCompiler::onEditorAboutToClose); this, &ExtraCompiler::onEditorAboutToClose);
// Use existing target files, where possible. Otherwise run the compiler. // Use existing target files, where possible. Otherwise run the compiler.
@@ -228,12 +228,12 @@ void ExtraCompiler::onTargetsBuilt(Project *project)
}); });
} }
void ExtraCompiler::onEditorChanged(Core::IEditor *editor) void ExtraCompiler::onEditorChanged(IEditor *editor)
{ {
// Handle old editor // Handle old editor
if (d->lastEditor) { if (d->lastEditor) {
Core::IDocument *doc = d->lastEditor->document(); IDocument *doc = d->lastEditor->document();
disconnect(doc, &Core::IDocument::contentsChanged, disconnect(doc, &IDocument::contentsChanged,
this, &ExtraCompiler::setDirty); this, &ExtraCompiler::setDirty);
if (d->dirty) { if (d->dirty) {
@@ -246,7 +246,7 @@ void ExtraCompiler::onEditorChanged(Core::IEditor *editor)
d->lastEditor = editor; d->lastEditor = editor;
// Handle new editor // Handle new editor
connect(d->lastEditor->document(), &Core::IDocument::contentsChanged, connect(d->lastEditor->document(), &IDocument::contentsChanged,
this, &ExtraCompiler::setDirty); this, &ExtraCompiler::setDirty);
} else { } else {
d->lastEditor = nullptr; d->lastEditor = nullptr;
@@ -259,15 +259,15 @@ void ExtraCompiler::setDirty()
d->timer.start(1000); d->timer.start(1000);
} }
void ExtraCompiler::onEditorAboutToClose(Core::IEditor *editor) void ExtraCompiler::onEditorAboutToClose(IEditor *editor)
{ {
if (d->lastEditor != editor) if (d->lastEditor != editor)
return; return;
// Oh no our editor is going to be closed // Oh no our editor is going to be closed
// get the content first // get the content first
Core::IDocument *doc = d->lastEditor->document(); IDocument *doc = d->lastEditor->document();
disconnect(doc, &Core::IDocument::contentsChanged, disconnect(doc, &IDocument::contentsChanged,
this, &ExtraCompiler::setDirty); this, &ExtraCompiler::setDirty);
if (d->dirty) { if (d->dirty) {
d->dirty = false; d->dirty = false;
@@ -278,22 +278,20 @@ void ExtraCompiler::onEditorAboutToClose(Core::IEditor *editor)
Environment ExtraCompiler::buildEnvironment() const Environment ExtraCompiler::buildEnvironment() const
{ {
if (Target *target = project()->activeTarget()) { Target *target = project()->activeTarget();
if (BuildConfiguration *bc = target->activeBuildConfiguration()) { if (!target)
return bc->environment(); return Environment::systemEnvironment();
} else {
EnvironmentItems changes =
EnvironmentKitAspect::environmentChanges(target->kit());
Environment env = Environment::systemEnvironment();
env.modify(changes);
return env;
}
}
return Environment::systemEnvironment(); if (BuildConfiguration *bc = target->activeBuildConfiguration())
return bc->environment();
const EnvironmentItems changes = EnvironmentKitAspect::environmentChanges(target->kit());
Environment env = Environment::systemEnvironment();
env.modify(changes);
return env;
} }
Utils::FutureSynchronizer *ExtraCompiler::futureSynchronizer() const FutureSynchronizer *ExtraCompiler::futureSynchronizer() const
{ {
return &d->m_futureSynchronizer; return &d->m_futureSynchronizer;
} }
@@ -335,8 +333,8 @@ Tasking::TaskItem ProcessExtraCompiler::taskItemImpl(const ContentProvider &prov
{ {
const auto setupTask = [=](AsyncTask<FileNameToContentsHash> &async) { const auto setupTask = [=](AsyncTask<FileNameToContentsHash> &async) {
async.setThreadPool(extraCompilerThreadPool()); async.setThreadPool(extraCompilerThreadPool());
async.setAsyncCallData(&ProcessExtraCompiler::runInThread, this, command(), async.setConcurrentCallData(&ProcessExtraCompiler::runInThread, this, command(),
workingDirectory(), arguments(), provider, buildEnvironment()); workingDirectory(), arguments(), provider, buildEnvironment());
async.setFutureSynchronizer(futureSynchronizer()); async.setFutureSynchronizer(futureSynchronizer());
}; };
const auto taskDone = [=](const AsyncTask<FileNameToContentsHash> &async) { const auto taskDone = [=](const AsyncTask<FileNameToContentsHash> &async) {
@@ -374,7 +372,7 @@ Tasks ProcessExtraCompiler::parseIssues(const QByteArray &stdErr)
return {}; return {};
} }
void ProcessExtraCompiler::runInThread(QFutureInterface<FileNameToContentsHash> &futureInterface, void ProcessExtraCompiler::runInThread(QPromise<FileNameToContentsHash> &promise,
const FilePath &cmd, const FilePath &workDir, const FilePath &cmd, const FilePath &workDir,
const QStringList &args, const ContentProvider &provider, const QStringList &args, const ContentProvider &provider,
const Environment &env) const Environment &env)
@@ -397,15 +395,15 @@ void ProcessExtraCompiler::runInThread(QFutureInterface<FileNameToContentsHash>
if (!process.waitForStarted()) if (!process.waitForStarted())
return; return;
while (!futureInterface.isCanceled()) { while (!promise.isCanceled()) {
if (process.waitForFinished(200)) if (process.waitForFinished(200))
break; break;
} }
if (futureInterface.isCanceled()) if (promise.isCanceled())
return; return;
futureInterface.reportResult(handleProcessFinished(&process)); promise.addResult(handleProcessFinished(&process));
} }
} // namespace ProjectExplorer } // namespace ProjectExplorer

View File

@@ -21,7 +21,7 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
template <typename T> template <typename T>
class QFutureInterface; class QPromise;
class QThreadPool; class QThreadPool;
QT_END_NAMESPACE QT_END_NAMESPACE
@@ -106,7 +106,7 @@ protected:
private: private:
Utils::Tasking::TaskItem taskItemImpl(const ContentProvider &provider) final; Utils::Tasking::TaskItem taskItemImpl(const ContentProvider &provider) final;
void runInThread(QFutureInterface<FileNameToContentsHash> &futureInterface, void runInThread(QPromise<FileNameToContentsHash> &promise,
const Utils::FilePath &cmd, const Utils::FilePath &workDir, const Utils::FilePath &cmd, const Utils::FilePath &workDir,
const QStringList &args, const ContentProvider &provider, const QStringList &args, const ContentProvider &provider,
const Utils::Environment &env); const Utils::Environment &env);