From 0bec769b69c660abec9ed0cc333522e4d4b0fc55 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Fri, 3 Mar 2023 20:53:58 +0100 Subject: QmlJS: Use QtConcurrent invocation for async run Add ModelManagerInterface::importScan() overload to avoid instantiating dummy QPromise arg on caller side. Change-Id: Idf836d30b2167d8840cc4e7ac6f95377c9d5622a Reviewed-by: hjk Reviewed-by: Qt CI Bot Reviewed-by: --- src/libs/qmljs/qmljsmodelmanagerinterface.cpp | 95 ++++++++++++++------------- 1 file changed, 48 insertions(+), 47 deletions(-) (limited to 'src/libs/qmljs/qmljsmodelmanagerinterface.cpp') diff --git a/src/libs/qmljs/qmljsmodelmanagerinterface.cpp b/src/libs/qmljs/qmljsmodelmanagerinterface.cpp index 3d2fc32120..f6c84fb88f 100644 --- a/src/libs/qmljs/qmljsmodelmanagerinterface.cpp +++ b/src/libs/qmljs/qmljsmodelmanagerinterface.cpp @@ -15,8 +15,8 @@ #include #include +#include #include -#include #include #ifdef WITH_TESTS @@ -337,11 +337,9 @@ QFuture ModelManagerInterface::refreshSourceFiles(const QList(); - QFuture result = Utils::runAsync(&m_threadPool, - &ModelManagerInterface::parse, - workingCopyInternal(), sourceFiles, - this, Dialect(Dialect::Qml), - emitDocumentOnDiskChanged); + QFuture result = Utils::asyncRun(&m_threadPool, &ModelManagerInterface::parse, + workingCopyInternal(), sourceFiles, this, + Dialect(Dialect::Qml), emitDocumentOnDiskChanged); addFuture(result); if (sourceFiles.count() > 1) @@ -365,13 +363,8 @@ QFuture ModelManagerInterface::refreshSourceFiles(const QList &files) @@ -1044,24 +1037,24 @@ void ModelManagerInterface::parseLoop(QSet &scannedPaths, class FutureReporter { public: - FutureReporter(QFutureInterface &future, int multiplier, int base) - : future(future), multiplier(multiplier), base(base) + FutureReporter(QPromise &promise, int multiplier, int base) + : m_promise(promise), m_multiplier(multiplier), m_base(base) {} bool operator()(qreal val) { - if (future.isCanceled()) + if (m_promise.isCanceled()) return false; - future.setProgressValue(int(base + multiplier * val)); + m_promise.setProgressValue(int(m_base + m_multiplier * val)); return true; } private: - QFutureInterface &future; - int multiplier; - int base; + QPromise &m_promise; + int m_multiplier; + int m_base; }; -void ModelManagerInterface::parse(QFutureInterface &future, +void ModelManagerInterface::parse(QPromise &promise, const WorkingCopy &workingCopy, QList files, ModelManagerInterface *modelManager, @@ -1069,8 +1062,8 @@ void ModelManagerInterface::parse(QFutureInterface &future, bool emitDocChangedOnDisk) { const int progressMax = 100; - FutureReporter reporter(future, progressMax, 0); - future.setProgressRange(0, progressMax); + FutureReporter reporter(promise, progressMax, 0); + promise.setProgressRange(0, progressMax); // paths we have scanned for files and added to the files list QSet scannedPaths; @@ -1078,7 +1071,7 @@ void ModelManagerInterface::parse(QFutureInterface &future, QSet newLibraries; parseLoop(scannedPaths, newLibraries, workingCopy, std::move(files), modelManager, mainLanguage, emitDocChangedOnDisk, reporter); - future.setProgressValue(progressMax); + promise.setProgressValue(progressMax); } struct ScanItem { @@ -1087,11 +1080,20 @@ struct ScanItem { Dialect language = Dialect::AnyLanguage; }; -void ModelManagerInterface::importScan(QFutureInterface &future, - const ModelManagerInterface::WorkingCopy &workingCopy, +void ModelManagerInterface::importScan(const WorkingCopy &workingCopy, const PathsAndLanguages &paths, ModelManagerInterface *modelManager, - bool emitDocChangedOnDisk, bool libOnly, bool forceRescan) + bool emitDocChanged, bool libOnly, bool forceRescan) +{ + QPromise promise; + promise.start(); + importScanAsync(promise, workingCopy, paths, modelManager, emitDocChanged, libOnly, forceRescan); +} + +void ModelManagerInterface::importScanAsync(QPromise &promise, const WorkingCopy &workingCopy, + const PathsAndLanguages &paths, + ModelManagerInterface *modelManager, + bool emitDocChanged, bool libOnly, bool forceRescan) { // paths we have scanned for files and added to the files list QSet scannedPaths; @@ -1118,9 +1120,9 @@ void ModelManagerInterface::importScan(QFutureInterface &future, int progressRange = pathsToScan.size() * (1 << (2 + maxScanDepth)); int totalWork = progressRange; int workDone = 0; - future.setProgressRange(0, progressRange); // update max length while iterating? + promise.setProgressRange(0, progressRange); // update max length while iterating? const Snapshot snapshot = modelManager->snapshot(); - bool isCanceled = future.isCanceled(); + bool isCanceled = promise.isCanceled(); while (!pathsToScan.isEmpty() && !isCanceled) { ScanItem toScan = pathsToScan.last(); pathsToScan.pop_back(); @@ -1135,16 +1137,16 @@ void ModelManagerInterface::importScan(QFutureInterface &future, toScan.language.companionLanguages()); } workDone += 1; - future.setProgressValue(progressRange * workDone / totalWork); + promise.setProgressValue(progressRange * workDone / totalWork); if (!importedFiles.isEmpty()) { - FutureReporter reporter(future, progressRange * pathBudget / (4 * totalWork), + FutureReporter reporter(promise, progressRange * pathBudget / (4 * totalWork), progressRange * workDone / totalWork); parseLoop(scannedPaths, newLibraries, workingCopy, importedFiles, modelManager, - toScan.language, emitDocChangedOnDisk, reporter); // run in parallel?? + toScan.language, emitDocChanged, reporter); // run in parallel?? importedFiles.clear(); } workDone += pathBudget / 4 - 1; - future.setProgressValue(progressRange * workDone / totalWork); + promise.setProgressValue(progressRange * workDone / totalWork); } else { workDone += pathBudget / 4; } @@ -1159,10 +1161,10 @@ void ModelManagerInterface::importScan(QFutureInterface &future, } else { workDone += pathBudget * 3 / 4; } - future.setProgressValue(progressRange * workDone / totalWork); - isCanceled = future.isCanceled(); + promise.setProgressValue(progressRange * workDone / totalWork); + isCanceled = promise.isCanceled(); } - future.setProgressValue(progressRange); + promise.setProgressValue(progressRange); if (isCanceled) { // assume no work has been done QMutexLocker l(&modelManager->m_mutex); @@ -1206,8 +1208,8 @@ void ModelManagerInterface::maybeScan(const PathsAndLanguages &importPaths) } if (pathToScan.length() >= 1) { - QFuture result = Utils::runAsync(&m_threadPool, - &ModelManagerInterface::importScan, + QFuture result = Utils::asyncRun(&m_threadPool, + &ModelManagerInterface::importScanAsync, workingCopyInternal(), pathToScan, this, true, true, false); addFuture(result); @@ -1373,8 +1375,8 @@ void ModelManagerInterface::startCppQmlTypeUpdate() if (!cppModelManager) return; - m_cppQmlTypesUpdater = Utils::runAsync(&ModelManagerInterface::updateCppQmlTypes, - this, cppModelManager->snapshot(), m_queuedCppDocuments); + m_cppQmlTypesUpdater = Utils::asyncRun(&ModelManagerInterface::updateCppQmlTypes, this, + cppModelManager->snapshot(), m_queuedCppDocuments); m_queuedCppDocuments.clear(); } @@ -1415,13 +1417,12 @@ bool rescanExports(const QString &fileName, FindExportedCppTypes &finder, return hasNewInfo; } -void ModelManagerInterface::updateCppQmlTypes( - QFutureInterface &futureInterface, ModelManagerInterface *qmlModelManager, - const CPlusPlus::Snapshot &snapshot, +void ModelManagerInterface::updateCppQmlTypes(QPromise &promise, + ModelManagerInterface *qmlModelManager, const CPlusPlus::Snapshot &snapshot, const QHash> &documents) { - futureInterface.setProgressRange(0, documents.size()); - futureInterface.setProgressValue(0); + promise.setProgressRange(0, documents.size()); + promise.setProgressValue(0); CppDataHash newData; QHash> newDeclarations; @@ -1436,9 +1437,9 @@ void ModelManagerInterface::updateCppQmlTypes( bool hasNewInfo = false; using DocScanPair = QPair; for (const DocScanPair &pair : documents) { - if (futureInterface.isCanceled()) + if (promise.isCanceled()) return; - futureInterface.setProgressValue(futureInterface.progressValue() + 1); + promise.setProgressValue(promise.future().progressValue() + 1); CPlusPlus::Document::Ptr doc = pair.first; const bool scan = pair.second; -- cgit v1.2.1