2016-05-31 16:07:09 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 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.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "clangjobs.h"
|
|
|
|
|
|
2017-05-04 12:43:38 +02:00
|
|
|
#include "clangdocument.h"
|
2016-05-31 16:07:09 +02:00
|
|
|
#include "clangiasyncjob.h"
|
2017-05-04 12:43:38 +02:00
|
|
|
#include "projects.h"
|
2016-05-31 16:07:09 +02:00
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QFutureSynchronizer>
|
|
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
|
|
|
|
|
#include <utils/algorithm.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
|
|
|
|
namespace ClangBackEnd {
|
|
|
|
|
|
2016-09-07 10:42:12 +02:00
|
|
|
Jobs::Jobs(Documents &documents,
|
2016-05-31 16:07:09 +02:00
|
|
|
UnsavedFiles &unsavedFiles,
|
|
|
|
|
ProjectParts &projectParts,
|
|
|
|
|
ClangCodeModelClientInterface &client)
|
2016-09-07 10:42:12 +02:00
|
|
|
: m_documents(documents)
|
2016-05-31 16:07:09 +02:00
|
|
|
, m_unsavedFiles(unsavedFiles)
|
2017-05-04 12:43:38 +02:00
|
|
|
, m_projectParts(projectParts)
|
2016-05-31 16:07:09 +02:00
|
|
|
, m_client(client)
|
2016-09-07 10:42:12 +02:00
|
|
|
, m_queue(documents, projectParts)
|
2016-05-31 16:07:09 +02:00
|
|
|
{
|
2016-10-06 12:54:22 +02:00
|
|
|
m_queue.setIsJobRunningForTranslationUnitHandler([this](const Utf8String &translationUnitId) {
|
|
|
|
|
return isJobRunningForTranslationUnit(translationUnitId);
|
|
|
|
|
});
|
|
|
|
|
m_queue.setIsJobRunningForJobRequestHandler([this](const JobRequest &jobRequest) {
|
|
|
|
|
return isJobRunningForJobRequest(jobRequest);
|
2016-05-31 16:07:09 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Jobs::~Jobs()
|
|
|
|
|
{
|
2016-09-08 15:49:54 +02:00
|
|
|
foreach (IAsyncJob *asyncJob, m_running.keys())
|
|
|
|
|
asyncJob->preventFinalization();
|
|
|
|
|
|
2016-05-31 16:07:09 +02:00
|
|
|
QFutureSynchronizer<void> waitForFinishedJobs;
|
|
|
|
|
foreach (const RunningJob &runningJob, m_running.values())
|
|
|
|
|
waitForFinishedJobs.addFuture(runningJob.future);
|
2016-09-08 15:49:54 +02:00
|
|
|
|
|
|
|
|
foreach (IAsyncJob *asyncJob, m_running.keys())
|
|
|
|
|
delete asyncJob;
|
2016-05-31 16:07:09 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-04 12:43:38 +02:00
|
|
|
JobRequest Jobs::createJobRequest(const Document &document,
|
|
|
|
|
JobRequest::Type type,
|
|
|
|
|
PreferredTranslationUnit preferredTranslationUnit) const
|
|
|
|
|
{
|
|
|
|
|
JobRequest jobRequest;
|
|
|
|
|
jobRequest.type = type;
|
2017-06-01 16:18:58 +02:00
|
|
|
jobRequest.expirationReasons = JobRequest::expirationReasonsForType(type);
|
2017-06-09 12:19:09 +02:00
|
|
|
jobRequest.conditions = JobRequest::conditionsForType(type);
|
2017-05-04 12:43:38 +02:00
|
|
|
jobRequest.filePath = document.filePath();
|
2017-05-04 17:57:59 +02:00
|
|
|
jobRequest.projectPartId = document.projectPart().id();
|
2017-05-04 12:43:38 +02:00
|
|
|
jobRequest.unsavedFilesChangeTimePoint = m_unsavedFiles.lastChangeTimePoint();
|
|
|
|
|
jobRequest.documentRevision = document.documentRevision();
|
|
|
|
|
jobRequest.preferredTranslationUnit = preferredTranslationUnit;
|
2017-05-04 17:57:59 +02:00
|
|
|
const ProjectPart &projectPart = m_projectParts.project(document.projectPart().id());
|
2017-05-04 12:43:38 +02:00
|
|
|
jobRequest.projectChangeTimePoint = projectPart.lastChangeTimePoint();
|
|
|
|
|
|
|
|
|
|
return jobRequest;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 16:07:09 +02:00
|
|
|
void Jobs::add(const JobRequest &job)
|
|
|
|
|
{
|
|
|
|
|
m_queue.add(job);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-04 12:43:38 +02:00
|
|
|
void Jobs::add(const Document &document,
|
|
|
|
|
JobRequest::Type type,
|
|
|
|
|
PreferredTranslationUnit preferredTranslationUnit)
|
|
|
|
|
{
|
|
|
|
|
const JobRequest jobRequest = createJobRequest(document, type, preferredTranslationUnit);
|
|
|
|
|
m_queue.add(jobRequest);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 16:07:09 +02:00
|
|
|
JobRequests Jobs::process()
|
|
|
|
|
{
|
|
|
|
|
const JobRequests jobsToRun = m_queue.processQueue();
|
|
|
|
|
const JobRequests jobsStarted = runJobs(jobsToRun);
|
|
|
|
|
|
|
|
|
|
QTC_CHECK(jobsToRun.size() == jobsStarted.size());
|
|
|
|
|
|
|
|
|
|
return jobsStarted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JobRequests Jobs::runJobs(const JobRequests &jobsRequests)
|
|
|
|
|
{
|
|
|
|
|
JobRequests jobsStarted;
|
|
|
|
|
|
|
|
|
|
foreach (const JobRequest &jobRequest, jobsRequests) {
|
|
|
|
|
if (runJob(jobRequest))
|
|
|
|
|
jobsStarted += jobRequest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return jobsStarted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Jobs::runJob(const JobRequest &jobRequest)
|
|
|
|
|
{
|
2016-09-15 12:34:15 +02:00
|
|
|
IAsyncJob *asyncJob = IAsyncJob::create(jobRequest.type);
|
|
|
|
|
QTC_ASSERT(asyncJob, return false);
|
|
|
|
|
|
|
|
|
|
JobContext context(jobRequest, &m_documents, &m_unsavedFiles, &m_client);
|
|
|
|
|
asyncJob->setContext(context);
|
|
|
|
|
|
|
|
|
|
if (const IAsyncJob::AsyncPrepareResult prepareResult = asyncJob->prepareAsyncRun()) {
|
|
|
|
|
qCDebug(jobsLog) << "Running" << jobRequest
|
|
|
|
|
<< "with TranslationUnit" << prepareResult.translationUnitId;
|
|
|
|
|
|
|
|
|
|
asyncJob->setFinishedHandler([this](IAsyncJob *asyncJob){ onJobFinished(asyncJob); });
|
|
|
|
|
const QFuture<void> future = asyncJob->runAsync();
|
|
|
|
|
|
|
|
|
|
const RunningJob runningJob{jobRequest, prepareResult.translationUnitId, future};
|
|
|
|
|
m_running.insert(asyncJob, runningJob);
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
qCDebug(jobsLog) << "Preparation failed for " << jobRequest;
|
|
|
|
|
delete asyncJob;
|
2016-05-31 16:07:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Jobs::onJobFinished(IAsyncJob *asyncJob)
|
|
|
|
|
{
|
|
|
|
|
qCDebug(jobsLog) << "Finishing" << asyncJob->context().jobRequest;
|
|
|
|
|
|
2016-09-14 16:16:10 +02:00
|
|
|
if (m_jobFinishedCallback) {
|
|
|
|
|
const RunningJob runningJob = m_running.value(asyncJob);
|
|
|
|
|
m_jobFinishedCallback(runningJob);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-31 16:07:09 +02:00
|
|
|
m_running.remove(asyncJob);
|
|
|
|
|
delete asyncJob;
|
|
|
|
|
|
|
|
|
|
process();
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-14 16:16:10 +02:00
|
|
|
void Jobs::setJobFinishedCallback(const JobFinishedCallback &jobFinishedCallback)
|
|
|
|
|
{
|
|
|
|
|
m_jobFinishedCallback = jobFinishedCallback;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-08 15:49:54 +02:00
|
|
|
QList<Jobs::RunningJob> Jobs::runningJobs() const
|
2016-05-31 16:07:09 +02:00
|
|
|
{
|
2016-09-08 15:49:54 +02:00
|
|
|
return m_running.values();
|
2016-05-31 16:07:09 +02:00
|
|
|
}
|
|
|
|
|
|
2017-06-09 12:19:09 +02:00
|
|
|
JobRequests &Jobs::queue()
|
2016-05-31 16:07:09 +02:00
|
|
|
{
|
|
|
|
|
return m_queue.queue();
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-06 12:54:22 +02:00
|
|
|
bool Jobs::isJobRunningForTranslationUnit(const Utf8String &translationUnitId) const
|
2016-05-31 16:07:09 +02:00
|
|
|
{
|
2016-10-06 12:54:22 +02:00
|
|
|
const auto hasTranslationUnitId = [translationUnitId](const RunningJob &runningJob) {
|
2016-09-13 13:57:08 +02:00
|
|
|
return runningJob.translationUnitId == translationUnitId;
|
2016-05-31 16:07:09 +02:00
|
|
|
};
|
|
|
|
|
|
2016-10-06 12:54:22 +02:00
|
|
|
return Utils::anyOf(m_running.values(), hasTranslationUnitId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Jobs::isJobRunningForJobRequest(const JobRequest &jobRequest) const
|
|
|
|
|
{
|
|
|
|
|
const auto hasJobRequest = [jobRequest](const RunningJob &runningJob) {
|
|
|
|
|
return runningJob.jobRequest == jobRequest;
|
|
|
|
|
};
|
|
|
|
|
|
2016-05-31 16:07:09 +02:00
|
|
|
return Utils::anyOf(m_running.values(), hasJobRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangBackEnd
|