ClangRefactoring: Process tasks after a task has been finished

It could be that processTasks is executed before the future is finished
but in that case there are other tasks which will be called later.

Change-Id: I9b1bfb6fdd642f23842b9c70d60d5b1552193b99
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
Marco Bubke
2018-08-27 15:54:08 +02:00
parent c81070aa7d
commit 2f41886c4b
9 changed files with 165 additions and 10 deletions

View File

@@ -26,7 +26,8 @@ HEADERS += \
$$PWD/sourcesmanager.h \
$$PWD/symbolindexertaskqueue.h \
$$PWD/symbolindexertaskscheduler.h \
$$PWD/symbolscollectormanagerinterface.h
$$PWD/symbolscollectormanagerinterface.h \
$$PWD/symbolindexertaskqueueinterface.h
!isEmpty(LIBTOOLING_LIBS) {
SOURCES += \

View File

@@ -131,4 +131,9 @@ std::vector<std::size_t> SymbolIndexerTaskQueue::projectPartNumberIds(const Util
return ids;
}
void SymbolIndexerTaskQueue::processTasks()
{
}
} // namespace ClangBackEnd

View File

@@ -25,6 +25,8 @@
#pragma once
#include "symbolindexertaskqueueinterface.h"
#include <filepathid.h>
#include <utils/smallstringvector.h>
@@ -92,6 +94,8 @@ public:
std::vector<std::size_t> projectPartNumberIds(const Utils::SmallStringVector &projectPartIds)
/* [[ensures result: std::is_sorted(result)]] */;
void processTasks() ;
private:
std::vector<Utils::SmallString> m_projectPartIds;
std::vector<SymbolIndexerTask> m_tasks;

View File

@@ -0,0 +1,38 @@
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
namespace ClangBackEnd {
class SymbolIndexerTaskQueueInterface
{
public:
virtual void processTasks() = 0;
protected:
~SymbolIndexerTaskQueueInterface() = default;
};
} // namespace ClangBackEnd

View File

@@ -25,28 +25,78 @@
#include "symbolindexertaskscheduler.h"
#include <symbolindexertaskqueueinterface.h>
#include <symbolscollectormanagerinterface.h>
#include <symbolscollectorinterface.h>
#include <QAbstractEventDispatcher>
#include <QCoreApplication>
#include <QMetaObject>
#include <QThread>
#include <algorithm>
#include <thread>
namespace ClangBackEnd {
namespace {
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
template <typename CallableType>
class CallableEvent : public QEvent {
public:
using Callable = std::decay_t<CallableType>;
CallableEvent(Callable &&callable)
: QEvent(QEvent::None),
callable(std::move(callable))
{}
CallableEvent(const Callable &callable)
: QEvent(QEvent::None),
callable(callable)
{}
~CallableEvent()
{
callable();
}
public:
Callable callable;
};
template <typename Callable>
void executeInLoop(Callable &&callable, QObject *object = QCoreApplication::instance()) {
if (QThread *thread = qobject_cast<QThread*>(object))
object = QAbstractEventDispatcher::instance(thread);
QCoreApplication::postEvent(object,
new CallableEvent<Callable>(std::forward<Callable>(callable)),
Qt::HighEventPriority);
}
#else
template <typename Callable>
void executeInLoop(Callable &&callable, QObject *object = QCoreApplication::instance()) {
if (QThread *thread = qobject_cast<QThread*>(object))
object = QAbstractEventDispatcher::instance(thread);
QMetaObject::invokeMethod(object, std::forward<Callable>(callable));
}
#endif
}
void SymbolIndexerTaskScheduler::addTasks(std::vector<Task> &&tasks)
{
for (auto &task : tasks) {
auto callWrapper = [task=std::move(task)] (
std::reference_wrapper<SymbolsCollectorInterface> symbolsCollector,
std::reference_wrapper<SymbolStorageInterface> symbolStorage)
auto callWrapper = [&, task=std::move(task)] (
std::reference_wrapper<SymbolsCollectorInterface> symbolsCollector)
-> SymbolsCollectorInterface& {
task(symbolsCollector.get(), symbolStorage.get());
task(symbolsCollector.get(), m_symbolStorage);
executeInLoop([&] {
m_symbolIndexerTaskQueue.processTasks();
});
return symbolsCollector;
};
m_futures.emplace_back(std::async(m_launchPolicy,
std::move(callWrapper),
std::ref(m_symbolsCollectorManager.unusedSymbolsCollector()),
std::ref(m_symbolStorage)));
std::ref(m_symbolsCollectorManager.unusedSymbolsCollector())));
}
}

View File

@@ -34,6 +34,7 @@ namespace ClangBackEnd {
class FilePathCachingInterface;
class SymbolsCollectorInterface;
class SymbolsCollectorManagerInterface;
class SymbolIndexerTaskQueueInterface;
class SymbolStorageInterface;
class SymbolIndexerTaskScheduler
@@ -45,10 +46,12 @@ public:
SymbolIndexerTaskScheduler(SymbolsCollectorManagerInterface &symbolsCollectorManager,
SymbolStorageInterface &symbolStorage,
SymbolIndexerTaskQueueInterface &symbolIndexerTaskQueue,
int hardware_concurrency,
std::launch launchPolicy = std::launch::async)
: m_symbolsCollectorManager(symbolsCollectorManager),
m_symbolStorage(symbolStorage),
m_symbolIndexerTaskQueue(symbolIndexerTaskQueue),
m_hardware_concurrency(hardware_concurrency),
m_launchPolicy(launchPolicy)
{}
@@ -68,6 +71,7 @@ private:
std::vector<Future> m_futures;
SymbolsCollectorManagerInterface &m_symbolsCollectorManager;
SymbolStorageInterface &m_symbolStorage;
SymbolIndexerTaskQueueInterface &m_symbolIndexerTaskQueue;
int m_hardware_concurrency;
std::launch m_launchPolicy;
};